This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
getpwnam(NULL) get "segmentation fault"
- From: Sheng Yong <shengyong1 at huawei dot com>
- To: <roland at hack dot frob dot com>
- Cc: <libc-alpha at sourceware dot org>
- Date: Sat, 30 May 2015 15:52:13 +0800
- Subject: getpwnam(NULL) get "segmentation fault"
- Authentication-results: sourceware.org; auth=none
Hi,
When using getpwnam(), we found that if the parameter is NULL, the program
would get a segmentation fault. I checked the glibc code, and found that
parameter passed to function is defined as a macro ADD_PARAMS and its value
is not checked before using.
It seems serval functions under nss directory doing so. But segmentation
fault is not friendly to users. Maybe we could add some parameter-check
code before using theses parameters like the following. But I don't know
if it is reasonable to do that. Any hint is appreciated.
thanks,
Sheng
diff --git a/nss/getXXbyYY.c b/nss/getXXbyYY.c
index 15fecf8..4ea7ee2 100644
--- a/nss/getXXbyYY.c
+++ b/nss/getXXbyYY.c
@@ -96,6 +96,8 @@ FUNCTION_NAME (ADD_PARAMS)
/* Get lock. */
__libc_lock_lock (lock);
+ CHECK_PARAMS();
+
if (buffer == NULL)
{
buffer_size = BUFLEN;
diff --git a/pwd/getpwnam.c b/pwd/getpwnam.c
index 9ec66d7..cb28279 100644
--- a/pwd/getpwnam.c
+++ b/pwd/getpwnam.c
@@ -25,5 +25,9 @@
#define ADD_PARAMS const char *name
#define ADD_VARIABLES name
#define BUFLEN NSS_BUFLEN_PASSWD
+#define CHECK_PARAMS() do { \
+ if (name == NULL) \
+ return NULL; \
+} while (0)
#include "../nss/getXXbyYY.c"