sys.c 42 KB

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