Skip to contentSkip to conversation

restorecond: GLib IO channel fixes

PATCH In review Fabian Vogt 2 participants 5 messages Oct 24, 2024 – Nov 18, 2024 SELinux security module

lore
Share & tools
lore
#0 lore raw

cover restorecond: GLib IO channel fixes

Downstream we hit a bug that "restorecond -u" kept running even after the user logged out (https://bugzilla.opensuse.org/show_bug.cgi?id=1231512). The cause for that was that SIGTERM was handled, but had no effect. While testing the fix (PATCH 2), I found that the inotify events weren't actually read properly. That's addressed by PATCH 1.

Fabian Vogt (2):
restorecond: Set GLib IO channels to binary mode
restorecond: Set GLib IO channels to nonblocking

 restorecond/user.c | 4 ++++
 1 file changed, 4 insertions(+)
Signature · 1 line
2.47.0
#1 lore raw

PATCH 1/2 restorecond: Set GLib IO channels to binary mode

By default, GIO channels use UTF-8 as encoding, which causes issues when reading binary data such as inotify events.

---
 restorecond/user.c | 2 ++
 1 file changed, 2 insertions(+)
restorecond/user.c +2 -0
index 3ae3ebbb7230..7188c22e3119 100644
@@ -238,6 +238,7 @@ static int local_server(void) {
 	}
 	/* watch for stdin/terminal going away */
 	GIOChannel *in = g_io_channel_unix_new(0);
+	g_io_channel_set_encoding(in, NULL, NULL);
 	g_io_add_watch_full( in,
 			     G_PRIORITY_HIGH,
 			     G_IO_IN|G_IO_ERR|G_IO_HUP,
@@ -282,6 +283,7 @@ int server(int master_fd, const char *watch_file) {
 	set_matchpathcon_flags(MATCHPATHCON_NOTRANS);
 
 	GIOChannel *c = g_io_channel_unix_new(master_fd);
+	g_io_channel_set_encoding(c, NULL, NULL);
 
 	g_io_add_watch_full(c,
 			    G_PRIORITY_HIGH,
Signature · 1 line
2.47.0
Acked-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] restorecond: Set GLib IO channels to binary mode

Quoted 5 lines · Fabian Vogt

By default, GIO channels use UTF-8 as encoding, which causes issues when reading binary data such as inotify events.

For these two patches:

Quoted 28 lines
---
 restorecond/user.c | 2 ++
 1 file changed, 2 insertions(+)
restorecond/user.c +2 -1
index 3ae3ebbb7230..7188c22e3119 100644
@@ -238,6 +238,7 @@ static int local_server(void) {
        }
        /* watch for stdin/terminal going away */
        GIOChannel *in = g_io_channel_unix_new(0);
+       g_io_channel_set_encoding(in, NULL, NULL);
        g_io_add_watch_full( in,
                             G_PRIORITY_HIGH,
                             G_IO_IN|G_IO_ERR|G_IO_HUP,
@@ -282,6 +283,7 @@ int server(int master_fd, const char *watch_file) {
        set_matchpathcon_flags(MATCHPATHCON_NOTRANS);
 
        GIOChannel *c = g_io_channel_unix_new(master_fd);
+       g_io_channel_set_encoding(c, NULL, NULL);
 
        g_io_add_watch_full(c,
                            G_PRIORITY_HIGH,
--

2.47.0

#3 lore raw

Re: [PATCH 1/2] restorecond: Set GLib IO channels to binary mode

Quoted 11 lines · James Carter
Quoted 5 lines · Fabian Vogt

By default, GIO channels use UTF-8 as encoding, which causes issues when reading binary data such as inotify events.

For these two patches:

These two patches have been merged.
Thanks,
Jim

Quoted 28 lines
Quoted 28 lines
---
 restorecond/user.c | 2 ++
 1 file changed, 2 insertions(+)
restorecond/user.c +2 -1
index 3ae3ebbb7230..7188c22e3119 100644
@@ -238,6 +238,7 @@ static int local_server(void) {
        }
        /* watch for stdin/terminal going away */
        GIOChannel *in = g_io_channel_unix_new(0);
+       g_io_channel_set_encoding(in, NULL, NULL);
        g_io_add_watch_full( in,
                             G_PRIORITY_HIGH,
                             G_IO_IN|G_IO_ERR|G_IO_HUP,
@@ -282,6 +283,7 @@ int server(int master_fd, const char *watch_file) {
        set_matchpathcon_flags(MATCHPATHCON_NOTRANS);
 
        GIOChannel *c = g_io_channel_unix_new(master_fd);
+       g_io_channel_set_encoding(c, NULL, NULL);
 
        g_io_add_watch_full(c,
                            G_PRIORITY_HIGH,
--

2.47.0

#4 lore raw

PATCH 2/2 restorecond: Set GLib IO channels to nonblocking

In reply to Fabian Vogt

Without nonblocking IO, g_io_channel_read_chars waits indefinitely for more data without ever returning control to the event loop.

Set the IO channels to nonblocking to fix SIGTERM handling.

---
 restorecond/user.c | 2 ++
 1 file changed, 2 insertions(+)
restorecond/user.c +2 -0
index 7188c22e3119..25e70ae15b94 100644
@@ -239,6 +239,7 @@ static int local_server(void) {
 	/* watch for stdin/terminal going away */
 	GIOChannel *in = g_io_channel_unix_new(0);
 	g_io_channel_set_encoding(in, NULL, NULL);
+	g_io_channel_set_flags(in, g_io_channel_get_flags(in) | G_IO_FLAG_NONBLOCK, NULL);
 	g_io_add_watch_full( in,
 			     G_PRIORITY_HIGH,
 			     G_IO_IN|G_IO_ERR|G_IO_HUP,
@@ -284,6 +285,7 @@ int server(int master_fd, const char *watch_file) {
 
 	GIOChannel *c = g_io_channel_unix_new(master_fd);
 	g_io_channel_set_encoding(c, NULL, NULL);
+	g_io_channel_set_flags(c, g_io_channel_get_flags(c) | G_IO_FLAG_NONBLOCK, NULL);
 
 	g_io_add_watch_full(c,
 			    G_PRIORITY_HIGH,
Signature · 1 line
2.47.0