cred.c 22 KB

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