sys.c 54 KB

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