sys.c 42 KB

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