sys.c 43 KB

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