sys.c 59 KB

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