sys.c 58 KB

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