sys.c 42 KB

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