Skip to contentSkip to conversation

restorecond: switch terminate to sig_atomic_t for async-signal-safety

PATCH In review Stephen Smalley 1 participant 5 messages Jul 16 – Jul 21 SELinux security module

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

PATCH 1/4 restorecond: switch terminate to sig_atomic_t for async-signal-safety

The terminate flag is written from a signal handler so switch it to volatile sig_atomic_t for async-signal-safety.

---
 restorecond/restorecond.c | 2 +-
 restorecond/restorecond.h | 4 +++-
 2 files changed, 4 insertions(+), 2 deletions(-)
restorecond/restorecond.c +1 -1
index b8ca4b21..0e973d83 100644
@@ -73,7 +73,7 @@ struct restore_opts r_opts;
 #include <selinux/selinux.h>
 
 int debug_mode = 0;
-int terminate = 0;
+volatile sig_atomic_t terminate = 0;
 int master_wd = -1;
 int run_as_user = 0;
 int foreground_mode = 0;
restorecond/restorecond.h +3 -1
index 9fb5c5d5..7e209226 100644
@@ -24,9 +24,11 @@
 #ifndef RESTORED_CONFIG_H
 #define RESTORED_CONFIG_H
 
+#include <signal.h>
+
 extern int debug_mode;
 extern const char *homedir;
-extern int terminate;
+extern volatile sig_atomic_t terminate;
 extern int master_wd;
 extern int run_as_user;
 
Signature · 1 line
2.55.0
author maintainer
#1 lore raw

PATCH 2/4 restorecond: copy homedir to avoid overwrite by later getpw*() calls

homedir was set to pwd->pw_dir returned by getpwuid() and the referenced string could be overwritten by subsequent getpw*() calls, e.g. from utmpwatcher.c:watch_file(). Create a copy to be safe. NB Intentionally not bothering with free'ing the copy on all the potential exit paths; it's small and will be taken care of at exit regardless.

---
 restorecond/restorecond.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
restorecond/restorecond.c +4 -1
index 0e973d83..6548634d 100644
@@ -206,7 +206,10 @@ int main(int argc, char **argv)
 	if (!pwd)
 		exitApp("getpwuid");
 
-	homedir = pwd->pw_dir;
+	homedir = strdup(pwd->pw_dir);
+	if (!homedir)
+		exitApp("strdup");
+
 	if (uid != 0) {
 		if (run_as_user)
 			return server(master_fd, user_watch_file);
Signature · 1 line
2.55.0
author maintainer
#2 lore raw

PATCH 3/4 restorecond: fix type of utmpwatcher_handle()

In reply to Stephen Smalley

utmpwatcher_handle() returns -1 in some cases and the caller has a switch statement on it that compares with -1. Switch the return type to just int rather than unsigned int to more accurately reflect the interface.

---
 restorecond/utmpwatcher.c | 2 +-
 restorecond/utmpwatcher.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
restorecond/utmpwatcher.c +1 -1
index c60fe0e5..e0cb48d6 100644
@@ -45,7 +45,7 @@
 static struct stringsList *utmp_ptr = NULL;
 static int utmp_wd = -1;
 
-unsigned int utmpwatcher_handle(int inotify_fd, int wd)
+int utmpwatcher_handle(int inotify_fd, int wd)
 {
 	int changed = 0;
 	struct utmp u;
restorecond/utmpwatcher.h +1 -1
index 19880108..19178f3c 100644
@@ -23,7 +23,7 @@
 #ifndef UTMPWATCHER_H
 #define UTMPWATCHER_H
 
-unsigned int utmpwatcher_handle(int inotify_fd, int wd);
+int utmpwatcher_handle(int inotify_fd, int wd);
 void utmpwatcher_add(int inotify_fd, const char *path);
 void utmpwatcher_free(void);
 
Signature · 1 line
2.55.0
author maintainer
#3 lore raw

PATCH 4/4 restorecond: fix isspace() call to cast to unsigned

In reply to Stephen Smalley

isspace() on a plain char argument is defined for negative values; cast to unsigned char in the caller.

---
 restorecond/watch.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
restorecond/watch.c +1 -1
index 18bd90db..4e3c382f 100644
@@ -424,7 +424,7 @@ static void process_config(int fd, FILE *cfg)
 
 	while (getline(&line_buf, &len, cfg) > 0) {
 		char *buffer = line_buf;
-		while (isspace(*buffer))
+		while (isspace((unsigned char)*buffer))
 			buffer++;
 		if (buffer[0] == '#')
 			continue;
Signature · 1 line
2.55.0
author maintainer
#4 lore raw
In reply to Stephen Smalley
Quoted 5 lines · Stephen Smalley

The terminate flag is written from a signal handler so switch it to volatile sig_atomic_t for async-signal-safety.

This series has been merged.

Quoted 38 lines
---
 restorecond/restorecond.c | 2 +-
 restorecond/restorecond.h | 4 +++-
 2 files changed, 4 insertions(+), 2 deletions(-)
restorecond/restorecond.c +1 -1
index b8ca4b21..0e973d83 100644
@@ -73,7 +73,7 @@ struct restore_opts r_opts;
 #include <selinux/selinux.h>
 
 int debug_mode = 0;
-int terminate = 0;
+volatile sig_atomic_t terminate = 0;
 int master_wd = -1;
 int run_as_user = 0;
 int foreground_mode = 0;
restorecond/restorecond.h +3 -2
index 9fb5c5d5..7e209226 100644
@@ -24,9 +24,11 @@
 #ifndef RESTORED_CONFIG_H
 #define RESTORED_CONFIG_H
 
+#include <signal.h>
+
 extern int debug_mode;
 extern const char *homedir;
-extern int terminate;
+extern volatile sig_atomic_t terminate;
 extern int master_wd;
 extern int run_as_user;
 
--

2.55.0