sys.c 49 KB

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