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/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. asmlinkage long sys_times(struct tms __user * tbuf)
  752. {
  753. /*
  754. * In the SMP world we might just be unlucky and have one of
  755. * the times increment as we use it. Since the value is an
  756. * atomically safe type this is just fine. Conceptually its
  757. * as if the syscall took an instant longer to occur.
  758. */
  759. if (tbuf) {
  760. struct tms tmp;
  761. struct task_struct *tsk = current;
  762. struct task_struct *t;
  763. cputime_t utime, stime, cutime, cstime;
  764. spin_lock_irq(&tsk->sighand->siglock);
  765. utime = tsk->signal->utime;
  766. stime = tsk->signal->stime;
  767. t = tsk;
  768. do {
  769. utime = cputime_add(utime, t->utime);
  770. stime = cputime_add(stime, t->stime);
  771. t = next_thread(t);
  772. } while (t != tsk);
  773. cutime = tsk->signal->cutime;
  774. cstime = tsk->signal->cstime;
  775. spin_unlock_irq(&tsk->sighand->siglock);
  776. tmp.tms_utime = cputime_to_clock_t(utime);
  777. tmp.tms_stime = cputime_to_clock_t(stime);
  778. tmp.tms_cutime = cputime_to_clock_t(cutime);
  779. tmp.tms_cstime = cputime_to_clock_t(cstime);
  780. if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
  781. return -EFAULT;
  782. }
  783. return (long) jiffies_64_to_clock_t(get_jiffies_64());
  784. }
  785. /*
  786. * This needs some heavy checking ...
  787. * I just haven't the stomach for it. I also don't fully
  788. * understand sessions/pgrp etc. Let somebody who does explain it.
  789. *
  790. * OK, I think I have the protection semantics right.... this is really
  791. * only important on a multi-user system anyway, to make sure one user
  792. * can't send a signal to a process owned by another. -TYT, 12/12/91
  793. *
  794. * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
  795. * LBT 04.03.94
  796. */
  797. asmlinkage long sys_setpgid(pid_t pid, pid_t pgid)
  798. {
  799. struct task_struct *p;
  800. struct task_struct *group_leader = current->group_leader;
  801. struct pid *pgrp;
  802. int err;
  803. if (!pid)
  804. pid = task_pid_vnr(group_leader);
  805. if (!pgid)
  806. pgid = pid;
  807. if (pgid < 0)
  808. return -EINVAL;
  809. /* From this point forward we keep holding onto the tasklist lock
  810. * so that our parent does not change from under us. -DaveM
  811. */
  812. write_lock_irq(&tasklist_lock);
  813. err = -ESRCH;
  814. p = find_task_by_vpid(pid);
  815. if (!p)
  816. goto out;
  817. err = -EINVAL;
  818. if (!thread_group_leader(p))
  819. goto out;
  820. if (same_thread_group(p->real_parent, group_leader)) {
  821. err = -EPERM;
  822. if (task_session(p) != task_session(group_leader))
  823. goto out;
  824. err = -EACCES;
  825. if (p->did_exec)
  826. goto out;
  827. } else {
  828. err = -ESRCH;
  829. if (p != group_leader)
  830. goto out;
  831. }
  832. err = -EPERM;
  833. if (p->signal->leader)
  834. goto out;
  835. pgrp = task_pid(p);
  836. if (pgid != pid) {
  837. struct task_struct *g;
  838. pgrp = find_vpid(pgid);
  839. g = pid_task(pgrp, PIDTYPE_PGID);
  840. if (!g || task_session(g) != task_session(group_leader))
  841. goto out;
  842. }
  843. err = security_task_setpgid(p, pgid);
  844. if (err)
  845. goto out;
  846. if (task_pgrp(p) != pgrp) {
  847. change_pid(p, PIDTYPE_PGID, pgrp);
  848. set_task_pgrp(p, pid_nr(pgrp));
  849. }
  850. err = 0;
  851. out:
  852. /* All paths lead to here, thus we are safe. -DaveM */
  853. write_unlock_irq(&tasklist_lock);
  854. return err;
  855. }
  856. asmlinkage long sys_getpgid(pid_t pid)
  857. {
  858. struct task_struct *p;
  859. struct pid *grp;
  860. int retval;
  861. rcu_read_lock();
  862. if (!pid)
  863. grp = task_pgrp(current);
  864. else {
  865. retval = -ESRCH;
  866. p = find_task_by_vpid(pid);
  867. if (!p)
  868. goto out;
  869. grp = task_pgrp(p);
  870. if (!grp)
  871. goto out;
  872. retval = security_task_getpgid(p);
  873. if (retval)
  874. goto out;
  875. }
  876. retval = pid_vnr(grp);
  877. out:
  878. rcu_read_unlock();
  879. return retval;
  880. }
  881. #ifdef __ARCH_WANT_SYS_GETPGRP
  882. asmlinkage long sys_getpgrp(void)
  883. {
  884. return sys_getpgid(0);
  885. }
  886. #endif
  887. asmlinkage long sys_getsid(pid_t pid)
  888. {
  889. struct task_struct *p;
  890. struct pid *sid;
  891. int retval;
  892. rcu_read_lock();
  893. if (!pid)
  894. sid = task_session(current);
  895. else {
  896. retval = -ESRCH;
  897. p = find_task_by_vpid(pid);
  898. if (!p)
  899. goto out;
  900. sid = task_session(p);
  901. if (!sid)
  902. goto out;
  903. retval = security_task_getsid(p);
  904. if (retval)
  905. goto out;
  906. }
  907. retval = pid_vnr(sid);
  908. out:
  909. rcu_read_unlock();
  910. return retval;
  911. }
  912. asmlinkage long sys_setsid(void)
  913. {
  914. struct task_struct *group_leader = current->group_leader;
  915. struct pid *sid = task_pid(group_leader);
  916. pid_t session = pid_vnr(sid);
  917. int err = -EPERM;
  918. write_lock_irq(&tasklist_lock);
  919. /* Fail if I am already a session leader */
  920. if (group_leader->signal->leader)
  921. goto out;
  922. /* Fail if a process group id already exists that equals the
  923. * proposed session id.
  924. */
  925. if (pid_task(sid, PIDTYPE_PGID))
  926. goto out;
  927. group_leader->signal->leader = 1;
  928. __set_special_pids(sid);
  929. proc_clear_tty(group_leader);
  930. err = session;
  931. out:
  932. write_unlock_irq(&tasklist_lock);
  933. return err;
  934. }
  935. /*
  936. * Supplementary group IDs
  937. */
  938. /* init to 2 - one for init_task, one to ensure it is never freed */
  939. struct group_info init_groups = { .usage = ATOMIC_INIT(2) };
  940. struct group_info *groups_alloc(int gidsetsize)
  941. {
  942. struct group_info *group_info;
  943. int nblocks;
  944. int i;
  945. nblocks = (gidsetsize + NGROUPS_PER_BLOCK - 1) / NGROUPS_PER_BLOCK;
  946. /* Make sure we always allocate at least one indirect block pointer */
  947. nblocks = nblocks ? : 1;
  948. group_info = kmalloc(sizeof(*group_info) + nblocks*sizeof(gid_t *), GFP_USER);
  949. if (!group_info)
  950. return NULL;
  951. group_info->ngroups = gidsetsize;
  952. group_info->nblocks = nblocks;
  953. atomic_set(&group_info->usage, 1);
  954. if (gidsetsize <= NGROUPS_SMALL)
  955. group_info->blocks[0] = group_info->small_block;
  956. else {
  957. for (i = 0; i < nblocks; i++) {
  958. gid_t *b;
  959. b = (void *)__get_free_page(GFP_USER);
  960. if (!b)
  961. goto out_undo_partial_alloc;
  962. group_info->blocks[i] = b;
  963. }
  964. }
  965. return group_info;
  966. out_undo_partial_alloc:
  967. while (--i >= 0) {
  968. free_page((unsigned long)group_info->blocks[i]);
  969. }
  970. kfree(group_info);
  971. return NULL;
  972. }
  973. EXPORT_SYMBOL(groups_alloc);
  974. void groups_free(struct group_info *group_info)
  975. {
  976. if (group_info->blocks[0] != group_info->small_block) {
  977. int i;
  978. for (i = 0; i < group_info->nblocks; i++)
  979. free_page((unsigned long)group_info->blocks[i]);
  980. }
  981. kfree(group_info);
  982. }
  983. EXPORT_SYMBOL(groups_free);
  984. /* export the group_info to a user-space array */
  985. static int groups_to_user(gid_t __user *grouplist,
  986. struct group_info *group_info)
  987. {
  988. int i;
  989. unsigned int count = group_info->ngroups;
  990. for (i = 0; i < group_info->nblocks; i++) {
  991. unsigned int cp_count = min(NGROUPS_PER_BLOCK, count);
  992. unsigned int len = cp_count * sizeof(*grouplist);
  993. if (copy_to_user(grouplist, group_info->blocks[i], len))
  994. return -EFAULT;
  995. grouplist += NGROUPS_PER_BLOCK;
  996. count -= cp_count;
  997. }
  998. return 0;
  999. }
  1000. /* fill a group_info from a user-space array - it must be allocated already */
  1001. static int groups_from_user(struct group_info *group_info,
  1002. gid_t __user *grouplist)
  1003. {
  1004. int i;
  1005. unsigned int count = group_info->ngroups;
  1006. for (i = 0; i < group_info->nblocks; i++) {
  1007. unsigned int cp_count = min(NGROUPS_PER_BLOCK, count);
  1008. unsigned int len = cp_count * sizeof(*grouplist);
  1009. if (copy_from_user(group_info->blocks[i], grouplist, len))
  1010. return -EFAULT;
  1011. grouplist += NGROUPS_PER_BLOCK;
  1012. count -= cp_count;
  1013. }
  1014. return 0;
  1015. }
  1016. /* a simple Shell sort */
  1017. static void groups_sort(struct group_info *group_info)
  1018. {
  1019. int base, max, stride;
  1020. int gidsetsize = group_info->ngroups;
  1021. for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1)
  1022. ; /* nothing */
  1023. stride /= 3;
  1024. while (stride) {
  1025. max = gidsetsize - stride;
  1026. for (base = 0; base < max; base++) {
  1027. int left = base;
  1028. int right = left + stride;
  1029. gid_t tmp = GROUP_AT(group_info, right);
  1030. while (left >= 0 && GROUP_AT(group_info, left) > tmp) {
  1031. GROUP_AT(group_info, right) =
  1032. GROUP_AT(group_info, left);
  1033. right = left;
  1034. left -= stride;
  1035. }
  1036. GROUP_AT(group_info, right) = tmp;
  1037. }
  1038. stride /= 3;
  1039. }
  1040. }
  1041. /* a simple bsearch */
  1042. int groups_search(struct group_info *group_info, gid_t grp)
  1043. {
  1044. unsigned int left, right;
  1045. if (!group_info)
  1046. return 0;
  1047. left = 0;
  1048. right = group_info->ngroups;
  1049. while (left < right) {
  1050. unsigned int mid = (left+right)/2;
  1051. int cmp = grp - GROUP_AT(group_info, mid);
  1052. if (cmp > 0)
  1053. left = mid + 1;
  1054. else if (cmp < 0)
  1055. right = mid;
  1056. else
  1057. return 1;
  1058. }
  1059. return 0;
  1060. }
  1061. /* validate and set current->group_info */
  1062. int set_current_groups(struct group_info *group_info)
  1063. {
  1064. int retval;
  1065. struct group_info *old_info;
  1066. retval = security_task_setgroups(group_info);
  1067. if (retval)
  1068. return retval;
  1069. groups_sort(group_info);
  1070. get_group_info(group_info);
  1071. task_lock(current);
  1072. old_info = current->group_info;
  1073. current->group_info = group_info;
  1074. task_unlock(current);
  1075. put_group_info(old_info);
  1076. return 0;
  1077. }
  1078. EXPORT_SYMBOL(set_current_groups);
  1079. asmlinkage long sys_getgroups(int gidsetsize, gid_t __user *grouplist)
  1080. {
  1081. int i = 0;
  1082. /*
  1083. * SMP: Nobody else can change our grouplist. Thus we are
  1084. * safe.
  1085. */
  1086. if (gidsetsize < 0)
  1087. return -EINVAL;
  1088. /* no need to grab task_lock here; it cannot change */
  1089. i = current->group_info->ngroups;
  1090. if (gidsetsize) {
  1091. if (i > gidsetsize) {
  1092. i = -EINVAL;
  1093. goto out;
  1094. }
  1095. if (groups_to_user(grouplist, current->group_info)) {
  1096. i = -EFAULT;
  1097. goto out;
  1098. }
  1099. }
  1100. out:
  1101. return i;
  1102. }
  1103. /*
  1104. * SMP: Our groups are copy-on-write. We can set them safely
  1105. * without another task interfering.
  1106. */
  1107. asmlinkage long sys_setgroups(int gidsetsize, gid_t __user *grouplist)
  1108. {
  1109. struct group_info *group_info;
  1110. int retval;
  1111. if (!capable(CAP_SETGID))
  1112. return -EPERM;
  1113. if ((unsigned)gidsetsize > NGROUPS_MAX)
  1114. return -EINVAL;
  1115. group_info = groups_alloc(gidsetsize);
  1116. if (!group_info)
  1117. return -ENOMEM;
  1118. retval = groups_from_user(group_info, grouplist);
  1119. if (retval) {
  1120. put_group_info(group_info);
  1121. return retval;
  1122. }
  1123. retval = set_current_groups(group_info);
  1124. put_group_info(group_info);
  1125. return retval;
  1126. }
  1127. /*
  1128. * Check whether we're fsgid/egid or in the supplemental group..
  1129. */
  1130. int in_group_p(gid_t grp)
  1131. {
  1132. int retval = 1;
  1133. if (grp != current->fsgid)
  1134. retval = groups_search(current->group_info, grp);
  1135. return retval;
  1136. }
  1137. EXPORT_SYMBOL(in_group_p);
  1138. int in_egroup_p(gid_t grp)
  1139. {
  1140. int retval = 1;
  1141. if (grp != current->egid)
  1142. retval = groups_search(current->group_info, grp);
  1143. return retval;
  1144. }
  1145. EXPORT_SYMBOL(in_egroup_p);
  1146. DECLARE_RWSEM(uts_sem);
  1147. asmlinkage long sys_newuname(struct new_utsname __user * name)
  1148. {
  1149. int errno = 0;
  1150. down_read(&uts_sem);
  1151. if (copy_to_user(name, utsname(), sizeof *name))
  1152. errno = -EFAULT;
  1153. up_read(&uts_sem);
  1154. return errno;
  1155. }
  1156. asmlinkage long sys_sethostname(char __user *name, int len)
  1157. {
  1158. int errno;
  1159. char tmp[__NEW_UTS_LEN];
  1160. if (!capable(CAP_SYS_ADMIN))
  1161. return -EPERM;
  1162. if (len < 0 || len > __NEW_UTS_LEN)
  1163. return -EINVAL;
  1164. down_write(&uts_sem);
  1165. errno = -EFAULT;
  1166. if (!copy_from_user(tmp, name, len)) {
  1167. memcpy(utsname()->nodename, tmp, len);
  1168. utsname()->nodename[len] = 0;
  1169. errno = 0;
  1170. }
  1171. up_write(&uts_sem);
  1172. return errno;
  1173. }
  1174. #ifdef __ARCH_WANT_SYS_GETHOSTNAME
  1175. asmlinkage long sys_gethostname(char __user *name, int len)
  1176. {
  1177. int i, errno;
  1178. if (len < 0)
  1179. return -EINVAL;
  1180. down_read(&uts_sem);
  1181. i = 1 + strlen(utsname()->nodename);
  1182. if (i > len)
  1183. i = len;
  1184. errno = 0;
  1185. if (copy_to_user(name, utsname()->nodename, i))
  1186. errno = -EFAULT;
  1187. up_read(&uts_sem);
  1188. return errno;
  1189. }
  1190. #endif
  1191. /*
  1192. * Only setdomainname; getdomainname can be implemented by calling
  1193. * uname()
  1194. */
  1195. asmlinkage long sys_setdomainname(char __user *name, int len)
  1196. {
  1197. int errno;
  1198. char tmp[__NEW_UTS_LEN];
  1199. if (!capable(CAP_SYS_ADMIN))
  1200. return -EPERM;
  1201. if (len < 0 || len > __NEW_UTS_LEN)
  1202. return -EINVAL;
  1203. down_write(&uts_sem);
  1204. errno = -EFAULT;
  1205. if (!copy_from_user(tmp, name, len)) {
  1206. memcpy(utsname()->domainname, tmp, len);
  1207. utsname()->domainname[len] = 0;
  1208. errno = 0;
  1209. }
  1210. up_write(&uts_sem);
  1211. return errno;
  1212. }
  1213. asmlinkage long sys_getrlimit(unsigned int resource, struct rlimit __user *rlim)
  1214. {
  1215. if (resource >= RLIM_NLIMITS)
  1216. return -EINVAL;
  1217. else {
  1218. struct rlimit value;
  1219. task_lock(current->group_leader);
  1220. value = current->signal->rlim[resource];
  1221. task_unlock(current->group_leader);
  1222. return copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
  1223. }
  1224. }
  1225. #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
  1226. /*
  1227. * Back compatibility for getrlimit. Needed for some apps.
  1228. */
  1229. asmlinkage long sys_old_getrlimit(unsigned int resource, struct rlimit __user *rlim)
  1230. {
  1231. struct rlimit x;
  1232. if (resource >= RLIM_NLIMITS)
  1233. return -EINVAL;
  1234. task_lock(current->group_leader);
  1235. x = current->signal->rlim[resource];
  1236. task_unlock(current->group_leader);
  1237. if (x.rlim_cur > 0x7FFFFFFF)
  1238. x.rlim_cur = 0x7FFFFFFF;
  1239. if (x.rlim_max > 0x7FFFFFFF)
  1240. x.rlim_max = 0x7FFFFFFF;
  1241. return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
  1242. }
  1243. #endif
  1244. asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit __user *rlim)
  1245. {
  1246. struct rlimit new_rlim, *old_rlim;
  1247. unsigned long it_prof_secs;
  1248. int retval;
  1249. if (resource >= RLIM_NLIMITS)
  1250. return -EINVAL;
  1251. if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
  1252. return -EFAULT;
  1253. old_rlim = current->signal->rlim + resource;
  1254. if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
  1255. !capable(CAP_SYS_RESOURCE))
  1256. return -EPERM;
  1257. if (resource == RLIMIT_NOFILE) {
  1258. if (new_rlim.rlim_max == RLIM_INFINITY)
  1259. new_rlim.rlim_max = sysctl_nr_open;
  1260. if (new_rlim.rlim_cur == RLIM_INFINITY)
  1261. new_rlim.rlim_cur = sysctl_nr_open;
  1262. if (new_rlim.rlim_max > sysctl_nr_open)
  1263. return -EPERM;
  1264. }
  1265. if (new_rlim.rlim_cur > new_rlim.rlim_max)
  1266. return -EINVAL;
  1267. retval = security_task_setrlimit(resource, &new_rlim);
  1268. if (retval)
  1269. return retval;
  1270. if (resource == RLIMIT_CPU && new_rlim.rlim_cur == 0) {
  1271. /*
  1272. * The caller is asking for an immediate RLIMIT_CPU
  1273. * expiry. But we use the zero value to mean "it was
  1274. * never set". So let's cheat and make it one second
  1275. * instead
  1276. */
  1277. new_rlim.rlim_cur = 1;
  1278. }
  1279. task_lock(current->group_leader);
  1280. *old_rlim = new_rlim;
  1281. task_unlock(current->group_leader);
  1282. if (resource != RLIMIT_CPU)
  1283. goto out;
  1284. /*
  1285. * RLIMIT_CPU handling. Note that the kernel fails to return an error
  1286. * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
  1287. * very long-standing error, and fixing it now risks breakage of
  1288. * applications, so we live with it
  1289. */
  1290. if (new_rlim.rlim_cur == RLIM_INFINITY)
  1291. goto out;
  1292. it_prof_secs = cputime_to_secs(current->signal->it_prof_expires);
  1293. if (it_prof_secs == 0 || new_rlim.rlim_cur <= it_prof_secs) {
  1294. unsigned long rlim_cur = new_rlim.rlim_cur;
  1295. cputime_t cputime;
  1296. cputime = secs_to_cputime(rlim_cur);
  1297. read_lock(&tasklist_lock);
  1298. spin_lock_irq(&current->sighand->siglock);
  1299. set_process_cpu_timer(current, CPUCLOCK_PROF, &cputime, NULL);
  1300. spin_unlock_irq(&current->sighand->siglock);
  1301. read_unlock(&tasklist_lock);
  1302. }
  1303. out:
  1304. return 0;
  1305. }
  1306. /*
  1307. * It would make sense to put struct rusage in the task_struct,
  1308. * except that would make the task_struct be *really big*. After
  1309. * task_struct gets moved into malloc'ed memory, it would
  1310. * make sense to do this. It will make moving the rest of the information
  1311. * a lot simpler! (Which we're not doing right now because we're not
  1312. * measuring them yet).
  1313. *
  1314. * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
  1315. * races with threads incrementing their own counters. But since word
  1316. * reads are atomic, we either get new values or old values and we don't
  1317. * care which for the sums. We always take the siglock to protect reading
  1318. * the c* fields from p->signal from races with exit.c updating those
  1319. * fields when reaping, so a sample either gets all the additions of a
  1320. * given child after it's reaped, or none so this sample is before reaping.
  1321. *
  1322. * Locking:
  1323. * We need to take the siglock for CHILDEREN, SELF and BOTH
  1324. * for the cases current multithreaded, non-current single threaded
  1325. * non-current multithreaded. Thread traversal is now safe with
  1326. * the siglock held.
  1327. * Strictly speaking, we donot need to take the siglock if we are current and
  1328. * single threaded, as no one else can take our signal_struct away, no one
  1329. * else can reap the children to update signal->c* counters, and no one else
  1330. * can race with the signal-> fields. If we do not take any lock, the
  1331. * signal-> fields could be read out of order while another thread was just
  1332. * exiting. So we should place a read memory barrier when we avoid the lock.
  1333. * On the writer side, write memory barrier is implied in __exit_signal
  1334. * as __exit_signal releases the siglock spinlock after updating the signal->
  1335. * fields. But we don't do this yet to keep things simple.
  1336. *
  1337. */
  1338. static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r,
  1339. cputime_t *utimep, cputime_t *stimep)
  1340. {
  1341. *utimep = cputime_add(*utimep, t->utime);
  1342. *stimep = cputime_add(*stimep, t->stime);
  1343. r->ru_nvcsw += t->nvcsw;
  1344. r->ru_nivcsw += t->nivcsw;
  1345. r->ru_minflt += t->min_flt;
  1346. r->ru_majflt += t->maj_flt;
  1347. r->ru_inblock += task_io_get_inblock(t);
  1348. r->ru_oublock += task_io_get_oublock(t);
  1349. }
  1350. static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
  1351. {
  1352. struct task_struct *t;
  1353. unsigned long flags;
  1354. cputime_t utime, stime;
  1355. memset((char *) r, 0, sizeof *r);
  1356. utime = stime = cputime_zero;
  1357. if (who == RUSAGE_THREAD) {
  1358. accumulate_thread_rusage(p, r, &utime, &stime);
  1359. goto out;
  1360. }
  1361. if (!lock_task_sighand(p, &flags))
  1362. return;
  1363. switch (who) {
  1364. case RUSAGE_BOTH:
  1365. case RUSAGE_CHILDREN:
  1366. utime = p->signal->cutime;
  1367. stime = p->signal->cstime;
  1368. r->ru_nvcsw = p->signal->cnvcsw;
  1369. r->ru_nivcsw = p->signal->cnivcsw;
  1370. r->ru_minflt = p->signal->cmin_flt;
  1371. r->ru_majflt = p->signal->cmaj_flt;
  1372. r->ru_inblock = p->signal->cinblock;
  1373. r->ru_oublock = p->signal->coublock;
  1374. if (who == RUSAGE_CHILDREN)
  1375. break;
  1376. case RUSAGE_SELF:
  1377. utime = cputime_add(utime, p->signal->utime);
  1378. stime = cputime_add(stime, p->signal->stime);
  1379. r->ru_nvcsw += p->signal->nvcsw;
  1380. r->ru_nivcsw += p->signal->nivcsw;
  1381. r->ru_minflt += p->signal->min_flt;
  1382. r->ru_majflt += p->signal->maj_flt;
  1383. r->ru_inblock += p->signal->inblock;
  1384. r->ru_oublock += p->signal->oublock;
  1385. t = p;
  1386. do {
  1387. accumulate_thread_rusage(t, r, &utime, &stime);
  1388. t = next_thread(t);
  1389. } while (t != p);
  1390. break;
  1391. default:
  1392. BUG();
  1393. }
  1394. unlock_task_sighand(p, &flags);
  1395. out:
  1396. cputime_to_timeval(utime, &r->ru_utime);
  1397. cputime_to_timeval(stime, &r->ru_stime);
  1398. }
  1399. int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
  1400. {
  1401. struct rusage r;
  1402. k_getrusage(p, who, &r);
  1403. return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
  1404. }
  1405. asmlinkage long sys_getrusage(int who, struct rusage __user *ru)
  1406. {
  1407. if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
  1408. who != RUSAGE_THREAD)
  1409. return -EINVAL;
  1410. return getrusage(current, who, ru);
  1411. }
  1412. asmlinkage long sys_umask(int mask)
  1413. {
  1414. mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
  1415. return mask;
  1416. }
  1417. asmlinkage long sys_prctl(int option, unsigned long arg2, unsigned long arg3,
  1418. unsigned long arg4, unsigned long arg5)
  1419. {
  1420. long error = 0;
  1421. if (security_task_prctl(option, arg2, arg3, arg4, arg5, &error))
  1422. return error;
  1423. switch (option) {
  1424. case PR_SET_PDEATHSIG:
  1425. if (!valid_signal(arg2)) {
  1426. error = -EINVAL;
  1427. break;
  1428. }
  1429. current->pdeath_signal = arg2;
  1430. break;
  1431. case PR_GET_PDEATHSIG:
  1432. error = put_user(current->pdeath_signal, (int __user *)arg2);
  1433. break;
  1434. case PR_GET_DUMPABLE:
  1435. error = get_dumpable(current->mm);
  1436. break;
  1437. case PR_SET_DUMPABLE:
  1438. if (arg2 < 0 || arg2 > 1) {
  1439. error = -EINVAL;
  1440. break;
  1441. }
  1442. set_dumpable(current->mm, arg2);
  1443. break;
  1444. case PR_SET_UNALIGN:
  1445. error = SET_UNALIGN_CTL(current, arg2);
  1446. break;
  1447. case PR_GET_UNALIGN:
  1448. error = GET_UNALIGN_CTL(current, arg2);
  1449. break;
  1450. case PR_SET_FPEMU:
  1451. error = SET_FPEMU_CTL(current, arg2);
  1452. break;
  1453. case PR_GET_FPEMU:
  1454. error = GET_FPEMU_CTL(current, arg2);
  1455. break;
  1456. case PR_SET_FPEXC:
  1457. error = SET_FPEXC_CTL(current, arg2);
  1458. break;
  1459. case PR_GET_FPEXC:
  1460. error = GET_FPEXC_CTL(current, arg2);
  1461. break;
  1462. case PR_GET_TIMING:
  1463. error = PR_TIMING_STATISTICAL;
  1464. break;
  1465. case PR_SET_TIMING:
  1466. if (arg2 != PR_TIMING_STATISTICAL)
  1467. error = -EINVAL;
  1468. break;
  1469. case PR_SET_NAME: {
  1470. struct task_struct *me = current;
  1471. unsigned char ncomm[sizeof(me->comm)];
  1472. ncomm[sizeof(me->comm)-1] = 0;
  1473. if (strncpy_from_user(ncomm, (char __user *)arg2,
  1474. sizeof(me->comm)-1) < 0)
  1475. return -EFAULT;
  1476. set_task_comm(me, ncomm);
  1477. return 0;
  1478. }
  1479. case PR_GET_NAME: {
  1480. struct task_struct *me = current;
  1481. unsigned char tcomm[sizeof(me->comm)];
  1482. get_task_comm(tcomm, me);
  1483. if (copy_to_user((char __user *)arg2, tcomm, sizeof(tcomm)))
  1484. return -EFAULT;
  1485. return 0;
  1486. }
  1487. case PR_GET_ENDIAN:
  1488. error = GET_ENDIAN(current, arg2);
  1489. break;
  1490. case PR_SET_ENDIAN:
  1491. error = SET_ENDIAN(current, arg2);
  1492. break;
  1493. case PR_GET_SECCOMP:
  1494. error = prctl_get_seccomp();
  1495. break;
  1496. case PR_SET_SECCOMP:
  1497. error = prctl_set_seccomp(arg2);
  1498. break;
  1499. case PR_GET_TSC:
  1500. error = GET_TSC_CTL(arg2);
  1501. break;
  1502. case PR_SET_TSC:
  1503. error = SET_TSC_CTL(arg2);
  1504. break;
  1505. default:
  1506. error = -EINVAL;
  1507. break;
  1508. }
  1509. return error;
  1510. }
  1511. asmlinkage long sys_getcpu(unsigned __user *cpup, unsigned __user *nodep,
  1512. struct getcpu_cache __user *unused)
  1513. {
  1514. int err = 0;
  1515. int cpu = raw_smp_processor_id();
  1516. if (cpup)
  1517. err |= put_user(cpu, cpup);
  1518. if (nodep)
  1519. err |= put_user(cpu_to_node(cpu), nodep);
  1520. return err ? -EFAULT : 0;
  1521. }
  1522. char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
  1523. static void argv_cleanup(char **argv, char **envp)
  1524. {
  1525. argv_free(argv);
  1526. }
  1527. /**
  1528. * orderly_poweroff - Trigger an orderly system poweroff
  1529. * @force: force poweroff if command execution fails
  1530. *
  1531. * This may be called from any context to trigger a system shutdown.
  1532. * If the orderly shutdown fails, it will force an immediate shutdown.
  1533. */
  1534. int orderly_poweroff(bool force)
  1535. {
  1536. int argc;
  1537. char **argv = argv_split(GFP_ATOMIC, poweroff_cmd, &argc);
  1538. static char *envp[] = {
  1539. "HOME=/",
  1540. "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
  1541. NULL
  1542. };
  1543. int ret = -ENOMEM;
  1544. struct subprocess_info *info;
  1545. if (argv == NULL) {
  1546. printk(KERN_WARNING "%s failed to allocate memory for \"%s\"\n",
  1547. __func__, poweroff_cmd);
  1548. goto out;
  1549. }
  1550. info = call_usermodehelper_setup(argv[0], argv, envp, GFP_ATOMIC);
  1551. if (info == NULL) {
  1552. argv_free(argv);
  1553. goto out;
  1554. }
  1555. call_usermodehelper_setcleanup(info, argv_cleanup);
  1556. ret = call_usermodehelper_exec(info, UMH_NO_WAIT);
  1557. out:
  1558. if (ret && force) {
  1559. printk(KERN_WARNING "Failed to start orderly shutdown: "
  1560. "forcing the issue\n");
  1561. /* I guess this should try to kick off some daemon to
  1562. sync and poweroff asap. Or not even bother syncing
  1563. if we're doing an emergency shutdown? */
  1564. emergency_sync();
  1565. kernel_power_off();
  1566. }
  1567. return ret;
  1568. }
  1569. EXPORT_SYMBOL_GPL(orderly_poweroff);