sys.c 61 KB

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