cred.c 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Landlock LSM - Credential hooks
  4. *
  5. * Copyright © 2017-2020 Mickaël Salaün <[email protected]>
  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 int hook_cred_prepare(struct cred *const new,
  15. const struct cred *const old, const gfp_t gfp)
  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. return 0;
  23. }
  24. static void hook_cred_free(struct cred *const cred)
  25. {
  26. struct landlock_ruleset *const dom = landlock_cred(cred)->domain;
  27. if (dom)
  28. landlock_put_ruleset_deferred(dom);
  29. }
  30. static struct security_hook_list landlock_hooks[] __lsm_ro_after_init = {
  31. LSM_HOOK_INIT(cred_prepare, hook_cred_prepare),
  32. LSM_HOOK_INIT(cred_free, hook_cred_free),
  33. };
  34. __init void landlock_add_cred_hooks(void)
  35. {
  36. security_add_hooks(landlock_hooks, ARRAY_SIZE(landlock_hooks),
  37. LANDLOCK_NAME);
  38. }