cred.c 22 KB

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