sys.c 53 KB

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