sys.c 42 KB

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