Skip to contentSkip to conversation

restorecond: don't set gl_offs for glob()

PATCH In review Stephen Smalley 1 participant 4 messages May 21 – May 26 SELinux security module

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

PATCH 1/3 restorecond: don't set gl_offs for glob()

The gl_offs field is intended to specify slots to reserve in gl_pathv, but is only used if GLOB_DOOFFS is set. Since this flag is NOT set and we never set gl_pathv[0] explicitly, drop the setting of this field. It will be initialized to zero by the memset that is already present.

---
 restorecond/watch.c | 1 -
 1 file changed, 1 deletion(-)
restorecond/watch.c +0 -1
index 466fc1b6..40d30e53 100644
@@ -183,7 +183,6 @@ void watch_list_add(int fd, const char *path)
 	globbuf.gl_closedir = nofollow_closedir;
 	globbuf.gl_lstat = nofollow_lstat;
 	globbuf.gl_stat = nofollow_lstat; /* never follow symlinks */
-	globbuf.gl_offs = 1;
 	if (glob(path,
 		 GLOB_TILDE | GLOB_PERIOD | GLOB_ALTDIRFUNC,
 		 NULL,
Signature · 1 line
2.54.0
author maintainer
#1 lore raw

PATCH 2/3 restorecond: do not pass the same string to basename()/dirname()

basename() and dirname() may modify their argument and may return a pointer to some part of their argument so we should pass separate copies of path to each one to prevent clobbering. For example, if path is "/foo", then:
x = strdup(path) = "/foo"
file = basename(x) = x+1 = "foo\0"
dir = dirname(x) writes '\0' to x+1 = "/\0oo"
And file will become "\0".

---
 restorecond/watch.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
restorecond/watch.c +8 -5
index 40d30e53..aceb9c5f 100644
@@ -170,10 +170,12 @@ void watch_list_add(int fd, const char *path)
 	size_t i = 0;
 	struct watchList *prev = NULL;
 	glob_t globbuf;
-	char *x = strdup(path);
-	if (!x) exitApp("Out of Memory");
-	char *file = basename(x);
-	char *dir = dirname(x);
+	char *xb = strdup(path);
+	if (!xb) exitApp("Out of Memory");
+	char *xd = strdup(path);
+	if (!xd) exitApp("Out of Memory");
+	char *file = basename(xb);
+	char *dir = dirname(xd);
 	ptr = firstDir;
 	int len;
 
@@ -240,7 +242,8 @@ void watch_list_add(int fd, const char *path)
 		printf("%d: Dir=%s, File=%s\n", ptr->wd, ptr->dir, file);
 
 end:
-	free(x);
+	free(xb);
+	free(xd);
 	return;
 }
 
Signature · 1 line
2.54.0
author maintainer
#2 lore raw

PATCH 3/3 restorecond: create a separate io channel callback for stdin

In reply to Stephen Smalley

restorecond registers a single io channel callback that handles both inotify events and stdin. This can erroneously end up trying to interpret stdin input as an inotify event. Create a separate callback for stdin.

---
 restorecond/user.c | 28 +++++++++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)
restorecond/user.c +27 -1
index 25e70ae1..3191829f 100644
@@ -169,6 +169,32 @@ io_channel_callback
   return TRUE;
 }
 
+static gboolean
+stdin_callback
+ (GIOChannel *source,
+  GIOCondition condition,
+  gpointer data __attribute__((__unused__)))
+{
+
+  char buffer[256];
+  gsize bytes_read;
+
+  if (condition & G_IO_IN) {
+    /* Data is available. */
+    while (g_io_channel_read_chars(source, buffer, sizeof (buffer),
+				    &bytes_read, NULL) == G_IO_STATUS_NORMAL
+	    && bytes_read > 0)
+	    ;
+    if (bytes_read == 0)
+	    exit(0);
+  }
+
+  if (condition & (G_IO_HUP | G_IO_ERR | G_IO_NVAL))
+	  exit(0);
+
+  return TRUE;
+}
+
 int start(void) {
 #ifdef HAVE_DBUS
 	GDBusConnection *bus;
@@ -243,7 +269,7 @@ static int local_server(void) {
 	g_io_add_watch_full( in,
 			     G_PRIORITY_HIGH,
 			     G_IO_IN|G_IO_ERR|G_IO_HUP,
-			     io_channel_callback, NULL, NULL);
+			     stdin_callback, NULL, NULL);
 
 	return 0;
 }
Signature · 1 line
2.54.0
author maintainer
#3 lore raw

Re: [PATCH 3/3] restorecond: create a separate io channel callback for stdin

Quoted 7 lines · Stephen Smalley

restorecond registers a single io channel callback that handles both inotify events and stdin. This can erroneously end up trying to interpret stdin input as an inotify event. Create a separate callback for stdin.

All three merged

Quoted 53 lines
---
 restorecond/user.c | 28 +++++++++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)
restorecond/user.c +27 -2
index 25e70ae1..3191829f 100644
@@ -169,6 +169,32 @@ io_channel_callback
   return TRUE;
 }
 
+static gboolean
+stdin_callback
+ (GIOChannel *source,
+  GIOCondition condition,
+  gpointer data __attribute__((__unused__)))
+{
+
+  char buffer[256];
+  gsize bytes_read;
+
+  if (condition & G_IO_IN) {
+    /* Data is available. */
+    while (g_io_channel_read_chars(source, buffer, sizeof (buffer),
+                                   &bytes_read, NULL) == G_IO_STATUS_NORMAL
+           && bytes_read > 0)
+           ;
+    if (bytes_read == 0)
+           exit(0);
+  }
+
+  if (condition & (G_IO_HUP | G_IO_ERR | G_IO_NVAL))
+         exit(0);
+
+  return TRUE;
+}
+
 int start(void) {
 #ifdef HAVE_DBUS
        GDBusConnection *bus;
@@ -243,7 +269,7 @@ static int local_server(void) {
        g_io_add_watch_full( in,
                             G_PRIORITY_HIGH,
                             G_IO_IN|G_IO_ERR|G_IO_HUP,
-                            io_channel_callback, NULL, NULL);
+                            stdin_callback, NULL, NULL);
 
        return 0;
 }
--

2.54.0