diff mbox

[6/9] libnsdb: Fix NUL-termination logic in nsdb_set_parentdir()

Message ID 20140627184302.52216.96952.stgit@seurat.1015granger.net
State Accepted
Headers show

Commit Message

Chuck Lever June 27, 2014, 6:43 p.m. UTC
Ensure that pathname buffers have space for the NUL terminator.
Note this function is currently unused.

Fixes: 0520ee72 (Initial commit)
Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
---
 src/libnsdb/nsdb.c |   24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)
diff mbox

Patch

diff --git a/src/libnsdb/nsdb.c b/src/libnsdb/nsdb.c
index 41d0e9e2b2a7..f4adc72074b4 100644
--- a/src/libnsdb/nsdb.c
+++ b/src/libnsdb/nsdb.c
@@ -95,7 +95,7 @@  static char fedfs_nsdbcerts_dirname[PATH_MAX + 1] =
 /**
  * Stores pathname of database containing FedFS persistent state
  */
-static char fedfs_db_filename[PATH_MAX] =
+static char fedfs_db_filename[PATH_MAX + 1] =
 			FEDFS_DEFAULT_STATEDIR "/" FEDFS_DATABASE_FILE;
 
 /**
@@ -110,15 +110,23 @@  static char fedfs_db_filename[PATH_MAX] =
 _Bool
 nsdb_set_parentdir(const char *parentdir)
 {
-	static char buf[PATH_MAX];
+	static char buf[PATH_MAX + 1];
 	struct stat st;
 	char *path;
 	int len;
 
+	if (parentdir == NULL) {
+		xlog(D_GENERAL, "%s: Passed a NULL argument", __func__);
+		return false;
+	}
+	if (strlen(parentdir) > PATH_MAX) {
+		xlog(D_GENERAL, "%s: Argument too long", __func__);
+		return false;
+	}
+
 	xlog(D_CALL, "%s: Setting up %s as our FedFS state directory",
 		__func__, parentdir);
 
-	/* First: test length of name and whether it exists */
 	if (lstat(parentdir, &st) == -1) {
 		xlog(D_GENERAL, "%s: Failed to stat %s: %m",
 			__func__, parentdir);
@@ -129,12 +137,10 @@  nsdb_set_parentdir(const char *parentdir)
 			__func__, parentdir);
 		return false;
 	}
-
-	/* Ensure we have a clean directory pathname */
-	strncpy(buf, parentdir, sizeof(buf));
+	strcpy(buf, parentdir);
 	path = dirname(buf);
 	if (*path == '.') {
-		xlog(D_GENERAL, "%s: Unusable pathname %s",
+		xlog(D_GENERAL, "%s: Pathname %s is relative",
 			__func__, parentdir);
 		return false;
 	}
@@ -154,9 +160,7 @@  nsdb_set_parentdir(const char *parentdir)
 		return false;
 	}
 	strcpy(fedfs_nsdbcerts_dirname, buf);
-
-	strncpy(fedfs_base_dirname, parentdir, sizeof(fedfs_base_dirname));
-
+	strcpy(fedfs_base_dirname, parentdir);
 	return true;
 }