sys.c 43 KB

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