sys.c 42 KB

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