sys.c 43 KB

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