sys.c 42 KB

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