sys.c 40 KB

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