sys.c 43 KB

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