sys.c 52 KB

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