cred.c 22 KB

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