sys.c 58 KB

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