sys.c 39 KB

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