sys.c 42 KB

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