sys.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656
  1. /*
  2. * linux/kernel/sys.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. #include <linux/module.h>
  7. #include <linux/mm.h>
  8. #include <linux/utsname.h>
  9. #include <linux/mman.h>
  10. #include <linux/smp_lock.h>
  11. #include <linux/notifier.h>
  12. #include <linux/reboot.h>
  13. #include <linux/prctl.h>
  14. #include <linux/highuid.h>
  15. #include <linux/fs.h>
  16. #include <linux/perf_event.h>
  17. #include <linux/resource.h>
  18. #include <linux/kernel.h>
  19. #include <linux/kexec.h>
  20. #include <linux/workqueue.h>
  21. #include <linux/capability.h>
  22. #include <linux/device.h>
  23. #include <linux/key.h>
  24. #include <linux/times.h>
  25. #include <linux/posix-timers.h>
  26. #include <linux/security.h>
  27. #include <linux/dcookies.h>
  28. #include <linux/suspend.h>
  29. #include <linux/tty.h>
  30. #include <linux/signal.h>
  31. #include <linux/cn_proc.h>
  32. #include <linux/getcpu.h>
  33. #include <linux/task_io_accounting_ops.h>
  34. #include <linux/seccomp.h>
  35. #include <linux/cpu.h>
  36. #include <linux/ptrace.h>
  37. #include <linux/fs_struct.h>
  38. #include <linux/compat.h>
  39. #include <linux/syscalls.h>
  40. #include <linux/kprobes.h>
  41. #include <linux/user_namespace.h>
  42. #include <asm/uaccess.h>
  43. #include <asm/io.h>
  44. #include <asm/unistd.h>
  45. #ifndef SET_UNALIGN_CTL
  46. # define SET_UNALIGN_CTL(a,b) (-EINVAL)
  47. #endif
  48. #ifndef GET_UNALIGN_CTL
  49. # define GET_UNALIGN_CTL(a,b) (-EINVAL)
  50. #endif
  51. #ifndef SET_FPEMU_CTL
  52. # define SET_FPEMU_CTL(a,b) (-EINVAL)
  53. #endif
  54. #ifndef GET_FPEMU_CTL
  55. # define GET_FPEMU_CTL(a,b) (-EINVAL)
  56. #endif
  57. #ifndef SET_FPEXC_CTL
  58. # define SET_FPEXC_CTL(a,b) (-EINVAL)
  59. #endif
  60. #ifndef GET_FPEXC_CTL
  61. # define GET_FPEXC_CTL(a,b) (-EINVAL)
  62. #endif
  63. #ifndef GET_ENDIAN
  64. # define GET_ENDIAN(a,b) (-EINVAL)
  65. #endif
  66. #ifndef SET_ENDIAN
  67. # define SET_ENDIAN(a,b) (-EINVAL)
  68. #endif
  69. #ifndef GET_TSC_CTL
  70. # define GET_TSC_CTL(a) (-EINVAL)
  71. #endif
  72. #ifndef SET_TSC_CTL
  73. # define SET_TSC_CTL(a) (-EINVAL)
  74. #endif
  75. /*
  76. * this is where the system-wide overflow UID and GID are defined, for
  77. * architectures that now have 32-bit UID/GID but didn't in the past
  78. */
  79. int overflowuid = DEFAULT_OVERFLOWUID;
  80. int overflowgid = DEFAULT_OVERFLOWGID;
  81. #ifdef CONFIG_UID16
  82. EXPORT_SYMBOL(overflowuid);
  83. EXPORT_SYMBOL(overflowgid);
  84. #endif
  85. /*
  86. * the same as above, but for filesystems which can only store a 16-bit
  87. * UID and GID. as such, this is needed on all architectures
  88. */
  89. int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
  90. int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
  91. EXPORT_SYMBOL(fs_overflowuid);
  92. EXPORT_SYMBOL(fs_overflowgid);
  93. /*
  94. * this indicates whether you can reboot with ctrl-alt-del: the default is yes
  95. */
  96. int C_A_D = 1;
  97. struct pid *cad_pid;
  98. EXPORT_SYMBOL(cad_pid);
  99. /*
  100. * If set, this is used for preparing the system to power off.
  101. */
  102. void (*pm_power_off_prepare)(void);
  103. /*
  104. * set the priority of a task
  105. * - the caller must hold the RCU read lock
  106. */
  107. static int set_one_prio(struct task_struct *p, int niceval, int error)
  108. {
  109. const struct cred *cred = current_cred(), *pcred = __task_cred(p);
  110. int no_nice;
  111. if (pcred->uid != cred->euid &&
  112. pcred->euid != cred->euid && !capable(CAP_SYS_NICE)) {
  113. error = -EPERM;
  114. goto out;
  115. }
  116. if (niceval < task_nice(p) && !can_nice(p, niceval)) {
  117. error = -EACCES;
  118. goto out;
  119. }
  120. no_nice = security_task_setnice(p, niceval);
  121. if (no_nice) {
  122. error = no_nice;
  123. goto out;
  124. }
  125. if (error == -ESRCH)
  126. error = 0;
  127. set_user_nice(p, niceval);
  128. out:
  129. return error;
  130. }
  131. SYSCALL_DEFINE3(setpriority, int, which, int, who, int, niceval)
  132. {
  133. struct task_struct *g, *p;
  134. struct user_struct *user;
  135. const struct cred *cred = current_cred();
  136. int error = -EINVAL;
  137. struct pid *pgrp;
  138. if (which > PRIO_USER || which < PRIO_PROCESS)
  139. goto out;
  140. /* normalize: avoid signed division (rounding problems) */
  141. error = -ESRCH;
  142. if (niceval < -20)
  143. niceval = -20;
  144. if (niceval > 19)
  145. niceval = 19;
  146. rcu_read_lock();
  147. read_lock(&tasklist_lock);
  148. switch (which) {
  149. case PRIO_PROCESS:
  150. if (who)
  151. p = find_task_by_vpid(who);
  152. else
  153. p = current;
  154. if (p)
  155. error = set_one_prio(p, niceval, error);
  156. break;
  157. case PRIO_PGRP:
  158. if (who)
  159. pgrp = find_vpid(who);
  160. else
  161. pgrp = task_pgrp(current);
  162. do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
  163. error = set_one_prio(p, niceval, error);
  164. } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
  165. break;
  166. case PRIO_USER:
  167. user = (struct user_struct *) cred->user;
  168. if (!who)
  169. who = cred->uid;
  170. else if ((who != cred->uid) &&
  171. !(user = find_user(who)))
  172. goto out_unlock; /* No processes for this user */
  173. do_each_thread(g, p)
  174. if (__task_cred(p)->uid == who)
  175. error = set_one_prio(p, niceval, error);
  176. while_each_thread(g, p);
  177. if (who != cred->uid)
  178. free_uid(user); /* For find_user() */
  179. break;
  180. }
  181. out_unlock:
  182. read_unlock(&tasklist_lock);
  183. rcu_read_unlock();
  184. out:
  185. return error;
  186. }
  187. /*
  188. * Ugh. To avoid negative return values, "getpriority()" will
  189. * not return the normal nice-value, but a negated value that
  190. * has been offset by 20 (ie it returns 40..1 instead of -20..19)
  191. * to stay compatible.
  192. */
  193. SYSCALL_DEFINE2(getpriority, int, which, int, who)
  194. {
  195. struct task_struct *g, *p;
  196. struct user_struct *user;
  197. const struct cred *cred = current_cred();
  198. long niceval, retval = -ESRCH;
  199. struct pid *pgrp;
  200. if (which > PRIO_USER || which < PRIO_PROCESS)
  201. return -EINVAL;
  202. read_lock(&tasklist_lock);
  203. switch (which) {
  204. case PRIO_PROCESS:
  205. if (who)
  206. p = find_task_by_vpid(who);
  207. else
  208. p = current;
  209. if (p) {
  210. niceval = 20 - task_nice(p);
  211. if (niceval > retval)
  212. retval = niceval;
  213. }
  214. break;
  215. case PRIO_PGRP:
  216. if (who)
  217. pgrp = find_vpid(who);
  218. else
  219. pgrp = task_pgrp(current);
  220. do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
  221. niceval = 20 - task_nice(p);
  222. if (niceval > retval)
  223. retval = niceval;
  224. } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
  225. break;
  226. case PRIO_USER:
  227. user = (struct user_struct *) cred->user;
  228. if (!who)
  229. who = cred->uid;
  230. else if ((who != cred->uid) &&
  231. !(user = find_user(who)))
  232. goto out_unlock; /* No processes for this user */
  233. do_each_thread(g, p)
  234. if (__task_cred(p)->uid == who) {
  235. niceval = 20 - task_nice(p);
  236. if (niceval > retval)
  237. retval = niceval;
  238. }
  239. while_each_thread(g, p);
  240. if (who != cred->uid)
  241. free_uid(user); /* for find_user() */
  242. break;
  243. }
  244. out_unlock:
  245. read_unlock(&tasklist_lock);
  246. return retval;
  247. }
  248. /**
  249. * emergency_restart - reboot the system
  250. *
  251. * Without shutting down any hardware or taking any locks
  252. * reboot the system. This is called when we know we are in
  253. * trouble so this is our best effort to reboot. This is
  254. * safe to call in interrupt context.
  255. */
  256. void emergency_restart(void)
  257. {
  258. machine_emergency_restart();
  259. }
  260. EXPORT_SYMBOL_GPL(emergency_restart);
  261. void kernel_restart_prepare(char *cmd)
  262. {
  263. blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
  264. system_state = SYSTEM_RESTART;
  265. device_shutdown();
  266. sysdev_shutdown();
  267. }
  268. /**
  269. * kernel_restart - reboot the system
  270. * @cmd: pointer to buffer containing command to execute for restart
  271. * or %NULL
  272. *
  273. * Shutdown everything and perform a clean reboot.
  274. * This is not safe to call in interrupt context.
  275. */
  276. void kernel_restart(char *cmd)
  277. {
  278. kernel_restart_prepare(cmd);
  279. if (!cmd)
  280. printk(KERN_EMERG "Restarting system.\n");
  281. else
  282. printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
  283. machine_restart(cmd);
  284. }
  285. EXPORT_SYMBOL_GPL(kernel_restart);
  286. static void kernel_shutdown_prepare(enum system_states state)
  287. {
  288. blocking_notifier_call_chain(&reboot_notifier_list,
  289. (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL);
  290. system_state = state;
  291. device_shutdown();
  292. }
  293. /**
  294. * kernel_halt - halt the system
  295. *
  296. * Shutdown everything and perform a clean system halt.
  297. */
  298. void kernel_halt(void)
  299. {
  300. kernel_shutdown_prepare(SYSTEM_HALT);
  301. sysdev_shutdown();
  302. printk(KERN_EMERG "System halted.\n");
  303. machine_halt();
  304. }
  305. EXPORT_SYMBOL_GPL(kernel_halt);
  306. /**
  307. * kernel_power_off - power_off the system
  308. *
  309. * Shutdown everything and perform a clean system power_off.
  310. */
  311. void kernel_power_off(void)
  312. {
  313. kernel_shutdown_prepare(SYSTEM_POWER_OFF);
  314. if (pm_power_off_prepare)
  315. pm_power_off_prepare();
  316. disable_nonboot_cpus();
  317. sysdev_shutdown();
  318. printk(KERN_EMERG "Power down.\n");
  319. machine_power_off();
  320. }
  321. EXPORT_SYMBOL_GPL(kernel_power_off);
  322. /*
  323. * Reboot system call: for obvious reasons only root may call it,
  324. * and even root needs to set up some magic numbers in the registers
  325. * so that some mistake won't make this reboot the whole machine.
  326. * You can also set the meaning of the ctrl-alt-del-key here.
  327. *
  328. * reboot doesn't sync: do that yourself before calling this.
  329. */
  330. SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
  331. void __user *, arg)
  332. {
  333. char buffer[256];
  334. int ret = 0;
  335. /* We only trust the superuser with rebooting the system. */
  336. if (!capable(CAP_SYS_BOOT))
  337. return -EPERM;
  338. /* For safety, we require "magic" arguments. */
  339. if (magic1 != LINUX_REBOOT_MAGIC1 ||
  340. (magic2 != LINUX_REBOOT_MAGIC2 &&
  341. magic2 != LINUX_REBOOT_MAGIC2A &&
  342. magic2 != LINUX_REBOOT_MAGIC2B &&
  343. magic2 != LINUX_REBOOT_MAGIC2C))
  344. return -EINVAL;
  345. /* Instead of trying to make the power_off code look like
  346. * halt when pm_power_off is not set do it the easy way.
  347. */
  348. if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
  349. cmd = LINUX_REBOOT_CMD_HALT;
  350. lock_kernel();
  351. switch (cmd) {
  352. case LINUX_REBOOT_CMD_RESTART:
  353. kernel_restart(NULL);
  354. break;
  355. case LINUX_REBOOT_CMD_CAD_ON:
  356. C_A_D = 1;
  357. break;
  358. case LINUX_REBOOT_CMD_CAD_OFF:
  359. C_A_D = 0;
  360. break;
  361. case LINUX_REBOOT_CMD_HALT:
  362. kernel_halt();
  363. unlock_kernel();
  364. do_exit(0);
  365. panic("cannot halt");
  366. case LINUX_REBOOT_CMD_POWER_OFF:
  367. kernel_power_off();
  368. unlock_kernel();
  369. do_exit(0);
  370. break;
  371. case LINUX_REBOOT_CMD_RESTART2:
  372. if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
  373. unlock_kernel();
  374. return -EFAULT;
  375. }
  376. buffer[sizeof(buffer) - 1] = '\0';
  377. kernel_restart(buffer);
  378. break;
  379. #ifdef CONFIG_KEXEC
  380. case LINUX_REBOOT_CMD_KEXEC:
  381. ret = kernel_kexec();
  382. break;
  383. #endif
  384. #ifdef CONFIG_HIBERNATION
  385. case LINUX_REBOOT_CMD_SW_SUSPEND:
  386. ret = hibernate();
  387. break;
  388. #endif
  389. default:
  390. ret = -EINVAL;
  391. break;
  392. }
  393. unlock_kernel();
  394. return ret;
  395. }
  396. static void deferred_cad(struct work_struct *dummy)
  397. {
  398. kernel_restart(NULL);
  399. }
  400. /*
  401. * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
  402. * As it's called within an interrupt, it may NOT sync: the only choice
  403. * is whether to reboot at once, or just ignore the ctrl-alt-del.
  404. */
  405. void ctrl_alt_del(void)
  406. {
  407. static DECLARE_WORK(cad_work, deferred_cad);
  408. if (C_A_D)
  409. schedule_work(&cad_work);
  410. else
  411. kill_cad_pid(SIGINT, 1);
  412. }
  413. /*
  414. * Unprivileged users may change the real gid to the effective gid
  415. * or vice versa. (BSD-style)
  416. *
  417. * If you set the real gid at all, or set the effective gid to a value not
  418. * equal to the real gid, then the saved gid is set to the new effective gid.
  419. *
  420. * This makes it possible for a setgid program to completely drop its
  421. * privileges, which is often a useful assertion to make when you are doing
  422. * a security audit over a program.
  423. *
  424. * The general idea is that a program which uses just setregid() will be
  425. * 100% compatible with BSD. A program which uses just setgid() will be
  426. * 100% compatible with POSIX with saved IDs.
  427. *
  428. * SMP: There are not races, the GIDs are checked only by filesystem
  429. * operations (as far as semantic preservation is concerned).
  430. */
  431. SYSCALL_DEFINE2(setregid, gid_t, rgid, gid_t, egid)
  432. {
  433. const struct cred *old;
  434. struct cred *new;
  435. int retval;
  436. new = prepare_creds();
  437. if (!new)
  438. return -ENOMEM;
  439. old = current_cred();
  440. retval = security_task_setgid(rgid, egid, (gid_t)-1, LSM_SETID_RE);
  441. if (retval)
  442. goto error;
  443. retval = -EPERM;
  444. if (rgid != (gid_t) -1) {
  445. if (old->gid == rgid ||
  446. old->egid == rgid ||
  447. capable(CAP_SETGID))
  448. new->gid = rgid;
  449. else
  450. goto error;
  451. }
  452. if (egid != (gid_t) -1) {
  453. if (old->gid == egid ||
  454. old->egid == egid ||
  455. old->sgid == egid ||
  456. capable(CAP_SETGID))
  457. new->egid = egid;
  458. else
  459. goto error;
  460. }
  461. if (rgid != (gid_t) -1 ||
  462. (egid != (gid_t) -1 && egid != old->gid))
  463. new->sgid = new->egid;
  464. new->fsgid = new->egid;
  465. return commit_creds(new);
  466. error:
  467. abort_creds(new);
  468. return retval;
  469. }
  470. /*
  471. * setgid() is implemented like SysV w/ SAVED_IDS
  472. *
  473. * SMP: Same implicit races as above.
  474. */
  475. SYSCALL_DEFINE1(setgid, gid_t, gid)
  476. {
  477. const struct cred *old;
  478. struct cred *new;
  479. int retval;
  480. new = prepare_creds();
  481. if (!new)
  482. return -ENOMEM;
  483. old = current_cred();
  484. retval = security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_ID);
  485. if (retval)
  486. goto error;
  487. retval = -EPERM;
  488. if (capable(CAP_SETGID))
  489. new->gid = new->egid = new->sgid = new->fsgid = gid;
  490. else if (gid == old->gid || gid == old->sgid)
  491. new->egid = new->fsgid = gid;
  492. else
  493. goto error;
  494. return commit_creds(new);
  495. error:
  496. abort_creds(new);
  497. return retval;
  498. }
  499. /*
  500. * change the user struct in a credentials set to match the new UID
  501. */
  502. static int set_user(struct cred *new)
  503. {
  504. struct user_struct *new_user;
  505. new_user = alloc_uid(current_user_ns(), new->uid);
  506. if (!new_user)
  507. return -EAGAIN;
  508. if (!task_can_switch_user(new_user, current)) {
  509. free_uid(new_user);
  510. return -EINVAL;
  511. }
  512. if (atomic_read(&new_user->processes) >=
  513. current->signal->rlim[RLIMIT_NPROC].rlim_cur &&
  514. new_user != INIT_USER) {
  515. free_uid(new_user);
  516. return -EAGAIN;
  517. }
  518. free_uid(new->user);
  519. new->user = new_user;
  520. return 0;
  521. }
  522. /*
  523. * Unprivileged users may change the real uid to the effective uid
  524. * or vice versa. (BSD-style)
  525. *
  526. * If you set the real uid at all, or set the effective uid to a value not
  527. * equal to the real uid, then the saved uid is set to the new effective uid.
  528. *
  529. * This makes it possible for a setuid program to completely drop its
  530. * privileges, which is often a useful assertion to make when you are doing
  531. * a security audit over a program.
  532. *
  533. * The general idea is that a program which uses just setreuid() will be
  534. * 100% compatible with BSD. A program which uses just setuid() will be
  535. * 100% compatible with POSIX with saved IDs.
  536. */
  537. SYSCALL_DEFINE2(setreuid, uid_t, ruid, uid_t, euid)
  538. {
  539. const struct cred *old;
  540. struct cred *new;
  541. int retval;
  542. new = prepare_creds();
  543. if (!new)
  544. return -ENOMEM;
  545. old = current_cred();
  546. retval = security_task_setuid(ruid, euid, (uid_t)-1, LSM_SETID_RE);
  547. if (retval)
  548. goto error;
  549. retval = -EPERM;
  550. if (ruid != (uid_t) -1) {
  551. new->uid = ruid;
  552. if (old->uid != ruid &&
  553. old->euid != ruid &&
  554. !capable(CAP_SETUID))
  555. goto error;
  556. }
  557. if (euid != (uid_t) -1) {
  558. new->euid = euid;
  559. if (old->uid != euid &&
  560. old->euid != euid &&
  561. old->suid != euid &&
  562. !capable(CAP_SETUID))
  563. goto error;
  564. }
  565. if (new->uid != old->uid) {
  566. retval = set_user(new);
  567. if (retval < 0)
  568. goto error;
  569. }
  570. if (ruid != (uid_t) -1 ||
  571. (euid != (uid_t) -1 && euid != old->uid))
  572. new->suid = new->euid;
  573. new->fsuid = new->euid;
  574. retval = security_task_fix_setuid(new, old, LSM_SETID_RE);
  575. if (retval < 0)
  576. goto error;
  577. return commit_creds(new);
  578. error:
  579. abort_creds(new);
  580. return retval;
  581. }
  582. /*
  583. * setuid() is implemented like SysV with SAVED_IDS
  584. *
  585. * Note that SAVED_ID's is deficient in that a setuid root program
  586. * like sendmail, for example, cannot set its uid to be a normal
  587. * user and then switch back, because if you're root, setuid() sets
  588. * the saved uid too. If you don't like this, blame the bright people
  589. * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
  590. * will allow a root program to temporarily drop privileges and be able to
  591. * regain them by swapping the real and effective uid.
  592. */
  593. SYSCALL_DEFINE1(setuid, uid_t, uid)
  594. {
  595. const struct cred *old;
  596. struct cred *new;
  597. int retval;
  598. new = prepare_creds();
  599. if (!new)
  600. return -ENOMEM;
  601. old = current_cred();
  602. retval = security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_ID);
  603. if (retval)
  604. goto error;
  605. retval = -EPERM;
  606. if (capable(CAP_SETUID)) {
  607. new->suid = new->uid = uid;
  608. if (uid != old->uid) {
  609. retval = set_user(new);
  610. if (retval < 0)
  611. goto error;
  612. }
  613. } else if (uid != old->uid && uid != new->suid) {
  614. goto error;
  615. }
  616. new->fsuid = new->euid = uid;
  617. retval = security_task_fix_setuid(new, old, LSM_SETID_ID);
  618. if (retval < 0)
  619. goto error;
  620. return commit_creds(new);
  621. error:
  622. abort_creds(new);
  623. return retval;
  624. }
  625. /*
  626. * This function implements a generic ability to update ruid, euid,
  627. * and suid. This allows you to implement the 4.4 compatible seteuid().
  628. */
  629. SYSCALL_DEFINE3(setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
  630. {
  631. const struct cred *old;
  632. struct cred *new;
  633. int retval;
  634. new = prepare_creds();
  635. if (!new)
  636. return -ENOMEM;
  637. retval = security_task_setuid(ruid, euid, suid, LSM_SETID_RES);
  638. if (retval)
  639. goto error;
  640. old = current_cred();
  641. retval = -EPERM;
  642. if (!capable(CAP_SETUID)) {
  643. if (ruid != (uid_t) -1 && ruid != old->uid &&
  644. ruid != old->euid && ruid != old->suid)
  645. goto error;
  646. if (euid != (uid_t) -1 && euid != old->uid &&
  647. euid != old->euid && euid != old->suid)
  648. goto error;
  649. if (suid != (uid_t) -1 && suid != old->uid &&
  650. suid != old->euid && suid != old->suid)
  651. goto error;
  652. }
  653. if (ruid != (uid_t) -1) {
  654. new->uid = ruid;
  655. if (ruid != old->uid) {
  656. retval = set_user(new);
  657. if (retval < 0)
  658. goto error;
  659. }
  660. }
  661. if (euid != (uid_t) -1)
  662. new->euid = euid;
  663. if (suid != (uid_t) -1)
  664. new->suid = suid;
  665. new->fsuid = new->euid;
  666. retval = security_task_fix_setuid(new, old, LSM_SETID_RES);
  667. if (retval < 0)
  668. goto error;
  669. return commit_creds(new);
  670. error:
  671. abort_creds(new);
  672. return retval;
  673. }
  674. SYSCALL_DEFINE3(getresuid, uid_t __user *, ruid, uid_t __user *, euid, uid_t __user *, suid)
  675. {
  676. const struct cred *cred = current_cred();
  677. int retval;
  678. if (!(retval = put_user(cred->uid, ruid)) &&
  679. !(retval = put_user(cred->euid, euid)))
  680. retval = put_user(cred->suid, suid);
  681. return retval;
  682. }
  683. /*
  684. * Same as above, but for rgid, egid, sgid.
  685. */
  686. SYSCALL_DEFINE3(setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
  687. {
  688. const struct cred *old;
  689. struct cred *new;
  690. int retval;
  691. new = prepare_creds();
  692. if (!new)
  693. return -ENOMEM;
  694. old = current_cred();
  695. retval = security_task_setgid(rgid, egid, sgid, LSM_SETID_RES);
  696. if (retval)
  697. goto error;
  698. retval = -EPERM;
  699. if (!capable(CAP_SETGID)) {
  700. if (rgid != (gid_t) -1 && rgid != old->gid &&
  701. rgid != old->egid && rgid != old->sgid)
  702. goto error;
  703. if (egid != (gid_t) -1 && egid != old->gid &&
  704. egid != old->egid && egid != old->sgid)
  705. goto error;
  706. if (sgid != (gid_t) -1 && sgid != old->gid &&
  707. sgid != old->egid && sgid != old->sgid)
  708. goto error;
  709. }
  710. if (rgid != (gid_t) -1)
  711. new->gid = rgid;
  712. if (egid != (gid_t) -1)
  713. new->egid = egid;
  714. if (sgid != (gid_t) -1)
  715. new->sgid = sgid;
  716. new->fsgid = new->egid;
  717. return commit_creds(new);
  718. error:
  719. abort_creds(new);
  720. return retval;
  721. }
  722. SYSCALL_DEFINE3(getresgid, gid_t __user *, rgid, gid_t __user *, egid, gid_t __user *, sgid)
  723. {
  724. const struct cred *cred = current_cred();
  725. int retval;
  726. if (!(retval = put_user(cred->gid, rgid)) &&
  727. !(retval = put_user(cred->egid, egid)))
  728. retval = put_user(cred->sgid, sgid);
  729. return retval;
  730. }
  731. /*
  732. * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
  733. * is used for "access()" and for the NFS daemon (letting nfsd stay at
  734. * whatever uid it wants to). It normally shadows "euid", except when
  735. * explicitly set by setfsuid() or for access..
  736. */
  737. SYSCALL_DEFINE1(setfsuid, uid_t, uid)
  738. {
  739. const struct cred *old;
  740. struct cred *new;
  741. uid_t old_fsuid;
  742. new = prepare_creds();
  743. if (!new)
  744. return current_fsuid();
  745. old = current_cred();
  746. old_fsuid = old->fsuid;
  747. if (security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS) < 0)
  748. goto error;
  749. if (uid == old->uid || uid == old->euid ||
  750. uid == old->suid || uid == old->fsuid ||
  751. capable(CAP_SETUID)) {
  752. if (uid != old_fsuid) {
  753. new->fsuid = uid;
  754. if (security_task_fix_setuid(new, old, LSM_SETID_FS) == 0)
  755. goto change_okay;
  756. }
  757. }
  758. error:
  759. abort_creds(new);
  760. return old_fsuid;
  761. change_okay:
  762. commit_creds(new);
  763. return old_fsuid;
  764. }
  765. /*
  766. * Samma på svenska..
  767. */
  768. SYSCALL_DEFINE1(setfsgid, gid_t, gid)
  769. {
  770. const struct cred *old;
  771. struct cred *new;
  772. gid_t old_fsgid;
  773. new = prepare_creds();
  774. if (!new)
  775. return current_fsgid();
  776. old = current_cred();
  777. old_fsgid = old->fsgid;
  778. if (security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_FS))
  779. goto error;
  780. if (gid == old->gid || gid == old->egid ||
  781. gid == old->sgid || gid == old->fsgid ||
  782. capable(CAP_SETGID)) {
  783. if (gid != old_fsgid) {
  784. new->fsgid = gid;
  785. goto change_okay;
  786. }
  787. }
  788. error:
  789. abort_creds(new);
  790. return old_fsgid;
  791. change_okay:
  792. commit_creds(new);
  793. return old_fsgid;
  794. }
  795. void do_sys_times(struct tms *tms)
  796. {
  797. cputime_t tgutime, tgstime, cutime, cstime;
  798. spin_lock_irq(&current->sighand->siglock);
  799. thread_group_times(current, &tgutime, &tgstime);
  800. cutime = current->signal->cutime;
  801. cstime = current->signal->cstime;
  802. spin_unlock_irq(&current->sighand->siglock);
  803. tms->tms_utime = cputime_to_clock_t(tgutime);
  804. tms->tms_stime = cputime_to_clock_t(tgstime);
  805. tms->tms_cutime = cputime_to_clock_t(cutime);
  806. tms->tms_cstime = cputime_to_clock_t(cstime);
  807. }
  808. SYSCALL_DEFINE1(times, struct tms __user *, tbuf)
  809. {
  810. if (tbuf) {
  811. struct tms tmp;
  812. do_sys_times(&tmp);
  813. if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
  814. return -EFAULT;
  815. }
  816. force_successful_syscall_return();
  817. return (long) jiffies_64_to_clock_t(get_jiffies_64());
  818. }
  819. /*
  820. * This needs some heavy checking ...
  821. * I just haven't the stomach for it. I also don't fully
  822. * understand sessions/pgrp etc. Let somebody who does explain it.
  823. *
  824. * OK, I think I have the protection semantics right.... this is really
  825. * only important on a multi-user system anyway, to make sure one user
  826. * can't send a signal to a process owned by another. -TYT, 12/12/91
  827. *
  828. * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
  829. * LBT 04.03.94
  830. */
  831. SYSCALL_DEFINE2(setpgid, pid_t, pid, pid_t, pgid)
  832. {
  833. struct task_struct *p;
  834. struct task_struct *group_leader = current->group_leader;
  835. struct pid *pgrp;
  836. int err;
  837. if (!pid)
  838. pid = task_pid_vnr(group_leader);
  839. if (!pgid)
  840. pgid = pid;
  841. if (pgid < 0)
  842. return -EINVAL;
  843. /* From this point forward we keep holding onto the tasklist lock
  844. * so that our parent does not change from under us. -DaveM
  845. */
  846. write_lock_irq(&tasklist_lock);
  847. err = -ESRCH;
  848. p = find_task_by_vpid(pid);
  849. if (!p)
  850. goto out;
  851. err = -EINVAL;
  852. if (!thread_group_leader(p))
  853. goto out;
  854. if (same_thread_group(p->real_parent, group_leader)) {
  855. err = -EPERM;
  856. if (task_session(p) != task_session(group_leader))
  857. goto out;
  858. err = -EACCES;
  859. if (p->did_exec)
  860. goto out;
  861. } else {
  862. err = -ESRCH;
  863. if (p != group_leader)
  864. goto out;
  865. }
  866. err = -EPERM;
  867. if (p->signal->leader)
  868. goto out;
  869. pgrp = task_pid(p);
  870. if (pgid != pid) {
  871. struct task_struct *g;
  872. pgrp = find_vpid(pgid);
  873. g = pid_task(pgrp, PIDTYPE_PGID);
  874. if (!g || task_session(g) != task_session(group_leader))
  875. goto out;
  876. }
  877. err = security_task_setpgid(p, pgid);
  878. if (err)
  879. goto out;
  880. if (task_pgrp(p) != pgrp)
  881. change_pid(p, PIDTYPE_PGID, pgrp);
  882. err = 0;
  883. out:
  884. /* All paths lead to here, thus we are safe. -DaveM */
  885. write_unlock_irq(&tasklist_lock);
  886. return err;
  887. }
  888. SYSCALL_DEFINE1(getpgid, pid_t, pid)
  889. {
  890. struct task_struct *p;
  891. struct pid *grp;
  892. int retval;
  893. rcu_read_lock();
  894. if (!pid)
  895. grp = task_pgrp(current);
  896. else {
  897. retval = -ESRCH;
  898. p = find_task_by_vpid(pid);
  899. if (!p)
  900. goto out;
  901. grp = task_pgrp(p);
  902. if (!grp)
  903. goto out;
  904. retval = security_task_getpgid(p);
  905. if (retval)
  906. goto out;
  907. }
  908. retval = pid_vnr(grp);
  909. out:
  910. rcu_read_unlock();
  911. return retval;
  912. }
  913. #ifdef __ARCH_WANT_SYS_GETPGRP
  914. SYSCALL_DEFINE0(getpgrp)
  915. {
  916. return sys_getpgid(0);
  917. }
  918. #endif
  919. SYSCALL_DEFINE1(getsid, pid_t, pid)
  920. {
  921. struct task_struct *p;
  922. struct pid *sid;
  923. int retval;
  924. rcu_read_lock();
  925. if (!pid)
  926. sid = task_session(current);
  927. else {
  928. retval = -ESRCH;
  929. p = find_task_by_vpid(pid);
  930. if (!p)
  931. goto out;
  932. sid = task_session(p);
  933. if (!sid)
  934. goto out;
  935. retval = security_task_getsid(p);
  936. if (retval)
  937. goto out;
  938. }
  939. retval = pid_vnr(sid);
  940. out:
  941. rcu_read_unlock();
  942. return retval;
  943. }
  944. SYSCALL_DEFINE0(setsid)
  945. {
  946. struct task_struct *group_leader = current->group_leader;
  947. struct pid *sid = task_pid(group_leader);
  948. pid_t session = pid_vnr(sid);
  949. int err = -EPERM;
  950. write_lock_irq(&tasklist_lock);
  951. /* Fail if I am already a session leader */
  952. if (group_leader->signal->leader)
  953. goto out;
  954. /* Fail if a process group id already exists that equals the
  955. * proposed session id.
  956. */
  957. if (pid_task(sid, PIDTYPE_PGID))
  958. goto out;
  959. group_leader->signal->leader = 1;
  960. __set_special_pids(sid);
  961. proc_clear_tty(group_leader);
  962. err = session;
  963. out:
  964. write_unlock_irq(&tasklist_lock);
  965. if (err > 0)
  966. proc_sid_connector(group_leader);
  967. return err;
  968. }
  969. DECLARE_RWSEM(uts_sem);
  970. SYSCALL_DEFINE1(newuname, struct new_utsname __user *, name)
  971. {
  972. int errno = 0;
  973. down_read(&uts_sem);
  974. if (copy_to_user(name, utsname(), sizeof *name))
  975. errno = -EFAULT;
  976. up_read(&uts_sem);
  977. return errno;
  978. }
  979. SYSCALL_DEFINE2(sethostname, char __user *, name, int, len)
  980. {
  981. int errno;
  982. char tmp[__NEW_UTS_LEN];
  983. if (!capable(CAP_SYS_ADMIN))
  984. return -EPERM;
  985. if (len < 0 || len > __NEW_UTS_LEN)
  986. return -EINVAL;
  987. down_write(&uts_sem);
  988. errno = -EFAULT;
  989. if (!copy_from_user(tmp, name, len)) {
  990. struct new_utsname *u = utsname();
  991. memcpy(u->nodename, tmp, len);
  992. memset(u->nodename + len, 0, sizeof(u->nodename) - len);
  993. errno = 0;
  994. }
  995. up_write(&uts_sem);
  996. return errno;
  997. }
  998. #ifdef __ARCH_WANT_SYS_GETHOSTNAME
  999. SYSCALL_DEFINE2(gethostname, char __user *, name, int, len)
  1000. {
  1001. int i, errno;
  1002. struct new_utsname *u;
  1003. if (len < 0)
  1004. return -EINVAL;
  1005. down_read(&uts_sem);
  1006. u = utsname();
  1007. i = 1 + strlen(u->nodename);
  1008. if (i > len)
  1009. i = len;
  1010. errno = 0;
  1011. if (copy_to_user(name, u->nodename, i))
  1012. errno = -EFAULT;
  1013. up_read(&uts_sem);
  1014. return errno;
  1015. }
  1016. #endif
  1017. /*
  1018. * Only setdomainname; getdomainname can be implemented by calling
  1019. * uname()
  1020. */
  1021. SYSCALL_DEFINE2(setdomainname, char __user *, name, int, len)
  1022. {
  1023. int errno;
  1024. char tmp[__NEW_UTS_LEN];
  1025. if (!capable(CAP_SYS_ADMIN))
  1026. return -EPERM;
  1027. if (len < 0 || len > __NEW_UTS_LEN)
  1028. return -EINVAL;
  1029. down_write(&uts_sem);
  1030. errno = -EFAULT;
  1031. if (!copy_from_user(tmp, name, len)) {
  1032. struct new_utsname *u = utsname();
  1033. memcpy(u->domainname, tmp, len);
  1034. memset(u->domainname + len, 0, sizeof(u->domainname) - len);
  1035. errno = 0;
  1036. }
  1037. up_write(&uts_sem);
  1038. return errno;
  1039. }
  1040. SYSCALL_DEFINE2(getrlimit, unsigned int, resource, struct rlimit __user *, rlim)
  1041. {
  1042. if (resource >= RLIM_NLIMITS)
  1043. return -EINVAL;
  1044. else {
  1045. struct rlimit value;
  1046. task_lock(current->group_leader);
  1047. value = current->signal->rlim[resource];
  1048. task_unlock(current->group_leader);
  1049. return copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
  1050. }
  1051. }
  1052. #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
  1053. /*
  1054. * Back compatibility for getrlimit. Needed for some apps.
  1055. */
  1056. SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
  1057. struct rlimit __user *, rlim)
  1058. {
  1059. struct rlimit x;
  1060. if (resource >= RLIM_NLIMITS)
  1061. return -EINVAL;
  1062. task_lock(current->group_leader);
  1063. x = current->signal->rlim[resource];
  1064. task_unlock(current->group_leader);
  1065. if (x.rlim_cur > 0x7FFFFFFF)
  1066. x.rlim_cur = 0x7FFFFFFF;
  1067. if (x.rlim_max > 0x7FFFFFFF)
  1068. x.rlim_max = 0x7FFFFFFF;
  1069. return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
  1070. }
  1071. #endif
  1072. SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim)
  1073. {
  1074. struct rlimit new_rlim, *old_rlim;
  1075. int retval;
  1076. if (resource >= RLIM_NLIMITS)
  1077. return -EINVAL;
  1078. if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
  1079. return -EFAULT;
  1080. if (new_rlim.rlim_cur > new_rlim.rlim_max)
  1081. return -EINVAL;
  1082. old_rlim = current->signal->rlim + resource;
  1083. if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
  1084. !capable(CAP_SYS_RESOURCE))
  1085. return -EPERM;
  1086. if (resource == RLIMIT_NOFILE && new_rlim.rlim_max > sysctl_nr_open)
  1087. return -EPERM;
  1088. retval = security_task_setrlimit(resource, &new_rlim);
  1089. if (retval)
  1090. return retval;
  1091. if (resource == RLIMIT_CPU && new_rlim.rlim_cur == 0) {
  1092. /*
  1093. * The caller is asking for an immediate RLIMIT_CPU
  1094. * expiry. But we use the zero value to mean "it was
  1095. * never set". So let's cheat and make it one second
  1096. * instead
  1097. */
  1098. new_rlim.rlim_cur = 1;
  1099. }
  1100. task_lock(current->group_leader);
  1101. *old_rlim = new_rlim;
  1102. task_unlock(current->group_leader);
  1103. if (resource != RLIMIT_CPU)
  1104. goto out;
  1105. /*
  1106. * RLIMIT_CPU handling. Note that the kernel fails to return an error
  1107. * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
  1108. * very long-standing error, and fixing it now risks breakage of
  1109. * applications, so we live with it
  1110. */
  1111. if (new_rlim.rlim_cur == RLIM_INFINITY)
  1112. goto out;
  1113. update_rlimit_cpu(new_rlim.rlim_cur);
  1114. out:
  1115. return 0;
  1116. }
  1117. /*
  1118. * It would make sense to put struct rusage in the task_struct,
  1119. * except that would make the task_struct be *really big*. After
  1120. * task_struct gets moved into malloc'ed memory, it would
  1121. * make sense to do this. It will make moving the rest of the information
  1122. * a lot simpler! (Which we're not doing right now because we're not
  1123. * measuring them yet).
  1124. *
  1125. * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
  1126. * races with threads incrementing their own counters. But since word
  1127. * reads are atomic, we either get new values or old values and we don't
  1128. * care which for the sums. We always take the siglock to protect reading
  1129. * the c* fields from p->signal from races with exit.c updating those
  1130. * fields when reaping, so a sample either gets all the additions of a
  1131. * given child after it's reaped, or none so this sample is before reaping.
  1132. *
  1133. * Locking:
  1134. * We need to take the siglock for CHILDEREN, SELF and BOTH
  1135. * for the cases current multithreaded, non-current single threaded
  1136. * non-current multithreaded. Thread traversal is now safe with
  1137. * the siglock held.
  1138. * Strictly speaking, we donot need to take the siglock if we are current and
  1139. * single threaded, as no one else can take our signal_struct away, no one
  1140. * else can reap the children to update signal->c* counters, and no one else
  1141. * can race with the signal-> fields. If we do not take any lock, the
  1142. * signal-> fields could be read out of order while another thread was just
  1143. * exiting. So we should place a read memory barrier when we avoid the lock.
  1144. * On the writer side, write memory barrier is implied in __exit_signal
  1145. * as __exit_signal releases the siglock spinlock after updating the signal->
  1146. * fields. But we don't do this yet to keep things simple.
  1147. *
  1148. */
  1149. static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r)
  1150. {
  1151. r->ru_nvcsw += t->nvcsw;
  1152. r->ru_nivcsw += t->nivcsw;
  1153. r->ru_minflt += t->min_flt;
  1154. r->ru_majflt += t->maj_flt;
  1155. r->ru_inblock += task_io_get_inblock(t);
  1156. r->ru_oublock += task_io_get_oublock(t);
  1157. }
  1158. static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
  1159. {
  1160. struct task_struct *t;
  1161. unsigned long flags;
  1162. cputime_t tgutime, tgstime, utime, stime;
  1163. unsigned long maxrss = 0;
  1164. memset((char *) r, 0, sizeof *r);
  1165. utime = stime = cputime_zero;
  1166. if (who == RUSAGE_THREAD) {
  1167. task_times(current, &utime, &stime);
  1168. accumulate_thread_rusage(p, r);
  1169. maxrss = p->signal->maxrss;
  1170. goto out;
  1171. }
  1172. if (!lock_task_sighand(p, &flags))
  1173. return;
  1174. switch (who) {
  1175. case RUSAGE_BOTH:
  1176. case RUSAGE_CHILDREN:
  1177. utime = p->signal->cutime;
  1178. stime = p->signal->cstime;
  1179. r->ru_nvcsw = p->signal->cnvcsw;
  1180. r->ru_nivcsw = p->signal->cnivcsw;
  1181. r->ru_minflt = p->signal->cmin_flt;
  1182. r->ru_majflt = p->signal->cmaj_flt;
  1183. r->ru_inblock = p->signal->cinblock;
  1184. r->ru_oublock = p->signal->coublock;
  1185. maxrss = p->signal->cmaxrss;
  1186. if (who == RUSAGE_CHILDREN)
  1187. break;
  1188. case RUSAGE_SELF:
  1189. thread_group_times(p, &tgutime, &tgstime);
  1190. utime = cputime_add(utime, tgutime);
  1191. stime = cputime_add(stime, tgstime);
  1192. r->ru_nvcsw += p->signal->nvcsw;
  1193. r->ru_nivcsw += p->signal->nivcsw;
  1194. r->ru_minflt += p->signal->min_flt;
  1195. r->ru_majflt += p->signal->maj_flt;
  1196. r->ru_inblock += p->signal->inblock;
  1197. r->ru_oublock += p->signal->oublock;
  1198. if (maxrss < p->signal->maxrss)
  1199. maxrss = p->signal->maxrss;
  1200. t = p;
  1201. do {
  1202. accumulate_thread_rusage(t, r);
  1203. t = next_thread(t);
  1204. } while (t != p);
  1205. break;
  1206. default:
  1207. BUG();
  1208. }
  1209. unlock_task_sighand(p, &flags);
  1210. out:
  1211. cputime_to_timeval(utime, &r->ru_utime);
  1212. cputime_to_timeval(stime, &r->ru_stime);
  1213. if (who != RUSAGE_CHILDREN) {
  1214. struct mm_struct *mm = get_task_mm(p);
  1215. if (mm) {
  1216. setmax_mm_hiwater_rss(&maxrss, mm);
  1217. mmput(mm);
  1218. }
  1219. }
  1220. r->ru_maxrss = maxrss * (PAGE_SIZE / 1024); /* convert pages to KBs */
  1221. }
  1222. int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
  1223. {
  1224. struct rusage r;
  1225. k_getrusage(p, who, &r);
  1226. return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
  1227. }
  1228. SYSCALL_DEFINE2(getrusage, int, who, struct rusage __user *, ru)
  1229. {
  1230. if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
  1231. who != RUSAGE_THREAD)
  1232. return -EINVAL;
  1233. return getrusage(current, who, ru);
  1234. }
  1235. SYSCALL_DEFINE1(umask, int, mask)
  1236. {
  1237. mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
  1238. return mask;
  1239. }
  1240. SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
  1241. unsigned long, arg4, unsigned long, arg5)
  1242. {
  1243. struct task_struct *me = current;
  1244. unsigned char comm[sizeof(me->comm)];
  1245. long error;
  1246. error = security_task_prctl(option, arg2, arg3, arg4, arg5);
  1247. if (error != -ENOSYS)
  1248. return error;
  1249. error = 0;
  1250. switch (option) {
  1251. case PR_SET_PDEATHSIG:
  1252. if (!valid_signal(arg2)) {
  1253. error = -EINVAL;
  1254. break;
  1255. }
  1256. me->pdeath_signal = arg2;
  1257. error = 0;
  1258. break;
  1259. case PR_GET_PDEATHSIG:
  1260. error = put_user(me->pdeath_signal, (int __user *)arg2);
  1261. break;
  1262. case PR_GET_DUMPABLE:
  1263. error = get_dumpable(me->mm);
  1264. break;
  1265. case PR_SET_DUMPABLE:
  1266. if (arg2 < 0 || arg2 > 1) {
  1267. error = -EINVAL;
  1268. break;
  1269. }
  1270. set_dumpable(me->mm, arg2);
  1271. error = 0;
  1272. break;
  1273. case PR_SET_UNALIGN:
  1274. error = SET_UNALIGN_CTL(me, arg2);
  1275. break;
  1276. case PR_GET_UNALIGN:
  1277. error = GET_UNALIGN_CTL(me, arg2);
  1278. break;
  1279. case PR_SET_FPEMU:
  1280. error = SET_FPEMU_CTL(me, arg2);
  1281. break;
  1282. case PR_GET_FPEMU:
  1283. error = GET_FPEMU_CTL(me, arg2);
  1284. break;
  1285. case PR_SET_FPEXC:
  1286. error = SET_FPEXC_CTL(me, arg2);
  1287. break;
  1288. case PR_GET_FPEXC:
  1289. error = GET_FPEXC_CTL(me, arg2);
  1290. break;
  1291. case PR_GET_TIMING:
  1292. error = PR_TIMING_STATISTICAL;
  1293. break;
  1294. case PR_SET_TIMING:
  1295. if (arg2 != PR_TIMING_STATISTICAL)
  1296. error = -EINVAL;
  1297. else
  1298. error = 0;
  1299. break;
  1300. case PR_SET_NAME:
  1301. comm[sizeof(me->comm)-1] = 0;
  1302. if (strncpy_from_user(comm, (char __user *)arg2,
  1303. sizeof(me->comm) - 1) < 0)
  1304. return -EFAULT;
  1305. set_task_comm(me, comm);
  1306. return 0;
  1307. case PR_GET_NAME:
  1308. get_task_comm(comm, me);
  1309. if (copy_to_user((char __user *)arg2, comm,
  1310. sizeof(comm)))
  1311. return -EFAULT;
  1312. return 0;
  1313. case PR_GET_ENDIAN:
  1314. error = GET_ENDIAN(me, arg2);
  1315. break;
  1316. case PR_SET_ENDIAN:
  1317. error = SET_ENDIAN(me, arg2);
  1318. break;
  1319. case PR_GET_SECCOMP:
  1320. error = prctl_get_seccomp();
  1321. break;
  1322. case PR_SET_SECCOMP:
  1323. error = prctl_set_seccomp(arg2);
  1324. break;
  1325. case PR_GET_TSC:
  1326. error = GET_TSC_CTL(arg2);
  1327. break;
  1328. case PR_SET_TSC:
  1329. error = SET_TSC_CTL(arg2);
  1330. break;
  1331. case PR_TASK_PERF_EVENTS_DISABLE:
  1332. error = perf_event_task_disable();
  1333. break;
  1334. case PR_TASK_PERF_EVENTS_ENABLE:
  1335. error = perf_event_task_enable();
  1336. break;
  1337. case PR_GET_TIMERSLACK:
  1338. error = current->timer_slack_ns;
  1339. break;
  1340. case PR_SET_TIMERSLACK:
  1341. if (arg2 <= 0)
  1342. current->timer_slack_ns =
  1343. current->default_timer_slack_ns;
  1344. else
  1345. current->timer_slack_ns = arg2;
  1346. error = 0;
  1347. break;
  1348. case PR_MCE_KILL:
  1349. if (arg4 | arg5)
  1350. return -EINVAL;
  1351. switch (arg2) {
  1352. case PR_MCE_KILL_CLEAR:
  1353. if (arg3 != 0)
  1354. return -EINVAL;
  1355. current->flags &= ~PF_MCE_PROCESS;
  1356. break;
  1357. case PR_MCE_KILL_SET:
  1358. current->flags |= PF_MCE_PROCESS;
  1359. if (arg3 == PR_MCE_KILL_EARLY)
  1360. current->flags |= PF_MCE_EARLY;
  1361. else if (arg3 == PR_MCE_KILL_LATE)
  1362. current->flags &= ~PF_MCE_EARLY;
  1363. else if (arg3 == PR_MCE_KILL_DEFAULT)
  1364. current->flags &=
  1365. ~(PF_MCE_EARLY|PF_MCE_PROCESS);
  1366. else
  1367. return -EINVAL;
  1368. break;
  1369. default:
  1370. return -EINVAL;
  1371. }
  1372. error = 0;
  1373. break;
  1374. case PR_MCE_KILL_GET:
  1375. if (arg2 | arg3 | arg4 | arg5)
  1376. return -EINVAL;
  1377. if (current->flags & PF_MCE_PROCESS)
  1378. error = (current->flags & PF_MCE_EARLY) ?
  1379. PR_MCE_KILL_EARLY : PR_MCE_KILL_LATE;
  1380. else
  1381. error = PR_MCE_KILL_DEFAULT;
  1382. break;
  1383. default:
  1384. error = -EINVAL;
  1385. break;
  1386. }
  1387. return error;
  1388. }
  1389. SYSCALL_DEFINE3(getcpu, unsigned __user *, cpup, unsigned __user *, nodep,
  1390. struct getcpu_cache __user *, unused)
  1391. {
  1392. int err = 0;
  1393. int cpu = raw_smp_processor_id();
  1394. if (cpup)
  1395. err |= put_user(cpu, cpup);
  1396. if (nodep)
  1397. err |= put_user(cpu_to_node(cpu), nodep);
  1398. return err ? -EFAULT : 0;
  1399. }
  1400. char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
  1401. static void argv_cleanup(char **argv, char **envp)
  1402. {
  1403. argv_free(argv);
  1404. }
  1405. /**
  1406. * orderly_poweroff - Trigger an orderly system poweroff
  1407. * @force: force poweroff if command execution fails
  1408. *
  1409. * This may be called from any context to trigger a system shutdown.
  1410. * If the orderly shutdown fails, it will force an immediate shutdown.
  1411. */
  1412. int orderly_poweroff(bool force)
  1413. {
  1414. int argc;
  1415. char **argv = argv_split(GFP_ATOMIC, poweroff_cmd, &argc);
  1416. static char *envp[] = {
  1417. "HOME=/",
  1418. "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
  1419. NULL
  1420. };
  1421. int ret = -ENOMEM;
  1422. struct subprocess_info *info;
  1423. if (argv == NULL) {
  1424. printk(KERN_WARNING "%s failed to allocate memory for \"%s\"\n",
  1425. __func__, poweroff_cmd);
  1426. goto out;
  1427. }
  1428. info = call_usermodehelper_setup(argv[0], argv, envp, GFP_ATOMIC);
  1429. if (info == NULL) {
  1430. argv_free(argv);
  1431. goto out;
  1432. }
  1433. call_usermodehelper_setcleanup(info, argv_cleanup);
  1434. ret = call_usermodehelper_exec(info, UMH_NO_WAIT);
  1435. out:
  1436. if (ret && force) {
  1437. printk(KERN_WARNING "Failed to start orderly shutdown: "
  1438. "forcing the issue\n");
  1439. /* I guess this should try to kick off some daemon to
  1440. sync and poweroff asap. Or not even bother syncing
  1441. if we're doing an emergency shutdown? */
  1442. emergency_sync();
  1443. kernel_power_off();
  1444. }
  1445. return ret;
  1446. }
  1447. EXPORT_SYMBOL_GPL(orderly_poweroff);