sys.c 58 KB

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