sys.c 43 KB

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