sys.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358
  1. /*
  2. * linux/kernel/sys.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. #include <linux/module.h>
  7. #include <linux/mm.h>
  8. #include <linux/utsname.h>
  9. #include <linux/mman.h>
  10. #include <linux/smp_lock.h>
  11. #include <linux/notifier.h>
  12. #include <linux/reboot.h>
  13. #include <linux/prctl.h>
  14. #include <linux/highuid.h>
  15. #include <linux/fs.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/compat.h>
  35. #include <linux/syscalls.h>
  36. #include <linux/kprobes.h>
  37. #include <linux/user_namespace.h>
  38. #include <asm/uaccess.h>
  39. #include <asm/io.h>
  40. #include <asm/unistd.h>
  41. #ifndef SET_UNALIGN_CTL
  42. # define SET_UNALIGN_CTL(a,b) (-EINVAL)
  43. #endif
  44. #ifndef GET_UNALIGN_CTL
  45. # define GET_UNALIGN_CTL(a,b) (-EINVAL)
  46. #endif
  47. #ifndef SET_FPEMU_CTL
  48. # define SET_FPEMU_CTL(a,b) (-EINVAL)
  49. #endif
  50. #ifndef GET_FPEMU_CTL
  51. # define GET_FPEMU_CTL(a,b) (-EINVAL)
  52. #endif
  53. #ifndef SET_FPEXC_CTL
  54. # define SET_FPEXC_CTL(a,b) (-EINVAL)
  55. #endif
  56. #ifndef GET_FPEXC_CTL
  57. # define GET_FPEXC_CTL(a,b) (-EINVAL)
  58. #endif
  59. #ifndef GET_ENDIAN
  60. # define GET_ENDIAN(a,b) (-EINVAL)
  61. #endif
  62. #ifndef SET_ENDIAN
  63. # define SET_ENDIAN(a,b) (-EINVAL)
  64. #endif
  65. /*
  66. * this is where the system-wide overflow UID and GID are defined, for
  67. * architectures that now have 32-bit UID/GID but didn't in the past
  68. */
  69. int overflowuid = DEFAULT_OVERFLOWUID;
  70. int overflowgid = DEFAULT_OVERFLOWGID;
  71. #ifdef CONFIG_UID16
  72. EXPORT_SYMBOL(overflowuid);
  73. EXPORT_SYMBOL(overflowgid);
  74. #endif
  75. /*
  76. * the same as above, but for filesystems which can only store a 16-bit
  77. * UID and GID. as such, this is needed on all architectures
  78. */
  79. int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
  80. int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
  81. EXPORT_SYMBOL(fs_overflowuid);
  82. EXPORT_SYMBOL(fs_overflowgid);
  83. /*
  84. * this indicates whether you can reboot with ctrl-alt-del: the default is yes
  85. */
  86. int C_A_D = 1;
  87. struct pid *cad_pid;
  88. EXPORT_SYMBOL(cad_pid);
  89. /*
  90. * If set, this is used for preparing the system to power off.
  91. */
  92. void (*pm_power_off_prepare)(void);
  93. EXPORT_SYMBOL(pm_power_off_prepare);
  94. /*
  95. * Notifier list for kernel code which wants to be called
  96. * at shutdown. This is used to stop any idling DMA operations
  97. * and the like.
  98. */
  99. static BLOCKING_NOTIFIER_HEAD(reboot_notifier_list);
  100. /*
  101. * Notifier chain core routines. The exported routines below
  102. * are layered on top of these, with appropriate locking added.
  103. */
  104. static int notifier_chain_register(struct notifier_block **nl,
  105. struct notifier_block *n)
  106. {
  107. while ((*nl) != NULL) {
  108. if (n->priority > (*nl)->priority)
  109. break;
  110. nl = &((*nl)->next);
  111. }
  112. n->next = *nl;
  113. rcu_assign_pointer(*nl, n);
  114. return 0;
  115. }
  116. static int notifier_chain_unregister(struct notifier_block **nl,
  117. struct notifier_block *n)
  118. {
  119. while ((*nl) != NULL) {
  120. if ((*nl) == n) {
  121. rcu_assign_pointer(*nl, n->next);
  122. return 0;
  123. }
  124. nl = &((*nl)->next);
  125. }
  126. return -ENOENT;
  127. }
  128. /**
  129. * notifier_call_chain - Informs the registered notifiers about an event.
  130. * @nl: Pointer to head of the blocking notifier chain
  131. * @val: Value passed unmodified to notifier function
  132. * @v: Pointer passed unmodified to notifier function
  133. * @nr_to_call: Number of notifier functions to be called. Don't care
  134. * value of this parameter is -1.
  135. * @nr_calls: Records the number of notifications sent. Don't care
  136. * value of this field is NULL.
  137. * @returns: notifier_call_chain returns the value returned by the
  138. * last notifier function called.
  139. */
  140. static int __kprobes notifier_call_chain(struct notifier_block **nl,
  141. unsigned long val, void *v,
  142. int nr_to_call, int *nr_calls)
  143. {
  144. int ret = NOTIFY_DONE;
  145. struct notifier_block *nb, *next_nb;
  146. nb = rcu_dereference(*nl);
  147. while (nb && nr_to_call) {
  148. next_nb = rcu_dereference(nb->next);
  149. ret = nb->notifier_call(nb, val, v);
  150. if (nr_calls)
  151. (*nr_calls)++;
  152. if ((ret & NOTIFY_STOP_MASK) == NOTIFY_STOP_MASK)
  153. break;
  154. nb = next_nb;
  155. nr_to_call--;
  156. }
  157. return ret;
  158. }
  159. /*
  160. * Atomic notifier chain routines. Registration and unregistration
  161. * use a spinlock, and call_chain is synchronized by RCU (no locks).
  162. */
  163. /**
  164. * atomic_notifier_chain_register - Add notifier to an atomic notifier chain
  165. * @nh: Pointer to head of the atomic notifier chain
  166. * @n: New entry in notifier chain
  167. *
  168. * Adds a notifier to an atomic notifier chain.
  169. *
  170. * Currently always returns zero.
  171. */
  172. int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
  173. struct notifier_block *n)
  174. {
  175. unsigned long flags;
  176. int ret;
  177. spin_lock_irqsave(&nh->lock, flags);
  178. ret = notifier_chain_register(&nh->head, n);
  179. spin_unlock_irqrestore(&nh->lock, flags);
  180. return ret;
  181. }
  182. EXPORT_SYMBOL_GPL(atomic_notifier_chain_register);
  183. /**
  184. * atomic_notifier_chain_unregister - Remove notifier from an atomic notifier chain
  185. * @nh: Pointer to head of the atomic notifier chain
  186. * @n: Entry to remove from notifier chain
  187. *
  188. * Removes a notifier from an atomic notifier chain.
  189. *
  190. * Returns zero on success or %-ENOENT on failure.
  191. */
  192. int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh,
  193. struct notifier_block *n)
  194. {
  195. unsigned long flags;
  196. int ret;
  197. spin_lock_irqsave(&nh->lock, flags);
  198. ret = notifier_chain_unregister(&nh->head, n);
  199. spin_unlock_irqrestore(&nh->lock, flags);
  200. synchronize_rcu();
  201. return ret;
  202. }
  203. EXPORT_SYMBOL_GPL(atomic_notifier_chain_unregister);
  204. /**
  205. * __atomic_notifier_call_chain - Call functions in an atomic notifier chain
  206. * @nh: Pointer to head of the atomic notifier chain
  207. * @val: Value passed unmodified to notifier function
  208. * @v: Pointer passed unmodified to notifier function
  209. * @nr_to_call: See the comment for notifier_call_chain.
  210. * @nr_calls: See the comment for notifier_call_chain.
  211. *
  212. * Calls each function in a notifier chain in turn. The functions
  213. * run in an atomic context, so they must not block.
  214. * This routine uses RCU to synchronize with changes to the chain.
  215. *
  216. * If the return value of the notifier can be and'ed
  217. * with %NOTIFY_STOP_MASK then atomic_notifier_call_chain()
  218. * will return immediately, with the return value of
  219. * the notifier function which halted execution.
  220. * Otherwise the return value is the return value
  221. * of the last notifier function called.
  222. */
  223. int __kprobes __atomic_notifier_call_chain(struct atomic_notifier_head *nh,
  224. unsigned long val, void *v,
  225. int nr_to_call, int *nr_calls)
  226. {
  227. int ret;
  228. rcu_read_lock();
  229. ret = notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
  230. rcu_read_unlock();
  231. return ret;
  232. }
  233. EXPORT_SYMBOL_GPL(__atomic_notifier_call_chain);
  234. int __kprobes atomic_notifier_call_chain(struct atomic_notifier_head *nh,
  235. unsigned long val, void *v)
  236. {
  237. return __atomic_notifier_call_chain(nh, val, v, -1, NULL);
  238. }
  239. EXPORT_SYMBOL_GPL(atomic_notifier_call_chain);
  240. /*
  241. * Blocking notifier chain routines. All access to the chain is
  242. * synchronized by an rwsem.
  243. */
  244. /**
  245. * blocking_notifier_chain_register - Add notifier to a blocking notifier chain
  246. * @nh: Pointer to head of the blocking notifier chain
  247. * @n: New entry in notifier chain
  248. *
  249. * Adds a notifier to a blocking notifier chain.
  250. * Must be called in process context.
  251. *
  252. * Currently always returns zero.
  253. */
  254. int blocking_notifier_chain_register(struct blocking_notifier_head *nh,
  255. struct notifier_block *n)
  256. {
  257. int ret;
  258. /*
  259. * This code gets used during boot-up, when task switching is
  260. * not yet working and interrupts must remain disabled. At
  261. * such times we must not call down_write().
  262. */
  263. if (unlikely(system_state == SYSTEM_BOOTING))
  264. return notifier_chain_register(&nh->head, n);
  265. down_write(&nh->rwsem);
  266. ret = notifier_chain_register(&nh->head, n);
  267. up_write(&nh->rwsem);
  268. return ret;
  269. }
  270. EXPORT_SYMBOL_GPL(blocking_notifier_chain_register);
  271. /**
  272. * blocking_notifier_chain_unregister - Remove notifier from a blocking notifier chain
  273. * @nh: Pointer to head of the blocking notifier chain
  274. * @n: Entry to remove from notifier chain
  275. *
  276. * Removes a notifier from a blocking notifier chain.
  277. * Must be called from process context.
  278. *
  279. * Returns zero on success or %-ENOENT on failure.
  280. */
  281. int blocking_notifier_chain_unregister(struct blocking_notifier_head *nh,
  282. struct notifier_block *n)
  283. {
  284. int ret;
  285. /*
  286. * This code gets used during boot-up, when task switching is
  287. * not yet working and interrupts must remain disabled. At
  288. * such times we must not call down_write().
  289. */
  290. if (unlikely(system_state == SYSTEM_BOOTING))
  291. return notifier_chain_unregister(&nh->head, n);
  292. down_write(&nh->rwsem);
  293. ret = notifier_chain_unregister(&nh->head, n);
  294. up_write(&nh->rwsem);
  295. return ret;
  296. }
  297. EXPORT_SYMBOL_GPL(blocking_notifier_chain_unregister);
  298. /**
  299. * __blocking_notifier_call_chain - Call functions in a blocking notifier chain
  300. * @nh: Pointer to head of the blocking notifier chain
  301. * @val: Value passed unmodified to notifier function
  302. * @v: Pointer passed unmodified to notifier function
  303. * @nr_to_call: See comment for notifier_call_chain.
  304. * @nr_calls: See comment for notifier_call_chain.
  305. *
  306. * Calls each function in a notifier chain in turn. The functions
  307. * run in a process context, so they are allowed to block.
  308. *
  309. * If the return value of the notifier can be and'ed
  310. * with %NOTIFY_STOP_MASK then blocking_notifier_call_chain()
  311. * will return immediately, with the return value of
  312. * the notifier function which halted execution.
  313. * Otherwise the return value is the return value
  314. * of the last notifier function called.
  315. */
  316. int __blocking_notifier_call_chain(struct blocking_notifier_head *nh,
  317. unsigned long val, void *v,
  318. int nr_to_call, int *nr_calls)
  319. {
  320. int ret = NOTIFY_DONE;
  321. /*
  322. * We check the head outside the lock, but if this access is
  323. * racy then it does not matter what the result of the test
  324. * is, we re-check the list after having taken the lock anyway:
  325. */
  326. if (rcu_dereference(nh->head)) {
  327. down_read(&nh->rwsem);
  328. ret = notifier_call_chain(&nh->head, val, v, nr_to_call,
  329. nr_calls);
  330. up_read(&nh->rwsem);
  331. }
  332. return ret;
  333. }
  334. EXPORT_SYMBOL_GPL(__blocking_notifier_call_chain);
  335. int blocking_notifier_call_chain(struct blocking_notifier_head *nh,
  336. unsigned long val, void *v)
  337. {
  338. return __blocking_notifier_call_chain(nh, val, v, -1, NULL);
  339. }
  340. EXPORT_SYMBOL_GPL(blocking_notifier_call_chain);
  341. /*
  342. * Raw notifier chain routines. There is no protection;
  343. * the caller must provide it. Use at your own risk!
  344. */
  345. /**
  346. * raw_notifier_chain_register - Add notifier to a raw notifier chain
  347. * @nh: Pointer to head of the raw notifier chain
  348. * @n: New entry in notifier chain
  349. *
  350. * Adds a notifier to a raw notifier chain.
  351. * All locking must be provided by the caller.
  352. *
  353. * Currently always returns zero.
  354. */
  355. int raw_notifier_chain_register(struct raw_notifier_head *nh,
  356. struct notifier_block *n)
  357. {
  358. return notifier_chain_register(&nh->head, n);
  359. }
  360. EXPORT_SYMBOL_GPL(raw_notifier_chain_register);
  361. /**
  362. * raw_notifier_chain_unregister - Remove notifier from a raw notifier chain
  363. * @nh: Pointer to head of the raw notifier chain
  364. * @n: Entry to remove from notifier chain
  365. *
  366. * Removes a notifier from a raw notifier chain.
  367. * All locking must be provided by the caller.
  368. *
  369. * Returns zero on success or %-ENOENT on failure.
  370. */
  371. int raw_notifier_chain_unregister(struct raw_notifier_head *nh,
  372. struct notifier_block *n)
  373. {
  374. return notifier_chain_unregister(&nh->head, n);
  375. }
  376. EXPORT_SYMBOL_GPL(raw_notifier_chain_unregister);
  377. /**
  378. * __raw_notifier_call_chain - Call functions in a raw notifier chain
  379. * @nh: Pointer to head of the raw notifier chain
  380. * @val: Value passed unmodified to notifier function
  381. * @v: Pointer passed unmodified to notifier function
  382. * @nr_to_call: See comment for notifier_call_chain.
  383. * @nr_calls: See comment for notifier_call_chain
  384. *
  385. * Calls each function in a notifier chain in turn. The functions
  386. * run in an undefined context.
  387. * All locking must be provided by the caller.
  388. *
  389. * If the return value of the notifier can be and'ed
  390. * with %NOTIFY_STOP_MASK then raw_notifier_call_chain()
  391. * will return immediately, with the return value of
  392. * the notifier function which halted execution.
  393. * Otherwise the return value is the return value
  394. * of the last notifier function called.
  395. */
  396. int __raw_notifier_call_chain(struct raw_notifier_head *nh,
  397. unsigned long val, void *v,
  398. int nr_to_call, int *nr_calls)
  399. {
  400. return notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
  401. }
  402. EXPORT_SYMBOL_GPL(__raw_notifier_call_chain);
  403. int raw_notifier_call_chain(struct raw_notifier_head *nh,
  404. unsigned long val, void *v)
  405. {
  406. return __raw_notifier_call_chain(nh, val, v, -1, NULL);
  407. }
  408. EXPORT_SYMBOL_GPL(raw_notifier_call_chain);
  409. /*
  410. * SRCU notifier chain routines. Registration and unregistration
  411. * use a mutex, and call_chain is synchronized by SRCU (no locks).
  412. */
  413. /**
  414. * srcu_notifier_chain_register - Add notifier to an SRCU notifier chain
  415. * @nh: Pointer to head of the SRCU notifier chain
  416. * @n: New entry in notifier chain
  417. *
  418. * Adds a notifier to an SRCU notifier chain.
  419. * Must be called in process context.
  420. *
  421. * Currently always returns zero.
  422. */
  423. int srcu_notifier_chain_register(struct srcu_notifier_head *nh,
  424. struct notifier_block *n)
  425. {
  426. int ret;
  427. /*
  428. * This code gets used during boot-up, when task switching is
  429. * not yet working and interrupts must remain disabled. At
  430. * such times we must not call mutex_lock().
  431. */
  432. if (unlikely(system_state == SYSTEM_BOOTING))
  433. return notifier_chain_register(&nh->head, n);
  434. mutex_lock(&nh->mutex);
  435. ret = notifier_chain_register(&nh->head, n);
  436. mutex_unlock(&nh->mutex);
  437. return ret;
  438. }
  439. EXPORT_SYMBOL_GPL(srcu_notifier_chain_register);
  440. /**
  441. * srcu_notifier_chain_unregister - Remove notifier from an SRCU notifier chain
  442. * @nh: Pointer to head of the SRCU notifier chain
  443. * @n: Entry to remove from notifier chain
  444. *
  445. * Removes a notifier from an SRCU notifier chain.
  446. * Must be called from process context.
  447. *
  448. * Returns zero on success or %-ENOENT on failure.
  449. */
  450. int srcu_notifier_chain_unregister(struct srcu_notifier_head *nh,
  451. struct notifier_block *n)
  452. {
  453. int ret;
  454. /*
  455. * This code gets used during boot-up, when task switching is
  456. * not yet working and interrupts must remain disabled. At
  457. * such times we must not call mutex_lock().
  458. */
  459. if (unlikely(system_state == SYSTEM_BOOTING))
  460. return notifier_chain_unregister(&nh->head, n);
  461. mutex_lock(&nh->mutex);
  462. ret = notifier_chain_unregister(&nh->head, n);
  463. mutex_unlock(&nh->mutex);
  464. synchronize_srcu(&nh->srcu);
  465. return ret;
  466. }
  467. EXPORT_SYMBOL_GPL(srcu_notifier_chain_unregister);
  468. /**
  469. * __srcu_notifier_call_chain - Call functions in an SRCU notifier chain
  470. * @nh: Pointer to head of the SRCU notifier chain
  471. * @val: Value passed unmodified to notifier function
  472. * @v: Pointer passed unmodified to notifier function
  473. * @nr_to_call: See comment for notifier_call_chain.
  474. * @nr_calls: See comment for notifier_call_chain
  475. *
  476. * Calls each function in a notifier chain in turn. The functions
  477. * run in a process context, so they are allowed to block.
  478. *
  479. * If the return value of the notifier can be and'ed
  480. * with %NOTIFY_STOP_MASK then srcu_notifier_call_chain()
  481. * will return immediately, with the return value of
  482. * the notifier function which halted execution.
  483. * Otherwise the return value is the return value
  484. * of the last notifier function called.
  485. */
  486. int __srcu_notifier_call_chain(struct srcu_notifier_head *nh,
  487. unsigned long val, void *v,
  488. int nr_to_call, int *nr_calls)
  489. {
  490. int ret;
  491. int idx;
  492. idx = srcu_read_lock(&nh->srcu);
  493. ret = notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
  494. srcu_read_unlock(&nh->srcu, idx);
  495. return ret;
  496. }
  497. EXPORT_SYMBOL_GPL(__srcu_notifier_call_chain);
  498. int srcu_notifier_call_chain(struct srcu_notifier_head *nh,
  499. unsigned long val, void *v)
  500. {
  501. return __srcu_notifier_call_chain(nh, val, v, -1, NULL);
  502. }
  503. EXPORT_SYMBOL_GPL(srcu_notifier_call_chain);
  504. /**
  505. * srcu_init_notifier_head - Initialize an SRCU notifier head
  506. * @nh: Pointer to head of the srcu notifier chain
  507. *
  508. * Unlike other sorts of notifier heads, SRCU notifier heads require
  509. * dynamic initialization. Be sure to call this routine before
  510. * calling any of the other SRCU notifier routines for this head.
  511. *
  512. * If an SRCU notifier head is deallocated, it must first be cleaned
  513. * up by calling srcu_cleanup_notifier_head(). Otherwise the head's
  514. * per-cpu data (used by the SRCU mechanism) will leak.
  515. */
  516. void srcu_init_notifier_head(struct srcu_notifier_head *nh)
  517. {
  518. mutex_init(&nh->mutex);
  519. if (init_srcu_struct(&nh->srcu) < 0)
  520. BUG();
  521. nh->head = NULL;
  522. }
  523. EXPORT_SYMBOL_GPL(srcu_init_notifier_head);
  524. /**
  525. * register_reboot_notifier - Register function to be called at reboot time
  526. * @nb: Info about notifier function to be called
  527. *
  528. * Registers a function with the list of functions
  529. * to be called at reboot time.
  530. *
  531. * Currently always returns zero, as blocking_notifier_chain_register()
  532. * always returns zero.
  533. */
  534. int register_reboot_notifier(struct notifier_block * nb)
  535. {
  536. return blocking_notifier_chain_register(&reboot_notifier_list, nb);
  537. }
  538. EXPORT_SYMBOL(register_reboot_notifier);
  539. /**
  540. * unregister_reboot_notifier - Unregister previously registered reboot notifier
  541. * @nb: Hook to be unregistered
  542. *
  543. * Unregisters a previously registered reboot
  544. * notifier function.
  545. *
  546. * Returns zero on success, or %-ENOENT on failure.
  547. */
  548. int unregister_reboot_notifier(struct notifier_block * nb)
  549. {
  550. return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
  551. }
  552. EXPORT_SYMBOL(unregister_reboot_notifier);
  553. static int set_one_prio(struct task_struct *p, int niceval, int error)
  554. {
  555. int no_nice;
  556. if (p->uid != current->euid &&
  557. p->euid != current->euid && !capable(CAP_SYS_NICE)) {
  558. error = -EPERM;
  559. goto out;
  560. }
  561. if (niceval < task_nice(p) && !can_nice(p, niceval)) {
  562. error = -EACCES;
  563. goto out;
  564. }
  565. no_nice = security_task_setnice(p, niceval);
  566. if (no_nice) {
  567. error = no_nice;
  568. goto out;
  569. }
  570. if (error == -ESRCH)
  571. error = 0;
  572. set_user_nice(p, niceval);
  573. out:
  574. return error;
  575. }
  576. asmlinkage long sys_setpriority(int which, int who, int niceval)
  577. {
  578. struct task_struct *g, *p;
  579. struct user_struct *user;
  580. int error = -EINVAL;
  581. struct pid *pgrp;
  582. if (which > PRIO_USER || which < PRIO_PROCESS)
  583. goto out;
  584. /* normalize: avoid signed division (rounding problems) */
  585. error = -ESRCH;
  586. if (niceval < -20)
  587. niceval = -20;
  588. if (niceval > 19)
  589. niceval = 19;
  590. read_lock(&tasklist_lock);
  591. switch (which) {
  592. case PRIO_PROCESS:
  593. if (who)
  594. p = find_task_by_pid(who);
  595. else
  596. p = current;
  597. if (p)
  598. error = set_one_prio(p, niceval, error);
  599. break;
  600. case PRIO_PGRP:
  601. if (who)
  602. pgrp = find_pid(who);
  603. else
  604. pgrp = task_pgrp(current);
  605. do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
  606. error = set_one_prio(p, niceval, error);
  607. } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
  608. break;
  609. case PRIO_USER:
  610. user = current->user;
  611. if (!who)
  612. who = current->uid;
  613. else
  614. if ((who != current->uid) && !(user = find_user(who)))
  615. goto out_unlock; /* No processes for this user */
  616. do_each_thread(g, p)
  617. if (p->uid == who)
  618. error = set_one_prio(p, niceval, error);
  619. while_each_thread(g, p);
  620. if (who != current->uid)
  621. free_uid(user); /* For find_user() */
  622. break;
  623. }
  624. out_unlock:
  625. read_unlock(&tasklist_lock);
  626. out:
  627. return error;
  628. }
  629. /*
  630. * Ugh. To avoid negative return values, "getpriority()" will
  631. * not return the normal nice-value, but a negated value that
  632. * has been offset by 20 (ie it returns 40..1 instead of -20..19)
  633. * to stay compatible.
  634. */
  635. asmlinkage long sys_getpriority(int which, int who)
  636. {
  637. struct task_struct *g, *p;
  638. struct user_struct *user;
  639. long niceval, retval = -ESRCH;
  640. struct pid *pgrp;
  641. if (which > PRIO_USER || which < PRIO_PROCESS)
  642. return -EINVAL;
  643. read_lock(&tasklist_lock);
  644. switch (which) {
  645. case PRIO_PROCESS:
  646. if (who)
  647. p = find_task_by_pid(who);
  648. else
  649. p = current;
  650. if (p) {
  651. niceval = 20 - task_nice(p);
  652. if (niceval > retval)
  653. retval = niceval;
  654. }
  655. break;
  656. case PRIO_PGRP:
  657. if (who)
  658. pgrp = find_pid(who);
  659. else
  660. pgrp = task_pgrp(current);
  661. do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
  662. niceval = 20 - task_nice(p);
  663. if (niceval > retval)
  664. retval = niceval;
  665. } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
  666. break;
  667. case PRIO_USER:
  668. user = current->user;
  669. if (!who)
  670. who = current->uid;
  671. else
  672. if ((who != current->uid) && !(user = find_user(who)))
  673. goto out_unlock; /* No processes for this user */
  674. do_each_thread(g, p)
  675. if (p->uid == who) {
  676. niceval = 20 - task_nice(p);
  677. if (niceval > retval)
  678. retval = niceval;
  679. }
  680. while_each_thread(g, p);
  681. if (who != current->uid)
  682. free_uid(user); /* for find_user() */
  683. break;
  684. }
  685. out_unlock:
  686. read_unlock(&tasklist_lock);
  687. return retval;
  688. }
  689. /**
  690. * emergency_restart - reboot the system
  691. *
  692. * Without shutting down any hardware or taking any locks
  693. * reboot the system. This is called when we know we are in
  694. * trouble so this is our best effort to reboot. This is
  695. * safe to call in interrupt context.
  696. */
  697. void emergency_restart(void)
  698. {
  699. machine_emergency_restart();
  700. }
  701. EXPORT_SYMBOL_GPL(emergency_restart);
  702. static void kernel_restart_prepare(char *cmd)
  703. {
  704. blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
  705. system_state = SYSTEM_RESTART;
  706. device_shutdown();
  707. sysdev_shutdown();
  708. }
  709. /**
  710. * kernel_restart - reboot the system
  711. * @cmd: pointer to buffer containing command to execute for restart
  712. * or %NULL
  713. *
  714. * Shutdown everything and perform a clean reboot.
  715. * This is not safe to call in interrupt context.
  716. */
  717. void kernel_restart(char *cmd)
  718. {
  719. kernel_restart_prepare(cmd);
  720. if (!cmd)
  721. printk(KERN_EMERG "Restarting system.\n");
  722. else
  723. printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
  724. machine_restart(cmd);
  725. }
  726. EXPORT_SYMBOL_GPL(kernel_restart);
  727. /**
  728. * kernel_kexec - reboot the system
  729. *
  730. * Move into place and start executing a preloaded standalone
  731. * executable. If nothing was preloaded return an error.
  732. */
  733. static void kernel_kexec(void)
  734. {
  735. #ifdef CONFIG_KEXEC
  736. struct kimage *image;
  737. image = xchg(&kexec_image, NULL);
  738. if (!image)
  739. return;
  740. kernel_restart_prepare(NULL);
  741. printk(KERN_EMERG "Starting new kernel\n");
  742. machine_shutdown();
  743. machine_kexec(image);
  744. #endif
  745. }
  746. void kernel_shutdown_prepare(enum system_states state)
  747. {
  748. blocking_notifier_call_chain(&reboot_notifier_list,
  749. (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL);
  750. system_state = state;
  751. device_shutdown();
  752. }
  753. /**
  754. * kernel_halt - halt the system
  755. *
  756. * Shutdown everything and perform a clean system halt.
  757. */
  758. void kernel_halt(void)
  759. {
  760. kernel_shutdown_prepare(SYSTEM_HALT);
  761. sysdev_shutdown();
  762. printk(KERN_EMERG "System halted.\n");
  763. machine_halt();
  764. }
  765. EXPORT_SYMBOL_GPL(kernel_halt);
  766. /**
  767. * kernel_power_off - power_off the system
  768. *
  769. * Shutdown everything and perform a clean system power_off.
  770. */
  771. void kernel_power_off(void)
  772. {
  773. kernel_shutdown_prepare(SYSTEM_POWER_OFF);
  774. if (pm_power_off_prepare)
  775. pm_power_off_prepare();
  776. sysdev_shutdown();
  777. printk(KERN_EMERG "Power down.\n");
  778. machine_power_off();
  779. }
  780. EXPORT_SYMBOL_GPL(kernel_power_off);
  781. /*
  782. * Reboot system call: for obvious reasons only root may call it,
  783. * and even root needs to set up some magic numbers in the registers
  784. * so that some mistake won't make this reboot the whole machine.
  785. * You can also set the meaning of the ctrl-alt-del-key here.
  786. *
  787. * reboot doesn't sync: do that yourself before calling this.
  788. */
  789. asmlinkage long sys_reboot(int magic1, int magic2, unsigned int cmd, void __user * arg)
  790. {
  791. char buffer[256];
  792. /* We only trust the superuser with rebooting the system. */
  793. if (!capable(CAP_SYS_BOOT))
  794. return -EPERM;
  795. /* For safety, we require "magic" arguments. */
  796. if (magic1 != LINUX_REBOOT_MAGIC1 ||
  797. (magic2 != LINUX_REBOOT_MAGIC2 &&
  798. magic2 != LINUX_REBOOT_MAGIC2A &&
  799. magic2 != LINUX_REBOOT_MAGIC2B &&
  800. magic2 != LINUX_REBOOT_MAGIC2C))
  801. return -EINVAL;
  802. /* Instead of trying to make the power_off code look like
  803. * halt when pm_power_off is not set do it the easy way.
  804. */
  805. if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
  806. cmd = LINUX_REBOOT_CMD_HALT;
  807. lock_kernel();
  808. switch (cmd) {
  809. case LINUX_REBOOT_CMD_RESTART:
  810. kernel_restart(NULL);
  811. break;
  812. case LINUX_REBOOT_CMD_CAD_ON:
  813. C_A_D = 1;
  814. break;
  815. case LINUX_REBOOT_CMD_CAD_OFF:
  816. C_A_D = 0;
  817. break;
  818. case LINUX_REBOOT_CMD_HALT:
  819. kernel_halt();
  820. unlock_kernel();
  821. do_exit(0);
  822. break;
  823. case LINUX_REBOOT_CMD_POWER_OFF:
  824. kernel_power_off();
  825. unlock_kernel();
  826. do_exit(0);
  827. break;
  828. case LINUX_REBOOT_CMD_RESTART2:
  829. if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
  830. unlock_kernel();
  831. return -EFAULT;
  832. }
  833. buffer[sizeof(buffer) - 1] = '\0';
  834. kernel_restart(buffer);
  835. break;
  836. case LINUX_REBOOT_CMD_KEXEC:
  837. kernel_kexec();
  838. unlock_kernel();
  839. return -EINVAL;
  840. #ifdef CONFIG_HIBERNATION
  841. case LINUX_REBOOT_CMD_SW_SUSPEND:
  842. {
  843. int ret = hibernate();
  844. unlock_kernel();
  845. return ret;
  846. }
  847. #endif
  848. default:
  849. unlock_kernel();
  850. return -EINVAL;
  851. }
  852. unlock_kernel();
  853. return 0;
  854. }
  855. static void deferred_cad(struct work_struct *dummy)
  856. {
  857. kernel_restart(NULL);
  858. }
  859. /*
  860. * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
  861. * As it's called within an interrupt, it may NOT sync: the only choice
  862. * is whether to reboot at once, or just ignore the ctrl-alt-del.
  863. */
  864. void ctrl_alt_del(void)
  865. {
  866. static DECLARE_WORK(cad_work, deferred_cad);
  867. if (C_A_D)
  868. schedule_work(&cad_work);
  869. else
  870. kill_cad_pid(SIGINT, 1);
  871. }
  872. /*
  873. * Unprivileged users may change the real gid to the effective gid
  874. * or vice versa. (BSD-style)
  875. *
  876. * If you set the real gid at all, or set the effective gid to a value not
  877. * equal to the real gid, then the saved gid is set to the new effective gid.
  878. *
  879. * This makes it possible for a setgid program to completely drop its
  880. * privileges, which is often a useful assertion to make when you are doing
  881. * a security audit over a program.
  882. *
  883. * The general idea is that a program which uses just setregid() will be
  884. * 100% compatible with BSD. A program which uses just setgid() will be
  885. * 100% compatible with POSIX with saved IDs.
  886. *
  887. * SMP: There are not races, the GIDs are checked only by filesystem
  888. * operations (as far as semantic preservation is concerned).
  889. */
  890. asmlinkage long sys_setregid(gid_t rgid, gid_t egid)
  891. {
  892. int old_rgid = current->gid;
  893. int old_egid = current->egid;
  894. int new_rgid = old_rgid;
  895. int new_egid = old_egid;
  896. int retval;
  897. retval = security_task_setgid(rgid, egid, (gid_t)-1, LSM_SETID_RE);
  898. if (retval)
  899. return retval;
  900. if (rgid != (gid_t) -1) {
  901. if ((old_rgid == rgid) ||
  902. (current->egid==rgid) ||
  903. capable(CAP_SETGID))
  904. new_rgid = rgid;
  905. else
  906. return -EPERM;
  907. }
  908. if (egid != (gid_t) -1) {
  909. if ((old_rgid == egid) ||
  910. (current->egid == egid) ||
  911. (current->sgid == egid) ||
  912. capable(CAP_SETGID))
  913. new_egid = egid;
  914. else
  915. return -EPERM;
  916. }
  917. if (new_egid != old_egid) {
  918. set_dumpable(current->mm, suid_dumpable);
  919. smp_wmb();
  920. }
  921. if (rgid != (gid_t) -1 ||
  922. (egid != (gid_t) -1 && egid != old_rgid))
  923. current->sgid = new_egid;
  924. current->fsgid = new_egid;
  925. current->egid = new_egid;
  926. current->gid = new_rgid;
  927. key_fsgid_changed(current);
  928. proc_id_connector(current, PROC_EVENT_GID);
  929. return 0;
  930. }
  931. /*
  932. * setgid() is implemented like SysV w/ SAVED_IDS
  933. *
  934. * SMP: Same implicit races as above.
  935. */
  936. asmlinkage long sys_setgid(gid_t gid)
  937. {
  938. int old_egid = current->egid;
  939. int retval;
  940. retval = security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_ID);
  941. if (retval)
  942. return retval;
  943. if (capable(CAP_SETGID)) {
  944. if (old_egid != gid) {
  945. set_dumpable(current->mm, suid_dumpable);
  946. smp_wmb();
  947. }
  948. current->gid = current->egid = current->sgid = current->fsgid = gid;
  949. } else if ((gid == current->gid) || (gid == current->sgid)) {
  950. if (old_egid != gid) {
  951. set_dumpable(current->mm, suid_dumpable);
  952. smp_wmb();
  953. }
  954. current->egid = current->fsgid = gid;
  955. }
  956. else
  957. return -EPERM;
  958. key_fsgid_changed(current);
  959. proc_id_connector(current, PROC_EVENT_GID);
  960. return 0;
  961. }
  962. static int set_user(uid_t new_ruid, int dumpclear)
  963. {
  964. struct user_struct *new_user;
  965. new_user = alloc_uid(current->nsproxy->user_ns, new_ruid);
  966. if (!new_user)
  967. return -EAGAIN;
  968. if (atomic_read(&new_user->processes) >=
  969. current->signal->rlim[RLIMIT_NPROC].rlim_cur &&
  970. new_user != current->nsproxy->user_ns->root_user) {
  971. free_uid(new_user);
  972. return -EAGAIN;
  973. }
  974. switch_uid(new_user);
  975. if (dumpclear) {
  976. set_dumpable(current->mm, suid_dumpable);
  977. smp_wmb();
  978. }
  979. current->uid = new_ruid;
  980. return 0;
  981. }
  982. /*
  983. * Unprivileged users may change the real uid to the effective uid
  984. * or vice versa. (BSD-style)
  985. *
  986. * If you set the real uid at all, or set the effective uid to a value not
  987. * equal to the real uid, then the saved uid is set to the new effective uid.
  988. *
  989. * This makes it possible for a setuid program to completely drop its
  990. * privileges, which is often a useful assertion to make when you are doing
  991. * a security audit over a program.
  992. *
  993. * The general idea is that a program which uses just setreuid() will be
  994. * 100% compatible with BSD. A program which uses just setuid() will be
  995. * 100% compatible with POSIX with saved IDs.
  996. */
  997. asmlinkage long sys_setreuid(uid_t ruid, uid_t euid)
  998. {
  999. int old_ruid, old_euid, old_suid, new_ruid, new_euid;
  1000. int retval;
  1001. retval = security_task_setuid(ruid, euid, (uid_t)-1, LSM_SETID_RE);
  1002. if (retval)
  1003. return retval;
  1004. new_ruid = old_ruid = current->uid;
  1005. new_euid = old_euid = current->euid;
  1006. old_suid = current->suid;
  1007. if (ruid != (uid_t) -1) {
  1008. new_ruid = ruid;
  1009. if ((old_ruid != ruid) &&
  1010. (current->euid != ruid) &&
  1011. !capable(CAP_SETUID))
  1012. return -EPERM;
  1013. }
  1014. if (euid != (uid_t) -1) {
  1015. new_euid = euid;
  1016. if ((old_ruid != euid) &&
  1017. (current->euid != euid) &&
  1018. (current->suid != euid) &&
  1019. !capable(CAP_SETUID))
  1020. return -EPERM;
  1021. }
  1022. if (new_ruid != old_ruid && set_user(new_ruid, new_euid != old_euid) < 0)
  1023. return -EAGAIN;
  1024. if (new_euid != old_euid) {
  1025. set_dumpable(current->mm, suid_dumpable);
  1026. smp_wmb();
  1027. }
  1028. current->fsuid = current->euid = new_euid;
  1029. if (ruid != (uid_t) -1 ||
  1030. (euid != (uid_t) -1 && euid != old_ruid))
  1031. current->suid = current->euid;
  1032. current->fsuid = current->euid;
  1033. key_fsuid_changed(current);
  1034. proc_id_connector(current, PROC_EVENT_UID);
  1035. return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RE);
  1036. }
  1037. /*
  1038. * setuid() is implemented like SysV with SAVED_IDS
  1039. *
  1040. * Note that SAVED_ID's is deficient in that a setuid root program
  1041. * like sendmail, for example, cannot set its uid to be a normal
  1042. * user and then switch back, because if you're root, setuid() sets
  1043. * the saved uid too. If you don't like this, blame the bright people
  1044. * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
  1045. * will allow a root program to temporarily drop privileges and be able to
  1046. * regain them by swapping the real and effective uid.
  1047. */
  1048. asmlinkage long sys_setuid(uid_t uid)
  1049. {
  1050. int old_euid = current->euid;
  1051. int old_ruid, old_suid, new_suid;
  1052. int retval;
  1053. retval = security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_ID);
  1054. if (retval)
  1055. return retval;
  1056. old_ruid = current->uid;
  1057. old_suid = current->suid;
  1058. new_suid = old_suid;
  1059. if (capable(CAP_SETUID)) {
  1060. if (uid != old_ruid && set_user(uid, old_euid != uid) < 0)
  1061. return -EAGAIN;
  1062. new_suid = uid;
  1063. } else if ((uid != current->uid) && (uid != new_suid))
  1064. return -EPERM;
  1065. if (old_euid != uid) {
  1066. set_dumpable(current->mm, suid_dumpable);
  1067. smp_wmb();
  1068. }
  1069. current->fsuid = current->euid = uid;
  1070. current->suid = new_suid;
  1071. key_fsuid_changed(current);
  1072. proc_id_connector(current, PROC_EVENT_UID);
  1073. return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_ID);
  1074. }
  1075. /*
  1076. * This function implements a generic ability to update ruid, euid,
  1077. * and suid. This allows you to implement the 4.4 compatible seteuid().
  1078. */
  1079. asmlinkage long sys_setresuid(uid_t ruid, uid_t euid, uid_t suid)
  1080. {
  1081. int old_ruid = current->uid;
  1082. int old_euid = current->euid;
  1083. int old_suid = current->suid;
  1084. int retval;
  1085. retval = security_task_setuid(ruid, euid, suid, LSM_SETID_RES);
  1086. if (retval)
  1087. return retval;
  1088. if (!capable(CAP_SETUID)) {
  1089. if ((ruid != (uid_t) -1) && (ruid != current->uid) &&
  1090. (ruid != current->euid) && (ruid != current->suid))
  1091. return -EPERM;
  1092. if ((euid != (uid_t) -1) && (euid != current->uid) &&
  1093. (euid != current->euid) && (euid != current->suid))
  1094. return -EPERM;
  1095. if ((suid != (uid_t) -1) && (suid != current->uid) &&
  1096. (suid != current->euid) && (suid != current->suid))
  1097. return -EPERM;
  1098. }
  1099. if (ruid != (uid_t) -1) {
  1100. if (ruid != current->uid && set_user(ruid, euid != current->euid) < 0)
  1101. return -EAGAIN;
  1102. }
  1103. if (euid != (uid_t) -1) {
  1104. if (euid != current->euid) {
  1105. set_dumpable(current->mm, suid_dumpable);
  1106. smp_wmb();
  1107. }
  1108. current->euid = euid;
  1109. }
  1110. current->fsuid = current->euid;
  1111. if (suid != (uid_t) -1)
  1112. current->suid = suid;
  1113. key_fsuid_changed(current);
  1114. proc_id_connector(current, PROC_EVENT_UID);
  1115. return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RES);
  1116. }
  1117. asmlinkage long sys_getresuid(uid_t __user *ruid, uid_t __user *euid, uid_t __user *suid)
  1118. {
  1119. int retval;
  1120. if (!(retval = put_user(current->uid, ruid)) &&
  1121. !(retval = put_user(current->euid, euid)))
  1122. retval = put_user(current->suid, suid);
  1123. return retval;
  1124. }
  1125. /*
  1126. * Same as above, but for rgid, egid, sgid.
  1127. */
  1128. asmlinkage long sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
  1129. {
  1130. int retval;
  1131. retval = security_task_setgid(rgid, egid, sgid, LSM_SETID_RES);
  1132. if (retval)
  1133. return retval;
  1134. if (!capable(CAP_SETGID)) {
  1135. if ((rgid != (gid_t) -1) && (rgid != current->gid) &&
  1136. (rgid != current->egid) && (rgid != current->sgid))
  1137. return -EPERM;
  1138. if ((egid != (gid_t) -1) && (egid != current->gid) &&
  1139. (egid != current->egid) && (egid != current->sgid))
  1140. return -EPERM;
  1141. if ((sgid != (gid_t) -1) && (sgid != current->gid) &&
  1142. (sgid != current->egid) && (sgid != current->sgid))
  1143. return -EPERM;
  1144. }
  1145. if (egid != (gid_t) -1) {
  1146. if (egid != current->egid) {
  1147. set_dumpable(current->mm, suid_dumpable);
  1148. smp_wmb();
  1149. }
  1150. current->egid = egid;
  1151. }
  1152. current->fsgid = current->egid;
  1153. if (rgid != (gid_t) -1)
  1154. current->gid = rgid;
  1155. if (sgid != (gid_t) -1)
  1156. current->sgid = sgid;
  1157. key_fsgid_changed(current);
  1158. proc_id_connector(current, PROC_EVENT_GID);
  1159. return 0;
  1160. }
  1161. asmlinkage long sys_getresgid(gid_t __user *rgid, gid_t __user *egid, gid_t __user *sgid)
  1162. {
  1163. int retval;
  1164. if (!(retval = put_user(current->gid, rgid)) &&
  1165. !(retval = put_user(current->egid, egid)))
  1166. retval = put_user(current->sgid, sgid);
  1167. return retval;
  1168. }
  1169. /*
  1170. * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
  1171. * is used for "access()" and for the NFS daemon (letting nfsd stay at
  1172. * whatever uid it wants to). It normally shadows "euid", except when
  1173. * explicitly set by setfsuid() or for access..
  1174. */
  1175. asmlinkage long sys_setfsuid(uid_t uid)
  1176. {
  1177. int old_fsuid;
  1178. old_fsuid = current->fsuid;
  1179. if (security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS))
  1180. return old_fsuid;
  1181. if (uid == current->uid || uid == current->euid ||
  1182. uid == current->suid || uid == current->fsuid ||
  1183. capable(CAP_SETUID)) {
  1184. if (uid != old_fsuid) {
  1185. set_dumpable(current->mm, suid_dumpable);
  1186. smp_wmb();
  1187. }
  1188. current->fsuid = uid;
  1189. }
  1190. key_fsuid_changed(current);
  1191. proc_id_connector(current, PROC_EVENT_UID);
  1192. security_task_post_setuid(old_fsuid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS);
  1193. return old_fsuid;
  1194. }
  1195. /*
  1196. * Samma på svenska..
  1197. */
  1198. asmlinkage long sys_setfsgid(gid_t gid)
  1199. {
  1200. int old_fsgid;
  1201. old_fsgid = current->fsgid;
  1202. if (security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_FS))
  1203. return old_fsgid;
  1204. if (gid == current->gid || gid == current->egid ||
  1205. gid == current->sgid || gid == current->fsgid ||
  1206. capable(CAP_SETGID)) {
  1207. if (gid != old_fsgid) {
  1208. set_dumpable(current->mm, suid_dumpable);
  1209. smp_wmb();
  1210. }
  1211. current->fsgid = gid;
  1212. key_fsgid_changed(current);
  1213. proc_id_connector(current, PROC_EVENT_GID);
  1214. }
  1215. return old_fsgid;
  1216. }
  1217. asmlinkage long sys_times(struct tms __user * tbuf)
  1218. {
  1219. /*
  1220. * In the SMP world we might just be unlucky and have one of
  1221. * the times increment as we use it. Since the value is an
  1222. * atomically safe type this is just fine. Conceptually its
  1223. * as if the syscall took an instant longer to occur.
  1224. */
  1225. if (tbuf) {
  1226. struct tms tmp;
  1227. struct task_struct *tsk = current;
  1228. struct task_struct *t;
  1229. cputime_t utime, stime, cutime, cstime;
  1230. spin_lock_irq(&tsk->sighand->siglock);
  1231. utime = tsk->signal->utime;
  1232. stime = tsk->signal->stime;
  1233. t = tsk;
  1234. do {
  1235. utime = cputime_add(utime, t->utime);
  1236. stime = cputime_add(stime, t->stime);
  1237. t = next_thread(t);
  1238. } while (t != tsk);
  1239. cutime = tsk->signal->cutime;
  1240. cstime = tsk->signal->cstime;
  1241. spin_unlock_irq(&tsk->sighand->siglock);
  1242. tmp.tms_utime = cputime_to_clock_t(utime);
  1243. tmp.tms_stime = cputime_to_clock_t(stime);
  1244. tmp.tms_cutime = cputime_to_clock_t(cutime);
  1245. tmp.tms_cstime = cputime_to_clock_t(cstime);
  1246. if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
  1247. return -EFAULT;
  1248. }
  1249. return (long) jiffies_64_to_clock_t(get_jiffies_64());
  1250. }
  1251. /*
  1252. * This needs some heavy checking ...
  1253. * I just haven't the stomach for it. I also don't fully
  1254. * understand sessions/pgrp etc. Let somebody who does explain it.
  1255. *
  1256. * OK, I think I have the protection semantics right.... this is really
  1257. * only important on a multi-user system anyway, to make sure one user
  1258. * can't send a signal to a process owned by another. -TYT, 12/12/91
  1259. *
  1260. * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
  1261. * LBT 04.03.94
  1262. */
  1263. asmlinkage long sys_setpgid(pid_t pid, pid_t pgid)
  1264. {
  1265. struct task_struct *p;
  1266. struct task_struct *group_leader = current->group_leader;
  1267. int err = -EINVAL;
  1268. if (!pid)
  1269. pid = group_leader->pid;
  1270. if (!pgid)
  1271. pgid = pid;
  1272. if (pgid < 0)
  1273. return -EINVAL;
  1274. /* From this point forward we keep holding onto the tasklist lock
  1275. * so that our parent does not change from under us. -DaveM
  1276. */
  1277. write_lock_irq(&tasklist_lock);
  1278. err = -ESRCH;
  1279. p = find_task_by_pid(pid);
  1280. if (!p)
  1281. goto out;
  1282. err = -EINVAL;
  1283. if (!thread_group_leader(p))
  1284. goto out;
  1285. if (p->real_parent == group_leader) {
  1286. err = -EPERM;
  1287. if (task_session(p) != task_session(group_leader))
  1288. goto out;
  1289. err = -EACCES;
  1290. if (p->did_exec)
  1291. goto out;
  1292. } else {
  1293. err = -ESRCH;
  1294. if (p != group_leader)
  1295. goto out;
  1296. }
  1297. err = -EPERM;
  1298. if (p->signal->leader)
  1299. goto out;
  1300. if (pgid != pid) {
  1301. struct task_struct *g =
  1302. find_task_by_pid_type(PIDTYPE_PGID, pgid);
  1303. if (!g || task_session(g) != task_session(group_leader))
  1304. goto out;
  1305. }
  1306. err = security_task_setpgid(p, pgid);
  1307. if (err)
  1308. goto out;
  1309. if (process_group(p) != pgid) {
  1310. detach_pid(p, PIDTYPE_PGID);
  1311. p->signal->pgrp = pgid;
  1312. attach_pid(p, PIDTYPE_PGID, find_pid(pgid));
  1313. }
  1314. err = 0;
  1315. out:
  1316. /* All paths lead to here, thus we are safe. -DaveM */
  1317. write_unlock_irq(&tasklist_lock);
  1318. return err;
  1319. }
  1320. asmlinkage long sys_getpgid(pid_t pid)
  1321. {
  1322. if (!pid)
  1323. return process_group(current);
  1324. else {
  1325. int retval;
  1326. struct task_struct *p;
  1327. read_lock(&tasklist_lock);
  1328. p = find_task_by_pid(pid);
  1329. retval = -ESRCH;
  1330. if (p) {
  1331. retval = security_task_getpgid(p);
  1332. if (!retval)
  1333. retval = process_group(p);
  1334. }
  1335. read_unlock(&tasklist_lock);
  1336. return retval;
  1337. }
  1338. }
  1339. #ifdef __ARCH_WANT_SYS_GETPGRP
  1340. asmlinkage long sys_getpgrp(void)
  1341. {
  1342. /* SMP - assuming writes are word atomic this is fine */
  1343. return process_group(current);
  1344. }
  1345. #endif
  1346. asmlinkage long sys_getsid(pid_t pid)
  1347. {
  1348. if (!pid)
  1349. return process_session(current);
  1350. else {
  1351. int retval;
  1352. struct task_struct *p;
  1353. read_lock(&tasklist_lock);
  1354. p = find_task_by_pid(pid);
  1355. retval = -ESRCH;
  1356. if (p) {
  1357. retval = security_task_getsid(p);
  1358. if (!retval)
  1359. retval = process_session(p);
  1360. }
  1361. read_unlock(&tasklist_lock);
  1362. return retval;
  1363. }
  1364. }
  1365. asmlinkage long sys_setsid(void)
  1366. {
  1367. struct task_struct *group_leader = current->group_leader;
  1368. pid_t session;
  1369. int err = -EPERM;
  1370. write_lock_irq(&tasklist_lock);
  1371. /* Fail if I am already a session leader */
  1372. if (group_leader->signal->leader)
  1373. goto out;
  1374. session = group_leader->pid;
  1375. /* Fail if a process group id already exists that equals the
  1376. * proposed session id.
  1377. *
  1378. * Don't check if session id == 1 because kernel threads use this
  1379. * session id and so the check will always fail and make it so
  1380. * init cannot successfully call setsid.
  1381. */
  1382. if (session > 1 && find_task_by_pid_type(PIDTYPE_PGID, session))
  1383. goto out;
  1384. group_leader->signal->leader = 1;
  1385. __set_special_pids(session, session);
  1386. spin_lock(&group_leader->sighand->siglock);
  1387. group_leader->signal->tty = NULL;
  1388. spin_unlock(&group_leader->sighand->siglock);
  1389. err = process_group(group_leader);
  1390. out:
  1391. write_unlock_irq(&tasklist_lock);
  1392. return err;
  1393. }
  1394. /*
  1395. * Supplementary group IDs
  1396. */
  1397. /* init to 2 - one for init_task, one to ensure it is never freed */
  1398. struct group_info init_groups = { .usage = ATOMIC_INIT(2) };
  1399. struct group_info *groups_alloc(int gidsetsize)
  1400. {
  1401. struct group_info *group_info;
  1402. int nblocks;
  1403. int i;
  1404. nblocks = (gidsetsize + NGROUPS_PER_BLOCK - 1) / NGROUPS_PER_BLOCK;
  1405. /* Make sure we always allocate at least one indirect block pointer */
  1406. nblocks = nblocks ? : 1;
  1407. group_info = kmalloc(sizeof(*group_info) + nblocks*sizeof(gid_t *), GFP_USER);
  1408. if (!group_info)
  1409. return NULL;
  1410. group_info->ngroups = gidsetsize;
  1411. group_info->nblocks = nblocks;
  1412. atomic_set(&group_info->usage, 1);
  1413. if (gidsetsize <= NGROUPS_SMALL)
  1414. group_info->blocks[0] = group_info->small_block;
  1415. else {
  1416. for (i = 0; i < nblocks; i++) {
  1417. gid_t *b;
  1418. b = (void *)__get_free_page(GFP_USER);
  1419. if (!b)
  1420. goto out_undo_partial_alloc;
  1421. group_info->blocks[i] = b;
  1422. }
  1423. }
  1424. return group_info;
  1425. out_undo_partial_alloc:
  1426. while (--i >= 0) {
  1427. free_page((unsigned long)group_info->blocks[i]);
  1428. }
  1429. kfree(group_info);
  1430. return NULL;
  1431. }
  1432. EXPORT_SYMBOL(groups_alloc);
  1433. void groups_free(struct group_info *group_info)
  1434. {
  1435. if (group_info->blocks[0] != group_info->small_block) {
  1436. int i;
  1437. for (i = 0; i < group_info->nblocks; i++)
  1438. free_page((unsigned long)group_info->blocks[i]);
  1439. }
  1440. kfree(group_info);
  1441. }
  1442. EXPORT_SYMBOL(groups_free);
  1443. /* export the group_info to a user-space array */
  1444. static int groups_to_user(gid_t __user *grouplist,
  1445. struct group_info *group_info)
  1446. {
  1447. int i;
  1448. int count = group_info->ngroups;
  1449. for (i = 0; i < group_info->nblocks; i++) {
  1450. int cp_count = min(NGROUPS_PER_BLOCK, count);
  1451. int off = i * NGROUPS_PER_BLOCK;
  1452. int len = cp_count * sizeof(*grouplist);
  1453. if (copy_to_user(grouplist+off, group_info->blocks[i], len))
  1454. return -EFAULT;
  1455. count -= cp_count;
  1456. }
  1457. return 0;
  1458. }
  1459. /* fill a group_info from a user-space array - it must be allocated already */
  1460. static int groups_from_user(struct group_info *group_info,
  1461. gid_t __user *grouplist)
  1462. {
  1463. int i;
  1464. int count = group_info->ngroups;
  1465. for (i = 0; i < group_info->nblocks; i++) {
  1466. int cp_count = min(NGROUPS_PER_BLOCK, count);
  1467. int off = i * NGROUPS_PER_BLOCK;
  1468. int len = cp_count * sizeof(*grouplist);
  1469. if (copy_from_user(group_info->blocks[i], grouplist+off, len))
  1470. return -EFAULT;
  1471. count -= cp_count;
  1472. }
  1473. return 0;
  1474. }
  1475. /* a simple Shell sort */
  1476. static void groups_sort(struct group_info *group_info)
  1477. {
  1478. int base, max, stride;
  1479. int gidsetsize = group_info->ngroups;
  1480. for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1)
  1481. ; /* nothing */
  1482. stride /= 3;
  1483. while (stride) {
  1484. max = gidsetsize - stride;
  1485. for (base = 0; base < max; base++) {
  1486. int left = base;
  1487. int right = left + stride;
  1488. gid_t tmp = GROUP_AT(group_info, right);
  1489. while (left >= 0 && GROUP_AT(group_info, left) > tmp) {
  1490. GROUP_AT(group_info, right) =
  1491. GROUP_AT(group_info, left);
  1492. right = left;
  1493. left -= stride;
  1494. }
  1495. GROUP_AT(group_info, right) = tmp;
  1496. }
  1497. stride /= 3;
  1498. }
  1499. }
  1500. /* a simple bsearch */
  1501. int groups_search(struct group_info *group_info, gid_t grp)
  1502. {
  1503. unsigned int left, right;
  1504. if (!group_info)
  1505. return 0;
  1506. left = 0;
  1507. right = group_info->ngroups;
  1508. while (left < right) {
  1509. unsigned int mid = (left+right)/2;
  1510. int cmp = grp - GROUP_AT(group_info, mid);
  1511. if (cmp > 0)
  1512. left = mid + 1;
  1513. else if (cmp < 0)
  1514. right = mid;
  1515. else
  1516. return 1;
  1517. }
  1518. return 0;
  1519. }
  1520. /* validate and set current->group_info */
  1521. int set_current_groups(struct group_info *group_info)
  1522. {
  1523. int retval;
  1524. struct group_info *old_info;
  1525. retval = security_task_setgroups(group_info);
  1526. if (retval)
  1527. return retval;
  1528. groups_sort(group_info);
  1529. get_group_info(group_info);
  1530. task_lock(current);
  1531. old_info = current->group_info;
  1532. current->group_info = group_info;
  1533. task_unlock(current);
  1534. put_group_info(old_info);
  1535. return 0;
  1536. }
  1537. EXPORT_SYMBOL(set_current_groups);
  1538. asmlinkage long sys_getgroups(int gidsetsize, gid_t __user *grouplist)
  1539. {
  1540. int i = 0;
  1541. /*
  1542. * SMP: Nobody else can change our grouplist. Thus we are
  1543. * safe.
  1544. */
  1545. if (gidsetsize < 0)
  1546. return -EINVAL;
  1547. /* no need to grab task_lock here; it cannot change */
  1548. i = current->group_info->ngroups;
  1549. if (gidsetsize) {
  1550. if (i > gidsetsize) {
  1551. i = -EINVAL;
  1552. goto out;
  1553. }
  1554. if (groups_to_user(grouplist, current->group_info)) {
  1555. i = -EFAULT;
  1556. goto out;
  1557. }
  1558. }
  1559. out:
  1560. return i;
  1561. }
  1562. /*
  1563. * SMP: Our groups are copy-on-write. We can set them safely
  1564. * without another task interfering.
  1565. */
  1566. asmlinkage long sys_setgroups(int gidsetsize, gid_t __user *grouplist)
  1567. {
  1568. struct group_info *group_info;
  1569. int retval;
  1570. if (!capable(CAP_SETGID))
  1571. return -EPERM;
  1572. if ((unsigned)gidsetsize > NGROUPS_MAX)
  1573. return -EINVAL;
  1574. group_info = groups_alloc(gidsetsize);
  1575. if (!group_info)
  1576. return -ENOMEM;
  1577. retval = groups_from_user(group_info, grouplist);
  1578. if (retval) {
  1579. put_group_info(group_info);
  1580. return retval;
  1581. }
  1582. retval = set_current_groups(group_info);
  1583. put_group_info(group_info);
  1584. return retval;
  1585. }
  1586. /*
  1587. * Check whether we're fsgid/egid or in the supplemental group..
  1588. */
  1589. int in_group_p(gid_t grp)
  1590. {
  1591. int retval = 1;
  1592. if (grp != current->fsgid)
  1593. retval = groups_search(current->group_info, grp);
  1594. return retval;
  1595. }
  1596. EXPORT_SYMBOL(in_group_p);
  1597. int in_egroup_p(gid_t grp)
  1598. {
  1599. int retval = 1;
  1600. if (grp != current->egid)
  1601. retval = groups_search(current->group_info, grp);
  1602. return retval;
  1603. }
  1604. EXPORT_SYMBOL(in_egroup_p);
  1605. DECLARE_RWSEM(uts_sem);
  1606. EXPORT_SYMBOL(uts_sem);
  1607. asmlinkage long sys_newuname(struct new_utsname __user * name)
  1608. {
  1609. int errno = 0;
  1610. down_read(&uts_sem);
  1611. if (copy_to_user(name, utsname(), sizeof *name))
  1612. errno = -EFAULT;
  1613. up_read(&uts_sem);
  1614. return errno;
  1615. }
  1616. asmlinkage long sys_sethostname(char __user *name, int len)
  1617. {
  1618. int errno;
  1619. char tmp[__NEW_UTS_LEN];
  1620. if (!capable(CAP_SYS_ADMIN))
  1621. return -EPERM;
  1622. if (len < 0 || len > __NEW_UTS_LEN)
  1623. return -EINVAL;
  1624. down_write(&uts_sem);
  1625. errno = -EFAULT;
  1626. if (!copy_from_user(tmp, name, len)) {
  1627. memcpy(utsname()->nodename, tmp, len);
  1628. utsname()->nodename[len] = 0;
  1629. errno = 0;
  1630. }
  1631. up_write(&uts_sem);
  1632. return errno;
  1633. }
  1634. #ifdef __ARCH_WANT_SYS_GETHOSTNAME
  1635. asmlinkage long sys_gethostname(char __user *name, int len)
  1636. {
  1637. int i, errno;
  1638. if (len < 0)
  1639. return -EINVAL;
  1640. down_read(&uts_sem);
  1641. i = 1 + strlen(utsname()->nodename);
  1642. if (i > len)
  1643. i = len;
  1644. errno = 0;
  1645. if (copy_to_user(name, utsname()->nodename, i))
  1646. errno = -EFAULT;
  1647. up_read(&uts_sem);
  1648. return errno;
  1649. }
  1650. #endif
  1651. /*
  1652. * Only setdomainname; getdomainname can be implemented by calling
  1653. * uname()
  1654. */
  1655. asmlinkage long sys_setdomainname(char __user *name, int len)
  1656. {
  1657. int errno;
  1658. char tmp[__NEW_UTS_LEN];
  1659. if (!capable(CAP_SYS_ADMIN))
  1660. return -EPERM;
  1661. if (len < 0 || len > __NEW_UTS_LEN)
  1662. return -EINVAL;
  1663. down_write(&uts_sem);
  1664. errno = -EFAULT;
  1665. if (!copy_from_user(tmp, name, len)) {
  1666. memcpy(utsname()->domainname, tmp, len);
  1667. utsname()->domainname[len] = 0;
  1668. errno = 0;
  1669. }
  1670. up_write(&uts_sem);
  1671. return errno;
  1672. }
  1673. asmlinkage long sys_getrlimit(unsigned int resource, struct rlimit __user *rlim)
  1674. {
  1675. if (resource >= RLIM_NLIMITS)
  1676. return -EINVAL;
  1677. else {
  1678. struct rlimit value;
  1679. task_lock(current->group_leader);
  1680. value = current->signal->rlim[resource];
  1681. task_unlock(current->group_leader);
  1682. return copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
  1683. }
  1684. }
  1685. #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
  1686. /*
  1687. * Back compatibility for getrlimit. Needed for some apps.
  1688. */
  1689. asmlinkage long sys_old_getrlimit(unsigned int resource, struct rlimit __user *rlim)
  1690. {
  1691. struct rlimit x;
  1692. if (resource >= RLIM_NLIMITS)
  1693. return -EINVAL;
  1694. task_lock(current->group_leader);
  1695. x = current->signal->rlim[resource];
  1696. task_unlock(current->group_leader);
  1697. if (x.rlim_cur > 0x7FFFFFFF)
  1698. x.rlim_cur = 0x7FFFFFFF;
  1699. if (x.rlim_max > 0x7FFFFFFF)
  1700. x.rlim_max = 0x7FFFFFFF;
  1701. return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
  1702. }
  1703. #endif
  1704. asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit __user *rlim)
  1705. {
  1706. struct rlimit new_rlim, *old_rlim;
  1707. unsigned long it_prof_secs;
  1708. int retval;
  1709. if (resource >= RLIM_NLIMITS)
  1710. return -EINVAL;
  1711. if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
  1712. return -EFAULT;
  1713. if (new_rlim.rlim_cur > new_rlim.rlim_max)
  1714. return -EINVAL;
  1715. old_rlim = current->signal->rlim + resource;
  1716. if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
  1717. !capable(CAP_SYS_RESOURCE))
  1718. return -EPERM;
  1719. if (resource == RLIMIT_NOFILE && new_rlim.rlim_max > NR_OPEN)
  1720. return -EPERM;
  1721. retval = security_task_setrlimit(resource, &new_rlim);
  1722. if (retval)
  1723. return retval;
  1724. if (resource == RLIMIT_CPU && new_rlim.rlim_cur == 0) {
  1725. /*
  1726. * The caller is asking for an immediate RLIMIT_CPU
  1727. * expiry. But we use the zero value to mean "it was
  1728. * never set". So let's cheat and make it one second
  1729. * instead
  1730. */
  1731. new_rlim.rlim_cur = 1;
  1732. }
  1733. task_lock(current->group_leader);
  1734. *old_rlim = new_rlim;
  1735. task_unlock(current->group_leader);
  1736. if (resource != RLIMIT_CPU)
  1737. goto out;
  1738. /*
  1739. * RLIMIT_CPU handling. Note that the kernel fails to return an error
  1740. * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
  1741. * very long-standing error, and fixing it now risks breakage of
  1742. * applications, so we live with it
  1743. */
  1744. if (new_rlim.rlim_cur == RLIM_INFINITY)
  1745. goto out;
  1746. it_prof_secs = cputime_to_secs(current->signal->it_prof_expires);
  1747. if (it_prof_secs == 0 || new_rlim.rlim_cur <= it_prof_secs) {
  1748. unsigned long rlim_cur = new_rlim.rlim_cur;
  1749. cputime_t cputime;
  1750. cputime = secs_to_cputime(rlim_cur);
  1751. read_lock(&tasklist_lock);
  1752. spin_lock_irq(&current->sighand->siglock);
  1753. set_process_cpu_timer(current, CPUCLOCK_PROF, &cputime, NULL);
  1754. spin_unlock_irq(&current->sighand->siglock);
  1755. read_unlock(&tasklist_lock);
  1756. }
  1757. out:
  1758. return 0;
  1759. }
  1760. /*
  1761. * It would make sense to put struct rusage in the task_struct,
  1762. * except that would make the task_struct be *really big*. After
  1763. * task_struct gets moved into malloc'ed memory, it would
  1764. * make sense to do this. It will make moving the rest of the information
  1765. * a lot simpler! (Which we're not doing right now because we're not
  1766. * measuring them yet).
  1767. *
  1768. * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
  1769. * races with threads incrementing their own counters. But since word
  1770. * reads are atomic, we either get new values or old values and we don't
  1771. * care which for the sums. We always take the siglock to protect reading
  1772. * the c* fields from p->signal from races with exit.c updating those
  1773. * fields when reaping, so a sample either gets all the additions of a
  1774. * given child after it's reaped, or none so this sample is before reaping.
  1775. *
  1776. * Locking:
  1777. * We need to take the siglock for CHILDEREN, SELF and BOTH
  1778. * for the cases current multithreaded, non-current single threaded
  1779. * non-current multithreaded. Thread traversal is now safe with
  1780. * the siglock held.
  1781. * Strictly speaking, we donot need to take the siglock if we are current and
  1782. * single threaded, as no one else can take our signal_struct away, no one
  1783. * else can reap the children to update signal->c* counters, and no one else
  1784. * can race with the signal-> fields. If we do not take any lock, the
  1785. * signal-> fields could be read out of order while another thread was just
  1786. * exiting. So we should place a read memory barrier when we avoid the lock.
  1787. * On the writer side, write memory barrier is implied in __exit_signal
  1788. * as __exit_signal releases the siglock spinlock after updating the signal->
  1789. * fields. But we don't do this yet to keep things simple.
  1790. *
  1791. */
  1792. static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
  1793. {
  1794. struct task_struct *t;
  1795. unsigned long flags;
  1796. cputime_t utime, stime;
  1797. memset((char *) r, 0, sizeof *r);
  1798. utime = stime = cputime_zero;
  1799. rcu_read_lock();
  1800. if (!lock_task_sighand(p, &flags)) {
  1801. rcu_read_unlock();
  1802. return;
  1803. }
  1804. switch (who) {
  1805. case RUSAGE_BOTH:
  1806. case RUSAGE_CHILDREN:
  1807. utime = p->signal->cutime;
  1808. stime = p->signal->cstime;
  1809. r->ru_nvcsw = p->signal->cnvcsw;
  1810. r->ru_nivcsw = p->signal->cnivcsw;
  1811. r->ru_minflt = p->signal->cmin_flt;
  1812. r->ru_majflt = p->signal->cmaj_flt;
  1813. r->ru_inblock = p->signal->cinblock;
  1814. r->ru_oublock = p->signal->coublock;
  1815. if (who == RUSAGE_CHILDREN)
  1816. break;
  1817. case RUSAGE_SELF:
  1818. utime = cputime_add(utime, p->signal->utime);
  1819. stime = cputime_add(stime, p->signal->stime);
  1820. r->ru_nvcsw += p->signal->nvcsw;
  1821. r->ru_nivcsw += p->signal->nivcsw;
  1822. r->ru_minflt += p->signal->min_flt;
  1823. r->ru_majflt += p->signal->maj_flt;
  1824. r->ru_inblock += p->signal->inblock;
  1825. r->ru_oublock += p->signal->oublock;
  1826. t = p;
  1827. do {
  1828. utime = cputime_add(utime, t->utime);
  1829. stime = cputime_add(stime, t->stime);
  1830. r->ru_nvcsw += t->nvcsw;
  1831. r->ru_nivcsw += t->nivcsw;
  1832. r->ru_minflt += t->min_flt;
  1833. r->ru_majflt += t->maj_flt;
  1834. r->ru_inblock += task_io_get_inblock(t);
  1835. r->ru_oublock += task_io_get_oublock(t);
  1836. t = next_thread(t);
  1837. } while (t != p);
  1838. break;
  1839. default:
  1840. BUG();
  1841. }
  1842. unlock_task_sighand(p, &flags);
  1843. rcu_read_unlock();
  1844. cputime_to_timeval(utime, &r->ru_utime);
  1845. cputime_to_timeval(stime, &r->ru_stime);
  1846. }
  1847. int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
  1848. {
  1849. struct rusage r;
  1850. k_getrusage(p, who, &r);
  1851. return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
  1852. }
  1853. asmlinkage long sys_getrusage(int who, struct rusage __user *ru)
  1854. {
  1855. if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN)
  1856. return -EINVAL;
  1857. return getrusage(current, who, ru);
  1858. }
  1859. asmlinkage long sys_umask(int mask)
  1860. {
  1861. mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
  1862. return mask;
  1863. }
  1864. asmlinkage long sys_prctl(int option, unsigned long arg2, unsigned long arg3,
  1865. unsigned long arg4, unsigned long arg5)
  1866. {
  1867. long error;
  1868. error = security_task_prctl(option, arg2, arg3, arg4, arg5);
  1869. if (error)
  1870. return error;
  1871. switch (option) {
  1872. case PR_SET_PDEATHSIG:
  1873. if (!valid_signal(arg2)) {
  1874. error = -EINVAL;
  1875. break;
  1876. }
  1877. current->pdeath_signal = arg2;
  1878. break;
  1879. case PR_GET_PDEATHSIG:
  1880. error = put_user(current->pdeath_signal, (int __user *)arg2);
  1881. break;
  1882. case PR_GET_DUMPABLE:
  1883. error = get_dumpable(current->mm);
  1884. break;
  1885. case PR_SET_DUMPABLE:
  1886. if (arg2 < 0 || arg2 > 1) {
  1887. error = -EINVAL;
  1888. break;
  1889. }
  1890. set_dumpable(current->mm, arg2);
  1891. break;
  1892. case PR_SET_UNALIGN:
  1893. error = SET_UNALIGN_CTL(current, arg2);
  1894. break;
  1895. case PR_GET_UNALIGN:
  1896. error = GET_UNALIGN_CTL(current, arg2);
  1897. break;
  1898. case PR_SET_FPEMU:
  1899. error = SET_FPEMU_CTL(current, arg2);
  1900. break;
  1901. case PR_GET_FPEMU:
  1902. error = GET_FPEMU_CTL(current, arg2);
  1903. break;
  1904. case PR_SET_FPEXC:
  1905. error = SET_FPEXC_CTL(current, arg2);
  1906. break;
  1907. case PR_GET_FPEXC:
  1908. error = GET_FPEXC_CTL(current, arg2);
  1909. break;
  1910. case PR_GET_TIMING:
  1911. error = PR_TIMING_STATISTICAL;
  1912. break;
  1913. case PR_SET_TIMING:
  1914. if (arg2 == PR_TIMING_STATISTICAL)
  1915. error = 0;
  1916. else
  1917. error = -EINVAL;
  1918. break;
  1919. case PR_GET_KEEPCAPS:
  1920. if (current->keep_capabilities)
  1921. error = 1;
  1922. break;
  1923. case PR_SET_KEEPCAPS:
  1924. if (arg2 != 0 && arg2 != 1) {
  1925. error = -EINVAL;
  1926. break;
  1927. }
  1928. current->keep_capabilities = arg2;
  1929. break;
  1930. case PR_SET_NAME: {
  1931. struct task_struct *me = current;
  1932. unsigned char ncomm[sizeof(me->comm)];
  1933. ncomm[sizeof(me->comm)-1] = 0;
  1934. if (strncpy_from_user(ncomm, (char __user *)arg2,
  1935. sizeof(me->comm)-1) < 0)
  1936. return -EFAULT;
  1937. set_task_comm(me, ncomm);
  1938. return 0;
  1939. }
  1940. case PR_GET_NAME: {
  1941. struct task_struct *me = current;
  1942. unsigned char tcomm[sizeof(me->comm)];
  1943. get_task_comm(tcomm, me);
  1944. if (copy_to_user((char __user *)arg2, tcomm, sizeof(tcomm)))
  1945. return -EFAULT;
  1946. return 0;
  1947. }
  1948. case PR_GET_ENDIAN:
  1949. error = GET_ENDIAN(current, arg2);
  1950. break;
  1951. case PR_SET_ENDIAN:
  1952. error = SET_ENDIAN(current, arg2);
  1953. break;
  1954. case PR_GET_SECCOMP:
  1955. error = prctl_get_seccomp();
  1956. break;
  1957. case PR_SET_SECCOMP:
  1958. error = prctl_set_seccomp(arg2);
  1959. break;
  1960. default:
  1961. error = -EINVAL;
  1962. break;
  1963. }
  1964. return error;
  1965. }
  1966. asmlinkage long sys_getcpu(unsigned __user *cpup, unsigned __user *nodep,
  1967. struct getcpu_cache __user *cache)
  1968. {
  1969. int err = 0;
  1970. int cpu = raw_smp_processor_id();
  1971. if (cpup)
  1972. err |= put_user(cpu, cpup);
  1973. if (nodep)
  1974. err |= put_user(cpu_to_node(cpu), nodep);
  1975. if (cache) {
  1976. /*
  1977. * The cache is not needed for this implementation,
  1978. * but make sure user programs pass something
  1979. * valid. vsyscall implementations can instead make
  1980. * good use of the cache. Only use t0 and t1 because
  1981. * these are available in both 32bit and 64bit ABI (no
  1982. * need for a compat_getcpu). 32bit has enough
  1983. * padding
  1984. */
  1985. unsigned long t0, t1;
  1986. get_user(t0, &cache->blob[0]);
  1987. get_user(t1, &cache->blob[1]);
  1988. t0++;
  1989. t1++;
  1990. put_user(t0, &cache->blob[0]);
  1991. put_user(t1, &cache->blob[1]);
  1992. }
  1993. return err ? -EFAULT : 0;
  1994. }
  1995. char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
  1996. static void argv_cleanup(char **argv, char **envp)
  1997. {
  1998. argv_free(argv);
  1999. }
  2000. /**
  2001. * orderly_poweroff - Trigger an orderly system poweroff
  2002. * @force: force poweroff if command execution fails
  2003. *
  2004. * This may be called from any context to trigger a system shutdown.
  2005. * If the orderly shutdown fails, it will force an immediate shutdown.
  2006. */
  2007. int orderly_poweroff(bool force)
  2008. {
  2009. int argc;
  2010. char **argv = argv_split(GFP_ATOMIC, poweroff_cmd, &argc);
  2011. static char *envp[] = {
  2012. "HOME=/",
  2013. "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
  2014. NULL
  2015. };
  2016. int ret = -ENOMEM;
  2017. struct subprocess_info *info;
  2018. if (argv == NULL) {
  2019. printk(KERN_WARNING "%s failed to allocate memory for \"%s\"\n",
  2020. __func__, poweroff_cmd);
  2021. goto out;
  2022. }
  2023. info = call_usermodehelper_setup(argv[0], argv, envp);
  2024. if (info == NULL) {
  2025. argv_free(argv);
  2026. goto out;
  2027. }
  2028. call_usermodehelper_setcleanup(info, argv_cleanup);
  2029. ret = call_usermodehelper_exec(info, UMH_NO_WAIT);
  2030. out:
  2031. if (ret && force) {
  2032. printk(KERN_WARNING "Failed to start orderly shutdown: "
  2033. "forcing the issue\n");
  2034. /* I guess this should try to kick off some daemon to
  2035. sync and poweroff asap. Or not even bother syncing
  2036. if we're doing an emergency shutdown? */
  2037. emergency_sync();
  2038. kernel_power_off();
  2039. }
  2040. return ret;
  2041. }
  2042. EXPORT_SYMBOL_GPL(orderly_poweroff);