sys.c 62 KB

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