sys.c 56 KB

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