sys.c 36 KB

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