sys.c 42 KB

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