sys.c 56 KB

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