Skip to content

Commit 58f89ce

Browse files
committed
apparmor: refactor code that alloc null profiles
Bother unconfined and learning profiles use the null profile as their base. Refactor so they are share a common base routine. This doesn't save much atm but will be important when the feature set of the parent is inherited. Signed-off-by: John Johansen <john.johansen@canonical.com>
1 parent 1f2bc06 commit 58f89ce

4 files changed

Lines changed: 43 additions & 28 deletions

File tree

security/apparmor/domain.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -681,8 +681,8 @@ static struct aa_label *profile_transition(struct aa_profile *profile,
681681
/* no exec permission - learning mode */
682682
struct aa_profile *new_profile = NULL;
683683

684-
new_profile = aa_new_null_profile(profile, false, name,
685-
GFP_KERNEL);
684+
new_profile = aa_new_learning_profile(profile, false, name,
685+
GFP_KERNEL);
686686
if (!new_profile) {
687687
error = -ENOMEM;
688688
info = "could not create null profile";
@@ -1009,8 +1009,8 @@ static struct aa_label *build_change_hat(struct aa_profile *profile,
10091009
if (!hat) {
10101010
error = -ENOENT;
10111011
if (COMPLAIN_MODE(profile)) {
1012-
hat = aa_new_null_profile(profile, true, name,
1013-
GFP_KERNEL);
1012+
hat = aa_new_learning_profile(profile, true, name,
1013+
GFP_KERNEL);
10141014
if (!hat) {
10151015
info = "failed null profile create";
10161016
error = -ENOMEM;
@@ -1361,8 +1361,8 @@ int aa_change_profile(const char *fqname, int flags)
13611361
!COMPLAIN_MODE(labels_profile(label)))
13621362
goto audit;
13631363
/* released below */
1364-
tprofile = aa_new_null_profile(labels_profile(label), false,
1365-
fqname, GFP_KERNEL);
1364+
tprofile = aa_new_learning_profile(labels_profile(label), false,
1365+
fqname, GFP_KERNEL);
13661366
if (!tprofile) {
13671367
info = "failed null profile create";
13681368
error = -ENOMEM;

security/apparmor/include/policy.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,10 @@ void aa_free_proxy_kref(struct kref *kref);
234234
struct aa_ruleset *aa_alloc_ruleset(gfp_t gfp);
235235
struct aa_profile *aa_alloc_profile(const char *name, struct aa_proxy *proxy,
236236
gfp_t gfp);
237-
struct aa_profile *aa_new_null_profile(struct aa_profile *parent, bool hat,
238-
const char *base, gfp_t gfp);
237+
struct aa_profile *aa_alloc_null(struct aa_profile *parent, const char *name,
238+
gfp_t gfp);
239+
struct aa_profile *aa_new_learning_profile(struct aa_profile *parent, bool hat,
240+
const char *base, gfp_t gfp);
239241
void aa_free_profile(struct aa_profile *profile);
240242
void aa_free_profile_kref(struct kref *kref);
241243
struct aa_profile *aa_find_child(struct aa_profile *parent, const char *name);

security/apparmor/policy.c

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -524,8 +524,36 @@ struct aa_profile *aa_fqlookupn_profile(struct aa_label *base,
524524
return profile;
525525
}
526526

527+
528+
struct aa_profile *aa_alloc_null(struct aa_profile *parent, const char *name,
529+
gfp_t gfp)
530+
{
531+
struct aa_profile *profile;
532+
struct aa_ruleset *rules;
533+
534+
profile = aa_alloc_profile(name, NULL, gfp);
535+
if (!profile)
536+
return NULL;
537+
538+
/* TODO: ideally we should inherit abi from parent */
539+
profile->label.flags |= FLAG_NULL;
540+
rules = list_first_entry(&profile->rules, typeof(*rules), list);
541+
rules->file.dfa = aa_get_dfa(nulldfa);
542+
rules->policy.dfa = aa_get_dfa(nulldfa);
543+
544+
if (parent) {
545+
profile->path_flags = parent->path_flags;
546+
547+
/* released on free_profile */
548+
rcu_assign_pointer(profile->parent, aa_get_profile(parent));
549+
profile->ns = aa_get_ns(parent->ns);
550+
}
551+
552+
return profile;
553+
}
554+
527555
/**
528-
* aa_new_null_profile - create or find a null-X learning profile
556+
* aa_new_learning_profile - create or find a null-X learning profile
529557
* @parent: profile that caused this profile to be created (NOT NULL)
530558
* @hat: true if the null- learning profile is a hat
531559
* @base: name to base the null profile off of
@@ -542,10 +570,9 @@ struct aa_profile *aa_fqlookupn_profile(struct aa_label *base,
542570
*
543571
* Returns: new refcounted profile else NULL on failure
544572
*/
545-
struct aa_profile *aa_new_null_profile(struct aa_profile *parent, bool hat,
546-
const char *base, gfp_t gfp)
573+
struct aa_profile *aa_new_learning_profile(struct aa_profile *parent, bool hat,
574+
const char *base, gfp_t gfp)
547575
{
548-
struct aa_ruleset *rules;
549576
struct aa_profile *p, *profile;
550577
const char *bname;
551578
char *name = NULL;
@@ -575,22 +602,12 @@ struct aa_profile *aa_new_null_profile(struct aa_profile *parent, bool hat,
575602
if (profile)
576603
goto out;
577604

578-
profile = aa_alloc_profile(name, NULL, gfp);
605+
profile = aa_alloc_null(parent, name, gfp);
579606
if (!profile)
580607
goto fail;
581-
582608
profile->mode = APPARMOR_COMPLAIN;
583-
profile->label.flags |= FLAG_NULL;
584609
if (hat)
585610
profile->label.flags |= FLAG_HAT;
586-
profile->path_flags = parent->path_flags;
587-
588-
/* released on free_profile */
589-
rcu_assign_pointer(profile->parent, aa_get_profile(parent));
590-
profile->ns = aa_get_ns(parent->ns);
591-
rules = list_first_entry(&profile->rules, typeof(*rules), list);
592-
rules->file.dfa = aa_get_dfa(nulldfa);
593-
rules->policy.dfa = aa_get_dfa(nulldfa);
594611

595612
mutex_lock_nested(&profile->ns->lock, profile->ns->level);
596613
p = __find_child(&parent->base.profiles, bname);

security/apparmor/policy_ns.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,14 @@ const char *aa_ns_name(struct aa_ns *curr, struct aa_ns *view, bool subns)
8383
static struct aa_profile *alloc_unconfined(const char *name)
8484
{
8585
struct aa_profile *profile;
86-
struct aa_ruleset *rules;
8786

88-
profile = aa_alloc_profile(name, NULL, GFP_KERNEL);
87+
profile = aa_alloc_null(NULL, name, GFP_KERNEL);
8988
if (!profile)
9089
return NULL;
9190

9291
profile->label.flags |= FLAG_IX_ON_NAME_ERROR |
9392
FLAG_IMMUTIBLE | FLAG_NS_COUNT | FLAG_UNCONFINED;
9493
profile->mode = APPARMOR_UNCONFINED;
95-
rules = list_first_entry(&profile->rules, typeof(*rules), list);
96-
rules->file.dfa = aa_get_dfa(nulldfa);
97-
rules->policy.dfa = aa_get_dfa(nulldfa);
9894

9995
return profile;
10096
}

0 commit comments

Comments
 (0)