OSDN Git Service

More ls -Z upgrading. Move TOYBOX_SELINUX and TOYBOX_SMACK support from
[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 #if CFG_TOYBOX_SELINUX
7 #include <selinux/selinux.h>
8 #else
9 #define is_selinux_enabled() 0
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
19 #if CFG_TOYBOX_SMACK
20 #include <sys/smack.h>
21 #include <sys/xattr.h>
22 #include <linux/xattr.h>
23 #else
24 #define XATTR_NAME_SMACK 0
25 //ssize_t fgetxattr (int fd, char *name, void *value, size_t size);
26 #define smack_smackfs_path(...) (-1)
27 #define smack_new_label_from_self(...) (-1)
28 #define smack_new_label_from_path(...) (-1)
29 #define smack_new_label_from_file(...) (-1)
30 #define smack_set_label_for_path(...) (-1)
31 #define smack_set_label_for_file(...) (-1)
32 #endif
33
34 // This turns into "return 0" when no LSM and lets code optimize out.
35 static inline int lsm_enabled(void)
36 {
37   if (CFG_TOYBOX_SMACK) return !!smack_smackfs_path();
38   else return is_selinux_enabled() == 1;
39 }
40
41 // Fetch this process's lsm context
42 static inline char *lsm_context(void)
43 {
44   int ok = 0;
45   char *result;
46
47   if (CFG_TOYBOX_SMACK) ok = smack_new_label_from_self(&result) > 0;
48   else ok = getcon(&result) == 0;
49
50   return ok ? result : strdup("?");
51 }
52
53 static inline int lsm_set_context(char *filename, char *context)
54 {
55   if (CFG_TOYBOX_SMACK)
56     return smack_set_label_for_path(filename, XATTR_NAME_SMACK, 1, context);
57   else return setfilecon(filename, context);
58 }
59
60 static inline int lsm_lset_context(char *filename, char *context)
61 {
62   if (CFG_TOYBOX_SMACK)
63     return smack_set_label_for_path(filename, XATTR_NAME_SMACK, 0, context);
64   else return lsetfilecon(filename, context);
65 }
66
67 static inline int lsm_fset_context(int file, char *context)
68 {
69   if (CFG_TOYBOX_SMACK)
70     return smack_set_label_for_file(file, XATTR_NAME_SMACK, context);
71   else return fsetfilecon(file, context);
72 }
73
74
75 // returns -1 in case of error or else the length of the context */
76 // context can be NULL to get the length only */
77 static inline int lsm_get_context(char *filename, char **context)
78 {
79   if (CFG_TOYBOX_SMACK)
80     return smack_new_label_from_path(filename, XATTR_NAME_SMACK, 1, context);
81   else return getfilecon(filename, context);
82 }
83
84 static inline int lsm_lget_context(char *filename, char **context)
85 {
86   if (CFG_TOYBOX_SMACK)
87     return smack_new_label_from_path(filename, XATTR_NAME_SMACK, 0, context);
88   else return lgetfilecon(filename, context);
89 }
90
91 static inline int lsm_fget_context(int file, char **context)
92 {
93   if (CFG_TOYBOX_SMACK)
94     return smack_new_label_from_file(file, XATTR_NAME_SMACK, context);
95   return fgetfilecon(file, context);
96 }