sys.c 43 KB

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