sys.c 56 KB

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