sys.c 43 KB

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