sys.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807
  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 = -EPERM;
  443. if (rgid != (gid_t) -1) {
  444. if (old->gid == rgid ||
  445. old->egid == rgid ||
  446. capable(CAP_SETGID))
  447. new->gid = rgid;
  448. else
  449. goto error;
  450. }
  451. if (egid != (gid_t) -1) {
  452. if (old->gid == egid ||
  453. old->egid == egid ||
  454. old->sgid == egid ||
  455. capable(CAP_SETGID))
  456. new->egid = egid;
  457. else
  458. goto error;
  459. }
  460. if (rgid != (gid_t) -1 ||
  461. (egid != (gid_t) -1 && egid != old->gid))
  462. new->sgid = new->egid;
  463. new->fsgid = new->egid;
  464. return commit_creds(new);
  465. error:
  466. abort_creds(new);
  467. return retval;
  468. }
  469. /*
  470. * setgid() is implemented like SysV w/ SAVED_IDS
  471. *
  472. * SMP: Same implicit races as above.
  473. */
  474. SYSCALL_DEFINE1(setgid, gid_t, gid)
  475. {
  476. const struct cred *old;
  477. struct cred *new;
  478. int retval;
  479. new = prepare_creds();
  480. if (!new)
  481. return -ENOMEM;
  482. old = current_cred();
  483. retval = -EPERM;
  484. if (capable(CAP_SETGID))
  485. new->gid = new->egid = new->sgid = new->fsgid = gid;
  486. else if (gid == old->gid || gid == old->sgid)
  487. new->egid = new->fsgid = gid;
  488. else
  489. goto error;
  490. return commit_creds(new);
  491. error:
  492. abort_creds(new);
  493. return retval;
  494. }
  495. /*
  496. * change the user struct in a credentials set to match the new UID
  497. */
  498. static int set_user(struct cred *new)
  499. {
  500. struct user_struct *new_user;
  501. new_user = alloc_uid(current_user_ns(), new->uid);
  502. if (!new_user)
  503. return -EAGAIN;
  504. if (atomic_read(&new_user->processes) >= rlimit(RLIMIT_NPROC) &&
  505. new_user != INIT_USER) {
  506. free_uid(new_user);
  507. return -EAGAIN;
  508. }
  509. free_uid(new->user);
  510. new->user = new_user;
  511. return 0;
  512. }
  513. /*
  514. * Unprivileged users may change the real uid to the effective uid
  515. * or vice versa. (BSD-style)
  516. *
  517. * If you set the real uid at all, or set the effective uid to a value not
  518. * equal to the real uid, then the saved uid is set to the new effective uid.
  519. *
  520. * This makes it possible for a setuid program to completely drop its
  521. * privileges, which is often a useful assertion to make when you are doing
  522. * a security audit over a program.
  523. *
  524. * The general idea is that a program which uses just setreuid() will be
  525. * 100% compatible with BSD. A program which uses just setuid() will be
  526. * 100% compatible with POSIX with saved IDs.
  527. */
  528. SYSCALL_DEFINE2(setreuid, uid_t, ruid, uid_t, euid)
  529. {
  530. const struct cred *old;
  531. struct cred *new;
  532. int retval;
  533. new = prepare_creds();
  534. if (!new)
  535. return -ENOMEM;
  536. old = current_cred();
  537. retval = -EPERM;
  538. if (ruid != (uid_t) -1) {
  539. new->uid = ruid;
  540. if (old->uid != ruid &&
  541. old->euid != ruid &&
  542. !capable(CAP_SETUID))
  543. goto error;
  544. }
  545. if (euid != (uid_t) -1) {
  546. new->euid = euid;
  547. if (old->uid != euid &&
  548. old->euid != euid &&
  549. old->suid != euid &&
  550. !capable(CAP_SETUID))
  551. goto error;
  552. }
  553. if (new->uid != old->uid) {
  554. retval = set_user(new);
  555. if (retval < 0)
  556. goto error;
  557. }
  558. if (ruid != (uid_t) -1 ||
  559. (euid != (uid_t) -1 && euid != old->uid))
  560. new->suid = new->euid;
  561. new->fsuid = new->euid;
  562. retval = security_task_fix_setuid(new, old, LSM_SETID_RE);
  563. if (retval < 0)
  564. goto error;
  565. return commit_creds(new);
  566. error:
  567. abort_creds(new);
  568. return retval;
  569. }
  570. /*
  571. * setuid() is implemented like SysV with SAVED_IDS
  572. *
  573. * Note that SAVED_ID's is deficient in that a setuid root program
  574. * like sendmail, for example, cannot set its uid to be a normal
  575. * user and then switch back, because if you're root, setuid() sets
  576. * the saved uid too. If you don't like this, blame the bright people
  577. * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
  578. * will allow a root program to temporarily drop privileges and be able to
  579. * regain them by swapping the real and effective uid.
  580. */
  581. SYSCALL_DEFINE1(setuid, uid_t, uid)
  582. {
  583. const struct cred *old;
  584. struct cred *new;
  585. int retval;
  586. new = prepare_creds();
  587. if (!new)
  588. return -ENOMEM;
  589. old = current_cred();
  590. retval = -EPERM;
  591. if (capable(CAP_SETUID)) {
  592. new->suid = new->uid = uid;
  593. if (uid != old->uid) {
  594. retval = set_user(new);
  595. if (retval < 0)
  596. goto error;
  597. }
  598. } else if (uid != old->uid && uid != new->suid) {
  599. goto error;
  600. }
  601. new->fsuid = new->euid = uid;
  602. retval = security_task_fix_setuid(new, old, LSM_SETID_ID);
  603. if (retval < 0)
  604. goto error;
  605. return commit_creds(new);
  606. error:
  607. abort_creds(new);
  608. return retval;
  609. }
  610. /*
  611. * This function implements a generic ability to update ruid, euid,
  612. * and suid. This allows you to implement the 4.4 compatible seteuid().
  613. */
  614. SYSCALL_DEFINE3(setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
  615. {
  616. const struct cred *old;
  617. struct cred *new;
  618. int retval;
  619. new = prepare_creds();
  620. if (!new)
  621. return -ENOMEM;
  622. old = current_cred();
  623. retval = -EPERM;
  624. if (!capable(CAP_SETUID)) {
  625. if (ruid != (uid_t) -1 && ruid != old->uid &&
  626. ruid != old->euid && ruid != old->suid)
  627. goto error;
  628. if (euid != (uid_t) -1 && euid != old->uid &&
  629. euid != old->euid && euid != old->suid)
  630. goto error;
  631. if (suid != (uid_t) -1 && suid != old->uid &&
  632. suid != old->euid && suid != old->suid)
  633. goto error;
  634. }
  635. if (ruid != (uid_t) -1) {
  636. new->uid = ruid;
  637. if (ruid != old->uid) {
  638. retval = set_user(new);
  639. if (retval < 0)
  640. goto error;
  641. }
  642. }
  643. if (euid != (uid_t) -1)
  644. new->euid = euid;
  645. if (suid != (uid_t) -1)
  646. new->suid = suid;
  647. new->fsuid = new->euid;
  648. retval = security_task_fix_setuid(new, old, LSM_SETID_RES);
  649. if (retval < 0)
  650. goto error;
  651. return commit_creds(new);
  652. error:
  653. abort_creds(new);
  654. return retval;
  655. }
  656. SYSCALL_DEFINE3(getresuid, uid_t __user *, ruid, uid_t __user *, euid, uid_t __user *, suid)
  657. {
  658. const struct cred *cred = current_cred();
  659. int retval;
  660. if (!(retval = put_user(cred->uid, ruid)) &&
  661. !(retval = put_user(cred->euid, euid)))
  662. retval = put_user(cred->suid, suid);
  663. return retval;
  664. }
  665. /*
  666. * Same as above, but for rgid, egid, sgid.
  667. */
  668. SYSCALL_DEFINE3(setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
  669. {
  670. const struct cred *old;
  671. struct cred *new;
  672. int retval;
  673. new = prepare_creds();
  674. if (!new)
  675. return -ENOMEM;
  676. old = current_cred();
  677. retval = -EPERM;
  678. if (!capable(CAP_SETGID)) {
  679. if (rgid != (gid_t) -1 && rgid != old->gid &&
  680. rgid != old->egid && rgid != old->sgid)
  681. goto error;
  682. if (egid != (gid_t) -1 && egid != old->gid &&
  683. egid != old->egid && egid != old->sgid)
  684. goto error;
  685. if (sgid != (gid_t) -1 && sgid != old->gid &&
  686. sgid != old->egid && sgid != old->sgid)
  687. goto error;
  688. }
  689. if (rgid != (gid_t) -1)
  690. new->gid = rgid;
  691. if (egid != (gid_t) -1)
  692. new->egid = egid;
  693. if (sgid != (gid_t) -1)
  694. new->sgid = sgid;
  695. new->fsgid = new->egid;
  696. return commit_creds(new);
  697. error:
  698. abort_creds(new);
  699. return retval;
  700. }
  701. SYSCALL_DEFINE3(getresgid, gid_t __user *, rgid, gid_t __user *, egid, gid_t __user *, sgid)
  702. {
  703. const struct cred *cred = current_cred();
  704. int retval;
  705. if (!(retval = put_user(cred->gid, rgid)) &&
  706. !(retval = put_user(cred->egid, egid)))
  707. retval = put_user(cred->sgid, sgid);
  708. return retval;
  709. }
  710. /*
  711. * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
  712. * is used for "access()" and for the NFS daemon (letting nfsd stay at
  713. * whatever uid it wants to). It normally shadows "euid", except when
  714. * explicitly set by setfsuid() or for access..
  715. */
  716. SYSCALL_DEFINE1(setfsuid, uid_t, uid)
  717. {
  718. const struct cred *old;
  719. struct cred *new;
  720. uid_t old_fsuid;
  721. new = prepare_creds();
  722. if (!new)
  723. return current_fsuid();
  724. old = current_cred();
  725. old_fsuid = old->fsuid;
  726. if (uid == old->uid || uid == old->euid ||
  727. uid == old->suid || uid == old->fsuid ||
  728. capable(CAP_SETUID)) {
  729. if (uid != old_fsuid) {
  730. new->fsuid = uid;
  731. if (security_task_fix_setuid(new, old, LSM_SETID_FS) == 0)
  732. goto change_okay;
  733. }
  734. }
  735. abort_creds(new);
  736. return old_fsuid;
  737. change_okay:
  738. commit_creds(new);
  739. return old_fsuid;
  740. }
  741. /*
  742. * Samma på svenska..
  743. */
  744. SYSCALL_DEFINE1(setfsgid, gid_t, gid)
  745. {
  746. const struct cred *old;
  747. struct cred *new;
  748. gid_t old_fsgid;
  749. new = prepare_creds();
  750. if (!new)
  751. return current_fsgid();
  752. old = current_cred();
  753. old_fsgid = old->fsgid;
  754. if (gid == old->gid || gid == old->egid ||
  755. gid == old->sgid || gid == old->fsgid ||
  756. capable(CAP_SETGID)) {
  757. if (gid != old_fsgid) {
  758. new->fsgid = gid;
  759. goto change_okay;
  760. }
  761. }
  762. abort_creds(new);
  763. return old_fsgid;
  764. change_okay:
  765. commit_creds(new);
  766. return old_fsgid;
  767. }
  768. void do_sys_times(struct tms *tms)
  769. {
  770. cputime_t tgutime, tgstime, cutime, cstime;
  771. spin_lock_irq(&current->sighand->siglock);
  772. thread_group_times(current, &tgutime, &tgstime);
  773. cutime = current->signal->cutime;
  774. cstime = current->signal->cstime;
  775. spin_unlock_irq(&current->sighand->siglock);
  776. tms->tms_utime = cputime_to_clock_t(tgutime);
  777. tms->tms_stime = cputime_to_clock_t(tgstime);
  778. tms->tms_cutime = cputime_to_clock_t(cutime);
  779. tms->tms_cstime = cputime_to_clock_t(cstime);
  780. }
  781. SYSCALL_DEFINE1(times, struct tms __user *, tbuf)
  782. {
  783. if (tbuf) {
  784. struct tms tmp;
  785. do_sys_times(&tmp);
  786. if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
  787. return -EFAULT;
  788. }
  789. force_successful_syscall_return();
  790. return (long) jiffies_64_to_clock_t(get_jiffies_64());
  791. }
  792. /*
  793. * This needs some heavy checking ...
  794. * I just haven't the stomach for it. I also don't fully
  795. * understand sessions/pgrp etc. Let somebody who does explain it.
  796. *
  797. * OK, I think I have the protection semantics right.... this is really
  798. * only important on a multi-user system anyway, to make sure one user
  799. * can't send a signal to a process owned by another. -TYT, 12/12/91
  800. *
  801. * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
  802. * LBT 04.03.94
  803. */
  804. SYSCALL_DEFINE2(setpgid, pid_t, pid, pid_t, pgid)
  805. {
  806. struct task_struct *p;
  807. struct task_struct *group_leader = current->group_leader;
  808. struct pid *pgrp;
  809. int err;
  810. if (!pid)
  811. pid = task_pid_vnr(group_leader);
  812. if (!pgid)
  813. pgid = pid;
  814. if (pgid < 0)
  815. return -EINVAL;
  816. rcu_read_lock();
  817. /* From this point forward we keep holding onto the tasklist lock
  818. * so that our parent does not change from under us. -DaveM
  819. */
  820. write_lock_irq(&tasklist_lock);
  821. err = -ESRCH;
  822. p = find_task_by_vpid(pid);
  823. if (!p)
  824. goto out;
  825. err = -EINVAL;
  826. if (!thread_group_leader(p))
  827. goto out;
  828. if (same_thread_group(p->real_parent, group_leader)) {
  829. err = -EPERM;
  830. if (task_session(p) != task_session(group_leader))
  831. goto out;
  832. err = -EACCES;
  833. if (p->did_exec)
  834. goto out;
  835. } else {
  836. err = -ESRCH;
  837. if (p != group_leader)
  838. goto out;
  839. }
  840. err = -EPERM;
  841. if (p->signal->leader)
  842. goto out;
  843. pgrp = task_pid(p);
  844. if (pgid != pid) {
  845. struct task_struct *g;
  846. pgrp = find_vpid(pgid);
  847. g = pid_task(pgrp, PIDTYPE_PGID);
  848. if (!g || task_session(g) != task_session(group_leader))
  849. goto out;
  850. }
  851. err = security_task_setpgid(p, pgid);
  852. if (err)
  853. goto out;
  854. if (task_pgrp(p) != pgrp)
  855. change_pid(p, PIDTYPE_PGID, pgrp);
  856. err = 0;
  857. out:
  858. /* All paths lead to here, thus we are safe. -DaveM */
  859. write_unlock_irq(&tasklist_lock);
  860. rcu_read_unlock();
  861. return err;
  862. }
  863. SYSCALL_DEFINE1(getpgid, pid_t, pid)
  864. {
  865. struct task_struct *p;
  866. struct pid *grp;
  867. int retval;
  868. rcu_read_lock();
  869. if (!pid)
  870. grp = task_pgrp(current);
  871. else {
  872. retval = -ESRCH;
  873. p = find_task_by_vpid(pid);
  874. if (!p)
  875. goto out;
  876. grp = task_pgrp(p);
  877. if (!grp)
  878. goto out;
  879. retval = security_task_getpgid(p);
  880. if (retval)
  881. goto out;
  882. }
  883. retval = pid_vnr(grp);
  884. out:
  885. rcu_read_unlock();
  886. return retval;
  887. }
  888. #ifdef __ARCH_WANT_SYS_GETPGRP
  889. SYSCALL_DEFINE0(getpgrp)
  890. {
  891. return sys_getpgid(0);
  892. }
  893. #endif
  894. SYSCALL_DEFINE1(getsid, pid_t, pid)
  895. {
  896. struct task_struct *p;
  897. struct pid *sid;
  898. int retval;
  899. rcu_read_lock();
  900. if (!pid)
  901. sid = task_session(current);
  902. else {
  903. retval = -ESRCH;
  904. p = find_task_by_vpid(pid);
  905. if (!p)
  906. goto out;
  907. sid = task_session(p);
  908. if (!sid)
  909. goto out;
  910. retval = security_task_getsid(p);
  911. if (retval)
  912. goto out;
  913. }
  914. retval = pid_vnr(sid);
  915. out:
  916. rcu_read_unlock();
  917. return retval;
  918. }
  919. SYSCALL_DEFINE0(setsid)
  920. {
  921. struct task_struct *group_leader = current->group_leader;
  922. struct pid *sid = task_pid(group_leader);
  923. pid_t session = pid_vnr(sid);
  924. int err = -EPERM;
  925. write_lock_irq(&tasklist_lock);
  926. /* Fail if I am already a session leader */
  927. if (group_leader->signal->leader)
  928. goto out;
  929. /* Fail if a process group id already exists that equals the
  930. * proposed session id.
  931. */
  932. if (pid_task(sid, PIDTYPE_PGID))
  933. goto out;
  934. group_leader->signal->leader = 1;
  935. __set_special_pids(sid);
  936. proc_clear_tty(group_leader);
  937. err = session;
  938. out:
  939. write_unlock_irq(&tasklist_lock);
  940. if (err > 0) {
  941. proc_sid_connector(group_leader);
  942. sched_autogroup_create_attach(group_leader);
  943. }
  944. return err;
  945. }
  946. DECLARE_RWSEM(uts_sem);
  947. #ifdef COMPAT_UTS_MACHINE
  948. #define override_architecture(name) \
  949. (personality(current->personality) == PER_LINUX32 && \
  950. copy_to_user(name->machine, COMPAT_UTS_MACHINE, \
  951. sizeof(COMPAT_UTS_MACHINE)))
  952. #else
  953. #define override_architecture(name) 0
  954. #endif
  955. SYSCALL_DEFINE1(newuname, struct new_utsname __user *, name)
  956. {
  957. int errno = 0;
  958. down_read(&uts_sem);
  959. if (copy_to_user(name, utsname(), sizeof *name))
  960. errno = -EFAULT;
  961. up_read(&uts_sem);
  962. if (!errno && override_architecture(name))
  963. errno = -EFAULT;
  964. return errno;
  965. }
  966. #ifdef __ARCH_WANT_SYS_OLD_UNAME
  967. /*
  968. * Old cruft
  969. */
  970. SYSCALL_DEFINE1(uname, struct old_utsname __user *, name)
  971. {
  972. int error = 0;
  973. if (!name)
  974. return -EFAULT;
  975. down_read(&uts_sem);
  976. if (copy_to_user(name, utsname(), sizeof(*name)))
  977. error = -EFAULT;
  978. up_read(&uts_sem);
  979. if (!error && override_architecture(name))
  980. error = -EFAULT;
  981. return error;
  982. }
  983. SYSCALL_DEFINE1(olduname, struct oldold_utsname __user *, name)
  984. {
  985. int error;
  986. if (!name)
  987. return -EFAULT;
  988. if (!access_ok(VERIFY_WRITE, name, sizeof(struct oldold_utsname)))
  989. return -EFAULT;
  990. down_read(&uts_sem);
  991. error = __copy_to_user(&name->sysname, &utsname()->sysname,
  992. __OLD_UTS_LEN);
  993. error |= __put_user(0, name->sysname + __OLD_UTS_LEN);
  994. error |= __copy_to_user(&name->nodename, &utsname()->nodename,
  995. __OLD_UTS_LEN);
  996. error |= __put_user(0, name->nodename + __OLD_UTS_LEN);
  997. error |= __copy_to_user(&name->release, &utsname()->release,
  998. __OLD_UTS_LEN);
  999. error |= __put_user(0, name->release + __OLD_UTS_LEN);
  1000. error |= __copy_to_user(&name->version, &utsname()->version,
  1001. __OLD_UTS_LEN);
  1002. error |= __put_user(0, name->version + __OLD_UTS_LEN);
  1003. error |= __copy_to_user(&name->machine, &utsname()->machine,
  1004. __OLD_UTS_LEN);
  1005. error |= __put_user(0, name->machine + __OLD_UTS_LEN);
  1006. up_read(&uts_sem);
  1007. if (!error && override_architecture(name))
  1008. error = -EFAULT;
  1009. return error ? -EFAULT : 0;
  1010. }
  1011. #endif
  1012. SYSCALL_DEFINE2(sethostname, char __user *, name, int, len)
  1013. {
  1014. int errno;
  1015. char tmp[__NEW_UTS_LEN];
  1016. if (!capable(CAP_SYS_ADMIN))
  1017. return -EPERM;
  1018. if (len < 0 || len > __NEW_UTS_LEN)
  1019. return -EINVAL;
  1020. down_write(&uts_sem);
  1021. errno = -EFAULT;
  1022. if (!copy_from_user(tmp, name, len)) {
  1023. struct new_utsname *u = utsname();
  1024. memcpy(u->nodename, tmp, len);
  1025. memset(u->nodename + len, 0, sizeof(u->nodename) - len);
  1026. errno = 0;
  1027. }
  1028. up_write(&uts_sem);
  1029. return errno;
  1030. }
  1031. #ifdef __ARCH_WANT_SYS_GETHOSTNAME
  1032. SYSCALL_DEFINE2(gethostname, char __user *, name, int, len)
  1033. {
  1034. int i, errno;
  1035. struct new_utsname *u;
  1036. if (len < 0)
  1037. return -EINVAL;
  1038. down_read(&uts_sem);
  1039. u = utsname();
  1040. i = 1 + strlen(u->nodename);
  1041. if (i > len)
  1042. i = len;
  1043. errno = 0;
  1044. if (copy_to_user(name, u->nodename, i))
  1045. errno = -EFAULT;
  1046. up_read(&uts_sem);
  1047. return errno;
  1048. }
  1049. #endif
  1050. /*
  1051. * Only setdomainname; getdomainname can be implemented by calling
  1052. * uname()
  1053. */
  1054. SYSCALL_DEFINE2(setdomainname, char __user *, name, int, len)
  1055. {
  1056. int errno;
  1057. char tmp[__NEW_UTS_LEN];
  1058. if (!capable(CAP_SYS_ADMIN))
  1059. return -EPERM;
  1060. if (len < 0 || len > __NEW_UTS_LEN)
  1061. return -EINVAL;
  1062. down_write(&uts_sem);
  1063. errno = -EFAULT;
  1064. if (!copy_from_user(tmp, name, len)) {
  1065. struct new_utsname *u = utsname();
  1066. memcpy(u->domainname, tmp, len);
  1067. memset(u->domainname + len, 0, sizeof(u->domainname) - len);
  1068. errno = 0;
  1069. }
  1070. up_write(&uts_sem);
  1071. return errno;
  1072. }
  1073. SYSCALL_DEFINE2(getrlimit, unsigned int, resource, struct rlimit __user *, rlim)
  1074. {
  1075. struct rlimit value;
  1076. int ret;
  1077. ret = do_prlimit(current, resource, NULL, &value);
  1078. if (!ret)
  1079. ret = copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
  1080. return ret;
  1081. }
  1082. #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
  1083. /*
  1084. * Back compatibility for getrlimit. Needed for some apps.
  1085. */
  1086. SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
  1087. struct rlimit __user *, rlim)
  1088. {
  1089. struct rlimit x;
  1090. if (resource >= RLIM_NLIMITS)
  1091. return -EINVAL;
  1092. task_lock(current->group_leader);
  1093. x = current->signal->rlim[resource];
  1094. task_unlock(current->group_leader);
  1095. if (x.rlim_cur > 0x7FFFFFFF)
  1096. x.rlim_cur = 0x7FFFFFFF;
  1097. if (x.rlim_max > 0x7FFFFFFF)
  1098. x.rlim_max = 0x7FFFFFFF;
  1099. return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
  1100. }
  1101. #endif
  1102. static inline bool rlim64_is_infinity(__u64 rlim64)
  1103. {
  1104. #if BITS_PER_LONG < 64
  1105. return rlim64 >= ULONG_MAX;
  1106. #else
  1107. return rlim64 == RLIM64_INFINITY;
  1108. #endif
  1109. }
  1110. static void rlim_to_rlim64(const struct rlimit *rlim, struct rlimit64 *rlim64)
  1111. {
  1112. if (rlim->rlim_cur == RLIM_INFINITY)
  1113. rlim64->rlim_cur = RLIM64_INFINITY;
  1114. else
  1115. rlim64->rlim_cur = rlim->rlim_cur;
  1116. if (rlim->rlim_max == RLIM_INFINITY)
  1117. rlim64->rlim_max = RLIM64_INFINITY;
  1118. else
  1119. rlim64->rlim_max = rlim->rlim_max;
  1120. }
  1121. static void rlim64_to_rlim(const struct rlimit64 *rlim64, struct rlimit *rlim)
  1122. {
  1123. if (rlim64_is_infinity(rlim64->rlim_cur))
  1124. rlim->rlim_cur = RLIM_INFINITY;
  1125. else
  1126. rlim->rlim_cur = (unsigned long)rlim64->rlim_cur;
  1127. if (rlim64_is_infinity(rlim64->rlim_max))
  1128. rlim->rlim_max = RLIM_INFINITY;
  1129. else
  1130. rlim->rlim_max = (unsigned long)rlim64->rlim_max;
  1131. }
  1132. /* make sure you are allowed to change @tsk limits before calling this */
  1133. int do_prlimit(struct task_struct *tsk, unsigned int resource,
  1134. struct rlimit *new_rlim, struct rlimit *old_rlim)
  1135. {
  1136. struct rlimit *rlim;
  1137. int retval = 0;
  1138. if (resource >= RLIM_NLIMITS)
  1139. return -EINVAL;
  1140. if (new_rlim) {
  1141. if (new_rlim->rlim_cur > new_rlim->rlim_max)
  1142. return -EINVAL;
  1143. if (resource == RLIMIT_NOFILE &&
  1144. new_rlim->rlim_max > sysctl_nr_open)
  1145. return -EPERM;
  1146. }
  1147. /* protect tsk->signal and tsk->sighand from disappearing */
  1148. read_lock(&tasklist_lock);
  1149. if (!tsk->sighand) {
  1150. retval = -ESRCH;
  1151. goto out;
  1152. }
  1153. rlim = tsk->signal->rlim + resource;
  1154. task_lock(tsk->group_leader);
  1155. if (new_rlim) {
  1156. if (new_rlim->rlim_max > rlim->rlim_max &&
  1157. !capable(CAP_SYS_RESOURCE))
  1158. retval = -EPERM;
  1159. if (!retval)
  1160. retval = security_task_setrlimit(tsk->group_leader,
  1161. resource, new_rlim);
  1162. if (resource == RLIMIT_CPU && new_rlim->rlim_cur == 0) {
  1163. /*
  1164. * The caller is asking for an immediate RLIMIT_CPU
  1165. * expiry. But we use the zero value to mean "it was
  1166. * never set". So let's cheat and make it one second
  1167. * instead
  1168. */
  1169. new_rlim->rlim_cur = 1;
  1170. }
  1171. }
  1172. if (!retval) {
  1173. if (old_rlim)
  1174. *old_rlim = *rlim;
  1175. if (new_rlim)
  1176. *rlim = *new_rlim;
  1177. }
  1178. task_unlock(tsk->group_leader);
  1179. /*
  1180. * RLIMIT_CPU handling. Note that the kernel fails to return an error
  1181. * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
  1182. * very long-standing error, and fixing it now risks breakage of
  1183. * applications, so we live with it
  1184. */
  1185. if (!retval && new_rlim && resource == RLIMIT_CPU &&
  1186. new_rlim->rlim_cur != RLIM_INFINITY)
  1187. update_rlimit_cpu(tsk, new_rlim->rlim_cur);
  1188. out:
  1189. read_unlock(&tasklist_lock);
  1190. return retval;
  1191. }
  1192. /* rcu lock must be held */
  1193. static int check_prlimit_permission(struct task_struct *task)
  1194. {
  1195. const struct cred *cred = current_cred(), *tcred;
  1196. tcred = __task_cred(task);
  1197. if ((cred->uid != tcred->euid ||
  1198. cred->uid != tcred->suid ||
  1199. cred->uid != tcred->uid ||
  1200. cred->gid != tcred->egid ||
  1201. cred->gid != tcred->sgid ||
  1202. cred->gid != tcred->gid) &&
  1203. !capable(CAP_SYS_RESOURCE)) {
  1204. return -EPERM;
  1205. }
  1206. return 0;
  1207. }
  1208. SYSCALL_DEFINE4(prlimit64, pid_t, pid, unsigned int, resource,
  1209. const struct rlimit64 __user *, new_rlim,
  1210. struct rlimit64 __user *, old_rlim)
  1211. {
  1212. struct rlimit64 old64, new64;
  1213. struct rlimit old, new;
  1214. struct task_struct *tsk;
  1215. int ret;
  1216. if (new_rlim) {
  1217. if (copy_from_user(&new64, new_rlim, sizeof(new64)))
  1218. return -EFAULT;
  1219. rlim64_to_rlim(&new64, &new);
  1220. }
  1221. rcu_read_lock();
  1222. tsk = pid ? find_task_by_vpid(pid) : current;
  1223. if (!tsk) {
  1224. rcu_read_unlock();
  1225. return -ESRCH;
  1226. }
  1227. ret = check_prlimit_permission(tsk);
  1228. if (ret) {
  1229. rcu_read_unlock();
  1230. return ret;
  1231. }
  1232. get_task_struct(tsk);
  1233. rcu_read_unlock();
  1234. ret = do_prlimit(tsk, resource, new_rlim ? &new : NULL,
  1235. old_rlim ? &old : NULL);
  1236. if (!ret && old_rlim) {
  1237. rlim_to_rlim64(&old, &old64);
  1238. if (copy_to_user(old_rlim, &old64, sizeof(old64)))
  1239. ret = -EFAULT;
  1240. }
  1241. put_task_struct(tsk);
  1242. return ret;
  1243. }
  1244. SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim)
  1245. {
  1246. struct rlimit new_rlim;
  1247. if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
  1248. return -EFAULT;
  1249. return do_prlimit(current, resource, &new_rlim, NULL);
  1250. }
  1251. /*
  1252. * It would make sense to put struct rusage in the task_struct,
  1253. * except that would make the task_struct be *really big*. After
  1254. * task_struct gets moved into malloc'ed memory, it would
  1255. * make sense to do this. It will make moving the rest of the information
  1256. * a lot simpler! (Which we're not doing right now because we're not
  1257. * measuring them yet).
  1258. *
  1259. * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
  1260. * races with threads incrementing their own counters. But since word
  1261. * reads are atomic, we either get new values or old values and we don't
  1262. * care which for the sums. We always take the siglock to protect reading
  1263. * the c* fields from p->signal from races with exit.c updating those
  1264. * fields when reaping, so a sample either gets all the additions of a
  1265. * given child after it's reaped, or none so this sample is before reaping.
  1266. *
  1267. * Locking:
  1268. * We need to take the siglock for CHILDEREN, SELF and BOTH
  1269. * for the cases current multithreaded, non-current single threaded
  1270. * non-current multithreaded. Thread traversal is now safe with
  1271. * the siglock held.
  1272. * Strictly speaking, we donot need to take the siglock if we are current and
  1273. * single threaded, as no one else can take our signal_struct away, no one
  1274. * else can reap the children to update signal->c* counters, and no one else
  1275. * can race with the signal-> fields. If we do not take any lock, the
  1276. * signal-> fields could be read out of order while another thread was just
  1277. * exiting. So we should place a read memory barrier when we avoid the lock.
  1278. * On the writer side, write memory barrier is implied in __exit_signal
  1279. * as __exit_signal releases the siglock spinlock after updating the signal->
  1280. * fields. But we don't do this yet to keep things simple.
  1281. *
  1282. */
  1283. static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r)
  1284. {
  1285. r->ru_nvcsw += t->nvcsw;
  1286. r->ru_nivcsw += t->nivcsw;
  1287. r->ru_minflt += t->min_flt;
  1288. r->ru_majflt += t->maj_flt;
  1289. r->ru_inblock += task_io_get_inblock(t);
  1290. r->ru_oublock += task_io_get_oublock(t);
  1291. }
  1292. static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
  1293. {
  1294. struct task_struct *t;
  1295. unsigned long flags;
  1296. cputime_t tgutime, tgstime, utime, stime;
  1297. unsigned long maxrss = 0;
  1298. memset((char *) r, 0, sizeof *r);
  1299. utime = stime = cputime_zero;
  1300. if (who == RUSAGE_THREAD) {
  1301. task_times(current, &utime, &stime);
  1302. accumulate_thread_rusage(p, r);
  1303. maxrss = p->signal->maxrss;
  1304. goto out;
  1305. }
  1306. if (!lock_task_sighand(p, &flags))
  1307. return;
  1308. switch (who) {
  1309. case RUSAGE_BOTH:
  1310. case RUSAGE_CHILDREN:
  1311. utime = p->signal->cutime;
  1312. stime = p->signal->cstime;
  1313. r->ru_nvcsw = p->signal->cnvcsw;
  1314. r->ru_nivcsw = p->signal->cnivcsw;
  1315. r->ru_minflt = p->signal->cmin_flt;
  1316. r->ru_majflt = p->signal->cmaj_flt;
  1317. r->ru_inblock = p->signal->cinblock;
  1318. r->ru_oublock = p->signal->coublock;
  1319. maxrss = p->signal->cmaxrss;
  1320. if (who == RUSAGE_CHILDREN)
  1321. break;
  1322. case RUSAGE_SELF:
  1323. thread_group_times(p, &tgutime, &tgstime);
  1324. utime = cputime_add(utime, tgutime);
  1325. stime = cputime_add(stime, tgstime);
  1326. r->ru_nvcsw += p->signal->nvcsw;
  1327. r->ru_nivcsw += p->signal->nivcsw;
  1328. r->ru_minflt += p->signal->min_flt;
  1329. r->ru_majflt += p->signal->maj_flt;
  1330. r->ru_inblock += p->signal->inblock;
  1331. r->ru_oublock += p->signal->oublock;
  1332. if (maxrss < p->signal->maxrss)
  1333. maxrss = p->signal->maxrss;
  1334. t = p;
  1335. do {
  1336. accumulate_thread_rusage(t, r);
  1337. t = next_thread(t);
  1338. } while (t != p);
  1339. break;
  1340. default:
  1341. BUG();
  1342. }
  1343. unlock_task_sighand(p, &flags);
  1344. out:
  1345. cputime_to_timeval(utime, &r->ru_utime);
  1346. cputime_to_timeval(stime, &r->ru_stime);
  1347. if (who != RUSAGE_CHILDREN) {
  1348. struct mm_struct *mm = get_task_mm(p);
  1349. if (mm) {
  1350. setmax_mm_hiwater_rss(&maxrss, mm);
  1351. mmput(mm);
  1352. }
  1353. }
  1354. r->ru_maxrss = maxrss * (PAGE_SIZE / 1024); /* convert pages to KBs */
  1355. }
  1356. int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
  1357. {
  1358. struct rusage r;
  1359. k_getrusage(p, who, &r);
  1360. return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
  1361. }
  1362. SYSCALL_DEFINE2(getrusage, int, who, struct rusage __user *, ru)
  1363. {
  1364. if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
  1365. who != RUSAGE_THREAD)
  1366. return -EINVAL;
  1367. return getrusage(current, who, ru);
  1368. }
  1369. SYSCALL_DEFINE1(umask, int, mask)
  1370. {
  1371. mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
  1372. return mask;
  1373. }
  1374. SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
  1375. unsigned long, arg4, unsigned long, arg5)
  1376. {
  1377. struct task_struct *me = current;
  1378. unsigned char comm[sizeof(me->comm)];
  1379. long error;
  1380. error = security_task_prctl(option, arg2, arg3, arg4, arg5);
  1381. if (error != -ENOSYS)
  1382. return error;
  1383. error = 0;
  1384. switch (option) {
  1385. case PR_SET_PDEATHSIG:
  1386. if (!valid_signal(arg2)) {
  1387. error = -EINVAL;
  1388. break;
  1389. }
  1390. me->pdeath_signal = arg2;
  1391. error = 0;
  1392. break;
  1393. case PR_GET_PDEATHSIG:
  1394. error = put_user(me->pdeath_signal, (int __user *)arg2);
  1395. break;
  1396. case PR_GET_DUMPABLE:
  1397. error = get_dumpable(me->mm);
  1398. break;
  1399. case PR_SET_DUMPABLE:
  1400. if (arg2 < 0 || arg2 > 1) {
  1401. error = -EINVAL;
  1402. break;
  1403. }
  1404. set_dumpable(me->mm, arg2);
  1405. error = 0;
  1406. break;
  1407. case PR_SET_UNALIGN:
  1408. error = SET_UNALIGN_CTL(me, arg2);
  1409. break;
  1410. case PR_GET_UNALIGN:
  1411. error = GET_UNALIGN_CTL(me, arg2);
  1412. break;
  1413. case PR_SET_FPEMU:
  1414. error = SET_FPEMU_CTL(me, arg2);
  1415. break;
  1416. case PR_GET_FPEMU:
  1417. error = GET_FPEMU_CTL(me, arg2);
  1418. break;
  1419. case PR_SET_FPEXC:
  1420. error = SET_FPEXC_CTL(me, arg2);
  1421. break;
  1422. case PR_GET_FPEXC:
  1423. error = GET_FPEXC_CTL(me, arg2);
  1424. break;
  1425. case PR_GET_TIMING:
  1426. error = PR_TIMING_STATISTICAL;
  1427. break;
  1428. case PR_SET_TIMING:
  1429. if (arg2 != PR_TIMING_STATISTICAL)
  1430. error = -EINVAL;
  1431. else
  1432. error = 0;
  1433. break;
  1434. case PR_SET_NAME:
  1435. comm[sizeof(me->comm)-1] = 0;
  1436. if (strncpy_from_user(comm, (char __user *)arg2,
  1437. sizeof(me->comm) - 1) < 0)
  1438. return -EFAULT;
  1439. set_task_comm(me, comm);
  1440. return 0;
  1441. case PR_GET_NAME:
  1442. get_task_comm(comm, me);
  1443. if (copy_to_user((char __user *)arg2, comm,
  1444. sizeof(comm)))
  1445. return -EFAULT;
  1446. return 0;
  1447. case PR_GET_ENDIAN:
  1448. error = GET_ENDIAN(me, arg2);
  1449. break;
  1450. case PR_SET_ENDIAN:
  1451. error = SET_ENDIAN(me, arg2);
  1452. break;
  1453. case PR_GET_SECCOMP:
  1454. error = prctl_get_seccomp();
  1455. break;
  1456. case PR_SET_SECCOMP:
  1457. error = prctl_set_seccomp(arg2);
  1458. break;
  1459. case PR_GET_TSC:
  1460. error = GET_TSC_CTL(arg2);
  1461. break;
  1462. case PR_SET_TSC:
  1463. error = SET_TSC_CTL(arg2);
  1464. break;
  1465. case PR_TASK_PERF_EVENTS_DISABLE:
  1466. error = perf_event_task_disable();
  1467. break;
  1468. case PR_TASK_PERF_EVENTS_ENABLE:
  1469. error = perf_event_task_enable();
  1470. break;
  1471. case PR_GET_TIMERSLACK:
  1472. error = current->timer_slack_ns;
  1473. break;
  1474. case PR_SET_TIMERSLACK:
  1475. if (arg2 <= 0)
  1476. current->timer_slack_ns =
  1477. current->default_timer_slack_ns;
  1478. else
  1479. current->timer_slack_ns = arg2;
  1480. error = 0;
  1481. break;
  1482. case PR_MCE_KILL:
  1483. if (arg4 | arg5)
  1484. return -EINVAL;
  1485. switch (arg2) {
  1486. case PR_MCE_KILL_CLEAR:
  1487. if (arg3 != 0)
  1488. return -EINVAL;
  1489. current->flags &= ~PF_MCE_PROCESS;
  1490. break;
  1491. case PR_MCE_KILL_SET:
  1492. current->flags |= PF_MCE_PROCESS;
  1493. if (arg3 == PR_MCE_KILL_EARLY)
  1494. current->flags |= PF_MCE_EARLY;
  1495. else if (arg3 == PR_MCE_KILL_LATE)
  1496. current->flags &= ~PF_MCE_EARLY;
  1497. else if (arg3 == PR_MCE_KILL_DEFAULT)
  1498. current->flags &=
  1499. ~(PF_MCE_EARLY|PF_MCE_PROCESS);
  1500. else
  1501. return -EINVAL;
  1502. break;
  1503. default:
  1504. return -EINVAL;
  1505. }
  1506. error = 0;
  1507. break;
  1508. case PR_MCE_KILL_GET:
  1509. if (arg2 | arg3 | arg4 | arg5)
  1510. return -EINVAL;
  1511. if (current->flags & PF_MCE_PROCESS)
  1512. error = (current->flags & PF_MCE_EARLY) ?
  1513. PR_MCE_KILL_EARLY : PR_MCE_KILL_LATE;
  1514. else
  1515. error = PR_MCE_KILL_DEFAULT;
  1516. break;
  1517. default:
  1518. error = -EINVAL;
  1519. break;
  1520. }
  1521. return error;
  1522. }
  1523. SYSCALL_DEFINE3(getcpu, unsigned __user *, cpup, unsigned __user *, nodep,
  1524. struct getcpu_cache __user *, unused)
  1525. {
  1526. int err = 0;
  1527. int cpu = raw_smp_processor_id();
  1528. if (cpup)
  1529. err |= put_user(cpu, cpup);
  1530. if (nodep)
  1531. err |= put_user(cpu_to_node(cpu), nodep);
  1532. return err ? -EFAULT : 0;
  1533. }
  1534. char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
  1535. static void argv_cleanup(struct subprocess_info *info)
  1536. {
  1537. argv_free(info->argv);
  1538. }
  1539. /**
  1540. * orderly_poweroff - Trigger an orderly system poweroff
  1541. * @force: force poweroff if command execution fails
  1542. *
  1543. * This may be called from any context to trigger a system shutdown.
  1544. * If the orderly shutdown fails, it will force an immediate shutdown.
  1545. */
  1546. int orderly_poweroff(bool force)
  1547. {
  1548. int argc;
  1549. char **argv = argv_split(GFP_ATOMIC, poweroff_cmd, &argc);
  1550. static char *envp[] = {
  1551. "HOME=/",
  1552. "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
  1553. NULL
  1554. };
  1555. int ret = -ENOMEM;
  1556. struct subprocess_info *info;
  1557. if (argv == NULL) {
  1558. printk(KERN_WARNING "%s failed to allocate memory for \"%s\"\n",
  1559. __func__, poweroff_cmd);
  1560. goto out;
  1561. }
  1562. info = call_usermodehelper_setup(argv[0], argv, envp, GFP_ATOMIC);
  1563. if (info == NULL) {
  1564. argv_free(argv);
  1565. goto out;
  1566. }
  1567. call_usermodehelper_setfns(info, NULL, argv_cleanup, NULL);
  1568. ret = call_usermodehelper_exec(info, UMH_NO_WAIT);
  1569. out:
  1570. if (ret && force) {
  1571. printk(KERN_WARNING "Failed to start orderly shutdown: "
  1572. "forcing the issue\n");
  1573. /* I guess this should try to kick off some daemon to
  1574. sync and poweroff asap. Or not even bother syncing
  1575. if we're doing an emergency shutdown? */
  1576. emergency_sync();
  1577. kernel_power_off();
  1578. }
  1579. return ret;
  1580. }
  1581. EXPORT_SYMBOL_GPL(orderly_poweroff);