sys.c 41 KB

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