sys.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461
  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/workqueue.h>
  19. #include <linux/capability.h>
  20. #include <linux/device.h>
  21. #include <linux/key.h>
  22. #include <linux/times.h>
  23. #include <linux/posix-timers.h>
  24. #include <linux/security.h>
  25. #include <linux/dcookies.h>
  26. #include <linux/suspend.h>
  27. #include <linux/tty.h>
  28. #include <linux/signal.h>
  29. #include <linux/cn_proc.h>
  30. #include <linux/getcpu.h>
  31. #include <linux/task_io_accounting_ops.h>
  32. #include <linux/seccomp.h>
  33. #include <linux/cpu.h>
  34. #include <linux/personality.h>
  35. #include <linux/ptrace.h>
  36. #include <linux/fs_struct.h>
  37. #include <linux/file.h>
  38. #include <linux/mount.h>
  39. #include <linux/gfp.h>
  40. #include <linux/syscore_ops.h>
  41. #include <linux/version.h>
  42. #include <linux/ctype.h>
  43. #include <linux/compat.h>
  44. #include <linux/syscalls.h>
  45. #include <linux/kprobes.h>
  46. #include <linux/user_namespace.h>
  47. #include <linux/binfmts.h>
  48. #include <linux/sched.h>
  49. #include <linux/sched/autogroup.h>
  50. #include <linux/sched/loadavg.h>
  51. #include <linux/sched/stat.h>
  52. #include <linux/sched/mm.h>
  53. #include <linux/sched/coredump.h>
  54. #include <linux/sched/task.h>
  55. #include <linux/sched/cputime.h>
  56. #include <linux/rcupdate.h>
  57. #include <linux/uidgid.h>
  58. #include <linux/cred.h>
  59. #include <linux/kmsg_dump.h>
  60. /* Move somewhere else to avoid recompiling? */
  61. #include <generated/utsrelease.h>
  62. #include <linux/uaccess.h>
  63. #include <asm/io.h>
  64. #include <asm/unistd.h>
  65. #ifndef SET_UNALIGN_CTL
  66. # define SET_UNALIGN_CTL(a, b) (-EINVAL)
  67. #endif
  68. #ifndef GET_UNALIGN_CTL
  69. # define GET_UNALIGN_CTL(a, b) (-EINVAL)
  70. #endif
  71. #ifndef SET_FPEMU_CTL
  72. # define SET_FPEMU_CTL(a, b) (-EINVAL)
  73. #endif
  74. #ifndef GET_FPEMU_CTL
  75. # define GET_FPEMU_CTL(a, b) (-EINVAL)
  76. #endif
  77. #ifndef SET_FPEXC_CTL
  78. # define SET_FPEXC_CTL(a, b) (-EINVAL)
  79. #endif
  80. #ifndef GET_FPEXC_CTL
  81. # define GET_FPEXC_CTL(a, b) (-EINVAL)
  82. #endif
  83. #ifndef GET_ENDIAN
  84. # define GET_ENDIAN(a, b) (-EINVAL)
  85. #endif
  86. #ifndef SET_ENDIAN
  87. # define SET_ENDIAN(a, b) (-EINVAL)
  88. #endif
  89. #ifndef GET_TSC_CTL
  90. # define GET_TSC_CTL(a) (-EINVAL)
  91. #endif
  92. #ifndef SET_TSC_CTL
  93. # define SET_TSC_CTL(a) (-EINVAL)
  94. #endif
  95. #ifndef MPX_ENABLE_MANAGEMENT
  96. # define MPX_ENABLE_MANAGEMENT() (-EINVAL)
  97. #endif
  98. #ifndef MPX_DISABLE_MANAGEMENT
  99. # define MPX_DISABLE_MANAGEMENT() (-EINVAL)
  100. #endif
  101. #ifndef GET_FP_MODE
  102. # define GET_FP_MODE(a) (-EINVAL)
  103. #endif
  104. #ifndef SET_FP_MODE
  105. # define SET_FP_MODE(a,b) (-EINVAL)
  106. #endif
  107. /*
  108. * this is where the system-wide overflow UID and GID are defined, for
  109. * architectures that now have 32-bit UID/GID but didn't in the past
  110. */
  111. int overflowuid = DEFAULT_OVERFLOWUID;
  112. int overflowgid = DEFAULT_OVERFLOWGID;
  113. EXPORT_SYMBOL(overflowuid);
  114. EXPORT_SYMBOL(overflowgid);
  115. /*
  116. * the same as above, but for filesystems which can only store a 16-bit
  117. * UID and GID. as such, this is needed on all architectures
  118. */
  119. int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
  120. int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
  121. EXPORT_SYMBOL(fs_overflowuid);
  122. EXPORT_SYMBOL(fs_overflowgid);
  123. /*
  124. * Returns true if current's euid is same as p's uid or euid,
  125. * or has CAP_SYS_NICE to p's user_ns.
  126. *
  127. * Called with rcu_read_lock, creds are safe
  128. */
  129. static bool set_one_prio_perm(struct task_struct *p)
  130. {
  131. const struct cred *cred = current_cred(), *pcred = __task_cred(p);
  132. if (uid_eq(pcred->uid, cred->euid) ||
  133. uid_eq(pcred->euid, cred->euid))
  134. return true;
  135. if (ns_capable(pcred->user_ns, CAP_SYS_NICE))
  136. return true;
  137. return false;
  138. }
  139. /*
  140. * set the priority of a task
  141. * - the caller must hold the RCU read lock
  142. */
  143. static int set_one_prio(struct task_struct *p, int niceval, int error)
  144. {
  145. int no_nice;
  146. if (!set_one_prio_perm(p)) {
  147. error = -EPERM;
  148. goto out;
  149. }
  150. if (niceval < task_nice(p) && !can_nice(p, niceval)) {
  151. error = -EACCES;
  152. goto out;
  153. }
  154. no_nice = security_task_setnice(p, niceval);
  155. if (no_nice) {
  156. error = no_nice;
  157. goto out;
  158. }
  159. if (error == -ESRCH)
  160. error = 0;
  161. set_user_nice(p, niceval);
  162. out:
  163. return error;
  164. }
  165. SYSCALL_DEFINE3(setpriority, int, which, int, who, int, niceval)
  166. {
  167. struct task_struct *g, *p;
  168. struct user_struct *user;
  169. const struct cred *cred = current_cred();
  170. int error = -EINVAL;
  171. struct pid *pgrp;
  172. kuid_t uid;
  173. if (which > PRIO_USER || which < PRIO_PROCESS)
  174. goto out;
  175. /* normalize: avoid signed division (rounding problems) */
  176. error = -ESRCH;
  177. if (niceval < MIN_NICE)
  178. niceval = MIN_NICE;
  179. if (niceval > MAX_NICE)
  180. niceval = MAX_NICE;
  181. rcu_read_lock();
  182. read_lock(&tasklist_lock);
  183. switch (which) {
  184. case PRIO_PROCESS:
  185. if (who)
  186. p = find_task_by_vpid(who);
  187. else
  188. p = current;
  189. if (p)
  190. error = set_one_prio(p, niceval, error);
  191. break;
  192. case PRIO_PGRP:
  193. if (who)
  194. pgrp = find_vpid(who);
  195. else
  196. pgrp = task_pgrp(current);
  197. do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
  198. error = set_one_prio(p, niceval, error);
  199. } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
  200. break;
  201. case PRIO_USER:
  202. uid = make_kuid(cred->user_ns, who);
  203. user = cred->user;
  204. if (!who)
  205. uid = cred->uid;
  206. else if (!uid_eq(uid, cred->uid)) {
  207. user = find_user(uid);
  208. if (!user)
  209. goto out_unlock; /* No processes for this user */
  210. }
  211. do_each_thread(g, p) {
  212. if (uid_eq(task_uid(p), uid) && task_pid_vnr(p))
  213. error = set_one_prio(p, niceval, error);
  214. } while_each_thread(g, p);
  215. if (!uid_eq(uid, cred->uid))
  216. free_uid(user); /* For find_user() */
  217. break;
  218. }
  219. out_unlock:
  220. read_unlock(&tasklist_lock);
  221. rcu_read_unlock();
  222. out:
  223. return error;
  224. }
  225. /*
  226. * Ugh. To avoid negative return values, "getpriority()" will
  227. * not return the normal nice-value, but a negated value that
  228. * has been offset by 20 (ie it returns 40..1 instead of -20..19)
  229. * to stay compatible.
  230. */
  231. SYSCALL_DEFINE2(getpriority, int, which, int, who)
  232. {
  233. struct task_struct *g, *p;
  234. struct user_struct *user;
  235. const struct cred *cred = current_cred();
  236. long niceval, retval = -ESRCH;
  237. struct pid *pgrp;
  238. kuid_t uid;
  239. if (which > PRIO_USER || which < PRIO_PROCESS)
  240. return -EINVAL;
  241. rcu_read_lock();
  242. read_lock(&tasklist_lock);
  243. switch (which) {
  244. case PRIO_PROCESS:
  245. if (who)
  246. p = find_task_by_vpid(who);
  247. else
  248. p = current;
  249. if (p) {
  250. niceval = nice_to_rlimit(task_nice(p));
  251. if (niceval > retval)
  252. retval = niceval;
  253. }
  254. break;
  255. case PRIO_PGRP:
  256. if (who)
  257. pgrp = find_vpid(who);
  258. else
  259. pgrp = task_pgrp(current);
  260. do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
  261. niceval = nice_to_rlimit(task_nice(p));
  262. if (niceval > retval)
  263. retval = niceval;
  264. } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
  265. break;
  266. case PRIO_USER:
  267. uid = make_kuid(cred->user_ns, who);
  268. user = cred->user;
  269. if (!who)
  270. uid = cred->uid;
  271. else if (!uid_eq(uid, cred->uid)) {
  272. user = find_user(uid);
  273. if (!user)
  274. goto out_unlock; /* No processes for this user */
  275. }
  276. do_each_thread(g, p) {
  277. if (uid_eq(task_uid(p), uid) && task_pid_vnr(p)) {
  278. niceval = nice_to_rlimit(task_nice(p));
  279. if (niceval > retval)
  280. retval = niceval;
  281. }
  282. } while_each_thread(g, p);
  283. if (!uid_eq(uid, cred->uid))
  284. free_uid(user); /* for find_user() */
  285. break;
  286. }
  287. out_unlock:
  288. read_unlock(&tasklist_lock);
  289. rcu_read_unlock();
  290. return retval;
  291. }
  292. /*
  293. * Unprivileged users may change the real gid to the effective gid
  294. * or vice versa. (BSD-style)
  295. *
  296. * If you set the real gid at all, or set the effective gid to a value not
  297. * equal to the real gid, then the saved gid is set to the new effective gid.
  298. *
  299. * This makes it possible for a setgid program to completely drop its
  300. * privileges, which is often a useful assertion to make when you are doing
  301. * a security audit over a program.
  302. *
  303. * The general idea is that a program which uses just setregid() will be
  304. * 100% compatible with BSD. A program which uses just setgid() will be
  305. * 100% compatible with POSIX with saved IDs.
  306. *
  307. * SMP: There are not races, the GIDs are checked only by filesystem
  308. * operations (as far as semantic preservation is concerned).
  309. */
  310. #ifdef CONFIG_MULTIUSER
  311. SYSCALL_DEFINE2(setregid, gid_t, rgid, gid_t, egid)
  312. {
  313. struct user_namespace *ns = current_user_ns();
  314. const struct cred *old;
  315. struct cred *new;
  316. int retval;
  317. kgid_t krgid, kegid;
  318. krgid = make_kgid(ns, rgid);
  319. kegid = make_kgid(ns, egid);
  320. if ((rgid != (gid_t) -1) && !gid_valid(krgid))
  321. return -EINVAL;
  322. if ((egid != (gid_t) -1) && !gid_valid(kegid))
  323. return -EINVAL;
  324. new = prepare_creds();
  325. if (!new)
  326. return -ENOMEM;
  327. old = current_cred();
  328. retval = -EPERM;
  329. if (rgid != (gid_t) -1) {
  330. if (gid_eq(old->gid, krgid) ||
  331. gid_eq(old->egid, krgid) ||
  332. ns_capable(old->user_ns, CAP_SETGID))
  333. new->gid = krgid;
  334. else
  335. goto error;
  336. }
  337. if (egid != (gid_t) -1) {
  338. if (gid_eq(old->gid, kegid) ||
  339. gid_eq(old->egid, kegid) ||
  340. gid_eq(old->sgid, kegid) ||
  341. ns_capable(old->user_ns, CAP_SETGID))
  342. new->egid = kegid;
  343. else
  344. goto error;
  345. }
  346. if (rgid != (gid_t) -1 ||
  347. (egid != (gid_t) -1 && !gid_eq(kegid, old->gid)))
  348. new->sgid = new->egid;
  349. new->fsgid = new->egid;
  350. return commit_creds(new);
  351. error:
  352. abort_creds(new);
  353. return retval;
  354. }
  355. /*
  356. * setgid() is implemented like SysV w/ SAVED_IDS
  357. *
  358. * SMP: Same implicit races as above.
  359. */
  360. SYSCALL_DEFINE1(setgid, gid_t, gid)
  361. {
  362. struct user_namespace *ns = current_user_ns();
  363. const struct cred *old;
  364. struct cred *new;
  365. int retval;
  366. kgid_t kgid;
  367. kgid = make_kgid(ns, gid);
  368. if (!gid_valid(kgid))
  369. return -EINVAL;
  370. new = prepare_creds();
  371. if (!new)
  372. return -ENOMEM;
  373. old = current_cred();
  374. retval = -EPERM;
  375. if (ns_capable(old->user_ns, CAP_SETGID))
  376. new->gid = new->egid = new->sgid = new->fsgid = kgid;
  377. else if (gid_eq(kgid, old->gid) || gid_eq(kgid, old->sgid))
  378. new->egid = new->fsgid = kgid;
  379. else
  380. goto error;
  381. return commit_creds(new);
  382. error:
  383. abort_creds(new);
  384. return retval;
  385. }
  386. /*
  387. * change the user struct in a credentials set to match the new UID
  388. */
  389. static int set_user(struct cred *new)
  390. {
  391. struct user_struct *new_user;
  392. new_user = alloc_uid(new->uid);
  393. if (!new_user)
  394. return -EAGAIN;
  395. /*
  396. * We don't fail in case of NPROC limit excess here because too many
  397. * poorly written programs don't check set*uid() return code, assuming
  398. * it never fails if called by root. We may still enforce NPROC limit
  399. * for programs doing set*uid()+execve() by harmlessly deferring the
  400. * failure to the execve() stage.
  401. */
  402. if (atomic_read(&new_user->processes) >= rlimit(RLIMIT_NPROC) &&
  403. new_user != INIT_USER)
  404. current->flags |= PF_NPROC_EXCEEDED;
  405. else
  406. current->flags &= ~PF_NPROC_EXCEEDED;
  407. free_uid(new->user);
  408. new->user = new_user;
  409. return 0;
  410. }
  411. /*
  412. * Unprivileged users may change the real uid to the effective uid
  413. * or vice versa. (BSD-style)
  414. *
  415. * If you set the real uid at all, or set the effective uid to a value not
  416. * equal to the real uid, then the saved uid is set to the new effective uid.
  417. *
  418. * This makes it possible for a setuid program to completely drop its
  419. * privileges, which is often a useful assertion to make when you are doing
  420. * a security audit over a program.
  421. *
  422. * The general idea is that a program which uses just setreuid() will be
  423. * 100% compatible with BSD. A program which uses just setuid() will be
  424. * 100% compatible with POSIX with saved IDs.
  425. */
  426. SYSCALL_DEFINE2(setreuid, uid_t, ruid, uid_t, euid)
  427. {
  428. struct user_namespace *ns = current_user_ns();
  429. const struct cred *old;
  430. struct cred *new;
  431. int retval;
  432. kuid_t kruid, keuid;
  433. kruid = make_kuid(ns, ruid);
  434. keuid = make_kuid(ns, euid);
  435. if ((ruid != (uid_t) -1) && !uid_valid(kruid))
  436. return -EINVAL;
  437. if ((euid != (uid_t) -1) && !uid_valid(keuid))
  438. return -EINVAL;
  439. new = prepare_creds();
  440. if (!new)
  441. return -ENOMEM;
  442. old = current_cred();
  443. retval = -EPERM;
  444. if (ruid != (uid_t) -1) {
  445. new->uid = kruid;
  446. if (!uid_eq(old->uid, kruid) &&
  447. !uid_eq(old->euid, kruid) &&
  448. !ns_capable(old->user_ns, CAP_SETUID))
  449. goto error;
  450. }
  451. if (euid != (uid_t) -1) {
  452. new->euid = keuid;
  453. if (!uid_eq(old->uid, keuid) &&
  454. !uid_eq(old->euid, keuid) &&
  455. !uid_eq(old->suid, keuid) &&
  456. !ns_capable(old->user_ns, CAP_SETUID))
  457. goto error;
  458. }
  459. if (!uid_eq(new->uid, old->uid)) {
  460. retval = set_user(new);
  461. if (retval < 0)
  462. goto error;
  463. }
  464. if (ruid != (uid_t) -1 ||
  465. (euid != (uid_t) -1 && !uid_eq(keuid, old->uid)))
  466. new->suid = new->euid;
  467. new->fsuid = new->euid;
  468. retval = security_task_fix_setuid(new, old, LSM_SETID_RE);
  469. if (retval < 0)
  470. goto error;
  471. return commit_creds(new);
  472. error:
  473. abort_creds(new);
  474. return retval;
  475. }
  476. /*
  477. * setuid() is implemented like SysV with SAVED_IDS
  478. *
  479. * Note that SAVED_ID's is deficient in that a setuid root program
  480. * like sendmail, for example, cannot set its uid to be a normal
  481. * user and then switch back, because if you're root, setuid() sets
  482. * the saved uid too. If you don't like this, blame the bright people
  483. * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
  484. * will allow a root program to temporarily drop privileges and be able to
  485. * regain them by swapping the real and effective uid.
  486. */
  487. SYSCALL_DEFINE1(setuid, uid_t, uid)
  488. {
  489. struct user_namespace *ns = current_user_ns();
  490. const struct cred *old;
  491. struct cred *new;
  492. int retval;
  493. kuid_t kuid;
  494. kuid = make_kuid(ns, uid);
  495. if (!uid_valid(kuid))
  496. return -EINVAL;
  497. new = prepare_creds();
  498. if (!new)
  499. return -ENOMEM;
  500. old = current_cred();
  501. retval = -EPERM;
  502. if (ns_capable(old->user_ns, CAP_SETUID)) {
  503. new->suid = new->uid = kuid;
  504. if (!uid_eq(kuid, old->uid)) {
  505. retval = set_user(new);
  506. if (retval < 0)
  507. goto error;
  508. }
  509. } else if (!uid_eq(kuid, old->uid) && !uid_eq(kuid, new->suid)) {
  510. goto error;
  511. }
  512. new->fsuid = new->euid = kuid;
  513. retval = security_task_fix_setuid(new, old, LSM_SETID_ID);
  514. if (retval < 0)
  515. goto error;
  516. return commit_creds(new);
  517. error:
  518. abort_creds(new);
  519. return retval;
  520. }
  521. /*
  522. * This function implements a generic ability to update ruid, euid,
  523. * and suid. This allows you to implement the 4.4 compatible seteuid().
  524. */
  525. SYSCALL_DEFINE3(setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
  526. {
  527. struct user_namespace *ns = current_user_ns();
  528. const struct cred *old;
  529. struct cred *new;
  530. int retval;
  531. kuid_t kruid, keuid, ksuid;
  532. kruid = make_kuid(ns, ruid);
  533. keuid = make_kuid(ns, euid);
  534. ksuid = make_kuid(ns, suid);
  535. if ((ruid != (uid_t) -1) && !uid_valid(kruid))
  536. return -EINVAL;
  537. if ((euid != (uid_t) -1) && !uid_valid(keuid))
  538. return -EINVAL;
  539. if ((suid != (uid_t) -1) && !uid_valid(ksuid))
  540. return -EINVAL;
  541. new = prepare_creds();
  542. if (!new)
  543. return -ENOMEM;
  544. old = current_cred();
  545. retval = -EPERM;
  546. if (!ns_capable(old->user_ns, CAP_SETUID)) {
  547. if (ruid != (uid_t) -1 && !uid_eq(kruid, old->uid) &&
  548. !uid_eq(kruid, old->euid) && !uid_eq(kruid, old->suid))
  549. goto error;
  550. if (euid != (uid_t) -1 && !uid_eq(keuid, old->uid) &&
  551. !uid_eq(keuid, old->euid) && !uid_eq(keuid, old->suid))
  552. goto error;
  553. if (suid != (uid_t) -1 && !uid_eq(ksuid, old->uid) &&
  554. !uid_eq(ksuid, old->euid) && !uid_eq(ksuid, old->suid))
  555. goto error;
  556. }
  557. if (ruid != (uid_t) -1) {
  558. new->uid = kruid;
  559. if (!uid_eq(kruid, old->uid)) {
  560. retval = set_user(new);
  561. if (retval < 0)
  562. goto error;
  563. }
  564. }
  565. if (euid != (uid_t) -1)
  566. new->euid = keuid;
  567. if (suid != (uid_t) -1)
  568. new->suid = ksuid;
  569. new->fsuid = new->euid;
  570. retval = security_task_fix_setuid(new, old, LSM_SETID_RES);
  571. if (retval < 0)
  572. goto error;
  573. return commit_creds(new);
  574. error:
  575. abort_creds(new);
  576. return retval;
  577. }
  578. SYSCALL_DEFINE3(getresuid, uid_t __user *, ruidp, uid_t __user *, euidp, uid_t __user *, suidp)
  579. {
  580. const struct cred *cred = current_cred();
  581. int retval;
  582. uid_t ruid, euid, suid;
  583. ruid = from_kuid_munged(cred->user_ns, cred->uid);
  584. euid = from_kuid_munged(cred->user_ns, cred->euid);
  585. suid = from_kuid_munged(cred->user_ns, cred->suid);
  586. retval = put_user(ruid, ruidp);
  587. if (!retval) {
  588. retval = put_user(euid, euidp);
  589. if (!retval)
  590. return put_user(suid, suidp);
  591. }
  592. return retval;
  593. }
  594. /*
  595. * Same as above, but for rgid, egid, sgid.
  596. */
  597. SYSCALL_DEFINE3(setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
  598. {
  599. struct user_namespace *ns = current_user_ns();
  600. const struct cred *old;
  601. struct cred *new;
  602. int retval;
  603. kgid_t krgid, kegid, ksgid;
  604. krgid = make_kgid(ns, rgid);
  605. kegid = make_kgid(ns, egid);
  606. ksgid = make_kgid(ns, sgid);
  607. if ((rgid != (gid_t) -1) && !gid_valid(krgid))
  608. return -EINVAL;
  609. if ((egid != (gid_t) -1) && !gid_valid(kegid))
  610. return -EINVAL;
  611. if ((sgid != (gid_t) -1) && !gid_valid(ksgid))
  612. return -EINVAL;
  613. new = prepare_creds();
  614. if (!new)
  615. return -ENOMEM;
  616. old = current_cred();
  617. retval = -EPERM;
  618. if (!ns_capable(old->user_ns, CAP_SETGID)) {
  619. if (rgid != (gid_t) -1 && !gid_eq(krgid, old->gid) &&
  620. !gid_eq(krgid, old->egid) && !gid_eq(krgid, old->sgid))
  621. goto error;
  622. if (egid != (gid_t) -1 && !gid_eq(kegid, old->gid) &&
  623. !gid_eq(kegid, old->egid) && !gid_eq(kegid, old->sgid))
  624. goto error;
  625. if (sgid != (gid_t) -1 && !gid_eq(ksgid, old->gid) &&
  626. !gid_eq(ksgid, old->egid) && !gid_eq(ksgid, old->sgid))
  627. goto error;
  628. }
  629. if (rgid != (gid_t) -1)
  630. new->gid = krgid;
  631. if (egid != (gid_t) -1)
  632. new->egid = kegid;
  633. if (sgid != (gid_t) -1)
  634. new->sgid = ksgid;
  635. new->fsgid = new->egid;
  636. return commit_creds(new);
  637. error:
  638. abort_creds(new);
  639. return retval;
  640. }
  641. SYSCALL_DEFINE3(getresgid, gid_t __user *, rgidp, gid_t __user *, egidp, gid_t __user *, sgidp)
  642. {
  643. const struct cred *cred = current_cred();
  644. int retval;
  645. gid_t rgid, egid, sgid;
  646. rgid = from_kgid_munged(cred->user_ns, cred->gid);
  647. egid = from_kgid_munged(cred->user_ns, cred->egid);
  648. sgid = from_kgid_munged(cred->user_ns, cred->sgid);
  649. retval = put_user(rgid, rgidp);
  650. if (!retval) {
  651. retval = put_user(egid, egidp);
  652. if (!retval)
  653. retval = put_user(sgid, sgidp);
  654. }
  655. return retval;
  656. }
  657. /*
  658. * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
  659. * is used for "access()" and for the NFS daemon (letting nfsd stay at
  660. * whatever uid it wants to). It normally shadows "euid", except when
  661. * explicitly set by setfsuid() or for access..
  662. */
  663. SYSCALL_DEFINE1(setfsuid, uid_t, uid)
  664. {
  665. const struct cred *old;
  666. struct cred *new;
  667. uid_t old_fsuid;
  668. kuid_t kuid;
  669. old = current_cred();
  670. old_fsuid = from_kuid_munged(old->user_ns, old->fsuid);
  671. kuid = make_kuid(old->user_ns, uid);
  672. if (!uid_valid(kuid))
  673. return old_fsuid;
  674. new = prepare_creds();
  675. if (!new)
  676. return old_fsuid;
  677. if (uid_eq(kuid, old->uid) || uid_eq(kuid, old->euid) ||
  678. uid_eq(kuid, old->suid) || uid_eq(kuid, old->fsuid) ||
  679. ns_capable(old->user_ns, CAP_SETUID)) {
  680. if (!uid_eq(kuid, old->fsuid)) {
  681. new->fsuid = kuid;
  682. if (security_task_fix_setuid(new, old, LSM_SETID_FS) == 0)
  683. goto change_okay;
  684. }
  685. }
  686. abort_creds(new);
  687. return old_fsuid;
  688. change_okay:
  689. commit_creds(new);
  690. return old_fsuid;
  691. }
  692. /*
  693. * Samma på svenska..
  694. */
  695. SYSCALL_DEFINE1(setfsgid, gid_t, gid)
  696. {
  697. const struct cred *old;
  698. struct cred *new;
  699. gid_t old_fsgid;
  700. kgid_t kgid;
  701. old = current_cred();
  702. old_fsgid = from_kgid_munged(old->user_ns, old->fsgid);
  703. kgid = make_kgid(old->user_ns, gid);
  704. if (!gid_valid(kgid))
  705. return old_fsgid;
  706. new = prepare_creds();
  707. if (!new)
  708. return old_fsgid;
  709. if (gid_eq(kgid, old->gid) || gid_eq(kgid, old->egid) ||
  710. gid_eq(kgid, old->sgid) || gid_eq(kgid, old->fsgid) ||
  711. ns_capable(old->user_ns, CAP_SETGID)) {
  712. if (!gid_eq(kgid, old->fsgid)) {
  713. new->fsgid = kgid;
  714. goto change_okay;
  715. }
  716. }
  717. abort_creds(new);
  718. return old_fsgid;
  719. change_okay:
  720. commit_creds(new);
  721. return old_fsgid;
  722. }
  723. #endif /* CONFIG_MULTIUSER */
  724. /**
  725. * sys_getpid - return the thread group id of the current process
  726. *
  727. * Note, despite the name, this returns the tgid not the pid. The tgid and
  728. * the pid are identical unless CLONE_THREAD was specified on clone() in
  729. * which case the tgid is the same in all threads of the same group.
  730. *
  731. * This is SMP safe as current->tgid does not change.
  732. */
  733. SYSCALL_DEFINE0(getpid)
  734. {
  735. return task_tgid_vnr(current);
  736. }
  737. /* Thread ID - the internal kernel "pid" */
  738. SYSCALL_DEFINE0(gettid)
  739. {
  740. return task_pid_vnr(current);
  741. }
  742. /*
  743. * Accessing ->real_parent is not SMP-safe, it could
  744. * change from under us. However, we can use a stale
  745. * value of ->real_parent under rcu_read_lock(), see
  746. * release_task()->call_rcu(delayed_put_task_struct).
  747. */
  748. SYSCALL_DEFINE0(getppid)
  749. {
  750. int pid;
  751. rcu_read_lock();
  752. pid = task_tgid_vnr(rcu_dereference(current->real_parent));
  753. rcu_read_unlock();
  754. return pid;
  755. }
  756. SYSCALL_DEFINE0(getuid)
  757. {
  758. /* Only we change this so SMP safe */
  759. return from_kuid_munged(current_user_ns(), current_uid());
  760. }
  761. SYSCALL_DEFINE0(geteuid)
  762. {
  763. /* Only we change this so SMP safe */
  764. return from_kuid_munged(current_user_ns(), current_euid());
  765. }
  766. SYSCALL_DEFINE0(getgid)
  767. {
  768. /* Only we change this so SMP safe */
  769. return from_kgid_munged(current_user_ns(), current_gid());
  770. }
  771. SYSCALL_DEFINE0(getegid)
  772. {
  773. /* Only we change this so SMP safe */
  774. return from_kgid_munged(current_user_ns(), current_egid());
  775. }
  776. void do_sys_times(struct tms *tms)
  777. {
  778. u64 tgutime, tgstime, cutime, cstime;
  779. thread_group_cputime_adjusted(current, &tgutime, &tgstime);
  780. cutime = current->signal->cutime;
  781. cstime = current->signal->cstime;
  782. tms->tms_utime = nsec_to_clock_t(tgutime);
  783. tms->tms_stime = nsec_to_clock_t(tgstime);
  784. tms->tms_cutime = nsec_to_clock_t(cutime);
  785. tms->tms_cstime = nsec_to_clock_t(cstime);
  786. }
  787. SYSCALL_DEFINE1(times, struct tms __user *, tbuf)
  788. {
  789. if (tbuf) {
  790. struct tms tmp;
  791. do_sys_times(&tmp);
  792. if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
  793. return -EFAULT;
  794. }
  795. force_successful_syscall_return();
  796. return (long) jiffies_64_to_clock_t(get_jiffies_64());
  797. }
  798. /*
  799. * This needs some heavy checking ...
  800. * I just haven't the stomach for it. I also don't fully
  801. * understand sessions/pgrp etc. Let somebody who does explain it.
  802. *
  803. * OK, I think I have the protection semantics right.... this is really
  804. * only important on a multi-user system anyway, to make sure one user
  805. * can't send a signal to a process owned by another. -TYT, 12/12/91
  806. *
  807. * !PF_FORKNOEXEC check to conform completely to POSIX.
  808. */
  809. SYSCALL_DEFINE2(setpgid, pid_t, pid, pid_t, pgid)
  810. {
  811. struct task_struct *p;
  812. struct task_struct *group_leader = current->group_leader;
  813. struct pid *pgrp;
  814. int err;
  815. if (!pid)
  816. pid = task_pid_vnr(group_leader);
  817. if (!pgid)
  818. pgid = pid;
  819. if (pgid < 0)
  820. return -EINVAL;
  821. rcu_read_lock();
  822. /* From this point forward we keep holding onto the tasklist lock
  823. * so that our parent does not change from under us. -DaveM
  824. */
  825. write_lock_irq(&tasklist_lock);
  826. err = -ESRCH;
  827. p = find_task_by_vpid(pid);
  828. if (!p)
  829. goto out;
  830. err = -EINVAL;
  831. if (!thread_group_leader(p))
  832. goto out;
  833. if (same_thread_group(p->real_parent, group_leader)) {
  834. err = -EPERM;
  835. if (task_session(p) != task_session(group_leader))
  836. goto out;
  837. err = -EACCES;
  838. if (!(p->flags & PF_FORKNOEXEC))
  839. goto out;
  840. } else {
  841. err = -ESRCH;
  842. if (p != group_leader)
  843. goto out;
  844. }
  845. err = -EPERM;
  846. if (p->signal->leader)
  847. goto out;
  848. pgrp = task_pid(p);
  849. if (pgid != pid) {
  850. struct task_struct *g;
  851. pgrp = find_vpid(pgid);
  852. g = pid_task(pgrp, PIDTYPE_PGID);
  853. if (!g || task_session(g) != task_session(group_leader))
  854. goto out;
  855. }
  856. err = security_task_setpgid(p, pgid);
  857. if (err)
  858. goto out;
  859. if (task_pgrp(p) != pgrp)
  860. change_pid(p, PIDTYPE_PGID, pgrp);
  861. err = 0;
  862. out:
  863. /* All paths lead to here, thus we are safe. -DaveM */
  864. write_unlock_irq(&tasklist_lock);
  865. rcu_read_unlock();
  866. return err;
  867. }
  868. SYSCALL_DEFINE1(getpgid, pid_t, pid)
  869. {
  870. struct task_struct *p;
  871. struct pid *grp;
  872. int retval;
  873. rcu_read_lock();
  874. if (!pid)
  875. grp = task_pgrp(current);
  876. else {
  877. retval = -ESRCH;
  878. p = find_task_by_vpid(pid);
  879. if (!p)
  880. goto out;
  881. grp = task_pgrp(p);
  882. if (!grp)
  883. goto out;
  884. retval = security_task_getpgid(p);
  885. if (retval)
  886. goto out;
  887. }
  888. retval = pid_vnr(grp);
  889. out:
  890. rcu_read_unlock();
  891. return retval;
  892. }
  893. #ifdef __ARCH_WANT_SYS_GETPGRP
  894. SYSCALL_DEFINE0(getpgrp)
  895. {
  896. return sys_getpgid(0);
  897. }
  898. #endif
  899. SYSCALL_DEFINE1(getsid, pid_t, pid)
  900. {
  901. struct task_struct *p;
  902. struct pid *sid;
  903. int retval;
  904. rcu_read_lock();
  905. if (!pid)
  906. sid = task_session(current);
  907. else {
  908. retval = -ESRCH;
  909. p = find_task_by_vpid(pid);
  910. if (!p)
  911. goto out;
  912. sid = task_session(p);
  913. if (!sid)
  914. goto out;
  915. retval = security_task_getsid(p);
  916. if (retval)
  917. goto out;
  918. }
  919. retval = pid_vnr(sid);
  920. out:
  921. rcu_read_unlock();
  922. return retval;
  923. }
  924. static void set_special_pids(struct pid *pid)
  925. {
  926. struct task_struct *curr = current->group_leader;
  927. if (task_session(curr) != pid)
  928. change_pid(curr, PIDTYPE_SID, pid);
  929. if (task_pgrp(curr) != pid)
  930. change_pid(curr, PIDTYPE_PGID, pid);
  931. }
  932. SYSCALL_DEFINE0(setsid)
  933. {
  934. struct task_struct *group_leader = current->group_leader;
  935. struct pid *sid = task_pid(group_leader);
  936. pid_t session = pid_vnr(sid);
  937. int err = -EPERM;
  938. write_lock_irq(&tasklist_lock);
  939. /* Fail if I am already a session leader */
  940. if (group_leader->signal->leader)
  941. goto out;
  942. /* Fail if a process group id already exists that equals the
  943. * proposed session id.
  944. */
  945. if (pid_task(sid, PIDTYPE_PGID))
  946. goto out;
  947. group_leader->signal->leader = 1;
  948. set_special_pids(sid);
  949. proc_clear_tty(group_leader);
  950. err = session;
  951. out:
  952. write_unlock_irq(&tasklist_lock);
  953. if (err > 0) {
  954. proc_sid_connector(group_leader);
  955. sched_autogroup_create_attach(group_leader);
  956. }
  957. return err;
  958. }
  959. DECLARE_RWSEM(uts_sem);
  960. #ifdef COMPAT_UTS_MACHINE
  961. #define override_architecture(name) \
  962. (personality(current->personality) == PER_LINUX32 && \
  963. copy_to_user(name->machine, COMPAT_UTS_MACHINE, \
  964. sizeof(COMPAT_UTS_MACHINE)))
  965. #else
  966. #define override_architecture(name) 0
  967. #endif
  968. /*
  969. * Work around broken programs that cannot handle "Linux 3.0".
  970. * Instead we map 3.x to 2.6.40+x, so e.g. 3.0 would be 2.6.40
  971. * And we map 4.x to 2.6.60+x, so 4.0 would be 2.6.60.
  972. */
  973. static int override_release(char __user *release, size_t len)
  974. {
  975. int ret = 0;
  976. if (current->personality & UNAME26) {
  977. const char *rest = UTS_RELEASE;
  978. char buf[65] = { 0 };
  979. int ndots = 0;
  980. unsigned v;
  981. size_t copy;
  982. while (*rest) {
  983. if (*rest == '.' && ++ndots >= 3)
  984. break;
  985. if (!isdigit(*rest) && *rest != '.')
  986. break;
  987. rest++;
  988. }
  989. v = ((LINUX_VERSION_CODE >> 8) & 0xff) + 60;
  990. copy = clamp_t(size_t, len, 1, sizeof(buf));
  991. copy = scnprintf(buf, copy, "2.6.%u%s", v, rest);
  992. ret = copy_to_user(release, buf, copy + 1);
  993. }
  994. return ret;
  995. }
  996. SYSCALL_DEFINE1(newuname, struct new_utsname __user *, name)
  997. {
  998. int errno = 0;
  999. down_read(&uts_sem);
  1000. if (copy_to_user(name, utsname(), sizeof *name))
  1001. errno = -EFAULT;
  1002. up_read(&uts_sem);
  1003. if (!errno && override_release(name->release, sizeof(name->release)))
  1004. errno = -EFAULT;
  1005. if (!errno && override_architecture(name))
  1006. errno = -EFAULT;
  1007. return errno;
  1008. }
  1009. #ifdef __ARCH_WANT_SYS_OLD_UNAME
  1010. /*
  1011. * Old cruft
  1012. */
  1013. SYSCALL_DEFINE1(uname, struct old_utsname __user *, name)
  1014. {
  1015. int error = 0;
  1016. if (!name)
  1017. return -EFAULT;
  1018. down_read(&uts_sem);
  1019. if (copy_to_user(name, utsname(), sizeof(*name)))
  1020. error = -EFAULT;
  1021. up_read(&uts_sem);
  1022. if (!error && override_release(name->release, sizeof(name->release)))
  1023. error = -EFAULT;
  1024. if (!error && override_architecture(name))
  1025. error = -EFAULT;
  1026. return error;
  1027. }
  1028. SYSCALL_DEFINE1(olduname, struct oldold_utsname __user *, name)
  1029. {
  1030. int error;
  1031. if (!name)
  1032. return -EFAULT;
  1033. if (!access_ok(VERIFY_WRITE, name, sizeof(struct oldold_utsname)))
  1034. return -EFAULT;
  1035. down_read(&uts_sem);
  1036. error = __copy_to_user(&name->sysname, &utsname()->sysname,
  1037. __OLD_UTS_LEN);
  1038. error |= __put_user(0, name->sysname + __OLD_UTS_LEN);
  1039. error |= __copy_to_user(&name->nodename, &utsname()->nodename,
  1040. __OLD_UTS_LEN);
  1041. error |= __put_user(0, name->nodename + __OLD_UTS_LEN);
  1042. error |= __copy_to_user(&name->release, &utsname()->release,
  1043. __OLD_UTS_LEN);
  1044. error |= __put_user(0, name->release + __OLD_UTS_LEN);
  1045. error |= __copy_to_user(&name->version, &utsname()->version,
  1046. __OLD_UTS_LEN);
  1047. error |= __put_user(0, name->version + __OLD_UTS_LEN);
  1048. error |= __copy_to_user(&name->machine, &utsname()->machine,
  1049. __OLD_UTS_LEN);
  1050. error |= __put_user(0, name->machine + __OLD_UTS_LEN);
  1051. up_read(&uts_sem);
  1052. if (!error && override_architecture(name))
  1053. error = -EFAULT;
  1054. if (!error && override_release(name->release, sizeof(name->release)))
  1055. error = -EFAULT;
  1056. return error ? -EFAULT : 0;
  1057. }
  1058. #endif
  1059. SYSCALL_DEFINE2(sethostname, char __user *, name, int, len)
  1060. {
  1061. int errno;
  1062. char tmp[__NEW_UTS_LEN];
  1063. if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
  1064. return -EPERM;
  1065. if (len < 0 || len > __NEW_UTS_LEN)
  1066. return -EINVAL;
  1067. down_write(&uts_sem);
  1068. errno = -EFAULT;
  1069. if (!copy_from_user(tmp, name, len)) {
  1070. struct new_utsname *u = utsname();
  1071. memcpy(u->nodename, tmp, len);
  1072. memset(u->nodename + len, 0, sizeof(u->nodename) - len);
  1073. errno = 0;
  1074. uts_proc_notify(UTS_PROC_HOSTNAME);
  1075. }
  1076. up_write(&uts_sem);
  1077. return errno;
  1078. }
  1079. #ifdef __ARCH_WANT_SYS_GETHOSTNAME
  1080. SYSCALL_DEFINE2(gethostname, char __user *, name, int, len)
  1081. {
  1082. int i, errno;
  1083. struct new_utsname *u;
  1084. if (len < 0)
  1085. return -EINVAL;
  1086. down_read(&uts_sem);
  1087. u = utsname();
  1088. i = 1 + strlen(u->nodename);
  1089. if (i > len)
  1090. i = len;
  1091. errno = 0;
  1092. if (copy_to_user(name, u->nodename, i))
  1093. errno = -EFAULT;
  1094. up_read(&uts_sem);
  1095. return errno;
  1096. }
  1097. #endif
  1098. /*
  1099. * Only setdomainname; getdomainname can be implemented by calling
  1100. * uname()
  1101. */
  1102. SYSCALL_DEFINE2(setdomainname, char __user *, name, int, len)
  1103. {
  1104. int errno;
  1105. char tmp[__NEW_UTS_LEN];
  1106. if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
  1107. return -EPERM;
  1108. if (len < 0 || len > __NEW_UTS_LEN)
  1109. return -EINVAL;
  1110. down_write(&uts_sem);
  1111. errno = -EFAULT;
  1112. if (!copy_from_user(tmp, name, len)) {
  1113. struct new_utsname *u = utsname();
  1114. memcpy(u->domainname, tmp, len);
  1115. memset(u->domainname + len, 0, sizeof(u->domainname) - len);
  1116. errno = 0;
  1117. uts_proc_notify(UTS_PROC_DOMAINNAME);
  1118. }
  1119. up_write(&uts_sem);
  1120. return errno;
  1121. }
  1122. SYSCALL_DEFINE2(getrlimit, unsigned int, resource, struct rlimit __user *, rlim)
  1123. {
  1124. struct rlimit value;
  1125. int ret;
  1126. ret = do_prlimit(current, resource, NULL, &value);
  1127. if (!ret)
  1128. ret = copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
  1129. return ret;
  1130. }
  1131. #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
  1132. /*
  1133. * Back compatibility for getrlimit. Needed for some apps.
  1134. */
  1135. SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
  1136. struct rlimit __user *, rlim)
  1137. {
  1138. struct rlimit x;
  1139. if (resource >= RLIM_NLIMITS)
  1140. return -EINVAL;
  1141. task_lock(current->group_leader);
  1142. x = current->signal->rlim[resource];
  1143. task_unlock(current->group_leader);
  1144. if (x.rlim_cur > 0x7FFFFFFF)
  1145. x.rlim_cur = 0x7FFFFFFF;
  1146. if (x.rlim_max > 0x7FFFFFFF)
  1147. x.rlim_max = 0x7FFFFFFF;
  1148. return copy_to_user(rlim, &x, sizeof(x)) ? -EFAULT : 0;
  1149. }
  1150. #endif
  1151. static inline bool rlim64_is_infinity(__u64 rlim64)
  1152. {
  1153. #if BITS_PER_LONG < 64
  1154. return rlim64 >= ULONG_MAX;
  1155. #else
  1156. return rlim64 == RLIM64_INFINITY;
  1157. #endif
  1158. }
  1159. static void rlim_to_rlim64(const struct rlimit *rlim, struct rlimit64 *rlim64)
  1160. {
  1161. if (rlim->rlim_cur == RLIM_INFINITY)
  1162. rlim64->rlim_cur = RLIM64_INFINITY;
  1163. else
  1164. rlim64->rlim_cur = rlim->rlim_cur;
  1165. if (rlim->rlim_max == RLIM_INFINITY)
  1166. rlim64->rlim_max = RLIM64_INFINITY;
  1167. else
  1168. rlim64->rlim_max = rlim->rlim_max;
  1169. }
  1170. static void rlim64_to_rlim(const struct rlimit64 *rlim64, struct rlimit *rlim)
  1171. {
  1172. if (rlim64_is_infinity(rlim64->rlim_cur))
  1173. rlim->rlim_cur = RLIM_INFINITY;
  1174. else
  1175. rlim->rlim_cur = (unsigned long)rlim64->rlim_cur;
  1176. if (rlim64_is_infinity(rlim64->rlim_max))
  1177. rlim->rlim_max = RLIM_INFINITY;
  1178. else
  1179. rlim->rlim_max = (unsigned long)rlim64->rlim_max;
  1180. }
  1181. /* make sure you are allowed to change @tsk limits before calling this */
  1182. int do_prlimit(struct task_struct *tsk, unsigned int resource,
  1183. struct rlimit *new_rlim, struct rlimit *old_rlim)
  1184. {
  1185. struct rlimit *rlim;
  1186. int retval = 0;
  1187. if (resource >= RLIM_NLIMITS)
  1188. return -EINVAL;
  1189. if (new_rlim) {
  1190. if (new_rlim->rlim_cur > new_rlim->rlim_max)
  1191. return -EINVAL;
  1192. if (resource == RLIMIT_NOFILE &&
  1193. new_rlim->rlim_max > sysctl_nr_open)
  1194. return -EPERM;
  1195. }
  1196. /* protect tsk->signal and tsk->sighand from disappearing */
  1197. read_lock(&tasklist_lock);
  1198. if (!tsk->sighand) {
  1199. retval = -ESRCH;
  1200. goto out;
  1201. }
  1202. rlim = tsk->signal->rlim + resource;
  1203. task_lock(tsk->group_leader);
  1204. if (new_rlim) {
  1205. /* Keep the capable check against init_user_ns until
  1206. cgroups can contain all limits */
  1207. if (new_rlim->rlim_max > rlim->rlim_max &&
  1208. !capable(CAP_SYS_RESOURCE))
  1209. retval = -EPERM;
  1210. if (!retval)
  1211. retval = security_task_setrlimit(tsk->group_leader,
  1212. resource, new_rlim);
  1213. if (resource == RLIMIT_CPU && new_rlim->rlim_cur == 0) {
  1214. /*
  1215. * The caller is asking for an immediate RLIMIT_CPU
  1216. * expiry. But we use the zero value to mean "it was
  1217. * never set". So let's cheat and make it one second
  1218. * instead
  1219. */
  1220. new_rlim->rlim_cur = 1;
  1221. }
  1222. }
  1223. if (!retval) {
  1224. if (old_rlim)
  1225. *old_rlim = *rlim;
  1226. if (new_rlim)
  1227. *rlim = *new_rlim;
  1228. }
  1229. task_unlock(tsk->group_leader);
  1230. /*
  1231. * RLIMIT_CPU handling. Note that the kernel fails to return an error
  1232. * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
  1233. * very long-standing error, and fixing it now risks breakage of
  1234. * applications, so we live with it
  1235. */
  1236. if (!retval && new_rlim && resource == RLIMIT_CPU &&
  1237. new_rlim->rlim_cur != RLIM_INFINITY &&
  1238. IS_ENABLED(CONFIG_POSIX_TIMERS))
  1239. update_rlimit_cpu(tsk, new_rlim->rlim_cur);
  1240. out:
  1241. read_unlock(&tasklist_lock);
  1242. return retval;
  1243. }
  1244. /* rcu lock must be held */
  1245. static int check_prlimit_permission(struct task_struct *task,
  1246. unsigned int flags)
  1247. {
  1248. const struct cred *cred = current_cred(), *tcred;
  1249. bool id_match;
  1250. if (current == task)
  1251. return 0;
  1252. tcred = __task_cred(task);
  1253. id_match = (uid_eq(cred->uid, tcred->euid) &&
  1254. uid_eq(cred->uid, tcred->suid) &&
  1255. uid_eq(cred->uid, tcred->uid) &&
  1256. gid_eq(cred->gid, tcred->egid) &&
  1257. gid_eq(cred->gid, tcred->sgid) &&
  1258. gid_eq(cred->gid, tcred->gid));
  1259. if (!id_match && !ns_capable(tcred->user_ns, CAP_SYS_RESOURCE))
  1260. return -EPERM;
  1261. return security_task_prlimit(cred, tcred, flags);
  1262. }
  1263. SYSCALL_DEFINE4(prlimit64, pid_t, pid, unsigned int, resource,
  1264. const struct rlimit64 __user *, new_rlim,
  1265. struct rlimit64 __user *, old_rlim)
  1266. {
  1267. struct rlimit64 old64, new64;
  1268. struct rlimit old, new;
  1269. struct task_struct *tsk;
  1270. unsigned int checkflags = 0;
  1271. int ret;
  1272. if (old_rlim)
  1273. checkflags |= LSM_PRLIMIT_READ;
  1274. if (new_rlim) {
  1275. if (copy_from_user(&new64, new_rlim, sizeof(new64)))
  1276. return -EFAULT;
  1277. rlim64_to_rlim(&new64, &new);
  1278. checkflags |= LSM_PRLIMIT_WRITE;
  1279. }
  1280. rcu_read_lock();
  1281. tsk = pid ? find_task_by_vpid(pid) : current;
  1282. if (!tsk) {
  1283. rcu_read_unlock();
  1284. return -ESRCH;
  1285. }
  1286. ret = check_prlimit_permission(tsk, checkflags);
  1287. if (ret) {
  1288. rcu_read_unlock();
  1289. return ret;
  1290. }
  1291. get_task_struct(tsk);
  1292. rcu_read_unlock();
  1293. ret = do_prlimit(tsk, resource, new_rlim ? &new : NULL,
  1294. old_rlim ? &old : NULL);
  1295. if (!ret && old_rlim) {
  1296. rlim_to_rlim64(&old, &old64);
  1297. if (copy_to_user(old_rlim, &old64, sizeof(old64)))
  1298. ret = -EFAULT;
  1299. }
  1300. put_task_struct(tsk);
  1301. return ret;
  1302. }
  1303. SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim)
  1304. {
  1305. struct rlimit new_rlim;
  1306. if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
  1307. return -EFAULT;
  1308. return do_prlimit(current, resource, &new_rlim, NULL);
  1309. }
  1310. /*
  1311. * It would make sense to put struct rusage in the task_struct,
  1312. * except that would make the task_struct be *really big*. After
  1313. * task_struct gets moved into malloc'ed memory, it would
  1314. * make sense to do this. It will make moving the rest of the information
  1315. * a lot simpler! (Which we're not doing right now because we're not
  1316. * measuring them yet).
  1317. *
  1318. * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
  1319. * races with threads incrementing their own counters. But since word
  1320. * reads are atomic, we either get new values or old values and we don't
  1321. * care which for the sums. We always take the siglock to protect reading
  1322. * the c* fields from p->signal from races with exit.c updating those
  1323. * fields when reaping, so a sample either gets all the additions of a
  1324. * given child after it's reaped, or none so this sample is before reaping.
  1325. *
  1326. * Locking:
  1327. * We need to take the siglock for CHILDEREN, SELF and BOTH
  1328. * for the cases current multithreaded, non-current single threaded
  1329. * non-current multithreaded. Thread traversal is now safe with
  1330. * the siglock held.
  1331. * Strictly speaking, we donot need to take the siglock if we are current and
  1332. * single threaded, as no one else can take our signal_struct away, no one
  1333. * else can reap the children to update signal->c* counters, and no one else
  1334. * can race with the signal-> fields. If we do not take any lock, the
  1335. * signal-> fields could be read out of order while another thread was just
  1336. * exiting. So we should place a read memory barrier when we avoid the lock.
  1337. * On the writer side, write memory barrier is implied in __exit_signal
  1338. * as __exit_signal releases the siglock spinlock after updating the signal->
  1339. * fields. But we don't do this yet to keep things simple.
  1340. *
  1341. */
  1342. static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r)
  1343. {
  1344. r->ru_nvcsw += t->nvcsw;
  1345. r->ru_nivcsw += t->nivcsw;
  1346. r->ru_minflt += t->min_flt;
  1347. r->ru_majflt += t->maj_flt;
  1348. r->ru_inblock += task_io_get_inblock(t);
  1349. r->ru_oublock += task_io_get_oublock(t);
  1350. }
  1351. static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
  1352. {
  1353. struct task_struct *t;
  1354. unsigned long flags;
  1355. u64 tgutime, tgstime, utime, stime;
  1356. unsigned long maxrss = 0;
  1357. memset((char *)r, 0, sizeof (*r));
  1358. utime = stime = 0;
  1359. if (who == RUSAGE_THREAD) {
  1360. task_cputime_adjusted(current, &utime, &stime);
  1361. accumulate_thread_rusage(p, r);
  1362. maxrss = p->signal->maxrss;
  1363. goto out;
  1364. }
  1365. if (!lock_task_sighand(p, &flags))
  1366. return;
  1367. switch (who) {
  1368. case RUSAGE_BOTH:
  1369. case RUSAGE_CHILDREN:
  1370. utime = p->signal->cutime;
  1371. stime = p->signal->cstime;
  1372. r->ru_nvcsw = p->signal->cnvcsw;
  1373. r->ru_nivcsw = p->signal->cnivcsw;
  1374. r->ru_minflt = p->signal->cmin_flt;
  1375. r->ru_majflt = p->signal->cmaj_flt;
  1376. r->ru_inblock = p->signal->cinblock;
  1377. r->ru_oublock = p->signal->coublock;
  1378. maxrss = p->signal->cmaxrss;
  1379. if (who == RUSAGE_CHILDREN)
  1380. break;
  1381. case RUSAGE_SELF:
  1382. thread_group_cputime_adjusted(p, &tgutime, &tgstime);
  1383. utime += tgutime;
  1384. stime += tgstime;
  1385. r->ru_nvcsw += p->signal->nvcsw;
  1386. r->ru_nivcsw += p->signal->nivcsw;
  1387. r->ru_minflt += p->signal->min_flt;
  1388. r->ru_majflt += p->signal->maj_flt;
  1389. r->ru_inblock += p->signal->inblock;
  1390. r->ru_oublock += p->signal->oublock;
  1391. if (maxrss < p->signal->maxrss)
  1392. maxrss = p->signal->maxrss;
  1393. t = p;
  1394. do {
  1395. accumulate_thread_rusage(t, r);
  1396. } while_each_thread(p, t);
  1397. break;
  1398. default:
  1399. BUG();
  1400. }
  1401. unlock_task_sighand(p, &flags);
  1402. out:
  1403. r->ru_utime = ns_to_timeval(utime);
  1404. r->ru_stime = ns_to_timeval(stime);
  1405. if (who != RUSAGE_CHILDREN) {
  1406. struct mm_struct *mm = get_task_mm(p);
  1407. if (mm) {
  1408. setmax_mm_hiwater_rss(&maxrss, mm);
  1409. mmput(mm);
  1410. }
  1411. }
  1412. r->ru_maxrss = maxrss * (PAGE_SIZE / 1024); /* convert pages to KBs */
  1413. }
  1414. int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
  1415. {
  1416. struct rusage r;
  1417. k_getrusage(p, who, &r);
  1418. return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
  1419. }
  1420. SYSCALL_DEFINE2(getrusage, int, who, struct rusage __user *, ru)
  1421. {
  1422. if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
  1423. who != RUSAGE_THREAD)
  1424. return -EINVAL;
  1425. return getrusage(current, who, ru);
  1426. }
  1427. #ifdef CONFIG_COMPAT
  1428. COMPAT_SYSCALL_DEFINE2(getrusage, int, who, struct compat_rusage __user *, ru)
  1429. {
  1430. struct rusage r;
  1431. if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
  1432. who != RUSAGE_THREAD)
  1433. return -EINVAL;
  1434. k_getrusage(current, who, &r);
  1435. return put_compat_rusage(&r, ru);
  1436. }
  1437. #endif
  1438. SYSCALL_DEFINE1(umask, int, mask)
  1439. {
  1440. mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
  1441. return mask;
  1442. }
  1443. static int prctl_set_mm_exe_file(struct mm_struct *mm, unsigned int fd)
  1444. {
  1445. struct fd exe;
  1446. struct file *old_exe, *exe_file;
  1447. struct inode *inode;
  1448. int err;
  1449. exe = fdget(fd);
  1450. if (!exe.file)
  1451. return -EBADF;
  1452. inode = file_inode(exe.file);
  1453. /*
  1454. * Because the original mm->exe_file points to executable file, make
  1455. * sure that this one is executable as well, to avoid breaking an
  1456. * overall picture.
  1457. */
  1458. err = -EACCES;
  1459. if (!S_ISREG(inode->i_mode) || path_noexec(&exe.file->f_path))
  1460. goto exit;
  1461. err = inode_permission(inode, MAY_EXEC);
  1462. if (err)
  1463. goto exit;
  1464. /*
  1465. * Forbid mm->exe_file change if old file still mapped.
  1466. */
  1467. exe_file = get_mm_exe_file(mm);
  1468. err = -EBUSY;
  1469. if (exe_file) {
  1470. struct vm_area_struct *vma;
  1471. down_read(&mm->mmap_sem);
  1472. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  1473. if (!vma->vm_file)
  1474. continue;
  1475. if (path_equal(&vma->vm_file->f_path,
  1476. &exe_file->f_path))
  1477. goto exit_err;
  1478. }
  1479. up_read(&mm->mmap_sem);
  1480. fput(exe_file);
  1481. }
  1482. err = 0;
  1483. /* set the new file, lockless */
  1484. get_file(exe.file);
  1485. old_exe = xchg(&mm->exe_file, exe.file);
  1486. if (old_exe)
  1487. fput(old_exe);
  1488. exit:
  1489. fdput(exe);
  1490. return err;
  1491. exit_err:
  1492. up_read(&mm->mmap_sem);
  1493. fput(exe_file);
  1494. goto exit;
  1495. }
  1496. /*
  1497. * WARNING: we don't require any capability here so be very careful
  1498. * in what is allowed for modification from userspace.
  1499. */
  1500. static int validate_prctl_map(struct prctl_mm_map *prctl_map)
  1501. {
  1502. unsigned long mmap_max_addr = TASK_SIZE;
  1503. struct mm_struct *mm = current->mm;
  1504. int error = -EINVAL, i;
  1505. static const unsigned char offsets[] = {
  1506. offsetof(struct prctl_mm_map, start_code),
  1507. offsetof(struct prctl_mm_map, end_code),
  1508. offsetof(struct prctl_mm_map, start_data),
  1509. offsetof(struct prctl_mm_map, end_data),
  1510. offsetof(struct prctl_mm_map, start_brk),
  1511. offsetof(struct prctl_mm_map, brk),
  1512. offsetof(struct prctl_mm_map, start_stack),
  1513. offsetof(struct prctl_mm_map, arg_start),
  1514. offsetof(struct prctl_mm_map, arg_end),
  1515. offsetof(struct prctl_mm_map, env_start),
  1516. offsetof(struct prctl_mm_map, env_end),
  1517. };
  1518. /*
  1519. * Make sure the members are not somewhere outside
  1520. * of allowed address space.
  1521. */
  1522. for (i = 0; i < ARRAY_SIZE(offsets); i++) {
  1523. u64 val = *(u64 *)((char *)prctl_map + offsets[i]);
  1524. if ((unsigned long)val >= mmap_max_addr ||
  1525. (unsigned long)val < mmap_min_addr)
  1526. goto out;
  1527. }
  1528. /*
  1529. * Make sure the pairs are ordered.
  1530. */
  1531. #define __prctl_check_order(__m1, __op, __m2) \
  1532. ((unsigned long)prctl_map->__m1 __op \
  1533. (unsigned long)prctl_map->__m2) ? 0 : -EINVAL
  1534. error = __prctl_check_order(start_code, <, end_code);
  1535. error |= __prctl_check_order(start_data, <, end_data);
  1536. error |= __prctl_check_order(start_brk, <=, brk);
  1537. error |= __prctl_check_order(arg_start, <=, arg_end);
  1538. error |= __prctl_check_order(env_start, <=, env_end);
  1539. if (error)
  1540. goto out;
  1541. #undef __prctl_check_order
  1542. error = -EINVAL;
  1543. /*
  1544. * @brk should be after @end_data in traditional maps.
  1545. */
  1546. if (prctl_map->start_brk <= prctl_map->end_data ||
  1547. prctl_map->brk <= prctl_map->end_data)
  1548. goto out;
  1549. /*
  1550. * Neither we should allow to override limits if they set.
  1551. */
  1552. if (check_data_rlimit(rlimit(RLIMIT_DATA), prctl_map->brk,
  1553. prctl_map->start_brk, prctl_map->end_data,
  1554. prctl_map->start_data))
  1555. goto out;
  1556. /*
  1557. * Someone is trying to cheat the auxv vector.
  1558. */
  1559. if (prctl_map->auxv_size) {
  1560. if (!prctl_map->auxv || prctl_map->auxv_size > sizeof(mm->saved_auxv))
  1561. goto out;
  1562. }
  1563. /*
  1564. * Finally, make sure the caller has the rights to
  1565. * change /proc/pid/exe link: only local root should
  1566. * be allowed to.
  1567. */
  1568. if (prctl_map->exe_fd != (u32)-1) {
  1569. struct user_namespace *ns = current_user_ns();
  1570. const struct cred *cred = current_cred();
  1571. if (!uid_eq(cred->uid, make_kuid(ns, 0)) ||
  1572. !gid_eq(cred->gid, make_kgid(ns, 0)))
  1573. goto out;
  1574. }
  1575. error = 0;
  1576. out:
  1577. return error;
  1578. }
  1579. #ifdef CONFIG_CHECKPOINT_RESTORE
  1580. static int prctl_set_mm_map(int opt, const void __user *addr, unsigned long data_size)
  1581. {
  1582. struct prctl_mm_map prctl_map = { .exe_fd = (u32)-1, };
  1583. unsigned long user_auxv[AT_VECTOR_SIZE];
  1584. struct mm_struct *mm = current->mm;
  1585. int error;
  1586. BUILD_BUG_ON(sizeof(user_auxv) != sizeof(mm->saved_auxv));
  1587. BUILD_BUG_ON(sizeof(struct prctl_mm_map) > 256);
  1588. if (opt == PR_SET_MM_MAP_SIZE)
  1589. return put_user((unsigned int)sizeof(prctl_map),
  1590. (unsigned int __user *)addr);
  1591. if (data_size != sizeof(prctl_map))
  1592. return -EINVAL;
  1593. if (copy_from_user(&prctl_map, addr, sizeof(prctl_map)))
  1594. return -EFAULT;
  1595. error = validate_prctl_map(&prctl_map);
  1596. if (error)
  1597. return error;
  1598. if (prctl_map.auxv_size) {
  1599. memset(user_auxv, 0, sizeof(user_auxv));
  1600. if (copy_from_user(user_auxv,
  1601. (const void __user *)prctl_map.auxv,
  1602. prctl_map.auxv_size))
  1603. return -EFAULT;
  1604. /* Last entry must be AT_NULL as specification requires */
  1605. user_auxv[AT_VECTOR_SIZE - 2] = AT_NULL;
  1606. user_auxv[AT_VECTOR_SIZE - 1] = AT_NULL;
  1607. }
  1608. if (prctl_map.exe_fd != (u32)-1) {
  1609. error = prctl_set_mm_exe_file(mm, prctl_map.exe_fd);
  1610. if (error)
  1611. return error;
  1612. }
  1613. down_write(&mm->mmap_sem);
  1614. /*
  1615. * We don't validate if these members are pointing to
  1616. * real present VMAs because application may have correspond
  1617. * VMAs already unmapped and kernel uses these members for statistics
  1618. * output in procfs mostly, except
  1619. *
  1620. * - @start_brk/@brk which are used in do_brk but kernel lookups
  1621. * for VMAs when updating these memvers so anything wrong written
  1622. * here cause kernel to swear at userspace program but won't lead
  1623. * to any problem in kernel itself
  1624. */
  1625. mm->start_code = prctl_map.start_code;
  1626. mm->end_code = prctl_map.end_code;
  1627. mm->start_data = prctl_map.start_data;
  1628. mm->end_data = prctl_map.end_data;
  1629. mm->start_brk = prctl_map.start_brk;
  1630. mm->brk = prctl_map.brk;
  1631. mm->start_stack = prctl_map.start_stack;
  1632. mm->arg_start = prctl_map.arg_start;
  1633. mm->arg_end = prctl_map.arg_end;
  1634. mm->env_start = prctl_map.env_start;
  1635. mm->env_end = prctl_map.env_end;
  1636. /*
  1637. * Note this update of @saved_auxv is lockless thus
  1638. * if someone reads this member in procfs while we're
  1639. * updating -- it may get partly updated results. It's
  1640. * known and acceptable trade off: we leave it as is to
  1641. * not introduce additional locks here making the kernel
  1642. * more complex.
  1643. */
  1644. if (prctl_map.auxv_size)
  1645. memcpy(mm->saved_auxv, user_auxv, sizeof(user_auxv));
  1646. up_write(&mm->mmap_sem);
  1647. return 0;
  1648. }
  1649. #endif /* CONFIG_CHECKPOINT_RESTORE */
  1650. static int prctl_set_auxv(struct mm_struct *mm, unsigned long addr,
  1651. unsigned long len)
  1652. {
  1653. /*
  1654. * This doesn't move the auxiliary vector itself since it's pinned to
  1655. * mm_struct, but it permits filling the vector with new values. It's
  1656. * up to the caller to provide sane values here, otherwise userspace
  1657. * tools which use this vector might be unhappy.
  1658. */
  1659. unsigned long user_auxv[AT_VECTOR_SIZE];
  1660. if (len > sizeof(user_auxv))
  1661. return -EINVAL;
  1662. if (copy_from_user(user_auxv, (const void __user *)addr, len))
  1663. return -EFAULT;
  1664. /* Make sure the last entry is always AT_NULL */
  1665. user_auxv[AT_VECTOR_SIZE - 2] = 0;
  1666. user_auxv[AT_VECTOR_SIZE - 1] = 0;
  1667. BUILD_BUG_ON(sizeof(user_auxv) != sizeof(mm->saved_auxv));
  1668. task_lock(current);
  1669. memcpy(mm->saved_auxv, user_auxv, len);
  1670. task_unlock(current);
  1671. return 0;
  1672. }
  1673. static int prctl_set_mm(int opt, unsigned long addr,
  1674. unsigned long arg4, unsigned long arg5)
  1675. {
  1676. struct mm_struct *mm = current->mm;
  1677. struct prctl_mm_map prctl_map;
  1678. struct vm_area_struct *vma;
  1679. int error;
  1680. if (arg5 || (arg4 && (opt != PR_SET_MM_AUXV &&
  1681. opt != PR_SET_MM_MAP &&
  1682. opt != PR_SET_MM_MAP_SIZE)))
  1683. return -EINVAL;
  1684. #ifdef CONFIG_CHECKPOINT_RESTORE
  1685. if (opt == PR_SET_MM_MAP || opt == PR_SET_MM_MAP_SIZE)
  1686. return prctl_set_mm_map(opt, (const void __user *)addr, arg4);
  1687. #endif
  1688. if (!capable(CAP_SYS_RESOURCE))
  1689. return -EPERM;
  1690. if (opt == PR_SET_MM_EXE_FILE)
  1691. return prctl_set_mm_exe_file(mm, (unsigned int)addr);
  1692. if (opt == PR_SET_MM_AUXV)
  1693. return prctl_set_auxv(mm, addr, arg4);
  1694. if (addr >= TASK_SIZE || addr < mmap_min_addr)
  1695. return -EINVAL;
  1696. error = -EINVAL;
  1697. down_write(&mm->mmap_sem);
  1698. vma = find_vma(mm, addr);
  1699. prctl_map.start_code = mm->start_code;
  1700. prctl_map.end_code = mm->end_code;
  1701. prctl_map.start_data = mm->start_data;
  1702. prctl_map.end_data = mm->end_data;
  1703. prctl_map.start_brk = mm->start_brk;
  1704. prctl_map.brk = mm->brk;
  1705. prctl_map.start_stack = mm->start_stack;
  1706. prctl_map.arg_start = mm->arg_start;
  1707. prctl_map.arg_end = mm->arg_end;
  1708. prctl_map.env_start = mm->env_start;
  1709. prctl_map.env_end = mm->env_end;
  1710. prctl_map.auxv = NULL;
  1711. prctl_map.auxv_size = 0;
  1712. prctl_map.exe_fd = -1;
  1713. switch (opt) {
  1714. case PR_SET_MM_START_CODE:
  1715. prctl_map.start_code = addr;
  1716. break;
  1717. case PR_SET_MM_END_CODE:
  1718. prctl_map.end_code = addr;
  1719. break;
  1720. case PR_SET_MM_START_DATA:
  1721. prctl_map.start_data = addr;
  1722. break;
  1723. case PR_SET_MM_END_DATA:
  1724. prctl_map.end_data = addr;
  1725. break;
  1726. case PR_SET_MM_START_STACK:
  1727. prctl_map.start_stack = addr;
  1728. break;
  1729. case PR_SET_MM_START_BRK:
  1730. prctl_map.start_brk = addr;
  1731. break;
  1732. case PR_SET_MM_BRK:
  1733. prctl_map.brk = addr;
  1734. break;
  1735. case PR_SET_MM_ARG_START:
  1736. prctl_map.arg_start = addr;
  1737. break;
  1738. case PR_SET_MM_ARG_END:
  1739. prctl_map.arg_end = addr;
  1740. break;
  1741. case PR_SET_MM_ENV_START:
  1742. prctl_map.env_start = addr;
  1743. break;
  1744. case PR_SET_MM_ENV_END:
  1745. prctl_map.env_end = addr;
  1746. break;
  1747. default:
  1748. goto out;
  1749. }
  1750. error = validate_prctl_map(&prctl_map);
  1751. if (error)
  1752. goto out;
  1753. switch (opt) {
  1754. /*
  1755. * If command line arguments and environment
  1756. * are placed somewhere else on stack, we can
  1757. * set them up here, ARG_START/END to setup
  1758. * command line argumets and ENV_START/END
  1759. * for environment.
  1760. */
  1761. case PR_SET_MM_START_STACK:
  1762. case PR_SET_MM_ARG_START:
  1763. case PR_SET_MM_ARG_END:
  1764. case PR_SET_MM_ENV_START:
  1765. case PR_SET_MM_ENV_END:
  1766. if (!vma) {
  1767. error = -EFAULT;
  1768. goto out;
  1769. }
  1770. }
  1771. mm->start_code = prctl_map.start_code;
  1772. mm->end_code = prctl_map.end_code;
  1773. mm->start_data = prctl_map.start_data;
  1774. mm->end_data = prctl_map.end_data;
  1775. mm->start_brk = prctl_map.start_brk;
  1776. mm->brk = prctl_map.brk;
  1777. mm->start_stack = prctl_map.start_stack;
  1778. mm->arg_start = prctl_map.arg_start;
  1779. mm->arg_end = prctl_map.arg_end;
  1780. mm->env_start = prctl_map.env_start;
  1781. mm->env_end = prctl_map.env_end;
  1782. error = 0;
  1783. out:
  1784. up_write(&mm->mmap_sem);
  1785. return error;
  1786. }
  1787. #ifdef CONFIG_CHECKPOINT_RESTORE
  1788. static int prctl_get_tid_address(struct task_struct *me, int __user **tid_addr)
  1789. {
  1790. return put_user(me->clear_child_tid, tid_addr);
  1791. }
  1792. #else
  1793. static int prctl_get_tid_address(struct task_struct *me, int __user **tid_addr)
  1794. {
  1795. return -EINVAL;
  1796. }
  1797. #endif
  1798. static int propagate_has_child_subreaper(struct task_struct *p, void *data)
  1799. {
  1800. /*
  1801. * If task has has_child_subreaper - all its decendants
  1802. * already have these flag too and new decendants will
  1803. * inherit it on fork, skip them.
  1804. *
  1805. * If we've found child_reaper - skip descendants in
  1806. * it's subtree as they will never get out pidns.
  1807. */
  1808. if (p->signal->has_child_subreaper ||
  1809. is_child_reaper(task_pid(p)))
  1810. return 0;
  1811. p->signal->has_child_subreaper = 1;
  1812. return 1;
  1813. }
  1814. SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
  1815. unsigned long, arg4, unsigned long, arg5)
  1816. {
  1817. struct task_struct *me = current;
  1818. unsigned char comm[sizeof(me->comm)];
  1819. long error;
  1820. error = security_task_prctl(option, arg2, arg3, arg4, arg5);
  1821. if (error != -ENOSYS)
  1822. return error;
  1823. error = 0;
  1824. switch (option) {
  1825. case PR_SET_PDEATHSIG:
  1826. if (!valid_signal(arg2)) {
  1827. error = -EINVAL;
  1828. break;
  1829. }
  1830. me->pdeath_signal = arg2;
  1831. break;
  1832. case PR_GET_PDEATHSIG:
  1833. error = put_user(me->pdeath_signal, (int __user *)arg2);
  1834. break;
  1835. case PR_GET_DUMPABLE:
  1836. error = get_dumpable(me->mm);
  1837. break;
  1838. case PR_SET_DUMPABLE:
  1839. if (arg2 != SUID_DUMP_DISABLE && arg2 != SUID_DUMP_USER) {
  1840. error = -EINVAL;
  1841. break;
  1842. }
  1843. set_dumpable(me->mm, arg2);
  1844. break;
  1845. case PR_SET_UNALIGN:
  1846. error = SET_UNALIGN_CTL(me, arg2);
  1847. break;
  1848. case PR_GET_UNALIGN:
  1849. error = GET_UNALIGN_CTL(me, arg2);
  1850. break;
  1851. case PR_SET_FPEMU:
  1852. error = SET_FPEMU_CTL(me, arg2);
  1853. break;
  1854. case PR_GET_FPEMU:
  1855. error = GET_FPEMU_CTL(me, arg2);
  1856. break;
  1857. case PR_SET_FPEXC:
  1858. error = SET_FPEXC_CTL(me, arg2);
  1859. break;
  1860. case PR_GET_FPEXC:
  1861. error = GET_FPEXC_CTL(me, arg2);
  1862. break;
  1863. case PR_GET_TIMING:
  1864. error = PR_TIMING_STATISTICAL;
  1865. break;
  1866. case PR_SET_TIMING:
  1867. if (arg2 != PR_TIMING_STATISTICAL)
  1868. error = -EINVAL;
  1869. break;
  1870. case PR_SET_NAME:
  1871. comm[sizeof(me->comm) - 1] = 0;
  1872. if (strncpy_from_user(comm, (char __user *)arg2,
  1873. sizeof(me->comm) - 1) < 0)
  1874. return -EFAULT;
  1875. set_task_comm(me, comm);
  1876. proc_comm_connector(me);
  1877. break;
  1878. case PR_GET_NAME:
  1879. get_task_comm(comm, me);
  1880. if (copy_to_user((char __user *)arg2, comm, sizeof(comm)))
  1881. return -EFAULT;
  1882. break;
  1883. case PR_GET_ENDIAN:
  1884. error = GET_ENDIAN(me, arg2);
  1885. break;
  1886. case PR_SET_ENDIAN:
  1887. error = SET_ENDIAN(me, arg2);
  1888. break;
  1889. case PR_GET_SECCOMP:
  1890. error = prctl_get_seccomp();
  1891. break;
  1892. case PR_SET_SECCOMP:
  1893. error = prctl_set_seccomp(arg2, (char __user *)arg3);
  1894. break;
  1895. case PR_GET_TSC:
  1896. error = GET_TSC_CTL(arg2);
  1897. break;
  1898. case PR_SET_TSC:
  1899. error = SET_TSC_CTL(arg2);
  1900. break;
  1901. case PR_TASK_PERF_EVENTS_DISABLE:
  1902. error = perf_event_task_disable();
  1903. break;
  1904. case PR_TASK_PERF_EVENTS_ENABLE:
  1905. error = perf_event_task_enable();
  1906. break;
  1907. case PR_GET_TIMERSLACK:
  1908. if (current->timer_slack_ns > ULONG_MAX)
  1909. error = ULONG_MAX;
  1910. else
  1911. error = current->timer_slack_ns;
  1912. break;
  1913. case PR_SET_TIMERSLACK:
  1914. if (arg2 <= 0)
  1915. current->timer_slack_ns =
  1916. current->default_timer_slack_ns;
  1917. else
  1918. current->timer_slack_ns = arg2;
  1919. break;
  1920. case PR_MCE_KILL:
  1921. if (arg4 | arg5)
  1922. return -EINVAL;
  1923. switch (arg2) {
  1924. case PR_MCE_KILL_CLEAR:
  1925. if (arg3 != 0)
  1926. return -EINVAL;
  1927. current->flags &= ~PF_MCE_PROCESS;
  1928. break;
  1929. case PR_MCE_KILL_SET:
  1930. current->flags |= PF_MCE_PROCESS;
  1931. if (arg3 == PR_MCE_KILL_EARLY)
  1932. current->flags |= PF_MCE_EARLY;
  1933. else if (arg3 == PR_MCE_KILL_LATE)
  1934. current->flags &= ~PF_MCE_EARLY;
  1935. else if (arg3 == PR_MCE_KILL_DEFAULT)
  1936. current->flags &=
  1937. ~(PF_MCE_EARLY|PF_MCE_PROCESS);
  1938. else
  1939. return -EINVAL;
  1940. break;
  1941. default:
  1942. return -EINVAL;
  1943. }
  1944. break;
  1945. case PR_MCE_KILL_GET:
  1946. if (arg2 | arg3 | arg4 | arg5)
  1947. return -EINVAL;
  1948. if (current->flags & PF_MCE_PROCESS)
  1949. error = (current->flags & PF_MCE_EARLY) ?
  1950. PR_MCE_KILL_EARLY : PR_MCE_KILL_LATE;
  1951. else
  1952. error = PR_MCE_KILL_DEFAULT;
  1953. break;
  1954. case PR_SET_MM:
  1955. error = prctl_set_mm(arg2, arg3, arg4, arg5);
  1956. break;
  1957. case PR_GET_TID_ADDRESS:
  1958. error = prctl_get_tid_address(me, (int __user **)arg2);
  1959. break;
  1960. case PR_SET_CHILD_SUBREAPER:
  1961. me->signal->is_child_subreaper = !!arg2;
  1962. if (!arg2)
  1963. break;
  1964. walk_process_tree(me, propagate_has_child_subreaper, NULL);
  1965. break;
  1966. case PR_GET_CHILD_SUBREAPER:
  1967. error = put_user(me->signal->is_child_subreaper,
  1968. (int __user *)arg2);
  1969. break;
  1970. case PR_SET_NO_NEW_PRIVS:
  1971. if (arg2 != 1 || arg3 || arg4 || arg5)
  1972. return -EINVAL;
  1973. task_set_no_new_privs(current);
  1974. break;
  1975. case PR_GET_NO_NEW_PRIVS:
  1976. if (arg2 || arg3 || arg4 || arg5)
  1977. return -EINVAL;
  1978. return task_no_new_privs(current) ? 1 : 0;
  1979. case PR_GET_THP_DISABLE:
  1980. if (arg2 || arg3 || arg4 || arg5)
  1981. return -EINVAL;
  1982. error = !!(me->mm->def_flags & VM_NOHUGEPAGE);
  1983. break;
  1984. case PR_SET_THP_DISABLE:
  1985. if (arg3 || arg4 || arg5)
  1986. return -EINVAL;
  1987. if (down_write_killable(&me->mm->mmap_sem))
  1988. return -EINTR;
  1989. if (arg2)
  1990. me->mm->def_flags |= VM_NOHUGEPAGE;
  1991. else
  1992. me->mm->def_flags &= ~VM_NOHUGEPAGE;
  1993. up_write(&me->mm->mmap_sem);
  1994. break;
  1995. case PR_MPX_ENABLE_MANAGEMENT:
  1996. if (arg2 || arg3 || arg4 || arg5)
  1997. return -EINVAL;
  1998. error = MPX_ENABLE_MANAGEMENT();
  1999. break;
  2000. case PR_MPX_DISABLE_MANAGEMENT:
  2001. if (arg2 || arg3 || arg4 || arg5)
  2002. return -EINVAL;
  2003. error = MPX_DISABLE_MANAGEMENT();
  2004. break;
  2005. case PR_SET_FP_MODE:
  2006. error = SET_FP_MODE(me, arg2);
  2007. break;
  2008. case PR_GET_FP_MODE:
  2009. error = GET_FP_MODE(me);
  2010. break;
  2011. default:
  2012. error = -EINVAL;
  2013. break;
  2014. }
  2015. return error;
  2016. }
  2017. SYSCALL_DEFINE3(getcpu, unsigned __user *, cpup, unsigned __user *, nodep,
  2018. struct getcpu_cache __user *, unused)
  2019. {
  2020. int err = 0;
  2021. int cpu = raw_smp_processor_id();
  2022. if (cpup)
  2023. err |= put_user(cpu, cpup);
  2024. if (nodep)
  2025. err |= put_user(cpu_to_node(cpu), nodep);
  2026. return err ? -EFAULT : 0;
  2027. }
  2028. /**
  2029. * do_sysinfo - fill in sysinfo struct
  2030. * @info: pointer to buffer to fill
  2031. */
  2032. static int do_sysinfo(struct sysinfo *info)
  2033. {
  2034. unsigned long mem_total, sav_total;
  2035. unsigned int mem_unit, bitcount;
  2036. struct timespec tp;
  2037. memset(info, 0, sizeof(struct sysinfo));
  2038. get_monotonic_boottime(&tp);
  2039. info->uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
  2040. get_avenrun(info->loads, 0, SI_LOAD_SHIFT - FSHIFT);
  2041. info->procs = nr_threads;
  2042. si_meminfo(info);
  2043. si_swapinfo(info);
  2044. /*
  2045. * If the sum of all the available memory (i.e. ram + swap)
  2046. * is less than can be stored in a 32 bit unsigned long then
  2047. * we can be binary compatible with 2.2.x kernels. If not,
  2048. * well, in that case 2.2.x was broken anyways...
  2049. *
  2050. * -Erik Andersen <andersee@debian.org>
  2051. */
  2052. mem_total = info->totalram + info->totalswap;
  2053. if (mem_total < info->totalram || mem_total < info->totalswap)
  2054. goto out;
  2055. bitcount = 0;
  2056. mem_unit = info->mem_unit;
  2057. while (mem_unit > 1) {
  2058. bitcount++;
  2059. mem_unit >>= 1;
  2060. sav_total = mem_total;
  2061. mem_total <<= 1;
  2062. if (mem_total < sav_total)
  2063. goto out;
  2064. }
  2065. /*
  2066. * If mem_total did not overflow, multiply all memory values by
  2067. * info->mem_unit and set it to 1. This leaves things compatible
  2068. * with 2.2.x, and also retains compatibility with earlier 2.4.x
  2069. * kernels...
  2070. */
  2071. info->mem_unit = 1;
  2072. info->totalram <<= bitcount;
  2073. info->freeram <<= bitcount;
  2074. info->sharedram <<= bitcount;
  2075. info->bufferram <<= bitcount;
  2076. info->totalswap <<= bitcount;
  2077. info->freeswap <<= bitcount;
  2078. info->totalhigh <<= bitcount;
  2079. info->freehigh <<= bitcount;
  2080. out:
  2081. return 0;
  2082. }
  2083. SYSCALL_DEFINE1(sysinfo, struct sysinfo __user *, info)
  2084. {
  2085. struct sysinfo val;
  2086. do_sysinfo(&val);
  2087. if (copy_to_user(info, &val, sizeof(struct sysinfo)))
  2088. return -EFAULT;
  2089. return 0;
  2090. }
  2091. #ifdef CONFIG_COMPAT
  2092. struct compat_sysinfo {
  2093. s32 uptime;
  2094. u32 loads[3];
  2095. u32 totalram;
  2096. u32 freeram;
  2097. u32 sharedram;
  2098. u32 bufferram;
  2099. u32 totalswap;
  2100. u32 freeswap;
  2101. u16 procs;
  2102. u16 pad;
  2103. u32 totalhigh;
  2104. u32 freehigh;
  2105. u32 mem_unit;
  2106. char _f[20-2*sizeof(u32)-sizeof(int)];
  2107. };
  2108. COMPAT_SYSCALL_DEFINE1(sysinfo, struct compat_sysinfo __user *, info)
  2109. {
  2110. struct sysinfo s;
  2111. do_sysinfo(&s);
  2112. /* Check to see if any memory value is too large for 32-bit and scale
  2113. * down if needed
  2114. */
  2115. if (upper_32_bits(s.totalram) || upper_32_bits(s.totalswap)) {
  2116. int bitcount = 0;
  2117. while (s.mem_unit < PAGE_SIZE) {
  2118. s.mem_unit <<= 1;
  2119. bitcount++;
  2120. }
  2121. s.totalram >>= bitcount;
  2122. s.freeram >>= bitcount;
  2123. s.sharedram >>= bitcount;
  2124. s.bufferram >>= bitcount;
  2125. s.totalswap >>= bitcount;
  2126. s.freeswap >>= bitcount;
  2127. s.totalhigh >>= bitcount;
  2128. s.freehigh >>= bitcount;
  2129. }
  2130. if (!access_ok(VERIFY_WRITE, info, sizeof(struct compat_sysinfo)) ||
  2131. __put_user(s.uptime, &info->uptime) ||
  2132. __put_user(s.loads[0], &info->loads[0]) ||
  2133. __put_user(s.loads[1], &info->loads[1]) ||
  2134. __put_user(s.loads[2], &info->loads[2]) ||
  2135. __put_user(s.totalram, &info->totalram) ||
  2136. __put_user(s.freeram, &info->freeram) ||
  2137. __put_user(s.sharedram, &info->sharedram) ||
  2138. __put_user(s.bufferram, &info->bufferram) ||
  2139. __put_user(s.totalswap, &info->totalswap) ||
  2140. __put_user(s.freeswap, &info->freeswap) ||
  2141. __put_user(s.procs, &info->procs) ||
  2142. __put_user(s.totalhigh, &info->totalhigh) ||
  2143. __put_user(s.freehigh, &info->freehigh) ||
  2144. __put_user(s.mem_unit, &info->mem_unit))
  2145. return -EFAULT;
  2146. return 0;
  2147. }
  2148. #endif /* CONFIG_COMPAT */