sys.c 58 KB

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