sys.c 43 KB

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