Skip to contentSkip to conversation

Another getcon() vs getcon_raw() issue in systemd

DISCUSSION Russell Coker 4 participants 8 messages Dec 16, 2016 – Dec 31, 2016 SELinux security module

lore
Share & tools
lore
#0 lore raw

http://selinux.tycho.nsa.narkive.com/cZUV3wmW/selinux-set-callback-problem

The above URL has the archive of the last time I raised an issue like this. Thanks to Nicolas Iooss for pointing out the solution.

type=USER_AVC msg=audit(1481891298.695:687055): pid=1 uid=0 auid=4294967295 ses=4294967295 subj=system_u:system_r:init_t:s0 msg='avc: denied { status } for auid=n/a uid=0 gid=0 cmdline="/lib/systemd/systemd-machined" scontext=system_u:system_r:systemd_machined_t:SystemLow tcontext=system_u:system_r:init_t:s0 tclass=system exe="/lib/systemd/systemd" sauid=0 hostname=? addr=? terminal=?'

Now in Debian/Unstable we have a similar issue with systemd 232-7 giving audit.log entries like the above.

# sed -e s/SystemLow/s0/ </var/log/audit/audit.log | audit2allow -l

#============= systemd_machined_t ==============

#!!!! This avc is allowed in the current policy
allow systemd_machined_t init_t:system status;

As an interesting twist it seems that I have some other problem with systemd- machined in that I have policy allowing the operation in question but it is rejected anyway. Audit2why says:

Was caused by:
Unknown - would be allowed by active policy Possible mismatch between this policy and the one under which
the audit message was generated.

Possible mismatch between current in-memory boolean settings
vs. permanent ones.

Any suggestions from systemd experts on where to look for a solution would be appreciated.

Signature · 2 lines
My Main Blog         http://etbe.coker.com.au/
My Documents Blog    http://doc.coker.com.au/
#1 lore raw
Quoted 15 lines · Russell Coker

http://selinux.tycho.nsa.narkive.com/cZUV3wmW/selinux-set-callback-problem

The above URL has the archive of the last time I raised an issue like this. Thanks to Nicolas Iooss for pointing out the solution.

type=USER_AVC msg=audit(1481891298.695:687055): pid=1 uid=0 auid=4294967295
ses=4294967295 subj=system_u:system_r:init_t:s0 msg='avc: denied { status }
for auid=n/a uid=0 gid=0 cmdline="/lib/systemd/systemd-machined" scontext=system_u:system_r:systemd_machined_t:SystemLow tcontext=system_u:system_r:init_t:s0 tclass=system exe="/lib/systemd/systemd"
sauid=0 hostname=? addr=? terminal=?'

Quoted 2 lines

Now in Debian/Unstable we have a similar issue with systemd 232-7 giving audit.log entries like the above.

Hi,
I quickly searched in systemd git tree where the source context comes from. My starting point was the call to sd_bus_query_sender_creds() in mac_selinux_generic_access_check() [1]. This function may call sd_bus_get_name_creds() [2] -> bus_get_name_creds_dbus1() [3] -> sd_bus_call_method("org.freedesktop.DBus",
..., "GetConnectionSELinuxSecurityContext", ..., "s", unique ? unique :
name) [4]. As the audit entry you sent comes from a DBus call between systemd-machined and systemd, this code path is likely to have been the origin of the source context.

The DBus method which is called is implemented in DBus function bus_driver_handle_get_connection_selinux_security_context() [5], which calls several functions until libselinux's getpeercon() [6]. As it is not getpeercon_raw(), this may give the translated label. In order to test this, could you please execute the following command and tell its output?

dbus-send --system --print-reply --dest=org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.GetConnectionSELinuxSecurityContext 'string:org.freedesktop.machine1'

Or, if you have not got dbus-send but have gdbus:

gdbus call --system --dest org.freedesktop.DBus --object-path /org/freedesktop/DBus --method org.freedesktop.DBus.GetConnectionSELinuxSecurityContext org.freedesktop.machine1

On my system, which does not use MLS (so I am currently not able to reproduce the issue), I get: array of bytes "system_u:system_r:systemd_machined_t". If on your system it gives a context with SystemLow, it will confirm the origin of the issue. Also, the DBus specification is quite strange about the method: "Returns the security context used by SELinux, in an unspecified format. If you know what this means, please contribute documentation via the D-Bus bug tracking system." [7]. It seems GetConnectionCredentials should be preferred. In order to see whether modifying systemd code would be enough to fix this, or whether it is a bug in the D-Bus interfaces, what does this command return on your system?

dbus-send --system --print-reply --dest=org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.GetConnectionCredentials 'string:org.freedesktop.machine1'

Cheers,
Nicolas

[1]
https://github.com/systemd/systemd/blob/v232/src/core/selinux-access.c#L206
[2]
https://github.com/systemd/systemd/blob/v232/src/libsystemd/sd-bus/bus-convenience.c#L542
[3]
https://github.com/systemd/systemd/blob/v232/src/libsystemd/sd-bus/bus-control.c#L929
[4]
https://github.com/systemd/systemd/blob/v232/src/libsystemd/sd-bus/bus-control.c#L865
[5]
https://cgit.freedesktop.org/dbus/dbus/tree/bus/driver.c?h=dbus-1.10#n1809
[6]
https://cgit.freedesktop.org/dbus/dbus/tree/bus/selinux.c?h=dbus-1.10#n724
[7]
https://cgit.freedesktop.org/dbus/dbus/tree/doc/dbus-specification.xml?h=dbus-1.10#n6200

#2 lore raw
Quoted 37 lines · AEDT Nicolas Iooss
Quoted 16 lines · Russell Coker

http://selinux.tycho.nsa.narkive.com/cZUV3wmW/selinux-set-callback-problem

The above URL has the archive of the last time I raised an issue like this.
Thanks to Nicolas Iooss for pointing out the solution.

type=USER_AVC msg=audit(1481891298.695:687055): pid=1 uid=0 auid=4294967295
ses=4294967295 subj=system_u:system_r:init_t:s0 msg='avc: denied { status }
for auid=n/a uid=0 gid=0 cmdline="/lib/systemd/systemd-machined" scontext=system_u:system_r:systemd_machined_t:SystemLow tcontext=system_u:system_r:init_t:s0 tclass=system exe="/lib/systemd/systemd"
sauid=0 hostname=? addr=? terminal=?'

Quoted 2 lines

Now in Debian/Unstable we have a similar issue with systemd 232-7 giving audit.log entries like the above.

Hi,
I quickly searched in systemd git tree where the source context comes from. My starting point was the call to sd_bus_query_sender_creds() in mac_selinux_generic_access_check() [1]. This function may call sd_bus_get_name_creds() [2] -> bus_get_name_creds_dbus1() [3] -> sd_bus_call_method("org.freedesktop.DBus",
..., "GetConnectionSELinuxSecurityContext", ..., "s", unique ? unique :
name) [4]. As the audit entry you sent comes from a DBus call between systemd-machined and systemd, this code path is likely to have been the origin of the source context.

The DBus method which is called is implemented in DBus function bus_driver_handle_get_connection_selinux_security_context() [5], which calls several functions until libselinux's getpeercon() [6]. As it is not getpeercon_raw(), this may give the translated label. In order to test this, could you please execute the following command and tell its output?

Quoted 4 lines

dbus-send --system --print-reply --dest=org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.GetConnectionSELinuxSecurityContext 'string:org.freedesktop.machine1'

Below is the output, looks like your analysis is correct.

method return time=1482748196.242707 sender=org.freedesktop.DBus -> destination=:1.1301 serial=3 reply_serial=2
array of bytes "system_u:system_r:systemd_machined_t:SystemLow"

Quoted 3 lines

DBus specification is quite strange about the method: "Returns the security context used by SELinux, in an unspecified format. If you know what this means, please contribute documentation via the D-Bus bug tracking system."

Strange, something for someone here to look into I guess.

Quoted 8 lines

[7]. It seems GetConnectionCredentials should be preferred. In order to see whether modifying systemd code would be enough to fix this, or whether it is a bug in the D-Bus interfaces, what does this command return on your system?

dbus-send --system --print-reply --dest=org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.GetConnectionCredentials 'string:org.freedesktop.machine1'

Here's the result, does it indicate that D-Bus or systemd should be changed?

method return time=1482748196.247091 sender=org.freedesktop.DBus -> destination=:1.1302 serial=3 reply_serial=2
array [

      dict entry(
         string "ProcessID"
         variant             uint32 1123
      )
      dict entry(
         string "UnixUserID"
         variant             uint32 0
      )
      dict entry(
         string "LinuxSecurityLabel"
         variant             array of bytes 
"system_u:system_r:systemd_machined_t:s0" + \0
      )

]

Thanks for your help. I hope to get this fixed before the next release of Debian is frozen...

Quoted 15 lines

[1]
https://github.com/systemd/systemd/blob/v232/src/core/selinux-access.c#L206
[2]
https://github.com/systemd/systemd/blob/v232/src/libsystemd/sd-bus/bus-conve
nience.c#L542 [3]
https://github.com/systemd/systemd/blob/v232/src/libsystemd/sd-bus/bus-contr
ol.c#L929 [4]
https://github.com/systemd/systemd/blob/v232/src/libsystemd/sd-bus/bus-contr
ol.c#L865 [5]
https://cgit.freedesktop.org/dbus/dbus/tree/bus/driver.c?h=dbus-1.10#n1809
[6]
https://cgit.freedesktop.org/dbus/dbus/tree/bus/selinux.c?h=dbus-1.10#n724
[7]
https://cgit.freedesktop.org/dbus/dbus/tree/doc/dbus-specification.xml?h=dbu
s-1.10#n6200

Signature · 2 lines
My Main Blog         http://etbe.coker.com.au/
My Documents Blog    http://doc.coker.com.au/
#3 lore raw
Quoted 74 lines · Russell Coker
Quoted 1 line · AEDT Nicolas Iooss

On Fri, Dec 16, 2016 at 1:33 PM, Russell Coker <[email protected]>

Quoted 1 line · wrote:
Quoted 1 line

http://selinux.tycho.nsa.narkive.com/cZUV3wmW/selinux-

set-callback-problem

Quoted 15 lines
Quoted 15 lines

The above URL has the archive of the last time I raised an issue like this.
Thanks to Nicolas Iooss for pointing out the solution.

type=USER_AVC msg=audit(1481891298.695:687055): pid=1 uid=0 auid=4294967295
ses=4294967295 subj=system_u:system_r:init_t:s0 msg='avc: denied { status }
for auid=n/a uid=0 gid=0 cmdline="/lib/systemd/systemd-machined" scontext=system_u:system_r:systemd_machined_t:SystemLow tcontext=system_u:system_r:init_t:s0 tclass=system exe="/lib/systemd/systemd"
sauid=0 hostname=? addr=? terminal=?'

Quoted 1 line
Quoted 1 line

Now in Debian/Unstable we have a similar issue with systemd 232-7

giving

Quoted 8 lines
Quoted 1 line

audit.log entries like the above.

[...]

dbus-send --system --print-reply --dest=org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.GetConnectionSELinuxSecurityContext 'string:org.freedesktop.machine1'

Below is the output, looks like your analysis is correct.

method return time=1482748196.242707 sender=org.freedesktop.DBus -> destination=:1.1301 serial=3 reply_serial=2
array of bytes "system_u:system_r:systemd_machined_t:SystemLow"

Quoted 1 line

DBus specification is quite strange about the method: "Returns the

security

Quoted 2 lines

context used by SELinux, in an unspecified format. If you know what this means, please contribute documentation via the D-Bus bug tracking

system."

Strange, something for someone here to look into I guess.

Quoted 1 line

[7]. It seems GetConnectionCredentials should be preferred. In order to

see

Quoted 7 lines

whether modifying systemd code would be enough to fix this, or whether it is a bug in the D-Bus interfaces, what does this command return on your system?

dbus-send --system --print-reply --dest=org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.GetConnectionCredentials 'string:org.freedesktop.machine1'

Here's the result, does it indicate that D-Bus or systemd should be changed?

method return time=1482748196.247091 sender=org.freedesktop.DBus -> destination=:1.1302 serial=3 reply_serial=2
array [

      dict entry(
         string "ProcessID"
         variant             uint32 1123
      )
      dict entry(
         string "UnixUserID"
         variant             uint32 0
      )
      dict entry(
         string "LinuxSecurityLabel"
         variant             array of bytes
"system_u:system_r:systemd_machined_t:s0" + \0
      )
Quoted 4 lines

]

Thanks for your help. I hope to get this fixed before the next release of Debian is frozen...

With the output you gave I searched in D-Bus source how the LinuxSecurityLabel was computed here. I discovered a lot of files in dbus/ directory which seem to duplicate things from bus/ I have described in my previous email. In dbus/dbus-sysdeps-unix.c there is a function which calls "getsockopt (client_fd, SOL_SOCKET, SO_PEERSEC, ...)" in order to get this "system_u:system_r:systemd_machined_t:s0" (add_linux_security_label_to_credentials,
https://cgit.freedesktop.org/dbus/dbus/tree/dbus/dbus-sysdeps-unix.c?h=dbus-1.10#n1760).
This function queries the same kernel API as libselinux's getpeercon_raw(), but without using libselinux (which is normal as it can also returns Smack or AppArmor labels). This is why the context is not translated.

The documentation of GetConnectionCredentials->LinuxSecurityLabel is way better than the one of GetConnectionSELinuxSecurityContext in D-Bus specification (
https://cgit.freedesktop.org/dbus/dbus/tree/doc/dbus-specification.xml?h=dbus-1.10#n6030)
so if I were to decide, I would rather migrate systemd's bus_get_name_creds_dbus1() to GetConnectionCredentials (in
https://github.com/systemd/systemd/blob/v232/src/libsystemd/sd-bus/bus-control.c#L865).
However this implies some non-trivial systemd code modifications and I do not know how systemd developers are willing to modify this part of their code.
An other option consists in making D-Bus use getpeercon_raw() in GetConnectionSELinuxSecurityContext (and documenting this in the D-Bus spec). I do not know what subtle side-effects such a change would have on a system (a quick search of users of this interface on searchcode.com gave some Android-related projects).

Cheers,
Nicolas

#4 lore raw
Quoted 12 lines · AEDT Nicolas Iooss

With the output you gave I searched in D-Bus source how the
LinuxSecurityLabel was computed here. I discovered a lot of files in dbus/
directory which seem to duplicate things from bus/ I have described in my
previous email. In dbus/dbus-sysdeps-unix.c there is a function which calls
"getsockopt (client_fd, SOL_SOCKET, SO_PEERSEC, ...)" in order to get this
"system_u:system_r:systemd_machined_t:s0"
(add_linux_security_label_to_credentials,
https://cgit.freedesktop.org/dbus/dbus/tree/dbus/dbus-sysdeps-unix.c?h=dbus-> 1.10#n1760). This function queries the same kernel API as libselinux's getpeercon_raw(), but without using libselinux (which is normal as it can
also returns Smack or AppArmor labels). This is why the context is not
translated.

Quoted 15 lines

The documentation of GetConnectionCredentials->LinuxSecurityLabel is way better than the one of GetConnectionSELinuxSecurityContext in D-Bus specification (
https://cgit.freedesktop.org/dbus/dbus/tree/doc/dbus-specification.xml?h=dbu
s-1.10#n6030) so if I were to decide, I would rather migrate systemd's bus_get_name_creds_dbus1() to GetConnectionCredentials (in
https://github.com/systemd/systemd/blob/v232/src/libsystemd/sd-bus/bus-contr
ol.c#L865). However this implies some non-trivial systemd code modifications and I do not know how systemd developers are willing to modify this part of their code.
An other option consists in making D-Bus use getpeercon_raw() in GetConnectionSELinuxSecurityContext (and documenting this in the D-Bus spec). I do not know what subtle side-effects such a change would have on a system (a quick search of users of this interface on searchcode.com gave some Android-related projects).

Thanks for your suggestions. I ran a VM of Fedora 24 with my policy to test this. Fedora 24 ran well with that and gave no such errors. Fedora has dbus 1.11.8 (development) and systemd 229 while Debian/Unstable has dbus 1.10.14 (stable) and systemd 332.

A did a diff on the Fedora 24 dbus source and the Debian dbus source and didn't find anything that seemed to be related. So I presume that it's more related to systemd - which doesn't necessarily imply that changing systemd is the correct way to fix it.

I'll try rawhide now.

Signature · 2 lines
My Main Blog         http://etbe.coker.com.au/
My Documents Blog    http://doc.coker.com.au/
#5 lore raw

On Dec 30, 2016 8:08 AM, "Russell Coker" <[email protected]> wrote:

Quoted 4 lines · AEDT Nicolas Iooss

With the output you gave I searched in D-Bus source how the LinuxSecurityLabel was computed here. I discovered a lot of files in dbus/ directory which seem to duplicate things from bus/ I have described in my previous email. In dbus/dbus-sysdeps-unix.c there is a function which

calls

Quoted 4 lines

"getsockopt (client_fd, SOL_SOCKET, SO_PEERSEC, ...)" in order to get this "system_u:system_r:systemd_machined_t:s0" (add_linux_security_label_to_credentials,
https://cgit.freedesktop.org/dbus/dbus/tree/dbus/dbus-

sysdeps-unix.c?h=dbus-> 1.10#n1760). This function queries the same kernel API as libselinux's

Quoted 8 lines

getpeercon_raw(), but without using libselinux (which is normal as it can also returns Smack or AppArmor labels). This is why the context is not translated.

The documentation of GetConnectionCredentials->LinuxSecurityLabel is way better than the one of GetConnectionSELinuxSecurityContext in D-Bus specification (
https://cgit.freedesktop.org/dbus/dbus/tree/doc/dbus-

specification.xml?h=dbu

Quoted 3 lines

s-1.10#n6030) so if I were to decide, I would rather migrate systemd's bus_get_name_creds_dbus1() to GetConnectionCredentials (in
https://github.com/systemd/systemd/blob/v232/src/

libsystemd/sd-bus/bus-contr

Quoted 1 line

ol.c#L865). However this implies some non-trivial systemd code

modifications

Quoted 1 line

and I do not know how systemd developers are willing to modify this part

of

Quoted 4 lines

their code.
An other option consists in making D-Bus use getpeercon_raw() in GetConnectionSELinuxSecurityContext (and documenting this in the D-Bus spec). I do not know what subtle side-effects such a change would have on

a

Quoted 2 lines

system (a quick search of users of this interface on searchcode.com gave some Android-related projects).

Thanks for your suggestions. I ran a VM of Fedora 24 with my policy to test this. Fedora 24 ran well with that and gave no such errors. Fedora has dbus
1.11.8 (development) and systemd 229 while Debian/Unstable has dbus 1.10.14 (stable) and systemd 332.

A did a diff on the Fedora 24 dbus source and the Debian dbus source and didn't find anything that seemed to be related. So I presume that it's more related to systemd - which doesn't necessarily imply that changing systemd is
the correct way to fix it.

I'll try rawhide now.

Fedora no longer runs mcstransd by default, so that may also be relevant. Dbusd should likely provide the raw context to clients so that they are free to use either the raw or translated interfaces without difficulty.

--
My Main Blog http://etbe.coker.com.au/
My Documents Blog http://doc.coker.com.au/

_______________________________________________
Selinux mailing list
[email protected]
To unsubscribe, send email to [email protected]. To get help, send an email containing "help" to [email protected].

#6 lore raw
Quoted 3 lines · AEDT Stephen Smalley

Fedora no longer runs mcstransd by default, so that may also be relevant. Dbusd should likely provide the raw context to clients so that they are free to use either the raw or translated interfaces without difficulty.

Good idea. This is the last important issue for SE Linux in Debian which has to be solved in a week or 2 if we are going to make the release. A worst-case scenario would be to just get mcstransd get kicked out of testing before the release. I don't think it's getting used much anyway and I'm sure that the people who do use it think that being able to login is more important. :-#

Signature · 2 lines
My Main Blog         http://etbe.coker.com.au/
My Documents Blog    http://doc.coker.com.au/