sys.c 52 KB

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