OSDN Git Service

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