sys.c 43 KB

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