Blame SOURCES/pam-1.3.1-pam-motd-avoid-unnecessary-logging.patch

287c98
From d57ab22133654033ee1da89f128a81572d320985 Mon Sep 17 00:00:00 2001
287c98
From: Tomas Mraz <tmraz@fedoraproject.org>
287c98
Date: Thu, 20 Dec 2018 13:59:25 +0100
287c98
Subject: [PATCH] pam_motd: Cleanup the code and avoid unnecessary logging
287c98
287c98
The pam_motd module will not log if the default motd.d directories
287c98
are missing.
287c98
287c98
Also cleanup some code cleanliness issues and fix compilation
287c98
warnings.
287c98
287c98
* modules/pam_motd/pam_motd.c: Constification of constant strings.
287c98
  (try_to_display_directory): Removed unused function.
287c98
  (pam_split_string): Replace uint with unsigned int. Fix warnings.
287c98
  (compare_strings): Fix warnings by proper constification.
287c98
  (try_to_display_directories_with_overrides): Cleanups. Switch
287c98
  off the logging if the motd.d directories are missing and they
287c98
  are default ones.
287c98
  (pam_sm_open_session): Cleanup warnings. Pass the information
287c98
  to try_to_display_directories_with_overrides() that non-default
287c98
  motd options are used.
287c98
---
287c98
 modules/pam_motd/pam_motd.c | 88 ++++++++++++++++---------------------
287c98
 1 file changed, 37 insertions(+), 51 deletions(-)
287c98
287c98
diff --git a/modules/pam_motd/pam_motd.c b/modules/pam_motd/pam_motd.c
287c98
index ec3ebd58..dbd718b6 100644
287c98
--- a/modules/pam_motd/pam_motd.c
287c98
+++ b/modules/pam_motd/pam_motd.c
287c98
@@ -22,6 +22,7 @@
287c98
 #include <sys/stat.h>
287c98
 #include <pwd.h>
287c98
 #include <syslog.h>
287c98
+#include <errno.h>
287c98
 
287c98
 #include <security/_pam_macros.h>
287c98
 #include <security/pam_ext.h>
287c98
@@ -48,8 +49,8 @@ pam_sm_close_session (pam_handle_t *pamh UNUSED, int flags UNUSED,
287c98
      return PAM_IGNORE;
287c98
 }
287c98
 
287c98
-static char default_motd[] = DEFAULT_MOTD;
287c98
-static char default_motd_dir[] = DEFAULT_MOTD_D;
287c98
+static const char default_motd[] = DEFAULT_MOTD;
287c98
+static const char default_motd_dir[] = DEFAULT_MOTD_D;
287c98
 
287c98
 static void try_to_display_fd(pam_handle_t *pamh, int fd)
287c98
 {
287c98
@@ -75,28 +76,6 @@ static void try_to_display_fd(pam_handle_t *pamh, int fd)
287c98
     _pam_drop(mtmp);
287c98
 }
287c98
 
287c98
-static void try_to_display_directory(pam_handle_t *pamh, const char *dirname)
287c98
-{
287c98
-    DIR *dirp;
287c98
-
287c98
-    dirp = opendir(dirname);
287c98
-
287c98
-    if (dirp != NULL) {
287c98
-	struct dirent *entry;
287c98
-
287c98
-	while ((entry = readdir(dirp))) {
287c98
-	    int fd = openat(dirfd(dirp), entry->d_name, O_RDONLY);
287c98
-
287c98
-	    if (fd >= 0) {
287c98
-		try_to_display_fd(pamh, fd);
287c98
-		close(fd);
287c98
-	    }
287c98
-	}
287c98
-
287c98
-	closedir(dirp);
287c98
-    }
287c98
-}
287c98
-
287c98
 /*
287c98
  * Split a DELIM-separated string ARG into an array.
287c98
  * Outputs a newly allocated array of strings OUT_ARG_SPLIT
287c98
@@ -104,14 +83,14 @@ static void try_to_display_directory(pam_handle_t *pamh, const char *dirname)
287c98
  * Returns 0 in case of error, 1 in case of success.
287c98
  */
287c98
 static int pam_split_string(const pam_handle_t *pamh, char *arg, char delim,
287c98
-			    char ***out_arg_split, uint *out_num_strs)
287c98
+			    char ***out_arg_split, unsigned int *out_num_strs)
287c98
 {
287c98
     char *arg_extracted = NULL;
287c98
     const char *arg_ptr = arg;
287c98
     char **arg_split = NULL;
287c98
     char delim_str[2];
287c98
-    int i = 0;
287c98
-    uint num_strs = 0;
287c98
+    unsigned int i = 0;
287c98
+    unsigned int num_strs = 0;
287c98
     int retval = 0;
287c98
 
287c98
     delim_str[0] = delim;
287c98
@@ -126,7 +105,7 @@ static int pam_split_string(const pam_handle_t *pamh, char *arg, char delim,
287c98
 	arg_ptr = strchr(arg_ptr + sizeof(const char), delim);
287c98
     }
287c98
 
287c98
-    arg_split = (char **)calloc(num_strs, sizeof(char *));
287c98
+    arg_split = calloc(num_strs, sizeof(char *));
287c98
     if (arg_split == NULL) {
287c98
 	pam_syslog(pamh, LOG_CRIT, "pam_motd: failed to allocate string array");
287c98
 	goto out;
287c98
@@ -180,10 +159,10 @@ static int join_dir_strings(char **strp_out, const char *a_str, const char *b_st
287c98
     return retval;
287c98
 }
287c98
 
287c98
-static int compare_strings(const void * a, const void * b)
287c98
+static int compare_strings(const void *a, const void *b)
287c98
 {
287c98
-    const char *a_str = *(char **)a;
287c98
-    const char *b_str = *(char **)b;
287c98
+    const char *a_str = *(const char * const *)a;
287c98
+    const char *b_str = *(const char * const *)b;
287c98
 
287c98
     if (a_str == NULL && b_str == NULL) {
287c98
         return 0;
287c98
@@ -205,13 +184,13 @@ static int filter_dirents(const struct dirent *d)
287c98
 }
287c98
 
287c98
 static void try_to_display_directories_with_overrides(pam_handle_t *pamh,
287c98
-	char **motd_dir_path_split, int num_motd_dirs)
287c98
+	char **motd_dir_path_split, unsigned int num_motd_dirs, int report_missing)
287c98
 {
287c98
     struct dirent ***dirscans = NULL;
287c98
-    int *dirscans_sizes = NULL;
287c98
-    int dirscans_size_total = 0;
287c98
+    unsigned int *dirscans_sizes = NULL;
287c98
+    unsigned int dirscans_size_total = 0;
287c98
     char **dirnames_all = NULL;
287c98
-    int i;
287c98
+    unsigned int i;
287c98
     int i_dirnames = 0;
287c98
 
287c98
     if (pamh == NULL || motd_dir_path_split == NULL) {
287c98
@@ -221,29 +200,31 @@ static void try_to_display_directories_with_overrides(pam_handle_t *pamh,
287c98
 	goto out;
287c98
     }
287c98
 
287c98
-    if ((dirscans = (struct dirent ***)calloc(num_motd_dirs,
287c98
-	    sizeof(struct dirent **))) == NULL) {
287c98
+    if ((dirscans = calloc(num_motd_dirs, sizeof(struct dirent **))) == NULL) {
287c98
 	pam_syslog(pamh, LOG_CRIT, "pam_motd: failed to allocate dirent arrays");
287c98
 	goto out;
287c98
     }
287c98
-    if ((dirscans_sizes = (int *)calloc(num_motd_dirs, sizeof(int))) == NULL) {
287c98
+    if ((dirscans_sizes = calloc(num_motd_dirs, sizeof(int))) == NULL) {
287c98
 	pam_syslog(pamh, LOG_CRIT, "pam_motd: failed to allocate dirent array sizes");
287c98
 	goto out;
287c98
     }
287c98
 
287c98
     for (i = 0; i < num_motd_dirs; i++) {
287c98
-	dirscans_sizes[i] = scandir(motd_dir_path_split[i], &(dirscans[i]),
287c98
+	int rv;
287c98
+	rv = scandir(motd_dir_path_split[i], &(dirscans[i]),
287c98
 		filter_dirents, alphasort);
287c98
-	if (dirscans_sizes[i] < 0) {
287c98
-	    pam_syslog(pamh, LOG_ERR, "pam_motd: error scanning directory %s", motd_dir_path_split[i]);
287c98
-	    dirscans_sizes[i] = 0;
287c98
+	if (rv < 0) {
287c98
+	    if (errno != ENOENT || report_missing) {
287c98
+		pam_syslog(pamh, LOG_ERR, "pam_motd: error scanning directory %s: %m",
287c98
+		    motd_dir_path_split[i]);
287c98
+	    }
287c98
+	    dirscans_sizes[i] = rv;
287c98
 	}
287c98
 	dirscans_size_total += dirscans_sizes[i];
287c98
     }
287c98
 
287c98
     /* Allocate space for all file names found in the directories, including duplicates. */
287c98
-    if ((dirnames_all = (char **)calloc(dirscans_size_total,
287c98
-	    sizeof(char *))) == NULL) {
287c98
+    if ((dirnames_all = calloc(dirscans_size_total, sizeof(char *))) == NULL) {
287c98
 	pam_syslog(pamh, LOG_CRIT, "pam_motd: failed to allocate dirname array");
287c98
 	goto out;
287c98
     }
287c98
@@ -253,7 +234,7 @@ static void try_to_display_directories_with_overrides(pam_handle_t *pamh,
287c98
     }
287c98
 
287c98
     for (i = 0; i < num_motd_dirs; i++) {
287c98
-	int j;
287c98
+	unsigned int j;
287c98
 
287c98
 	for (j = 0; j < dirscans_sizes[i]; j++) {
287c98
 	    dirnames_all[i_dirnames] = dirscans[i][j]->d_name;
287c98
@@ -265,7 +246,7 @@ static void try_to_display_directories_with_overrides(pam_handle_t *pamh,
287c98
 	    sizeof(const char *), compare_strings);
287c98
 
287c98
     for (i = 0; i < dirscans_size_total; i++) {
287c98
-	int j;
287c98
+	unsigned int j;
287c98
 
287c98
 	if (dirnames_all[i] == NULL) {
287c98
 	    continue;
287c98
@@ -301,7 +282,8 @@ static void try_to_display_directories_with_overrides(pam_handle_t *pamh,
287c98
   out:
287c98
     _pam_drop(dirnames_all);
287c98
     for (i = 0; i < num_motd_dirs; i++) {
287c98
-	int j;
287c98
+	unsigned int j;
287c98
+
287c98
 	for (j = 0; j < dirscans_sizes[i]; j++) {
287c98
 	    _pam_drop(dirscans[i][j]);
287c98
 	}
287c98
@@ -319,12 +301,13 @@ int pam_sm_open_session(pam_handle_t *pamh, int flags,
287c98
     int retval = PAM_IGNORE;
287c98
     const char *motd_path = NULL;
287c98
     char *motd_path_copy = NULL;
287c98
-    int num_motd_paths = 0;
287c98
+    unsigned int num_motd_paths = 0;
287c98
     char **motd_path_split = NULL;
287c98
     const char *motd_dir_path = NULL;
287c98
     char *motd_dir_path_copy = NULL;
287c98
-    int num_motd_dir_paths = 0;
287c98
+    unsigned int num_motd_dir_paths = 0;
287c98
     char **motd_dir_path_split = NULL;
287c98
+    int report_missing;
287c98
 
287c98
     if (flags & PAM_SILENT) {
287c98
 	return retval;
287c98
@@ -360,6 +343,9 @@ int pam_sm_open_session(pam_handle_t *pamh, int flags,
287c98
     if (motd_path == NULL && motd_dir_path == NULL) {
287c98
 	motd_path = default_motd;
287c98
 	motd_dir_path = default_motd_dir;
287c98
+	report_missing = 0;
287c98
+    } else {
287c98
+	report_missing = 1;
287c98
     }
287c98
 
287c98
     if (motd_path != NULL) {
287c98
@@ -385,7 +371,7 @@ int pam_sm_open_session(pam_handle_t *pamh, int flags,
287c98
     }
287c98
 
287c98
     if (motd_path_split != NULL) {
287c98
-	int i;
287c98
+	unsigned int i;
287c98
 
287c98
 	for (i = 0; i < num_motd_paths; i++) {
287c98
 	    int fd = open(motd_path_split[i], O_RDONLY, 0);
287c98
@@ -402,7 +388,7 @@ int pam_sm_open_session(pam_handle_t *pamh, int flags,
287c98
 
287c98
     if (motd_dir_path_split != NULL)
287c98
 	try_to_display_directories_with_overrides(pamh, motd_dir_path_split,
287c98
-		num_motd_dir_paths);
287c98
+		num_motd_dir_paths, report_missing);
287c98
 
287c98
   out:
287c98
     _pam_drop(motd_path_copy);
287c98
-- 
287c98
2.37.3
287c98
287c98
From c2c0434bd634a817f2b16ce7f58fc96c04e88b03 Mon Sep 17 00:00:00 2001
287c98
From: "Dmitry V. Levin" <ldv@altlinux.org>
287c98
Date: Sun, 26 Apr 2020 11:12:59 +0000
287c98
Subject: [PATCH] pam_motd: fix NULL dereference when at least one of motd
287c98
 directories is not available
287c98
287c98
* modules/pam_motd/pam_motd.c
287c98
(try_to_display_directories_with_overrides): Do not assign -1U to
287c98
dirscans_sizes[i] when scandir(motd_dir_path_split[i]) returns an error.
287c98
287c98
Resolves: https://bugzilla.altlinux.org/38389
287c98
Fixes: d57ab221 ("pam_motd: Cleanup the code and avoid unnecessary logging")
287c98
---
287c98
 modules/pam_motd/pam_motd.c | 1 +
287c98
 1 file changed, 1 insertion(+)
287c98
287c98
diff --git a/modules/pam_motd/pam_motd.c b/modules/pam_motd/pam_motd.c
287c98
index df09b7d0..8147c6fd 100644
287c98
--- a/modules/pam_motd/pam_motd.c
287c98
+++ b/modules/pam_motd/pam_motd.c
287c98
@@ -219,6 +219,7 @@ static void try_to_display_directories_with_overrides(pam_handle_t *pamh,
287c98
 		pam_syslog(pamh, LOG_ERR, "pam_motd: error scanning directory %s: %m",
287c98
 		    motd_dir_path_split[i]);
287c98
 	    }
287c98
+	} else {
287c98
 	    dirscans_sizes[i] = rv;
287c98
 	}
287c98
 	dirscans_size_total += dirscans_sizes[i];
287c98
-- 
287c98
2.37.3
287c98