cred.c 22 KB

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