cred.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Landlock LSM - Credential hooks
  4. *
  5. * Copyright © 2017-2020 Mickaël Salaün <mic@digikod.net>
  6. * Copyright © 2018-2020 ANSSI
  7. */
  8. #include <linux/cred.h>
  9. #include <linux/lsm_hooks.h>
  10. #include "common.h"
  11. #include "cred.h"
  12. #include "ruleset.h"
  13. #include "setup.h"
  14. static void hook_cred_transfer(struct cred *const new,
  15. const struct cred *const old)
  16. {
  17. struct landlock_ruleset *const old_dom = landlock_cred(old)->domain;
  18. if (old_dom) {
  19. landlock_get_ruleset(old_dom);
  20. landlock_cred(new)->domain = old_dom;
  21. }
  22. }
  23. static int hook_cred_prepare(struct cred *const new,
  24. const struct cred *const old, const gfp_t gfp)
  25. {
  26. hook_cred_transfer(new, old);
  27. return 0;
  28. }
  29. static void hook_cred_free(struct cred *const cred)
  30. {
  31. struct landlock_ruleset *const dom = landlock_cred(cred)->domain;
  32. if (dom)
  33. landlock_put_ruleset_deferred(dom);
  34. }
  35. static struct security_hook_list landlock_hooks[] __ro_after_init = {
  36. LSM_HOOK_INIT(cred_prepare, hook_cred_prepare),
  37. LSM_HOOK_INIT(cred_transfer, hook_cred_transfer),
  38. LSM_HOOK_INIT(cred_free, hook_cred_free),
  39. };
  40. __init void landlock_add_cred_hooks(void)
  41. {
  42. security_add_hooks(landlock_hooks, ARRAY_SIZE(landlock_hooks),
  43. &landlock_lsmid);
  44. }