sys.c 42 KB

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