cred.c 22 KB

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