sys.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782
  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. spin_lock(&group_leader->sighand->siglock);
  920. group_leader->signal->tty = NULL;
  921. spin_unlock(&group_leader->sighand->siglock);
  922. err = session;
  923. out:
  924. write_unlock_irq(&tasklist_lock);
  925. return err;
  926. }
  927. /*
  928. * Supplementary group IDs
  929. */
  930. /* init to 2 - one for init_task, one to ensure it is never freed */
  931. struct group_info init_groups = { .usage = ATOMIC_INIT(2) };
  932. struct group_info *groups_alloc(int gidsetsize)
  933. {
  934. struct group_info *group_info;
  935. int nblocks;
  936. int i;
  937. nblocks = (gidsetsize + NGROUPS_PER_BLOCK - 1) / NGROUPS_PER_BLOCK;
  938. /* Make sure we always allocate at least one indirect block pointer */
  939. nblocks = nblocks ? : 1;
  940. group_info = kmalloc(sizeof(*group_info) + nblocks*sizeof(gid_t *), GFP_USER);
  941. if (!group_info)
  942. return NULL;
  943. group_info->ngroups = gidsetsize;
  944. group_info->nblocks = nblocks;
  945. atomic_set(&group_info->usage, 1);
  946. if (gidsetsize <= NGROUPS_SMALL)
  947. group_info->blocks[0] = group_info->small_block;
  948. else {
  949. for (i = 0; i < nblocks; i++) {
  950. gid_t *b;
  951. b = (void *)__get_free_page(GFP_USER);
  952. if (!b)
  953. goto out_undo_partial_alloc;
  954. group_info->blocks[i] = b;
  955. }
  956. }
  957. return group_info;
  958. out_undo_partial_alloc:
  959. while (--i >= 0) {
  960. free_page((unsigned long)group_info->blocks[i]);
  961. }
  962. kfree(group_info);
  963. return NULL;
  964. }
  965. EXPORT_SYMBOL(groups_alloc);
  966. void groups_free(struct group_info *group_info)
  967. {
  968. if (group_info->blocks[0] != group_info->small_block) {
  969. int i;
  970. for (i = 0; i < group_info->nblocks; i++)
  971. free_page((unsigned long)group_info->blocks[i]);
  972. }
  973. kfree(group_info);
  974. }
  975. EXPORT_SYMBOL(groups_free);
  976. /* export the group_info to a user-space array */
  977. static int groups_to_user(gid_t __user *grouplist,
  978. struct group_info *group_info)
  979. {
  980. int i;
  981. unsigned int count = group_info->ngroups;
  982. for (i = 0; i < group_info->nblocks; i++) {
  983. unsigned int cp_count = min(NGROUPS_PER_BLOCK, count);
  984. unsigned int len = cp_count * sizeof(*grouplist);
  985. if (copy_to_user(grouplist, group_info->blocks[i], len))
  986. return -EFAULT;
  987. grouplist += NGROUPS_PER_BLOCK;
  988. count -= cp_count;
  989. }
  990. return 0;
  991. }
  992. /* fill a group_info from a user-space array - it must be allocated already */
  993. static int groups_from_user(struct group_info *group_info,
  994. gid_t __user *grouplist)
  995. {
  996. int i;
  997. unsigned int count = group_info->ngroups;
  998. for (i = 0; i < group_info->nblocks; i++) {
  999. unsigned int cp_count = min(NGROUPS_PER_BLOCK, count);
  1000. unsigned int len = cp_count * sizeof(*grouplist);
  1001. if (copy_from_user(group_info->blocks[i], grouplist, len))
  1002. return -EFAULT;
  1003. grouplist += NGROUPS_PER_BLOCK;
  1004. count -= cp_count;
  1005. }
  1006. return 0;
  1007. }
  1008. /* a simple Shell sort */
  1009. static void groups_sort(struct group_info *group_info)
  1010. {
  1011. int base, max, stride;
  1012. int gidsetsize = group_info->ngroups;
  1013. for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1)
  1014. ; /* nothing */
  1015. stride /= 3;
  1016. while (stride) {
  1017. max = gidsetsize - stride;
  1018. for (base = 0; base < max; base++) {
  1019. int left = base;
  1020. int right = left + stride;
  1021. gid_t tmp = GROUP_AT(group_info, right);
  1022. while (left >= 0 && GROUP_AT(group_info, left) > tmp) {
  1023. GROUP_AT(group_info, right) =
  1024. GROUP_AT(group_info, left);
  1025. right = left;
  1026. left -= stride;
  1027. }
  1028. GROUP_AT(group_info, right) = tmp;
  1029. }
  1030. stride /= 3;
  1031. }
  1032. }
  1033. /* a simple bsearch */
  1034. int groups_search(struct group_info *group_info, gid_t grp)
  1035. {
  1036. unsigned int left, right;
  1037. if (!group_info)
  1038. return 0;
  1039. left = 0;
  1040. right = group_info->ngroups;
  1041. while (left < right) {
  1042. unsigned int mid = (left+right)/2;
  1043. int cmp = grp - GROUP_AT(group_info, mid);
  1044. if (cmp > 0)
  1045. left = mid + 1;
  1046. else if (cmp < 0)
  1047. right = mid;
  1048. else
  1049. return 1;
  1050. }
  1051. return 0;
  1052. }
  1053. /* validate and set current->group_info */
  1054. int set_current_groups(struct group_info *group_info)
  1055. {
  1056. int retval;
  1057. struct group_info *old_info;
  1058. retval = security_task_setgroups(group_info);
  1059. if (retval)
  1060. return retval;
  1061. groups_sort(group_info);
  1062. get_group_info(group_info);
  1063. task_lock(current);
  1064. old_info = current->group_info;
  1065. current->group_info = group_info;
  1066. task_unlock(current);
  1067. put_group_info(old_info);
  1068. return 0;
  1069. }
  1070. EXPORT_SYMBOL(set_current_groups);
  1071. asmlinkage long sys_getgroups(int gidsetsize, gid_t __user *grouplist)
  1072. {
  1073. int i = 0;
  1074. /*
  1075. * SMP: Nobody else can change our grouplist. Thus we are
  1076. * safe.
  1077. */
  1078. if (gidsetsize < 0)
  1079. return -EINVAL;
  1080. /* no need to grab task_lock here; it cannot change */
  1081. i = current->group_info->ngroups;
  1082. if (gidsetsize) {
  1083. if (i > gidsetsize) {
  1084. i = -EINVAL;
  1085. goto out;
  1086. }
  1087. if (groups_to_user(grouplist, current->group_info)) {
  1088. i = -EFAULT;
  1089. goto out;
  1090. }
  1091. }
  1092. out:
  1093. return i;
  1094. }
  1095. /*
  1096. * SMP: Our groups are copy-on-write. We can set them safely
  1097. * without another task interfering.
  1098. */
  1099. asmlinkage long sys_setgroups(int gidsetsize, gid_t __user *grouplist)
  1100. {
  1101. struct group_info *group_info;
  1102. int retval;
  1103. if (!capable(CAP_SETGID))
  1104. return -EPERM;
  1105. if ((unsigned)gidsetsize > NGROUPS_MAX)
  1106. return -EINVAL;
  1107. group_info = groups_alloc(gidsetsize);
  1108. if (!group_info)
  1109. return -ENOMEM;
  1110. retval = groups_from_user(group_info, grouplist);
  1111. if (retval) {
  1112. put_group_info(group_info);
  1113. return retval;
  1114. }
  1115. retval = set_current_groups(group_info);
  1116. put_group_info(group_info);
  1117. return retval;
  1118. }
  1119. /*
  1120. * Check whether we're fsgid/egid or in the supplemental group..
  1121. */
  1122. int in_group_p(gid_t grp)
  1123. {
  1124. int retval = 1;
  1125. if (grp != current->fsgid)
  1126. retval = groups_search(current->group_info, grp);
  1127. return retval;
  1128. }
  1129. EXPORT_SYMBOL(in_group_p);
  1130. int in_egroup_p(gid_t grp)
  1131. {
  1132. int retval = 1;
  1133. if (grp != current->egid)
  1134. retval = groups_search(current->group_info, grp);
  1135. return retval;
  1136. }
  1137. EXPORT_SYMBOL(in_egroup_p);
  1138. DECLARE_RWSEM(uts_sem);
  1139. asmlinkage long sys_newuname(struct new_utsname __user * name)
  1140. {
  1141. int errno = 0;
  1142. down_read(&uts_sem);
  1143. if (copy_to_user(name, utsname(), sizeof *name))
  1144. errno = -EFAULT;
  1145. up_read(&uts_sem);
  1146. return errno;
  1147. }
  1148. asmlinkage long sys_sethostname(char __user *name, int len)
  1149. {
  1150. int errno;
  1151. char tmp[__NEW_UTS_LEN];
  1152. if (!capable(CAP_SYS_ADMIN))
  1153. return -EPERM;
  1154. if (len < 0 || len > __NEW_UTS_LEN)
  1155. return -EINVAL;
  1156. down_write(&uts_sem);
  1157. errno = -EFAULT;
  1158. if (!copy_from_user(tmp, name, len)) {
  1159. memcpy(utsname()->nodename, tmp, len);
  1160. utsname()->nodename[len] = 0;
  1161. errno = 0;
  1162. }
  1163. up_write(&uts_sem);
  1164. return errno;
  1165. }
  1166. #ifdef __ARCH_WANT_SYS_GETHOSTNAME
  1167. asmlinkage long sys_gethostname(char __user *name, int len)
  1168. {
  1169. int i, errno;
  1170. if (len < 0)
  1171. return -EINVAL;
  1172. down_read(&uts_sem);
  1173. i = 1 + strlen(utsname()->nodename);
  1174. if (i > len)
  1175. i = len;
  1176. errno = 0;
  1177. if (copy_to_user(name, utsname()->nodename, i))
  1178. errno = -EFAULT;
  1179. up_read(&uts_sem);
  1180. return errno;
  1181. }
  1182. #endif
  1183. /*
  1184. * Only setdomainname; getdomainname can be implemented by calling
  1185. * uname()
  1186. */
  1187. asmlinkage long sys_setdomainname(char __user *name, int len)
  1188. {
  1189. int errno;
  1190. char tmp[__NEW_UTS_LEN];
  1191. if (!capable(CAP_SYS_ADMIN))
  1192. return -EPERM;
  1193. if (len < 0 || len > __NEW_UTS_LEN)
  1194. return -EINVAL;
  1195. down_write(&uts_sem);
  1196. errno = -EFAULT;
  1197. if (!copy_from_user(tmp, name, len)) {
  1198. memcpy(utsname()->domainname, tmp, len);
  1199. utsname()->domainname[len] = 0;
  1200. errno = 0;
  1201. }
  1202. up_write(&uts_sem);
  1203. return errno;
  1204. }
  1205. asmlinkage long sys_getrlimit(unsigned int resource, struct rlimit __user *rlim)
  1206. {
  1207. if (resource >= RLIM_NLIMITS)
  1208. return -EINVAL;
  1209. else {
  1210. struct rlimit value;
  1211. task_lock(current->group_leader);
  1212. value = current->signal->rlim[resource];
  1213. task_unlock(current->group_leader);
  1214. return copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
  1215. }
  1216. }
  1217. #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
  1218. /*
  1219. * Back compatibility for getrlimit. Needed for some apps.
  1220. */
  1221. asmlinkage long sys_old_getrlimit(unsigned int resource, struct rlimit __user *rlim)
  1222. {
  1223. struct rlimit x;
  1224. if (resource >= RLIM_NLIMITS)
  1225. return -EINVAL;
  1226. task_lock(current->group_leader);
  1227. x = current->signal->rlim[resource];
  1228. task_unlock(current->group_leader);
  1229. if (x.rlim_cur > 0x7FFFFFFF)
  1230. x.rlim_cur = 0x7FFFFFFF;
  1231. if (x.rlim_max > 0x7FFFFFFF)
  1232. x.rlim_max = 0x7FFFFFFF;
  1233. return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
  1234. }
  1235. #endif
  1236. asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit __user *rlim)
  1237. {
  1238. struct rlimit new_rlim, *old_rlim;
  1239. int retval;
  1240. if (resource >= RLIM_NLIMITS)
  1241. return -EINVAL;
  1242. if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
  1243. return -EFAULT;
  1244. if (new_rlim.rlim_cur > new_rlim.rlim_max)
  1245. return -EINVAL;
  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 && new_rlim.rlim_max > sysctl_nr_open)
  1251. return -EPERM;
  1252. retval = security_task_setrlimit(resource, &new_rlim);
  1253. if (retval)
  1254. return retval;
  1255. if (resource == RLIMIT_CPU && new_rlim.rlim_cur == 0) {
  1256. /*
  1257. * The caller is asking for an immediate RLIMIT_CPU
  1258. * expiry. But we use the zero value to mean "it was
  1259. * never set". So let's cheat and make it one second
  1260. * instead
  1261. */
  1262. new_rlim.rlim_cur = 1;
  1263. }
  1264. task_lock(current->group_leader);
  1265. *old_rlim = new_rlim;
  1266. task_unlock(current->group_leader);
  1267. if (resource != RLIMIT_CPU)
  1268. goto out;
  1269. /*
  1270. * RLIMIT_CPU handling. Note that the kernel fails to return an error
  1271. * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
  1272. * very long-standing error, and fixing it now risks breakage of
  1273. * applications, so we live with it
  1274. */
  1275. if (new_rlim.rlim_cur == RLIM_INFINITY)
  1276. goto out;
  1277. update_rlimit_cpu(new_rlim.rlim_cur);
  1278. out:
  1279. return 0;
  1280. }
  1281. /*
  1282. * It would make sense to put struct rusage in the task_struct,
  1283. * except that would make the task_struct be *really big*. After
  1284. * task_struct gets moved into malloc'ed memory, it would
  1285. * make sense to do this. It will make moving the rest of the information
  1286. * a lot simpler! (Which we're not doing right now because we're not
  1287. * measuring them yet).
  1288. *
  1289. * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
  1290. * races with threads incrementing their own counters. But since word
  1291. * reads are atomic, we either get new values or old values and we don't
  1292. * care which for the sums. We always take the siglock to protect reading
  1293. * the c* fields from p->signal from races with exit.c updating those
  1294. * fields when reaping, so a sample either gets all the additions of a
  1295. * given child after it's reaped, or none so this sample is before reaping.
  1296. *
  1297. * Locking:
  1298. * We need to take the siglock for CHILDEREN, SELF and BOTH
  1299. * for the cases current multithreaded, non-current single threaded
  1300. * non-current multithreaded. Thread traversal is now safe with
  1301. * the siglock held.
  1302. * Strictly speaking, we donot need to take the siglock if we are current and
  1303. * single threaded, as no one else can take our signal_struct away, no one
  1304. * else can reap the children to update signal->c* counters, and no one else
  1305. * can race with the signal-> fields. If we do not take any lock, the
  1306. * signal-> fields could be read out of order while another thread was just
  1307. * exiting. So we should place a read memory barrier when we avoid the lock.
  1308. * On the writer side, write memory barrier is implied in __exit_signal
  1309. * as __exit_signal releases the siglock spinlock after updating the signal->
  1310. * fields. But we don't do this yet to keep things simple.
  1311. *
  1312. */
  1313. static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r)
  1314. {
  1315. r->ru_nvcsw += t->nvcsw;
  1316. r->ru_nivcsw += t->nivcsw;
  1317. r->ru_minflt += t->min_flt;
  1318. r->ru_majflt += t->maj_flt;
  1319. r->ru_inblock += task_io_get_inblock(t);
  1320. r->ru_oublock += task_io_get_oublock(t);
  1321. }
  1322. static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
  1323. {
  1324. struct task_struct *t;
  1325. unsigned long flags;
  1326. cputime_t utime, stime;
  1327. struct task_cputime cputime;
  1328. memset((char *) r, 0, sizeof *r);
  1329. utime = stime = cputime_zero;
  1330. if (who == RUSAGE_THREAD) {
  1331. accumulate_thread_rusage(p, r);
  1332. goto out;
  1333. }
  1334. if (!lock_task_sighand(p, &flags))
  1335. return;
  1336. switch (who) {
  1337. case RUSAGE_BOTH:
  1338. case RUSAGE_CHILDREN:
  1339. utime = p->signal->cutime;
  1340. stime = p->signal->cstime;
  1341. r->ru_nvcsw = p->signal->cnvcsw;
  1342. r->ru_nivcsw = p->signal->cnivcsw;
  1343. r->ru_minflt = p->signal->cmin_flt;
  1344. r->ru_majflt = p->signal->cmaj_flt;
  1345. r->ru_inblock = p->signal->cinblock;
  1346. r->ru_oublock = p->signal->coublock;
  1347. if (who == RUSAGE_CHILDREN)
  1348. break;
  1349. case RUSAGE_SELF:
  1350. thread_group_cputime(p, &cputime);
  1351. utime = cputime_add(utime, cputime.utime);
  1352. stime = cputime_add(stime, cputime.stime);
  1353. r->ru_nvcsw += p->signal->nvcsw;
  1354. r->ru_nivcsw += p->signal->nivcsw;
  1355. r->ru_minflt += p->signal->min_flt;
  1356. r->ru_majflt += p->signal->maj_flt;
  1357. r->ru_inblock += p->signal->inblock;
  1358. r->ru_oublock += p->signal->oublock;
  1359. t = p;
  1360. do {
  1361. accumulate_thread_rusage(t, r);
  1362. t = next_thread(t);
  1363. } while (t != p);
  1364. break;
  1365. default:
  1366. BUG();
  1367. }
  1368. unlock_task_sighand(p, &flags);
  1369. out:
  1370. cputime_to_timeval(utime, &r->ru_utime);
  1371. cputime_to_timeval(stime, &r->ru_stime);
  1372. }
  1373. int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
  1374. {
  1375. struct rusage r;
  1376. k_getrusage(p, who, &r);
  1377. return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
  1378. }
  1379. asmlinkage long sys_getrusage(int who, struct rusage __user *ru)
  1380. {
  1381. if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
  1382. who != RUSAGE_THREAD)
  1383. return -EINVAL;
  1384. return getrusage(current, who, ru);
  1385. }
  1386. asmlinkage long sys_umask(int mask)
  1387. {
  1388. mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
  1389. return mask;
  1390. }
  1391. asmlinkage long sys_prctl(int option, unsigned long arg2, unsigned long arg3,
  1392. unsigned long arg4, unsigned long arg5)
  1393. {
  1394. long error = 0;
  1395. if (security_task_prctl(option, arg2, arg3, arg4, arg5, &error))
  1396. return error;
  1397. switch (option) {
  1398. case PR_SET_PDEATHSIG:
  1399. if (!valid_signal(arg2)) {
  1400. error = -EINVAL;
  1401. break;
  1402. }
  1403. current->pdeath_signal = arg2;
  1404. break;
  1405. case PR_GET_PDEATHSIG:
  1406. error = put_user(current->pdeath_signal, (int __user *)arg2);
  1407. break;
  1408. case PR_GET_DUMPABLE:
  1409. error = get_dumpable(current->mm);
  1410. break;
  1411. case PR_SET_DUMPABLE:
  1412. if (arg2 < 0 || arg2 > 1) {
  1413. error = -EINVAL;
  1414. break;
  1415. }
  1416. set_dumpable(current->mm, arg2);
  1417. break;
  1418. case PR_SET_UNALIGN:
  1419. error = SET_UNALIGN_CTL(current, arg2);
  1420. break;
  1421. case PR_GET_UNALIGN:
  1422. error = GET_UNALIGN_CTL(current, arg2);
  1423. break;
  1424. case PR_SET_FPEMU:
  1425. error = SET_FPEMU_CTL(current, arg2);
  1426. break;
  1427. case PR_GET_FPEMU:
  1428. error = GET_FPEMU_CTL(current, arg2);
  1429. break;
  1430. case PR_SET_FPEXC:
  1431. error = SET_FPEXC_CTL(current, arg2);
  1432. break;
  1433. case PR_GET_FPEXC:
  1434. error = GET_FPEXC_CTL(current, arg2);
  1435. break;
  1436. case PR_GET_TIMING:
  1437. error = PR_TIMING_STATISTICAL;
  1438. break;
  1439. case PR_SET_TIMING:
  1440. if (arg2 != PR_TIMING_STATISTICAL)
  1441. error = -EINVAL;
  1442. break;
  1443. case PR_SET_NAME: {
  1444. struct task_struct *me = current;
  1445. unsigned char ncomm[sizeof(me->comm)];
  1446. ncomm[sizeof(me->comm)-1] = 0;
  1447. if (strncpy_from_user(ncomm, (char __user *)arg2,
  1448. sizeof(me->comm)-1) < 0)
  1449. return -EFAULT;
  1450. set_task_comm(me, ncomm);
  1451. return 0;
  1452. }
  1453. case PR_GET_NAME: {
  1454. struct task_struct *me = current;
  1455. unsigned char tcomm[sizeof(me->comm)];
  1456. get_task_comm(tcomm, me);
  1457. if (copy_to_user((char __user *)arg2, tcomm, sizeof(tcomm)))
  1458. return -EFAULT;
  1459. return 0;
  1460. }
  1461. case PR_GET_ENDIAN:
  1462. error = GET_ENDIAN(current, arg2);
  1463. break;
  1464. case PR_SET_ENDIAN:
  1465. error = SET_ENDIAN(current, arg2);
  1466. break;
  1467. case PR_GET_SECCOMP:
  1468. error = prctl_get_seccomp();
  1469. break;
  1470. case PR_SET_SECCOMP:
  1471. error = prctl_set_seccomp(arg2);
  1472. break;
  1473. case PR_GET_TSC:
  1474. error = GET_TSC_CTL(arg2);
  1475. break;
  1476. case PR_SET_TSC:
  1477. error = SET_TSC_CTL(arg2);
  1478. break;
  1479. default:
  1480. error = -EINVAL;
  1481. break;
  1482. }
  1483. return error;
  1484. }
  1485. asmlinkage long sys_getcpu(unsigned __user *cpup, unsigned __user *nodep,
  1486. struct getcpu_cache __user *unused)
  1487. {
  1488. int err = 0;
  1489. int cpu = raw_smp_processor_id();
  1490. if (cpup)
  1491. err |= put_user(cpu, cpup);
  1492. if (nodep)
  1493. err |= put_user(cpu_to_node(cpu), nodep);
  1494. return err ? -EFAULT : 0;
  1495. }
  1496. char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
  1497. static void argv_cleanup(char **argv, char **envp)
  1498. {
  1499. argv_free(argv);
  1500. }
  1501. /**
  1502. * orderly_poweroff - Trigger an orderly system poweroff
  1503. * @force: force poweroff if command execution fails
  1504. *
  1505. * This may be called from any context to trigger a system shutdown.
  1506. * If the orderly shutdown fails, it will force an immediate shutdown.
  1507. */
  1508. int orderly_poweroff(bool force)
  1509. {
  1510. int argc;
  1511. char **argv = argv_split(GFP_ATOMIC, poweroff_cmd, &argc);
  1512. static char *envp[] = {
  1513. "HOME=/",
  1514. "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
  1515. NULL
  1516. };
  1517. int ret = -ENOMEM;
  1518. struct subprocess_info *info;
  1519. if (argv == NULL) {
  1520. printk(KERN_WARNING "%s failed to allocate memory for \"%s\"\n",
  1521. __func__, poweroff_cmd);
  1522. goto out;
  1523. }
  1524. info = call_usermodehelper_setup(argv[0], argv, envp, GFP_ATOMIC);
  1525. if (info == NULL) {
  1526. argv_free(argv);
  1527. goto out;
  1528. }
  1529. call_usermodehelper_setcleanup(info, argv_cleanup);
  1530. ret = call_usermodehelper_exec(info, UMH_NO_WAIT);
  1531. out:
  1532. if (ret && force) {
  1533. printk(KERN_WARNING "Failed to start orderly shutdown: "
  1534. "forcing the issue\n");
  1535. /* I guess this should try to kick off some daemon to
  1536. sync and poweroff asap. Or not even bother syncing
  1537. if we're doing an emergency shutdown? */
  1538. emergency_sync();
  1539. kernel_power_off();
  1540. }
  1541. return ret;
  1542. }
  1543. EXPORT_SYMBOL_GPL(orderly_poweroff);