sys.c 56 KB

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