Skip to content

Commit cef064f

Browse files
rmr167gregkh
authored andcommitted
apparmor: test: make static symbols visible during kunit testing
[ Upstream commit b11e51d ] Use macros, VISIBLE_IF_KUNIT and EXPORT_SYMBOL_IF_KUNIT, to allow static symbols to be conditionally set to be visible during apparmor_policy_unpack_test, which removes the need to include the testing file in the implementation file. Change the namespace of the symbols that are now conditionally visible (by adding the prefix aa_) to avoid confusion with symbols of the same name. Allow the test to be built as a module and namespace the module name from policy_unpack_test to apparmor_policy_unpack_test to improve clarity of the module name. Provide an example of how static symbols can be dealt with in testing. Signed-off-by: Rae Moar <[email protected]> Reviewed-by: David Gow <[email protected]> Acked-by: John Johansen <[email protected]> Signed-off-by: Shuah Khan <[email protected]> Stable-dep-of: 8884ba0 ("apparmor: fix invalid reference on profile->disconnected") Signed-off-by: Sasha Levin <[email protected]>
1 parent cfce1e2 commit cef064f

File tree

5 files changed

+196
-168
lines changed

5 files changed

+196
-168
lines changed

security/apparmor/Kconfig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ config SECURITY_APPARMOR_PARANOID_LOAD
106106
Disabling the check will speed up policy loads.
107107

108108
config SECURITY_APPARMOR_KUNIT_TEST
109-
bool "Build KUnit tests for policy_unpack.c" if !KUNIT_ALL_TESTS
110-
depends on KUNIT=y && SECURITY_APPARMOR
109+
tristate "Build KUnit tests for policy_unpack.c" if !KUNIT_ALL_TESTS
110+
depends on KUNIT && SECURITY_APPARMOR
111111
default KUNIT_ALL_TESTS
112112
help
113113
This builds the AppArmor KUnit tests.

security/apparmor/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ apparmor-y := apparmorfs.o audit.o capability.o task.o ipc.o lib.o match.o \
88
resource.o secid.o file.o policy_ns.o label.o mount.o net.o
99
apparmor-$(CONFIG_SECURITY_APPARMOR_HASH) += crypto.o
1010

11+
obj-$(CONFIG_SECURITY_APPARMOR_KUNIT_TEST) += apparmor_policy_unpack_test.o
12+
apparmor_policy_unpack_test-objs += policy_unpack_test.o
13+
1114
clean-files := capability_names.h rlim_names.h net_names.h
1215

1316
# Build a lower case string table of address family names

security/apparmor/include/policy_unpack.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,43 @@ enum {
4848
AAFS_LOADDATA_NDENTS /* count of entries */
4949
};
5050

51+
/*
52+
* The AppArmor interface treats data as a type byte followed by the
53+
* actual data. The interface has the notion of a named entry
54+
* which has a name (AA_NAME typecode followed by name string) followed by
55+
* the entries typecode and data. Named types allow for optional
56+
* elements and extensions to be added and tested for without breaking
57+
* backwards compatibility.
58+
*/
59+
60+
enum aa_code {
61+
AA_U8,
62+
AA_U16,
63+
AA_U32,
64+
AA_U64,
65+
AA_NAME, /* same as string except it is items name */
66+
AA_STRING,
67+
AA_BLOB,
68+
AA_STRUCT,
69+
AA_STRUCTEND,
70+
AA_LIST,
71+
AA_LISTEND,
72+
AA_ARRAY,
73+
AA_ARRAYEND,
74+
};
75+
76+
/*
77+
* aa_ext is the read of the buffer containing the serialized profile. The
78+
* data is copied into a kernel buffer in apparmorfs and then handed off to
79+
* the unpack routines.
80+
*/
81+
struct aa_ext {
82+
void *start;
83+
void *end;
84+
void *pos; /* pointer to current position in the buffer */
85+
u32 version;
86+
};
87+
5188
/*
5289
* struct aa_loaddata - buffer of policy raw_data set
5390
*
@@ -126,4 +163,17 @@ static inline void aa_put_loaddata(struct aa_loaddata *data)
126163
kref_put(&data->count, aa_loaddata_kref);
127164
}
128165

166+
#if IS_ENABLED(CONFIG_KUNIT)
167+
bool aa_inbounds(struct aa_ext *e, size_t size);
168+
size_t aa_unpack_u16_chunk(struct aa_ext *e, char **chunk);
169+
bool aa_unpack_X(struct aa_ext *e, enum aa_code code);
170+
bool aa_unpack_nameX(struct aa_ext *e, enum aa_code code, const char *name);
171+
bool aa_unpack_u32(struct aa_ext *e, u32 *data, const char *name);
172+
bool aa_unpack_u64(struct aa_ext *e, u64 *data, const char *name);
173+
size_t aa_unpack_array(struct aa_ext *e, const char *name);
174+
size_t aa_unpack_blob(struct aa_ext *e, char **blob, const char *name);
175+
int aa_unpack_str(struct aa_ext *e, const char **string, const char *name);
176+
int aa_unpack_strdup(struct aa_ext *e, char **string, const char *name);
177+
#endif
178+
129179
#endif /* __POLICY_INTERFACE_H */

0 commit comments

Comments
 (0)