sys.c 42 KB

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