sys.c 43 KB

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