600-loginutils-login.c-libselinux-get_default_context-ex.patch 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. From 850a6d031039237b0b13d8fab9f10a7cd4752907 Mon Sep 17 00:00:00 2001
  2. From: Dominick Grift <[email protected]>
  3. Date: Sat, 5 Apr 2025 13:40:26 +0200
  4. Subject: [PATCH] loginutils/login.c: libselinux get_default_context() expects
  5. seuser
  6. Use getseuserbyname() to get the seuser associated with username and use that
  7. instead with get_default_context()
  8. >From get_default_context.3:
  9. "These functions takes a SELinux user identity that must be defined in the SELinux policy as their input, not a Linux username."
  10. Fixes: #19075
  11. Upstream-Status: Submitted [https://lists.busybox.net/pipermail/busybox/2025-April/091407.html]
  12. Signed-off-by: Dominick Grift <[email protected]>
  13. ---
  14. loginutils/login.c | 11 ++++++++++-
  15. 1 file changed, 10 insertions(+), 1 deletion(-)
  16. --- a/loginutils/login.c
  17. +++ b/loginutils/login.c
  18. @@ -183,12 +183,16 @@ static void die_if_nologin(void)
  19. static void initselinux(char *username, char *full_tty,
  20. security_context_t *user_sid)
  21. {
  22. + char *seuser = NULL, *level = NULL;
  23. security_context_t old_tty_sid, new_tty_sid;
  24. if (!is_selinux_enabled())
  25. return;
  26. - if (get_default_context(username, NULL, user_sid)) {
  27. + if (getseuserbyname(username, &seuser, &level)) {
  28. + bb_error_msg_and_die("can't get seuser for %s", username);
  29. + }
  30. + if (get_default_context(seuser, NULL, user_sid)) {
  31. bb_error_msg_and_die("can't get SID for %s", username);
  32. }
  33. if (getfilecon(full_tty, &old_tty_sid) < 0) {
  34. @@ -201,6 +205,11 @@ static void initselinux(char *username,
  35. if (setfilecon(full_tty, new_tty_sid) != 0) {
  36. bb_perror_msg_and_die("chsid(%s, %s) failed", full_tty, new_tty_sid);
  37. }
  38. +
  39. + if (ENABLE_FEATURE_CLEAN_UP) {
  40. + free(seuser);
  41. + free(level);
  42. + }
  43. }
  44. #endif