20 #include <asm/unaligned.h>
21 #include <linux/ctype.h>
22 #include <linux/errno.h>
25 #include "include/audit.h"
73 if (sa->aad->iface.target) {
78 if (sa->aad->iface.pos)
102 aad.
iface.target =
new;
112 static bool inbounds(
struct aa_ext *e,
size_t size)
114 return (size <= e->
end - e->
pos);
124 static size_t unpack_u16_chunk(
struct aa_ext *e,
char **
chunk)
128 if (!inbounds(e,
sizeof(
u16)))
132 if (!inbounds(e, size))
144 if (*(
u8 *) e->
pos != code)
166 static bool unpack_nameX(
struct aa_ext *e,
enum aa_code code,
const char *name)
178 size_t size = unpack_u16_chunk(e, &tag);
180 if (name && (!size ||
strcmp(name, tag)))
188 if (unpack_X(e, code))
196 static bool unpack_u32(
struct aa_ext *e,
u32 *
data,
const char *name)
198 if (unpack_nameX(e,
AA_U32, name)) {
199 if (!inbounds(e,
sizeof(
u32)))
209 static bool unpack_u64(
struct aa_ext *e,
u64 *data,
const char *name)
211 if (unpack_nameX(e,
AA_U64, name)) {
212 if (!inbounds(e,
sizeof(
u64)))
222 static size_t unpack_array(
struct aa_ext *e,
const char *name)
224 if (unpack_nameX(e,
AA_ARRAY, name)) {
226 if (!inbounds(e,
sizeof(
u16)))
235 static size_t unpack_blob(
struct aa_ext *e,
char **
blob,
const char *name)
237 if (unpack_nameX(e,
AA_BLOB, name)) {
239 if (!inbounds(e,
sizeof(
u32)))
243 if (inbounds(e, (
size_t) size)) {
252 static int unpack_str(
struct aa_ext *e,
const char **
string,
const char *name)
259 size = unpack_u16_chunk(e, &src_str);
262 if (src_str[size - 1] != 0)
274 static int unpack_strdup(
struct aa_ext *e,
char **
string,
const char *name)
278 int res = unpack_str(e, &tmp, name);
300 static bool verify_accept(
struct aa_dfa *dfa,
int flags)
329 size = unpack_blob(e, &blob,
"aadfa");
335 size_t sz = blob - (
char *)e->
start;
349 if (!verify_accept(dfa, flags))
367 static bool unpack_trans_table(
struct aa_ext *e,
struct aa_profile *profile)
372 if (unpack_nameX(e,
AA_STRUCT,
"xtable")) {
375 size = unpack_array(e,
NULL);
379 profile->
file.trans.table = kzalloc(
sizeof(
char *) * size,
381 if (!profile->
file.trans.table)
385 for (i = 0; i <
size; i++) {
387 int c,
j, size2 = unpack_strdup(e, &str,
NULL);
399 for (c = j = 0; j < size2 - 2; j++) {
435 if (unpack_nameX(e,
AA_STRUCT,
"rlimits")) {
438 if (!unpack_u32(e, &tmp,
NULL))
442 size = unpack_array(e,
NULL);
445 for (i = 0; i <
size; i++) {
448 if (!unpack_u64(e, &tmp2,
NULL))
450 profile->
rlimits.limits[
a].rlim_max = tmp2;
473 const char *name =
NULL;
479 if (!unpack_nameX(e,
AA_STRUCT,
"profile"))
481 if (!unpack_str(e, &name,
NULL))
489 (
void) unpack_str(e, &profile->
rename,
"rename");
492 profile->
xmatch = unpack_dfa(e);
493 if (IS_ERR(profile->
xmatch)) {
494 error = PTR_ERR(profile->
xmatch);
500 if (!unpack_u32(e, &tmp,
NULL))
506 if (!unpack_nameX(e,
AA_STRUCT,
"flags"))
508 if (!unpack_u32(e, &tmp,
NULL))
512 if (!unpack_u32(e, &tmp,
NULL))
516 if (!unpack_u32(e, &tmp,
NULL))
525 if (unpack_u32(e, &profile->
path_flags,
"path_flags"))
531 if (!unpack_u32(e, &(profile->
caps.allow.cap[0]),
NULL))
533 if (!unpack_u32(e, &(profile->
caps.audit.cap[0]),
NULL))
535 if (!unpack_u32(e, &(profile->
caps.quiet.cap[0]),
NULL))
537 if (!unpack_u32(e, &tmpcap.
cap[0],
NULL))
540 if (unpack_nameX(e,
AA_STRUCT,
"caps64")) {
542 if (!unpack_u32(e, &(profile->
caps.allow.cap[1]),
NULL))
544 if (!unpack_u32(e, &(profile->
caps.audit.cap[1]),
NULL))
546 if (!unpack_u32(e, &(profile->
caps.quiet.cap[1]),
NULL))
548 if (!unpack_u32(e, &(tmpcap.
cap[1]),
NULL))
554 if (unpack_nameX(e,
AA_STRUCT,
"capsx")) {
556 if (!unpack_u32(e, &(profile->
caps.extended.cap[0]),
NULL))
558 if (!unpack_u32(e, &(profile->
caps.extended.cap[1]),
NULL))
564 if (!unpack_rlimits(e, profile))
567 if (unpack_nameX(e,
AA_STRUCT,
"policydb")) {
569 profile->
policy.dfa = unpack_dfa(e);
570 if (IS_ERR(profile->
policy.dfa)) {
571 error = PTR_ERR(profile->
policy.dfa);
575 if (!unpack_u32(e, &profile->
policy.start[0],
"start"))
590 profile->
file.dfa = unpack_dfa(e);
591 if (IS_ERR(profile->
file.dfa)) {
592 error = PTR_ERR(profile->
file.dfa);
597 if (!unpack_u32(e, &profile->
file.start,
"dfa_start"))
601 if (!unpack_trans_table(e, profile))
614 audit_iface(profile, name,
"failed to unpack profile", e, error);
615 aa_put_profile(profile);
617 return ERR_PTR(error);
627 static int verify_header(
struct aa_ext *e,
const char **
ns)
631 if (!unpack_u32(e, &e->
version,
"version")) {
632 audit_iface(
NULL,
NULL,
"invalid profile format", e, error);
638 audit_iface(
NULL,
NULL,
"unsupported interface version", e,
644 if (!unpack_str(e, ns,
"namespace"))
650 static bool verify_xindex(
int xindex,
int table_size)
655 if (xtype ==
AA_X_TABLE && index > table_size)
661 static bool verify_dfa_xindex(
struct aa_dfa *dfa,
int table_size)
679 static int verify_profile(
struct aa_profile *profile)
682 if (profile->
file.dfa &&
683 !verify_dfa_xindex(profile->
file.dfa,
684 profile->
file.trans.size)) {
685 audit_iface(profile,
NULL,
"Invalid named transition",
714 error = verify_header(&e, ns);
716 return ERR_PTR(error);
718 profile = unpack_profile(&e);
722 error = verify_profile(profile);
724 aa_put_profile(profile);
725 profile = ERR_PTR(error);