cred.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. /* Task credentials management - see Documentation/credentials.txt
  2. *
  3. * Copyright (C) 2008 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/cred.h>
  13. #include <linux/slab.h>
  14. #include <linux/sched.h>
  15. #include <linux/key.h>
  16. #include <linux/keyctl.h>
  17. #include <linux/init_task.h>
  18. #include <linux/security.h>
  19. #include <linux/cn_proc.h>
  20. #include "cred-internals.h"
  21. #if 0
  22. #define kdebug(FMT, ...) \
  23. printk("[%-5.5s%5u] "FMT"\n", current->comm, current->pid ,##__VA_ARGS__)
  24. #else
  25. static inline __attribute__((format(printf, 1, 2)))
  26. void no_printk(const char *fmt, ...)
  27. {
  28. }
  29. #define kdebug(FMT, ...) \
  30. no_printk("[%-5.5s%5u] "FMT"\n", current->comm, current->pid ,##__VA_ARGS__)
  31. #endif
  32. static struct kmem_cache *cred_jar;
  33. /*
  34. * The common credentials for the initial task's thread group
  35. */
  36. #ifdef CONFIG_KEYS
  37. static struct thread_group_cred init_tgcred = {
  38. .usage = ATOMIC_INIT(2),
  39. .tgid = 0,
  40. .lock = SPIN_LOCK_UNLOCKED,
  41. };
  42. #endif
  43. /*
  44. * The initial credentials for the initial task
  45. */
  46. struct cred init_cred = {
  47. .usage = ATOMIC_INIT(4),
  48. #ifdef CONFIG_DEBUG_CREDENTIALS
  49. .subscribers = ATOMIC_INIT(2),
  50. .magic = CRED_MAGIC,
  51. #endif
  52. .securebits = SECUREBITS_DEFAULT,
  53. .cap_inheritable = CAP_INIT_INH_SET,
  54. .cap_permitted = CAP_FULL_SET,
  55. .cap_effective = CAP_INIT_EFF_SET,
  56. .cap_bset = CAP_INIT_BSET,
  57. .user = INIT_USER,
  58. .group_info = &init_groups,
  59. #ifdef CONFIG_KEYS
  60. .tgcred = &init_tgcred,
  61. #endif
  62. };
  63. static inline void set_cred_subscribers(struct cred *cred, int n)
  64. {
  65. #ifdef CONFIG_DEBUG_CREDENTIALS
  66. atomic_set(&cred->subscribers, n);
  67. #endif
  68. }
  69. static inline int read_cred_subscribers(const struct cred *cred)
  70. {
  71. #ifdef CONFIG_DEBUG_CREDENTIALS
  72. return atomic_read(&cred->subscribers);
  73. #else
  74. return 0;
  75. #endif
  76. }
  77. static inline void alter_cred_subscribers(const struct cred *_cred, int n)
  78. {
  79. #ifdef CONFIG_DEBUG_CREDENTIALS
  80. struct cred *cred = (struct cred *) _cred;
  81. atomic_add(n, &cred->subscribers);
  82. #endif
  83. }
  84. /*
  85. * Dispose of the shared task group credentials
  86. */
  87. #ifdef CONFIG_KEYS
  88. static void release_tgcred_rcu(struct rcu_head *rcu)
  89. {
  90. struct thread_group_cred *tgcred =
  91. container_of(rcu, struct thread_group_cred, rcu);
  92. BUG_ON(atomic_read(&tgcred->usage) != 0);
  93. key_put(tgcred->session_keyring);
  94. key_put(tgcred->process_keyring);
  95. kfree(tgcred);
  96. }
  97. #endif
  98. /*
  99. * Release a set of thread group credentials.
  100. */
  101. static void release_tgcred(struct cred *cred)
  102. {
  103. #ifdef CONFIG_KEYS
  104. struct thread_group_cred *tgcred = cred->tgcred;
  105. if (atomic_dec_and_test(&tgcred->usage))
  106. call_rcu(&tgcred->rcu, release_tgcred_rcu);
  107. #endif
  108. }
  109. /*
  110. * The RCU callback to actually dispose of a set of credentials
  111. */
  112. static void put_cred_rcu(struct rcu_head *rcu)
  113. {
  114. struct cred *cred = container_of(rcu, struct cred, rcu);
  115. kdebug("put_cred_rcu(%p)", cred);
  116. #ifdef CONFIG_DEBUG_CREDENTIALS
  117. if (cred->magic != CRED_MAGIC_DEAD ||
  118. atomic_read(&cred->usage) != 0 ||
  119. read_cred_subscribers(cred) != 0)
  120. panic("CRED: put_cred_rcu() sees %p with"
  121. " mag %x, put %p, usage %d, subscr %d\n",
  122. cred, cred->magic, cred->put_addr,
  123. atomic_read(&cred->usage),
  124. read_cred_subscribers(cred));
  125. #else
  126. if (atomic_read(&cred->usage) != 0)
  127. panic("CRED: put_cred_rcu() sees %p with usage %d\n",
  128. cred, atomic_read(&cred->usage));
  129. #endif
  130. security_cred_free(cred);
  131. key_put(cred->thread_keyring);
  132. key_put(cred->request_key_auth);
  133. release_tgcred(cred);
  134. if (cred->group_info)
  135. put_group_info(cred->group_info);
  136. free_uid(cred->user);
  137. kmem_cache_free(cred_jar, cred);
  138. }
  139. /**
  140. * __put_cred - Destroy a set of credentials
  141. * @cred: The record to release
  142. *
  143. * Destroy a set of credentials on which no references remain.
  144. */
  145. void __put_cred(struct cred *cred)
  146. {
  147. kdebug("__put_cred(%p{%d,%d})", cred,
  148. atomic_read(&cred->usage),
  149. read_cred_subscribers(cred));
  150. BUG_ON(atomic_read(&cred->usage) != 0);
  151. #ifdef CONFIG_DEBUG_CREDENTIALS
  152. BUG_ON(read_cred_subscribers(cred) != 0);
  153. cred->magic = CRED_MAGIC_DEAD;
  154. cred->put_addr = __builtin_return_address(0);
  155. #endif
  156. BUG_ON(cred == current->cred);
  157. BUG_ON(cred == current->real_cred);
  158. call_rcu(&cred->rcu, put_cred_rcu);
  159. }
  160. EXPORT_SYMBOL(__put_cred);
  161. /*
  162. * Clean up a task's credentials when it exits
  163. */
  164. void exit_creds(struct task_struct *tsk)
  165. {
  166. struct cred *cred;
  167. kdebug("exit_creds(%u,%p,%p,{%d,%d})", tsk->pid, tsk->real_cred, tsk->cred,
  168. atomic_read(&tsk->cred->usage),
  169. read_cred_subscribers(tsk->cred));
  170. cred = (struct cred *) tsk->real_cred;
  171. tsk->real_cred = NULL;
  172. validate_creds(cred);
  173. alter_cred_subscribers(cred, -1);
  174. put_cred(cred);
  175. cred = (struct cred *) tsk->cred;
  176. tsk->cred = NULL;
  177. validate_creds(cred);
  178. alter_cred_subscribers(cred, -1);
  179. put_cred(cred);
  180. cred = (struct cred *) tsk->replacement_session_keyring;
  181. if (cred) {
  182. tsk->replacement_session_keyring = NULL;
  183. validate_creds(cred);
  184. put_cred(cred);
  185. }
  186. }
  187. /*
  188. * Allocate blank credentials, such that the credentials can be filled in at a
  189. * later date without risk of ENOMEM.
  190. */
  191. struct cred *cred_alloc_blank(void)
  192. {
  193. struct cred *new;
  194. new = kmem_cache_zalloc(cred_jar, GFP_KERNEL);
  195. if (!new)
  196. return NULL;
  197. #ifdef CONFIG_KEYS
  198. new->tgcred = kzalloc(sizeof(*new->tgcred), GFP_KERNEL);
  199. if (!new->tgcred) {
  200. kmem_cache_free(cred_jar, new);
  201. return NULL;
  202. }
  203. atomic_set(&new->tgcred->usage, 1);
  204. #endif
  205. atomic_set(&new->usage, 1);
  206. if (security_cred_alloc_blank(new, GFP_KERNEL) < 0)
  207. goto error;
  208. #ifdef CONFIG_DEBUG_CREDENTIALS
  209. new->magic = CRED_MAGIC;
  210. #endif
  211. return new;
  212. error:
  213. abort_creds(new);
  214. return NULL;
  215. }
  216. /**
  217. * prepare_creds - Prepare a new set of credentials for modification
  218. *
  219. * Prepare a new set of task credentials for modification. A task's creds
  220. * shouldn't generally be modified directly, therefore this function is used to
  221. * prepare a new copy, which the caller then modifies and then commits by
  222. * calling commit_creds().
  223. *
  224. * Preparation involves making a copy of the objective creds for modification.
  225. *
  226. * Returns a pointer to the new creds-to-be if successful, NULL otherwise.
  227. *
  228. * Call commit_creds() or abort_creds() to clean up.
  229. */
  230. struct cred *prepare_creds(void)
  231. {
  232. struct task_struct *task = current;
  233. const struct cred *old;
  234. struct cred *new;
  235. validate_process_creds();
  236. new = kmem_cache_alloc(cred_jar, GFP_KERNEL);
  237. if (!new)
  238. return NULL;
  239. kdebug("prepare_creds() alloc %p", new);
  240. old = task->cred;
  241. memcpy(new, old, sizeof(struct cred));
  242. atomic_set(&new->usage, 1);
  243. set_cred_subscribers(new, 0);
  244. get_group_info(new->group_info);
  245. get_uid(new->user);
  246. #ifdef CONFIG_KEYS
  247. key_get(new->thread_keyring);
  248. key_get(new->request_key_auth);
  249. atomic_inc(&new->tgcred->usage);
  250. #endif
  251. #ifdef CONFIG_SECURITY
  252. new->security = NULL;
  253. #endif
  254. if (security_prepare_creds(new, old, GFP_KERNEL) < 0)
  255. goto error;
  256. validate_creds(new);
  257. return new;
  258. error:
  259. abort_creds(new);
  260. return NULL;
  261. }
  262. EXPORT_SYMBOL(prepare_creds);
  263. /*
  264. * Prepare credentials for current to perform an execve()
  265. * - The caller must hold current->cred_guard_mutex
  266. */
  267. struct cred *prepare_exec_creds(void)
  268. {
  269. struct thread_group_cred *tgcred = NULL;
  270. struct cred *new;
  271. #ifdef CONFIG_KEYS
  272. tgcred = kmalloc(sizeof(*tgcred), GFP_KERNEL);
  273. if (!tgcred)
  274. return NULL;
  275. #endif
  276. new = prepare_creds();
  277. if (!new) {
  278. kfree(tgcred);
  279. return new;
  280. }
  281. #ifdef CONFIG_KEYS
  282. /* newly exec'd tasks don't get a thread keyring */
  283. key_put(new->thread_keyring);
  284. new->thread_keyring = NULL;
  285. /* create a new per-thread-group creds for all this set of threads to
  286. * share */
  287. memcpy(tgcred, new->tgcred, sizeof(struct thread_group_cred));
  288. atomic_set(&tgcred->usage, 1);
  289. spin_lock_init(&tgcred->lock);
  290. /* inherit the session keyring; new process keyring */
  291. key_get(tgcred->session_keyring);
  292. tgcred->process_keyring = NULL;
  293. release_tgcred(new);
  294. new->tgcred = tgcred;
  295. #endif
  296. return new;
  297. }
  298. /*
  299. * prepare new credentials for the usermode helper dispatcher
  300. */
  301. struct cred *prepare_usermodehelper_creds(void)
  302. {
  303. #ifdef CONFIG_KEYS
  304. struct thread_group_cred *tgcred = NULL;
  305. #endif
  306. struct cred *new;
  307. #ifdef CONFIG_KEYS
  308. tgcred = kzalloc(sizeof(*new->tgcred), GFP_ATOMIC);
  309. if (!tgcred)
  310. return NULL;
  311. #endif
  312. new = kmem_cache_alloc(cred_jar, GFP_ATOMIC);
  313. if (!new)
  314. goto free_tgcred;
  315. kdebug("prepare_usermodehelper_creds() alloc %p", new);
  316. memcpy(new, &init_cred, sizeof(struct cred));
  317. atomic_set(&new->usage, 1);
  318. set_cred_subscribers(new, 0);
  319. get_group_info(new->group_info);
  320. get_uid(new->user);
  321. #ifdef CONFIG_KEYS
  322. new->thread_keyring = NULL;
  323. new->request_key_auth = NULL;
  324. new->jit_keyring = KEY_REQKEY_DEFL_DEFAULT;
  325. atomic_set(&tgcred->usage, 1);
  326. spin_lock_init(&tgcred->lock);
  327. new->tgcred = tgcred;
  328. #endif
  329. #ifdef CONFIG_SECURITY
  330. new->security = NULL;
  331. #endif
  332. if (security_prepare_creds(new, &init_cred, GFP_ATOMIC) < 0)
  333. goto error;
  334. validate_creds(new);
  335. BUG_ON(atomic_read(&new->usage) != 1);
  336. return new;
  337. error:
  338. put_cred(new);
  339. free_tgcred:
  340. #ifdef CONFIG_KEYS
  341. kfree(tgcred);
  342. #endif
  343. return NULL;
  344. }
  345. /*
  346. * Copy credentials for the new process created by fork()
  347. *
  348. * We share if we can, but under some circumstances we have to generate a new
  349. * set.
  350. *
  351. * The new process gets the current process's subjective credentials as its
  352. * objective and subjective credentials
  353. */
  354. int copy_creds(struct task_struct *p, unsigned long clone_flags)
  355. {
  356. #ifdef CONFIG_KEYS
  357. struct thread_group_cred *tgcred;
  358. #endif
  359. struct cred *new;
  360. int ret;
  361. mutex_init(&p->cred_guard_mutex);
  362. if (
  363. #ifdef CONFIG_KEYS
  364. !p->cred->thread_keyring &&
  365. #endif
  366. clone_flags & CLONE_THREAD
  367. ) {
  368. p->real_cred = get_cred(p->cred);
  369. get_cred(p->cred);
  370. alter_cred_subscribers(p->cred, 2);
  371. kdebug("share_creds(%p{%d,%d})",
  372. p->cred, atomic_read(&p->cred->usage),
  373. read_cred_subscribers(p->cred));
  374. atomic_inc(&p->cred->user->processes);
  375. return 0;
  376. }
  377. new = prepare_creds();
  378. if (!new)
  379. return -ENOMEM;
  380. if (clone_flags & CLONE_NEWUSER) {
  381. ret = create_user_ns(new);
  382. if (ret < 0)
  383. goto error_put;
  384. }
  385. #ifdef CONFIG_KEYS
  386. /* new threads get their own thread keyrings if their parent already
  387. * had one */
  388. if (new->thread_keyring) {
  389. key_put(new->thread_keyring);
  390. new->thread_keyring = NULL;
  391. if (clone_flags & CLONE_THREAD)
  392. install_thread_keyring_to_cred(new);
  393. }
  394. /* we share the process and session keyrings between all the threads in
  395. * a process - this is slightly icky as we violate COW credentials a
  396. * bit */
  397. if (!(clone_flags & CLONE_THREAD)) {
  398. tgcred = kmalloc(sizeof(*tgcred), GFP_KERNEL);
  399. if (!tgcred) {
  400. ret = -ENOMEM;
  401. goto error_put;
  402. }
  403. atomic_set(&tgcred->usage, 1);
  404. spin_lock_init(&tgcred->lock);
  405. tgcred->process_keyring = NULL;
  406. tgcred->session_keyring = key_get(new->tgcred->session_keyring);
  407. release_tgcred(new);
  408. new->tgcred = tgcred;
  409. }
  410. #endif
  411. atomic_inc(&new->user->processes);
  412. p->cred = p->real_cred = get_cred(new);
  413. alter_cred_subscribers(new, 2);
  414. validate_creds(new);
  415. return 0;
  416. error_put:
  417. put_cred(new);
  418. return ret;
  419. }
  420. /**
  421. * commit_creds - Install new credentials upon the current task
  422. * @new: The credentials to be assigned
  423. *
  424. * Install a new set of credentials to the current task, using RCU to replace
  425. * the old set. Both the objective and the subjective credentials pointers are
  426. * updated. This function may not be called if the subjective credentials are
  427. * in an overridden state.
  428. *
  429. * This function eats the caller's reference to the new credentials.
  430. *
  431. * Always returns 0 thus allowing this function to be tail-called at the end
  432. * of, say, sys_setgid().
  433. */
  434. int commit_creds(struct cred *new)
  435. {
  436. struct task_struct *task = current;
  437. const struct cred *old = task->real_cred;
  438. kdebug("commit_creds(%p{%d,%d})", new,
  439. atomic_read(&new->usage),
  440. read_cred_subscribers(new));
  441. BUG_ON(task->cred != old);
  442. #ifdef CONFIG_DEBUG_CREDENTIALS
  443. BUG_ON(read_cred_subscribers(old) < 2);
  444. validate_creds(old);
  445. validate_creds(new);
  446. #endif
  447. BUG_ON(atomic_read(&new->usage) < 1);
  448. security_commit_creds(new, old);
  449. get_cred(new); /* we will require a ref for the subj creds too */
  450. /* dumpability changes */
  451. if (old->euid != new->euid ||
  452. old->egid != new->egid ||
  453. old->fsuid != new->fsuid ||
  454. old->fsgid != new->fsgid ||
  455. !cap_issubset(new->cap_permitted, old->cap_permitted)) {
  456. if (task->mm)
  457. set_dumpable(task->mm, suid_dumpable);
  458. task->pdeath_signal = 0;
  459. smp_wmb();
  460. }
  461. /* alter the thread keyring */
  462. if (new->fsuid != old->fsuid)
  463. key_fsuid_changed(task);
  464. if (new->fsgid != old->fsgid)
  465. key_fsgid_changed(task);
  466. /* do it
  467. * - What if a process setreuid()'s and this brings the
  468. * new uid over his NPROC rlimit? We can check this now
  469. * cheaply with the new uid cache, so if it matters
  470. * we should be checking for it. -DaveM
  471. */
  472. alter_cred_subscribers(new, 2);
  473. if (new->user != old->user)
  474. atomic_inc(&new->user->processes);
  475. rcu_assign_pointer(task->real_cred, new);
  476. rcu_assign_pointer(task->cred, new);
  477. if (new->user != old->user)
  478. atomic_dec(&old->user->processes);
  479. alter_cred_subscribers(old, -2);
  480. sched_switch_user(task);
  481. /* send notifications */
  482. if (new->uid != old->uid ||
  483. new->euid != old->euid ||
  484. new->suid != old->suid ||
  485. new->fsuid != old->fsuid)
  486. proc_id_connector(task, PROC_EVENT_UID);
  487. if (new->gid != old->gid ||
  488. new->egid != old->egid ||
  489. new->sgid != old->sgid ||
  490. new->fsgid != old->fsgid)
  491. proc_id_connector(task, PROC_EVENT_GID);
  492. /* release the old obj and subj refs both */
  493. put_cred(old);
  494. put_cred(old);
  495. return 0;
  496. }
  497. EXPORT_SYMBOL(commit_creds);
  498. /**
  499. * abort_creds - Discard a set of credentials and unlock the current task
  500. * @new: The credentials that were going to be applied
  501. *
  502. * Discard a set of credentials that were under construction and unlock the
  503. * current task.
  504. */
  505. void abort_creds(struct cred *new)
  506. {
  507. kdebug("abort_creds(%p{%d,%d})", new,
  508. atomic_read(&new->usage),
  509. read_cred_subscribers(new));
  510. #ifdef CONFIG_DEBUG_CREDENTIALS
  511. BUG_ON(read_cred_subscribers(new) != 0);
  512. #endif
  513. BUG_ON(atomic_read(&new->usage) < 1);
  514. put_cred(new);
  515. }
  516. EXPORT_SYMBOL(abort_creds);
  517. /**
  518. * override_creds - Override the current process's subjective credentials
  519. * @new: The credentials to be assigned
  520. *
  521. * Install a set of temporary override subjective credentials on the current
  522. * process, returning the old set for later reversion.
  523. */
  524. const struct cred *override_creds(const struct cred *new)
  525. {
  526. const struct cred *old = current->cred;
  527. kdebug("override_creds(%p{%d,%d})", new,
  528. atomic_read(&new->usage),
  529. read_cred_subscribers(new));
  530. validate_creds(old);
  531. validate_creds(new);
  532. get_cred(new);
  533. alter_cred_subscribers(new, 1);
  534. rcu_assign_pointer(current->cred, new);
  535. alter_cred_subscribers(old, -1);
  536. kdebug("override_creds() = %p{%d,%d}", old,
  537. atomic_read(&old->usage),
  538. read_cred_subscribers(old));
  539. return old;
  540. }
  541. EXPORT_SYMBOL(override_creds);
  542. /**
  543. * revert_creds - Revert a temporary subjective credentials override
  544. * @old: The credentials to be restored
  545. *
  546. * Revert a temporary set of override subjective credentials to an old set,
  547. * discarding the override set.
  548. */
  549. void revert_creds(const struct cred *old)
  550. {
  551. const struct cred *override = current->cred;
  552. kdebug("revert_creds(%p{%d,%d})", old,
  553. atomic_read(&old->usage),
  554. read_cred_subscribers(old));
  555. validate_creds(old);
  556. validate_creds(override);
  557. alter_cred_subscribers(old, 1);
  558. rcu_assign_pointer(current->cred, old);
  559. alter_cred_subscribers(override, -1);
  560. put_cred(override);
  561. }
  562. EXPORT_SYMBOL(revert_creds);
  563. /*
  564. * initialise the credentials stuff
  565. */
  566. void __init cred_init(void)
  567. {
  568. /* allocate a slab in which we can store credentials */
  569. cred_jar = kmem_cache_create("cred_jar", sizeof(struct cred),
  570. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  571. }
  572. /**
  573. * prepare_kernel_cred - Prepare a set of credentials for a kernel service
  574. * @daemon: A userspace daemon to be used as a reference
  575. *
  576. * Prepare a set of credentials for a kernel service. This can then be used to
  577. * override a task's own credentials so that work can be done on behalf of that
  578. * task that requires a different subjective context.
  579. *
  580. * @daemon is used to provide a base for the security record, but can be NULL.
  581. * If @daemon is supplied, then the security data will be derived from that;
  582. * otherwise they'll be set to 0 and no groups, full capabilities and no keys.
  583. *
  584. * The caller may change these controls afterwards if desired.
  585. *
  586. * Returns the new credentials or NULL if out of memory.
  587. *
  588. * Does not take, and does not return holding current->cred_replace_mutex.
  589. */
  590. struct cred *prepare_kernel_cred(struct task_struct *daemon)
  591. {
  592. const struct cred *old;
  593. struct cred *new;
  594. new = kmem_cache_alloc(cred_jar, GFP_KERNEL);
  595. if (!new)
  596. return NULL;
  597. kdebug("prepare_kernel_cred() alloc %p", new);
  598. if (daemon)
  599. old = get_task_cred(daemon);
  600. else
  601. old = get_cred(&init_cred);
  602. validate_creds(old);
  603. *new = *old;
  604. get_uid(new->user);
  605. get_group_info(new->group_info);
  606. #ifdef CONFIG_KEYS
  607. atomic_inc(&init_tgcred.usage);
  608. new->tgcred = &init_tgcred;
  609. new->request_key_auth = NULL;
  610. new->thread_keyring = NULL;
  611. new->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
  612. #endif
  613. #ifdef CONFIG_SECURITY
  614. new->security = NULL;
  615. #endif
  616. if (security_prepare_creds(new, old, GFP_KERNEL) < 0)
  617. goto error;
  618. atomic_set(&new->usage, 1);
  619. set_cred_subscribers(new, 0);
  620. put_cred(old);
  621. validate_creds(new);
  622. return new;
  623. error:
  624. put_cred(new);
  625. put_cred(old);
  626. return NULL;
  627. }
  628. EXPORT_SYMBOL(prepare_kernel_cred);
  629. /**
  630. * set_security_override - Set the security ID in a set of credentials
  631. * @new: The credentials to alter
  632. * @secid: The LSM security ID to set
  633. *
  634. * Set the LSM security ID in a set of credentials so that the subjective
  635. * security is overridden when an alternative set of credentials is used.
  636. */
  637. int set_security_override(struct cred *new, u32 secid)
  638. {
  639. return security_kernel_act_as(new, secid);
  640. }
  641. EXPORT_SYMBOL(set_security_override);
  642. /**
  643. * set_security_override_from_ctx - Set the security ID in a set of credentials
  644. * @new: The credentials to alter
  645. * @secctx: The LSM security context to generate the security ID from.
  646. *
  647. * Set the LSM security ID in a set of credentials so that the subjective
  648. * security is overridden when an alternative set of credentials is used. The
  649. * security ID is specified in string form as a security context to be
  650. * interpreted by the LSM.
  651. */
  652. int set_security_override_from_ctx(struct cred *new, const char *secctx)
  653. {
  654. u32 secid;
  655. int ret;
  656. ret = security_secctx_to_secid(secctx, strlen(secctx), &secid);
  657. if (ret < 0)
  658. return ret;
  659. return set_security_override(new, secid);
  660. }
  661. EXPORT_SYMBOL(set_security_override_from_ctx);
  662. /**
  663. * set_create_files_as - Set the LSM file create context in a set of credentials
  664. * @new: The credentials to alter
  665. * @inode: The inode to take the context from
  666. *
  667. * Change the LSM file creation context in a set of credentials to be the same
  668. * as the object context of the specified inode, so that the new inodes have
  669. * the same MAC context as that inode.
  670. */
  671. int set_create_files_as(struct cred *new, struct inode *inode)
  672. {
  673. new->fsuid = inode->i_uid;
  674. new->fsgid = inode->i_gid;
  675. return security_kernel_create_files_as(new, inode);
  676. }
  677. EXPORT_SYMBOL(set_create_files_as);
  678. #ifdef CONFIG_DEBUG_CREDENTIALS
  679. bool creds_are_invalid(const struct cred *cred)
  680. {
  681. if (cred->magic != CRED_MAGIC)
  682. return true;
  683. if (atomic_read(&cred->usage) < atomic_read(&cred->subscribers))
  684. return true;
  685. #ifdef CONFIG_SECURITY_SELINUX
  686. if (selinux_is_enabled()) {
  687. if ((unsigned long) cred->security < PAGE_SIZE)
  688. return true;
  689. if ((*(u32 *)cred->security & 0xffffff00) ==
  690. (POISON_FREE << 24 | POISON_FREE << 16 | POISON_FREE << 8))
  691. return true;
  692. }
  693. #endif
  694. return false;
  695. }
  696. EXPORT_SYMBOL(creds_are_invalid);
  697. /*
  698. * dump invalid credentials
  699. */
  700. static void dump_invalid_creds(const struct cred *cred, const char *label,
  701. const struct task_struct *tsk)
  702. {
  703. printk(KERN_ERR "CRED: %s credentials: %p %s%s%s\n",
  704. label, cred,
  705. cred == &init_cred ? "[init]" : "",
  706. cred == tsk->real_cred ? "[real]" : "",
  707. cred == tsk->cred ? "[eff]" : "");
  708. printk(KERN_ERR "CRED: ->magic=%x, put_addr=%p\n",
  709. cred->magic, cred->put_addr);
  710. printk(KERN_ERR "CRED: ->usage=%d, subscr=%d\n",
  711. atomic_read(&cred->usage),
  712. read_cred_subscribers(cred));
  713. printk(KERN_ERR "CRED: ->*uid = { %d,%d,%d,%d }\n",
  714. cred->uid, cred->euid, cred->suid, cred->fsuid);
  715. printk(KERN_ERR "CRED: ->*gid = { %d,%d,%d,%d }\n",
  716. cred->gid, cred->egid, cred->sgid, cred->fsgid);
  717. #ifdef CONFIG_SECURITY
  718. printk(KERN_ERR "CRED: ->security is %p\n", cred->security);
  719. if ((unsigned long) cred->security >= PAGE_SIZE &&
  720. (((unsigned long) cred->security & 0xffffff00) !=
  721. (POISON_FREE << 24 | POISON_FREE << 16 | POISON_FREE << 8)))
  722. printk(KERN_ERR "CRED: ->security {%x, %x}\n",
  723. ((u32*)cred->security)[0],
  724. ((u32*)cred->security)[1]);
  725. #endif
  726. }
  727. /*
  728. * report use of invalid credentials
  729. */
  730. void __invalid_creds(const struct cred *cred, const char *file, unsigned line)
  731. {
  732. printk(KERN_ERR "CRED: Invalid credentials\n");
  733. printk(KERN_ERR "CRED: At %s:%u\n", file, line);
  734. dump_invalid_creds(cred, "Specified", current);
  735. BUG();
  736. }
  737. EXPORT_SYMBOL(__invalid_creds);
  738. /*
  739. * check the credentials on a process
  740. */
  741. void __validate_process_creds(struct task_struct *tsk,
  742. const char *file, unsigned line)
  743. {
  744. if (tsk->cred == tsk->real_cred) {
  745. if (unlikely(read_cred_subscribers(tsk->cred) < 2 ||
  746. creds_are_invalid(tsk->cred)))
  747. goto invalid_creds;
  748. } else {
  749. if (unlikely(read_cred_subscribers(tsk->real_cred) < 1 ||
  750. read_cred_subscribers(tsk->cred) < 1 ||
  751. creds_are_invalid(tsk->real_cred) ||
  752. creds_are_invalid(tsk->cred)))
  753. goto invalid_creds;
  754. }
  755. return;
  756. invalid_creds:
  757. printk(KERN_ERR "CRED: Invalid process credentials\n");
  758. printk(KERN_ERR "CRED: At %s:%u\n", file, line);
  759. dump_invalid_creds(tsk->real_cred, "Real", tsk);
  760. if (tsk->cred != tsk->real_cred)
  761. dump_invalid_creds(tsk->cred, "Effective", tsk);
  762. else
  763. printk(KERN_ERR "CRED: Effective creds == Real creds\n");
  764. BUG();
  765. }
  766. EXPORT_SYMBOL(__validate_process_creds);
  767. /*
  768. * check creds for do_exit()
  769. */
  770. void validate_creds_for_do_exit(struct task_struct *tsk)
  771. {
  772. kdebug("validate_creds_for_do_exit(%p,%p{%d,%d})",
  773. tsk->real_cred, tsk->cred,
  774. atomic_read(&tsk->cred->usage),
  775. read_cred_subscribers(tsk->cred));
  776. __validate_process_creds(tsk, __FILE__, __LINE__);
  777. }
  778. #endif /* CONFIG_DEBUG_CREDENTIALS */