sys.c 42 KB

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