getpwnam(NULL) get "segmentation fault"
Sheng Yong
shengyong1@huawei.com
Sun May 31 04:56:00 GMT 2015
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"
More information about the Libc-alpha
mailing list