sys.c 42 KB

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