lsm.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* lsm.h - header file for lib directory
  2. *
  3. * Copyright 2015 Rob Landley <rob@landley.net>
  4. */
  5. #if CFG_TOYBOX_SELINUX
  6. #include <selinux/selinux.h>
  7. #else
  8. #define is_selinux_enabled() 0
  9. #define setfscreatecon(...) (-1)
  10. #define getcon(...) (-1)
  11. #define getfilecon(...) (-1)
  12. #define lgetfilecon(...) (-1)
  13. #define fgetfilecon(...) (-1)
  14. #define setfilecon(...) (-1)
  15. #define lsetfilecon(...) (-1)
  16. #define fsetfilecon(...) (-1)
  17. #endif
  18. #if CFG_TOYBOX_SMACK
  19. #include <sys/smack.h>
  20. #include <linux/xattr.h>
  21. #else
  22. #ifndef XATTR_NAME_SMACK
  23. #define XATTR_NAME_SMACK 0
  24. #endif
  25. #define smack_smackfs_path(...) (-1)
  26. #define smack_new_label_from_self(...) (-1)
  27. #define smack_new_label_from_path(...) (-1)
  28. #define smack_new_label_from_file(...) (-1)
  29. #define smack_set_label_for_self(...) (-1)
  30. #define smack_set_label_for_path(...) (-1)
  31. #define smack_set_label_for_file(...) (-1)
  32. #endif
  33. // This turns into "return 0" when no LSM and lets code optimize out.
  34. static inline int lsm_enabled(void)
  35. {
  36. if (CFG_TOYBOX_SMACK) return !!smack_smackfs_path();
  37. else return is_selinux_enabled() == 1;
  38. }
  39. static inline char *lsm_name(void)
  40. {
  41. if (CFG_TOYBOX_SMACK) return "Smack";
  42. if (CFG_TOYBOX_SELINUX) return "SELinux";
  43. return "LSM";
  44. }
  45. // Fetch this process's lsm context
  46. static inline char *lsm_context(void)
  47. {
  48. int ok = 0;
  49. char *result = 0;
  50. if (CFG_TOYBOX_SMACK) ok = smack_new_label_from_self(&result) > 0;
  51. else ok = getcon(&result) == 0;
  52. return ok ? result : strdup("?");
  53. }
  54. // Set default label to apply to newly created stuff (NULL to clear it)
  55. static inline int lsm_set_create(char *context)
  56. {
  57. if (CFG_TOYBOX_SMACK) return smack_set_label_for_self(context);
  58. else return setfscreatecon(context);
  59. }
  60. // Label a file, following symlinks
  61. static inline int lsm_set_context(char *filename, char *context)
  62. {
  63. if (CFG_TOYBOX_SMACK)
  64. return smack_set_label_for_path(filename, XATTR_NAME_SMACK, 1, context);
  65. else return setfilecon(filename, context);
  66. }
  67. // Label a file, don't follow symlinks
  68. static inline int lsm_lset_context(char *filename, char *context)
  69. {
  70. if (CFG_TOYBOX_SMACK)
  71. return smack_set_label_for_path(filename, XATTR_NAME_SMACK, 0, context);
  72. else return lsetfilecon(filename, context);
  73. }
  74. // Label a file by filehandle
  75. static inline int lsm_fset_context(int file, char *context)
  76. {
  77. if (CFG_TOYBOX_SMACK)
  78. return smack_set_label_for_file(file, XATTR_NAME_SMACK, context);
  79. else return fsetfilecon(file, context);
  80. }
  81. // returns -1 in case of error or else the length of the context */
  82. // context can be NULL to get the length only */
  83. static inline int lsm_get_context(char *filename, char **context)
  84. {
  85. if (CFG_TOYBOX_SMACK)
  86. return smack_new_label_from_path(filename, XATTR_NAME_SMACK, 1, context);
  87. else return getfilecon(filename, context);
  88. }
  89. static inline int lsm_lget_context(char *filename, char **context)
  90. {
  91. if (CFG_TOYBOX_SMACK)
  92. return smack_new_label_from_path(filename, XATTR_NAME_SMACK, 0, context);
  93. else return lgetfilecon(filename, context);
  94. }
  95. static inline int lsm_fget_context(int file, char **context)
  96. {
  97. if (CFG_TOYBOX_SMACK)
  98. return smack_new_label_from_file(file, XATTR_NAME_SMACK, context);
  99. return fgetfilecon(file, context);
  100. }