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