sys.c 42 KB

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