sys.c 42 KB

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