sys.c 42 KB

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