Skip to contentSkip to conversation

Change default error handling in libselinux matchpathcon to use syslog instead of stderr.

PATCH Daniel J Walsh 4 participants 10 messages Sep 18, 2007 – Sep 19, 2007 SELinux security module

lore
Share & tools
lore
#0 lore raw

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Current behavior causes problems with network applications.

https://bugzilla.redhat.com/show_bug.cgi?id=273081

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

iD8DBQFG7/AnrlYvE4MpobMRAksuAJ4uqcUDsSlB9uEpXePUEje39kNAywCeJbDE sZ99tA7s4PgSxD/RnS4MHdg=
=g4kG
-----END PGP SIGNATURE-----

src/matchpathcon.c +4 -1
diff --exclude-from=exclude -N -u -r nsalibselinux/src/matchpathcon.c libselinux-2.0.33/src/matchpathcon.c
@@ -2,6 +2,7 @@
 #include <string.h>
 #include <errno.h>
 #include <stdio.h>
+#include <syslog.h>
 #include "selinux_internal.h"
 #include "label_internal.h"
 #include "callbacks.h"
@@ -55,10 +56,12 @@
 #endif
     default_printf(const char *fmt, ...)
 {
+	char buf[BUFSIZ];
 	va_list ap;
 	va_start(ap, fmt);
-	vfprintf(stderr, fmt, ap);
+	vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
 	va_end(ap);
+	syslog(LOG_ERR, buf, strlen(buf));
 }
 
 void
#1 lore raw
Quoted 6 lines · Daniel J Walsh

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Current behavior causes problems with network applications.

https://bugzilla.redhat.com/show_bug.cgi?id=273081

I'm not sure about changing current behavior, as that is effectively an "interface" change. Or if it isn't, then there are other such changes I would make too (e.g. changing the default for SETLOCALDEFS in libselinux comes to mind).

What if they used the newer interfaces introduced by Eamon (selabel*)? setfiles has already been rewritten by him to use those interfaces, and thus matchpathcon is a legacy interface at this point. Does the same problem exist there?

I suppose that doesn't help you though with RHEL 5, as it would only have the older interfaces.

Changing the interface from one arbitrary default (stderr) to another arbitrary default (syslog) doesn't seem like an improvement, although it would fix this particular issue I suppose. We would need to at least make sure that setfiles -c continues to report the error messages as expected.

One comment below on the code itself.

Quoted 22 lines

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

iD8DBQFG7/AnrlYvE4MpobMRAksuAJ4uqcUDsSlB9uEpXePUEje39kNAywCeJbDE
sZ99tA7s4PgSxD/RnS4MHdg=
=g4kG
-----END PGP SIGNATURE-----
plain text document attachment (diff)
diff --exclude-from=exclude -N -u -r nsalibselinux/src/matchpathcon.c libselinux-2.0.33/src/matchpathcon.c

src/matchpathcon.c +1 -0
@@ -2,6 +2,7 @@
 #include <string.h>
 #include <errno.h>
 #include <stdio.h>
+#include <syslog.h>
 #include "selinux_internal.h"
 #include "label_internal.h"
 #include "callbacks.h"
@@ -55,10 +56,12 @@
 #endif

src/matchpathcon.c line 65

Quoted 10 lines
src/matchpathcon.c +3 -1
     default_printf(const char *fmt, ...)
 {
+	char buf[BUFSIZ];
 	va_list ap;
 	va_start(ap, fmt);
-	vfprintf(stderr, fmt, ap);
+	vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
 	va_end(ap);
+	syslog(LOG_ERR, buf, strlen(buf));
 }

Could you use vsyslog() instead to avoid the need for a fixed size buffer?

Quoted 2 lines
src/matchpathcon.c +0 -0
 void
Signature · 8 lines
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
#3 lore raw
In reply to Daniel J Walsh
Quoted 7 lines · Daniel J Walsh

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Current behavior causes problems with network applications.

https://bugzilla.redhat.com/show_bug.cgi?id=273081

Wait, what?

I don't think it is an improvement to call the command line matchpathcon applications and have the logs go somewhere not visible by the user. IMO we should take the same approach libsemanage and libsepol did where the logging callbacks can be overridden by specific users of the library but for the command line applications the output still goes to stderr.

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#4 lore raw
Quoted 9 lines · Joshua Brindle
Quoted 7 lines · Daniel J Walsh

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Current behavior causes problems with network applications.

https://bugzilla.redhat.com/show_bug.cgi?id=273081

Quoted 7 lines

Wait, what?

I don't think it is an improvement to call the command line matchpathcon applications and have the logs go somewhere not visible by the user. IMO we should take the same approach libsemanage and libsepol did where the logging callbacks can be overridden by specific users of the library but for the command line applications the output still goes to stderr.

You already can override the callback (set_matchpathcon_printf), but the claim in the bug report is that isn't usable from a library function that calls matchpathcon because it doesn't know whether it has already been set by the application.

Signature · 8 lines
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
#5 lore raw
Quoted 14 lines · Stephen Smalley
Quoted 12 lines · Joshua Brindle
Quoted 8 lines · Daniel J Walsh

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Current behavior causes problems with network applications.

https://bugzilla.redhat.com/show_bug.cgi?id=273081

Wait, what?

Quoted 8 lines
Quoted 2 lines

I don't think it is an improvement to call the command line matchpathcon applications and have the logs go somewhere not visible by the user. IMO we should take the same approach libsemanage and libsepol did where the logging callbacks can be overridden by specific users of the library but for the command line applications the output still goes to stderr.

You already can override the callback (set_matchpathcon_printf), but the claim in the bug report is that isn't usable from a library function that calls matchpathcon because it doesn't know whether it has already been set by the application.

Hrm... so the alternative is to send it to syslog? I don't buy it. Can we just add an interface to see if the default callbacks have been overridden?

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#6 lore raw
Quoted 28 lines · Joshua Brindle
Quoted 21 lines · Stephen Smalley
Quoted 10 lines · Joshua Brindle
Quoted 8 lines · Daniel J Walsh

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Current behavior causes problems with network applications.

https://bugzilla.redhat.com/show_bug.cgi?id=273081

Quoted 8 lines

Wait, what?

I don't think it is an improvement to call the command line matchpathcon applications and have the logs go somewhere not visible by the user. IMO we should take the same approach libsemanage and libsepol did where the logging callbacks can be overridden by specific users of the library but for the command line applications the output still goes to stderr.

Quoted 5 lines

You already can override the callback (set_matchpathcon_printf), but the claim in the bug report is that isn't usable from a library function that calls matchpathcon because it doesn't know whether it has already been set by the application.

Quoted 3 lines

Hrm... so the alternative is to send it to syslog? I don't buy it. Can we just add an interface to see if the default callbacks have been overridden?

Yep. And it should be based on the newer interfaces. There is already a selinux_set_callback() and adding a selinux_get_callback() should be trivial. setfiles is already rewritten to use the new interfaces in trunk, and we should be moving everything off of matchpathcon and onto selabel_lookup.

For RHEL 5, I suppose there might be a matchpathcon-specific interface for getting the callback.

Signature · 8 lines
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
#2 lore raw
In reply to Stephen Smalley

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quoted 26 lines · Stephen Smalley

On Tue, 2007-09-18 at 11:35 -0400, Daniel J Walsh wrote:
Current behavior causes problems with network applications.

https://bugzilla.redhat.com/show_bug.cgi?id=273081

Quoted 20 lines

I'm not sure about changing current behavior, as that is effectively an "interface" change. Or if it isn't, then there are other such changes I would make too (e.g. changing the default for SETLOCALDEFS in libselinux comes to mind).

What if they used the newer interfaces introduced by Eamon (selabel*)? setfiles has already been rewritten by him to use those interfaces, and thus matchpathcon is a legacy interface at this point. Does the same problem exist there?

I suppose that doesn't help you though with RHEL 5, as it would only have the older interfaces.

Changing the interface from one arbitrary default (stderr) to another arbitrary default (syslog) doesn't seem like an improvement, although it would fix this particular issue I suppose. We would need to at least make sure that setfiles -c continues to report the error messages as expected.

One comment below on the code itself.

plain text document attachment (diff)
diff --exclude-from=exclude -N -u -r nsalibselinux/src/matchpathcon.c libselinux-2.0.33/src/matchpathcon.c
- --- nsalibselinux/src/matchpathcon.c 2007-08-10 11:58:34.000000000 -0400
+++ libselinux-2.0.33/src/matchpathcon.c 2007-09-18 11:28:07.000000000 -0400
@@ -2,6 +2,7 @@
#include <string.h>
#include <errno.h>
#include <stdio.h>
+#include <syslog.h>
#include "selinux_internal.h"
#include "label_internal.h"
#include "callbacks.h"
@@ -55,10 +56,12 @@
#endif
default_printf(const char *fmt, ...)
{
+ char buf[BUFSIZ];
va_list ap;
va_start(ap, fmt);
- - vfprintf(stderr, fmt, ap);
+ vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
va_end(ap);
+ syslog(LOG_ERR, buf, strlen(buf));
}

Quoted 2 lines

Could you use vsyslog() instead to avoid the need for a fixed size buffer?

void

Sure, I did not know that interface existed.

man syslog and
man 3 syslog are different.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

iD8DBQFG8CtXrlYvE4MpobMRArAnAJsGDG1/UQtq8O/3sOWsWg6mbs+OaACg11BH 44PynbOo4cSB9hcAq66yjiw=
=dPQV
-----END PGP SIGNATURE-----

src/matchpathcon.c +2 -1
diff --exclude-from=exclude -N -u -r nsalibselinux/src/matchpathcon.c libselinux-2.0.33/src/matchpathcon.c
@@ -2,6 +2,7 @@
 #include <string.h>
 #include <errno.h>
 #include <stdio.h>
+#include <syslog.h>
 #include "selinux_internal.h"
 #include "label_internal.h"
 #include "callbacks.h"
@@ -57,7 +58,7 @@
 {
 	va_list ap;
 	va_start(ap, fmt);
-	vfprintf(stderr, fmt, ap);
+	vsyslog(LOG_ERR, fmt, ap);
 	va_end(ap);
 }
 
#7 lore raw
In reply to Stephen Smalley

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quoted 31 lines · Stephen Smalley
Quoted 21 lines · Joshua Brindle
Quoted 20 lines · Stephen Smalley
Quoted 18 lines · Joshua Brindle
Quoted 8 lines · Daniel J Walsh

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Current behavior causes problems with network applications.

https://bugzilla.redhat.com/show_bug.cgi?id=273081

Wait, what?

I don't think it is an improvement to call the command line matchpathcon applications and have the logs go somewhere not visible by the user. IMO we should take the same approach libsemanage and libsepol did where the logging callbacks can be overridden by specific users of the library but for the command line applications the output still goes to stderr.

Quoted 8 lines
Quoted 5 lines

You already can override the callback (set_matchpathcon_printf), but the claim in the bug report is that isn't usable from a library function that calls matchpathcon because it doesn't know whether it has already been set by the application.

Hrm... so the alternative is to send it to syslog? I don't buy it. Can we just add an interface to see if the default callbacks have been overridden?

Quoted 9 lines

Yep. And it should be based on the newer interfaces. There is already a selinux_set_callback() and adding a selinux_get_callback() should be trivial. setfiles is already rewritten to use the new interfaces in trunk, and we should be moving everything off of matchpathcon and onto selabel_lookup.

For RHEL 5, I suppose there might be a matchpathcon-specific interface for getting the callback.

I would still prefer the default not to spew to stdout where an admin might not see it or it might not get caught to using syslog.

Updated diff to include changing matchpathcon the exec

udev/install/mkinitrd/kerberos libraries and probably a few others are using this interface.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

iD8DBQFG8C0LrlYvE4MpobMRApKVAKCx3bLTKq8pDe+rgARw6DdUo7O0wACgr5Cn isUZTJ4Dh8jifv0uXaZ4xWI=
=HciW
-----END PGP SIGNATURE-----

src/matchpathcon.c +2 -1
diff --exclude-from=exclude -N -u -r nsalibselinux/src/matchpathcon.c libselinux-2.0.33/src/matchpathcon.c
@@ -2,6 +2,7 @@
 #include <string.h>
 #include <errno.h>
 #include <stdio.h>
+#include <syslog.h>
 #include "selinux_internal.h"
 #include "label_internal.h"
 #include "callbacks.h"
@@ -57,7 +58,7 @@
 {
 	va_list ap;
 	va_start(ap, fmt);
-	vfprintf(stderr, fmt, ap);
+	vsyslog(LOG_ERR, fmt, ap);
 	va_end(ap);
 }
 
utils/matchpathcon.c +15 -1
diff --exclude-from=exclude -N -u -r nsalibselinux/utils/matchpathcon.c libselinux-2.0.33/utils/matchpathcon.c
@@ -17,10 +17,24 @@
 	exit(1);
 }
 
+static void
+#ifdef __GNUC__
+    __attribute__ ((format(printf, 1, 2)))
+#endif
+    myprintf(const char *fmt, ...)
+{
+	va_list ap;
+	va_start(ap, fmt);
+	vfprintf(stderr, fmt, ap);
+	va_end(ap);
+}
+
 int printmatchpathcon(char *path, int header, int mode)
 {
 	char *buf;
-	int rc = matchpathcon(path, mode, &buf);
+	int rc;
+	set_matchpathcon_printf(myprintf);
+	rc = matchpathcon(path, mode, &buf);
 	if (rc < 0) {
 		fprintf(stderr, "matchpathcon(%s) failed: %s\n", path,
 			strerror(errno));
#8 lore raw
Quoted 44 lines · Daniel J Walsh

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quoted 31 lines · Stephen Smalley
Quoted 21 lines · Joshua Brindle
Quoted 20 lines · Stephen Smalley
Quoted 18 lines · Joshua Brindle
Quoted 8 lines · Daniel J Walsh

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Current behavior causes problems with network applications.

https://bugzilla.redhat.com/show_bug.cgi?id=273081

Wait, what?

I don't think it is an improvement to call the command line matchpathcon applications and have the logs go somewhere not visible by the user. IMO we should take the same approach libsemanage and libsepol did where the logging callbacks can be overridden by specific users of the library but for the command line applications the output still goes to stderr.

Quoted 8 lines
Quoted 5 lines

You already can override the callback (set_matchpathcon_printf), but the claim in the bug report is that isn't usable from a library function that calls matchpathcon because it doesn't know whether it has already been set by the application.

Hrm... so the alternative is to send it to syslog? I don't buy it. Can we just add an interface to see if the default callbacks have been overridden?

Quoted 9 lines

Yep. And it should be based on the newer interfaces. There is already a selinux_set_callback() and adding a selinux_get_callback() should be trivial. setfiles is already rewritten to use the new interfaces in trunk, and we should be moving everything off of matchpathcon and onto selabel_lookup.

For RHEL 5, I suppose there might be a matchpathcon-specific interface for getting the callback.

Quoted 7 lines

I would still prefer the default not to spew to stdout where an admin might not see it or it might not get caught to using syslog.

Updated diff to include changing matchpathcon the exec

udev/install/mkinitrd/kerberos libraries and probably a few others are using this interface.

I added a comment to the bug report to get feedback on the alternate proposal of adding a get_matchpathcon_printf interface that could be used by library code to get the current function pointer (if set) prior to manipulating it via set_matchpathcon_printf; then the library can reset it to the original value afterward. That avoids changing the default behavior of the existing interface while letting library callers override safely, I think.

Quoted 58 lines

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

iD8DBQFG8C0LrlYvE4MpobMRApKVAKCx3bLTKq8pDe+rgARw6DdUo7O0wACgr5Cn
isUZTJ4Dh8jifv0uXaZ4xWI=
=HciW
-----END PGP SIGNATURE-----
plain text document attachment (diff)
diff --exclude-from=exclude -N -u -r nsalibselinux/src/matchpathcon.c libselinux-2.0.33/src/matchpathcon.c

src/matchpathcon.c +2 -1
@@ -2,6 +2,7 @@
 #include <string.h>
 #include <errno.h>
 #include <stdio.h>
+#include <syslog.h>
 #include "selinux_internal.h"
 #include "label_internal.h"
 #include "callbacks.h"
@@ -57,7 +58,7 @@
 {
 	va_list ap;
 	va_start(ap, fmt);
-	vfprintf(stderr, fmt, ap);
+	vsyslog(LOG_ERR, fmt, ap);
 	va_end(ap);
 }
 

diff --exclude-from=exclude -N -u -r nsalibselinux/utils/matchpathcon.c libselinux-2.0.33/utils/matchpathcon.c

utils/matchpathcon.c +15 -1
@@ -17,10 +17,24 @@
 	exit(1);
 }
 
+static void
+#ifdef __GNUC__
+    __attribute__ ((format(printf, 1, 2)))
+#endif
+    myprintf(const char *fmt, ...)
+{
+	va_list ap;
+	va_start(ap, fmt);
+	vfprintf(stderr, fmt, ap);
+	va_end(ap);
+}
+
 int printmatchpathcon(char *path, int header, int mode)
 {
 	char *buf;
-	int rc = matchpathcon(path, mode, &buf);
+	int rc;
+	set_matchpathcon_printf(myprintf);
+	rc = matchpathcon(path, mode, &buf);
 	if (rc < 0) {
 		fprintf(stderr, "matchpathcon(%s) failed: %s\n", path,
 			strerror(errno));
Signature · 8 lines
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
#9 lore raw
In reply to Stephen Smalley
Quoted 2 lines · Stephen Smalley
Quoted 1 line · Joshua Brindle

Stephen Smalley wrote:

[...]

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

You already can override the callback (set_matchpathcon_printf), but the claim in the bug report is that isn't usable from a library function that calls matchpathcon because it doesn't know whether it has already been set by the application.

Quoted 7 lines
Quoted 1 line

Hrm... so the alternative is to send it to syslog? I don't buy it. Can we just add an interface to see if the default callbacks have been overridden?

Yep. And it should be based on the newer interfaces. There is already a selinux_set_callback() and adding a selinux_get_callback() should be trivial. setfiles is already rewritten to use the new interfaces in trunk, and we should be moving everything off of matchpathcon and onto selabel_lookup.

We could add a get function, and/or modify selinux_set_callback(), currently void, to return the previous callback in the same way as signal(2).

Quoted 4 lines

For RHEL 5, I suppose there might be a matchpathcon-specific interface for getting the callback.

Signature · 7 lines
Eamon Walsh <[email protected]>
National Security Agency

--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.