Skip to contentSkip to conversation

selinux: enable genfscon labeling for securityfs

PATCH Superseded Christian Göttsche 3 participants 9 messages Sep 15, 2021 – Sep 28, 2021 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selinux: enable genfscon labeling for securityfs

Add support for genfscon per-file labeling of securityfs files. This allows for separate labels and therby permissions for different files, e.g. /sys/kernel/security/integrity/ima/policy.

---
 security/selinux/hooks.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
security/selinux/hooks.c +2 -1
index 6517f221d52c..a18626424731 100644
@@ -760,7 +760,8 @@ static int selinux_set_mnt_opts(struct super_block *sb,
 	    !strcmp(sb->s_type->name, "tracefs") ||
 	    !strcmp(sb->s_type->name, "binder") ||
 	    !strcmp(sb->s_type->name, "bpf") ||
-	    !strcmp(sb->s_type->name, "pstore"))
+	    !strcmp(sb->s_type->name, "pstore") ||
+	    !strcmp(sb->s_type->name, "securityfs"))
 		sbsec->flags |= SE_SBGENFS;
 
 	if (!strcmp(sb->s_type->name, "sysfs") ||
Signature · 1 line
2.33.0
maintainer
#1 lore raw
Quoted 9 lines · Christian Göttsche

Add support for genfscon per-file labeling of securityfs files. This allows for separate labels and therby permissions for different files, e.g. /sys/kernel/security/integrity/ima/policy.

---
 security/selinux/hooks.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Hi Christian,

It would be nice if you could add some additional notes on how this was tested to the description above.

Quoted 16 lines
security/selinux/hooks.c +2 -2
index 6517f221d52c..a18626424731 100644
@@ -760,7 +760,8 @@ static int selinux_set_mnt_opts(struct super_block *sb,
            !strcmp(sb->s_type->name, "tracefs") ||
            !strcmp(sb->s_type->name, "binder") ||
            !strcmp(sb->s_type->name, "bpf") ||
-           !strcmp(sb->s_type->name, "pstore"))
+           !strcmp(sb->s_type->name, "pstore") ||
+           !strcmp(sb->s_type->name, "securityfs"))
                sbsec->flags |= SE_SBGENFS;
 
        if (!strcmp(sb->s_type->name, "sysfs") ||
--

2.33.0

Signature · 2 lines
paul moore
www.paul-moore.com
#2 lore raw
Quoted 31 lines · Paul Moore
Quoted 9 lines · Christian Göttsche

Add support for genfscon per-file labeling of securityfs files. This allows for separate labels and therby permissions for different files, e.g. /sys/kernel/security/integrity/ima/policy.

---
 security/selinux/hooks.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Hi Christian,

It would be nice if you could add some additional notes on how this was tested to the description above.

Quoted 13 lines
security/selinux/hooks.c +2 -1
index 6517f221d52c..a18626424731 100644
@@ -760,7 +760,8 @@ static int selinux_set_mnt_opts(struct super_block *sb,
            !strcmp(sb->s_type->name, "tracefs") ||
            !strcmp(sb->s_type->name, "binder") ||
            !strcmp(sb->s_type->name, "bpf") ||
-           !strcmp(sb->s_type->name, "pstore"))
+           !strcmp(sb->s_type->name, "pstore") ||
+           !strcmp(sb->s_type->name, "securityfs"))
                sbsec->flags |= SE_SBGENFS;
 
Quoted 7 lines
Quoted 3 lines
security/selinux/hooks.c +0 -1
        if (!strcmp(sb->s_type->name, "sysfs") ||
--

2.33.0

--
paul moore
www.paul-moore.com

Something like:

Add support for genfscon per-file labeling of securityfs files. This allows for separate labels and thereby access control for different files. For example a genfscon statement
genfscon securityfs /integrity/ima/policy
system_u:object_r:ima_policy_t:s0
will set a specific label to the IMA policy file and thus allow to
control the ability
to set the IMA policy.
Setting labels directly, e.g. via chcon(1) or setfiles(8), is
still not supported.

?

maintainer
#3 lore raw
Quoted 53 lines · Christian Göttsche
Quoted 31 lines · Paul Moore
Quoted 9 lines · Christian Göttsche

Add support for genfscon per-file labeling of securityfs files. This allows for separate labels and therby permissions for different files, e.g. /sys/kernel/security/integrity/ima/policy.

---
 security/selinux/hooks.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Hi Christian,

It would be nice if you could add some additional notes on how this was tested to the description above.

Quoted 13 lines
security/selinux/hooks.c +2 -1
index 6517f221d52c..a18626424731 100644
@@ -760,7 +760,8 @@ static int selinux_set_mnt_opts(struct super_block *sb,
            !strcmp(sb->s_type->name, "tracefs") ||
            !strcmp(sb->s_type->name, "binder") ||
            !strcmp(sb->s_type->name, "bpf") ||
-           !strcmp(sb->s_type->name, "pstore"))
+           !strcmp(sb->s_type->name, "pstore") ||
+           !strcmp(sb->s_type->name, "securityfs"))
                sbsec->flags |= SE_SBGENFS;
 
Quoted 7 lines
Quoted 3 lines
security/selinux/hooks.c +0 -1
        if (!strcmp(sb->s_type->name, "sysfs") ||
--

2.33.0

--
paul moore
www.paul-moore.com

Something like:

Add support for genfscon per-file labeling of securityfs files. This allows for separate labels and thereby access control for different files. For example a genfscon statement
genfscon securityfs /integrity/ima/policy
system_u:object_r:ima_policy_t:s0
will set a specific label to the IMA policy file and thus allow to
control the ability
to set the IMA policy.
Setting labels directly, e.g. via chcon(1) or setfiles(8), is
still not supported.

Quoted 1 line

?

That's a much better description of the functionality, especially for those who may not be very familiar with SELinux, thank you. However I was hoping to also hear some confirmation that you have tested this and it worked without problem?

Signature · 2 lines
paul moore
www.paul-moore.com
#4 lore raw
Quoted 58 lines · Paul Moore
Quoted 53 lines · Christian Göttsche
Quoted 31 lines · Paul Moore
Quoted 9 lines · Christian Göttsche

Add support for genfscon per-file labeling of securityfs files. This allows for separate labels and therby permissions for different files, e.g. /sys/kernel/security/integrity/ima/policy.

---
 security/selinux/hooks.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Hi Christian,

It would be nice if you could add some additional notes on how this was tested to the description above.

Quoted 13 lines
security/selinux/hooks.c +2 -1
index 6517f221d52c..a18626424731 100644
@@ -760,7 +760,8 @@ static int selinux_set_mnt_opts(struct super_block *sb,
            !strcmp(sb->s_type->name, "tracefs") ||
            !strcmp(sb->s_type->name, "binder") ||
            !strcmp(sb->s_type->name, "bpf") ||
-           !strcmp(sb->s_type->name, "pstore"))
+           !strcmp(sb->s_type->name, "pstore") ||
+           !strcmp(sb->s_type->name, "securityfs"))
                sbsec->flags |= SE_SBGENFS;
 
Quoted 7 lines
Quoted 3 lines
security/selinux/hooks.c +0 -1
        if (!strcmp(sb->s_type->name, "sysfs") ||
--

2.33.0

--
paul moore
www.paul-moore.com

Something like:

Add support for genfscon per-file labeling of securityfs files. This allows for separate labels and thereby access control for different files. For example a genfscon statement
genfscon securityfs /integrity/ima/policy
system_u:object_r:ima_policy_t:s0
will set a specific label to the IMA policy file and thus allow to
control the ability
to set the IMA policy.
Setting labels directly, e.g. via chcon(1) or setfiles(8), is
still not supported.

Quoted 1 line

?

Quoted 8 lines

That's a much better description of the functionality, especially for those who may not be very familiar with SELinux, thank you. However I was hoping to also hear some confirmation that you have tested this and it worked without problem?

--
paul moore
www.paul-moore.com

With the following genfscon statements

genfscon securityfs / system_u:object_r:securityfs_t:s0 genfscon securityfs /integrity/ima system_u:object_r:ima_t:s0 genfscon securityfs /integrity/ima/policy system_u:object_r:ima_policy_t:s0 genfscon securityfs /lockdown system_u:object_r:lockdown_t:s0

the labels are

    $ ls -laZ /sys/kernel/security/ /sys/kernel/security/integrity/ima/
    /sys/kernel/security/:
    total 0
    drwxr-xr-x.  3 root root system_u:object_r:securityfs_t:s0 0 Sep 17 09:38 .
    drwxr-xr-x. 15 root root system_u:object_r:sysfs_t:s0      0 Sep 17 09:38 ..
    lr--r--r--.  1 root root system_u:object_r:securityfs_t:s0 0 Sep
17 09:38 evm -> integrity/evm/evm
    lr--r--r--.  1 root root system_u:object_r:securityfs_t:s0 0 Sep
17 09:38 ima -> integrity/ima
    drwxr-xr-x.  4 root root system_u:object_r:securityfs_t:s0 0 Sep
17 09:38 integrity
    -rw-r--r--.  1 root root system_u:object_r:lockdown_t:s0   0 Sep
17 09:38 lockdown
    -r--r--r--.  1 root root system_u:object_r:securityfs_t:s0 0 Sep
17 09:38 lsm
    /sys/kernel/security/integrity/ima/:
    total 0
    drwxr-xr-x. 2 root root system_u:object_r:ima_t:s0        0 Sep 17 09:38 .
    rwxr-xr-x. 4 root root system_u:object_r:securityfs_t:s0 0 Sep 17 09:38 ..
    -r--r-----. 1 root root system_u:object_r:ima_t:s0        0 Sep 17
09:38 ascii_runtime_measurements
    -r--r-----. 1 root root system_u:object_r:ima_t:s0        0 Sep 17
09:38 binary_runtime_measurements
    --w-------. 1 root root system_u:object_r:ima_policy_t:s0 0 Sep 17
09:38 policy
    -r--r-----. 1 root root system_u:object_r:ima_t:s0        0 Sep 17
09:38 runtime_measurements_count
    -r--r-----. 1 root root system_u:object_r:ima_t:s0        0 Sep 17

09:38 violations

and audit logs are generated like

    Sep 17 09:38:28 debianBullseye audit[331]: AVC avc:  denied  {
read } for  pid=331 comm="systemd-tmpfile" name="ima" dev="securityfs"
ino=167 scontext=system_u:system_r:systemd_tmpfiles_t:s0
tcontext=system_u:object_r:ima_t:s0 tclass=dir permissive=1
    Sep 17 09:38:28 debianBullseye audit[331]: AVC avc:  denied  {

open } for pid=331 comm="systemd-tmpfile" path="/sys/kernel/security/integrity/ima" dev="securityfs" ino=167 scontext=system_u:system_r:systemd_tmpfiles_t:s0 tcontext=system_u:object_r:ima_t:s0 tclass=dir permissive=1

    Sep 17 09:38:28 debianBullseye audit[331]: AVC avc:  denied  {

getattr } for pid=331 comm="systemd-tmpfile" path="/sys/kernel/security/integrity/ima/policy" dev="securityfs" ino=173 scontext=system_u:system_r:systemd_tmpfiles_t:s0 tcontext=system_u:object_r:ima_policy_t:s0 tclass=file permissive=1

chcon(1) fails like

    $ chcon -v -t securityfs_t /sys/kernel/security/integrity/ima/policy
    changing security context of '/sys/kernel/security/integrity/ima/policy'
    chcon: failed to change context of
'/sys/kernel/security/integrity/ima/policy' to
'system_u:object_r:securityfs_t:s0': Operation not supported

A file context definition of

/sys/kernel/security/integrity/ima/policy --
gen_context(system_u:object_r:securityfs_t,s0)

results in

    $ matchpathcon -V /sys/kernel/security/integrity/ima/policy
    Deprecated, use selabel_lookup
    /sys/kernel/security/integrity/ima/policy has context
system_u:object_r:ima_policy_t:s0, should be
system_u:object_r:securityfs_t:s0
    $ restorecon -vRF /sys/kernel/security/
    # nothing #

Are there any other use cases or edge cases I need to test.

One thing I noticed is that a genfscon statements take affect only after a reboot, not after remounting the securityfs via

mount -v -o remount -t securityfs /sys/kernel/security

Is this expected?

maintainer
#5 lore raw
Quoted 150 lines · Christian Göttsche
Quoted 58 lines · Paul Moore
Quoted 53 lines · Christian Göttsche
Quoted 31 lines · Paul Moore
Quoted 9 lines · Christian Göttsche

Add support for genfscon per-file labeling of securityfs files. This allows for separate labels and therby permissions for different files, e.g. /sys/kernel/security/integrity/ima/policy.

---
 security/selinux/hooks.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Hi Christian,

It would be nice if you could add some additional notes on how this was tested to the description above.

Quoted 13 lines
security/selinux/hooks.c +2 -1
index 6517f221d52c..a18626424731 100644
@@ -760,7 +760,8 @@ static int selinux_set_mnt_opts(struct super_block *sb,
            !strcmp(sb->s_type->name, "tracefs") ||
            !strcmp(sb->s_type->name, "binder") ||
            !strcmp(sb->s_type->name, "bpf") ||
-           !strcmp(sb->s_type->name, "pstore"))
+           !strcmp(sb->s_type->name, "pstore") ||
+           !strcmp(sb->s_type->name, "securityfs"))
                sbsec->flags |= SE_SBGENFS;
 
Quoted 7 lines
Quoted 3 lines
security/selinux/hooks.c +0 -1
        if (!strcmp(sb->s_type->name, "sysfs") ||
--

2.33.0

--
paul moore
www.paul-moore.com

Something like:

Add support for genfscon per-file labeling of securityfs files. This allows for separate labels and thereby access control for different files. For example a genfscon statement
genfscon securityfs /integrity/ima/policy
system_u:object_r:ima_policy_t:s0
will set a specific label to the IMA policy file and thus allow to
control the ability
to set the IMA policy.
Setting labels directly, e.g. via chcon(1) or setfiles(8), is
still not supported.

Quoted 1 line

?

Quoted 8 lines

That's a much better description of the functionality, especially for those who may not be very familiar with SELinux, thank you. However I was hoping to also hear some confirmation that you have tested this and it worked without problem?

--
paul moore
www.paul-moore.com

With the following genfscon statements

genfscon securityfs / system_u:object_r:securityfs_t:s0 genfscon securityfs /integrity/ima system_u:object_r:ima_t:s0 genfscon securityfs /integrity/ima/policy system_u:object_r:ima_policy_t:s0 genfscon securityfs /lockdown system_u:object_r:lockdown_t:s0

the labels are

    $ ls -laZ /sys/kernel/security/ /sys/kernel/security/integrity/ima/
    /sys/kernel/security/:
    total 0
    drwxr-xr-x.  3 root root system_u:object_r:securityfs_t:s0 0 Sep 17 09:38 .
    drwxr-xr-x. 15 root root system_u:object_r:sysfs_t:s0      0 Sep 17 09:38 ..
    lr--r--r--.  1 root root system_u:object_r:securityfs_t:s0 0 Sep
17 09:38 evm -> integrity/evm/evm
    lr--r--r--.  1 root root system_u:object_r:securityfs_t:s0 0 Sep
17 09:38 ima -> integrity/ima
    drwxr-xr-x.  4 root root system_u:object_r:securityfs_t:s0 0 Sep
17 09:38 integrity
    -rw-r--r--.  1 root root system_u:object_r:lockdown_t:s0   0 Sep
17 09:38 lockdown
    -r--r--r--.  1 root root system_u:object_r:securityfs_t:s0 0 Sep
17 09:38 lsm
    /sys/kernel/security/integrity/ima/:
    total 0
    drwxr-xr-x. 2 root root system_u:object_r:ima_t:s0        0 Sep 17 09:38 .
    rwxr-xr-x. 4 root root system_u:object_r:securityfs_t:s0 0 Sep 17 09:38 ..
    -r--r-----. 1 root root system_u:object_r:ima_t:s0        0 Sep 17
09:38 ascii_runtime_measurements
    -r--r-----. 1 root root system_u:object_r:ima_t:s0        0 Sep 17
09:38 binary_runtime_measurements
    --w-------. 1 root root system_u:object_r:ima_policy_t:s0 0 Sep 17
09:38 policy
    -r--r-----. 1 root root system_u:object_r:ima_t:s0        0 Sep 17
09:38 runtime_measurements_count
    -r--r-----. 1 root root system_u:object_r:ima_t:s0        0 Sep 17

09:38 violations

and audit logs are generated like

    Sep 17 09:38:28 debianBullseye audit[331]: AVC avc:  denied  {
read } for  pid=331 comm="systemd-tmpfile" name="ima" dev="securityfs"
ino=167 scontext=system_u:system_r:systemd_tmpfiles_t:s0
tcontext=system_u:object_r:ima_t:s0 tclass=dir permissive=1
    Sep 17 09:38:28 debianBullseye audit[331]: AVC avc:  denied  {

open } for pid=331 comm="systemd-tmpfile" path="/sys/kernel/security/integrity/ima" dev="securityfs" ino=167 scontext=system_u:system_r:systemd_tmpfiles_t:s0 tcontext=system_u:object_r:ima_t:s0 tclass=dir permissive=1

    Sep 17 09:38:28 debianBullseye audit[331]: AVC avc:  denied  {

getattr } for pid=331 comm="systemd-tmpfile" path="/sys/kernel/security/integrity/ima/policy" dev="securityfs" ino=173 scontext=system_u:system_r:systemd_tmpfiles_t:s0 tcontext=system_u:object_r:ima_policy_t:s0 tclass=file permissive=1

chcon(1) fails like

    $ chcon -v -t securityfs_t /sys/kernel/security/integrity/ima/policy
    changing security context of '/sys/kernel/security/integrity/ima/policy'
    chcon: failed to change context of
'/sys/kernel/security/integrity/ima/policy' to
'system_u:object_r:securityfs_t:s0': Operation not supported

A file context definition of

/sys/kernel/security/integrity/ima/policy --
gen_context(system_u:object_r:securityfs_t,s0)

results in

    $ matchpathcon -V /sys/kernel/security/integrity/ima/policy
    Deprecated, use selabel_lookup
    /sys/kernel/security/integrity/ima/policy has context
system_u:object_r:ima_policy_t:s0, should be
system_u:object_r:securityfs_t:s0
    $ restorecon -vRF /sys/kernel/security/
    # nothing #
Quoted 8 lines

Are there any other use cases or edge cases I need to test.

One thing I noticed is that a genfscon statements take affect only after a reboot, not after remounting the securityfs via

mount -v -o remount -t securityfs /sys/kernel/security

Is this expected?

genfscon is only consulted when a dentry is first instantiated for an inode, typically on first lookup or creation.
So you'd have to unmount all securityfs mounts to force eviction of the existing dentries/inodes and get them re-created.
And likely can't be done if the kernel does an internal mount for its own usage (but doesn't look like that is the case for securityfs, just selinuxfs).

maintainer
#6 lore raw
In reply to Paul Moore
Quoted 55 lines · Paul Moore
Quoted 53 lines · Christian Göttsche
Quoted 31 lines · Paul Moore
Quoted 9 lines · Christian Göttsche

Add support for genfscon per-file labeling of securityfs files. This allows for separate labels and therby permissions for different files, e.g. /sys/kernel/security/integrity/ima/policy.

---
 security/selinux/hooks.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Hi Christian,

It would be nice if you could add some additional notes on how this was tested to the description above.

Quoted 13 lines
security/selinux/hooks.c +2 -1
index 6517f221d52c..a18626424731 100644
@@ -760,7 +760,8 @@ static int selinux_set_mnt_opts(struct super_block *sb,
            !strcmp(sb->s_type->name, "tracefs") ||
            !strcmp(sb->s_type->name, "binder") ||
            !strcmp(sb->s_type->name, "bpf") ||
-           !strcmp(sb->s_type->name, "pstore"))
+           !strcmp(sb->s_type->name, "pstore") ||
+           !strcmp(sb->s_type->name, "securityfs"))
                sbsec->flags |= SE_SBGENFS;
 
Quoted 7 lines
Quoted 3 lines
security/selinux/hooks.c +0 -1
        if (!strcmp(sb->s_type->name, "sysfs") ||
--

2.33.0

--
paul moore
www.paul-moore.com

Something like:

Add support for genfscon per-file labeling of securityfs files. This allows for separate labels and thereby access control for different files. For example a genfscon statement
genfscon securityfs /integrity/ima/policy
system_u:object_r:ima_policy_t:s0
will set a specific label to the IMA policy file and thus allow to
control the ability
to set the IMA policy.
Setting labels directly, e.g. via chcon(1) or setfiles(8), is
still not supported.

Quoted 6 lines
Quoted 1 line

?

That's a much better description of the functionality, especially for those who may not be very familiar with SELinux, thank you. However I was hoping to also hear some confirmation that you have tested this and it worked without problem?

Hi Christian, my apologies on the delay, I was distracted by a few other SELinux issues. Thank you for sending out your testing notes in the meantime.

Are you okay if I replace the original commit description with your more verbose version? If not, could you resend the patch with the update commit description?

Thanks.

Signature · 2 lines
paul moore
www.paul-moore.com
#7 lore raw

PATCH v2 1/1 selinux: enable genfscon labeling for securityfs

In reply to Christian Göttsche

Add support for genfscon per-file labeling of securityfs files. This allows for separate labels and thereby access control for different files. For example a genfscon statement

genfscon securityfs /integrity/ima/policy system_u:object_r:ima_policy_t:s0

will set a private label to the IMA policy file and thus allow to control the ability to set the IMA policy.
Setting labels directly with setxattr(2), e.g. by chcon(1) or setfiles(8), is still not supported.

---
v2:
- improve commit description

 security/selinux/hooks.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
security/selinux/hooks.c +2 -1
index 012e8504ed9e..549f631e9832 100644
@@ -760,7 +760,8 @@ static int selinux_set_mnt_opts(struct super_block *sb,
 	    !strcmp(sb->s_type->name, "tracefs") ||
 	    !strcmp(sb->s_type->name, "binder") ||
 	    !strcmp(sb->s_type->name, "bpf") ||
-	    !strcmp(sb->s_type->name, "pstore"))
+	    !strcmp(sb->s_type->name, "pstore") ||
+	    !strcmp(sb->s_type->name, "securityfs"))
 		sbsec->flags |= SE_SBGENFS;
 
 	if (!strcmp(sb->s_type->name, "sysfs") ||
Signature · 1 line
2.33.0
maintainer Applied
#8 lore raw
Quoted 12 lines · Christian Göttsche

Add support for genfscon per-file labeling of securityfs files. This allows for separate labels and thereby access control for different files. For example a genfscon statement

genfscon securityfs /integrity/ima/policy system_u:object_r:ima_policy_t:s0

will set a private label to the IMA policy file and thus allow to control the ability to set the IMA policy.
Setting labels directly with setxattr(2), e.g. by chcon(1) or setfiles(8), is still not supported.

Quoted 8 lines

---
v2:
- improve commit description

 security/selinux/hooks.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Merged into selinux/next, thanks Christian!

Quoted 16 lines
security/selinux/hooks.c +2 -2
index 012e8504ed9e..549f631e9832 100644
@@ -760,7 +760,8 @@ static int selinux_set_mnt_opts(struct super_block *sb,
            !strcmp(sb->s_type->name, "tracefs") ||
            !strcmp(sb->s_type->name, "binder") ||
            !strcmp(sb->s_type->name, "bpf") ||
-           !strcmp(sb->s_type->name, "pstore"))
+           !strcmp(sb->s_type->name, "pstore") ||
+           !strcmp(sb->s_type->name, "securityfs"))
                sbsec->flags |= SE_SBGENFS;
 
        if (!strcmp(sb->s_type->name, "sysfs") ||
--

2.33.0

Signature · 2 lines
paul moore
www.paul-moore.com