sys.c 37 KB

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