Skip to contentSkip to conversation

libxl: dm_restrict: DEFINE_USERLOOKUP_HELPER returned a pointer to an auto

PATCH In review Ian Jackson 2 participants 3 messages Oct 13, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libxl: dm_restrict: DEFINE_USERLOOKUP_HELPER returned a pointer to an auto

When I converted the previous open-coded user lookup functionality into DEFINE_USERLOOKUP_HELPER, I moved the struct passwd buffer into the function generated by the macro. This is wrong because that buffer is used by get{pw,gr}* for its return value, so the helper function would contrive to return a pointer to the buffer on its own stack.

Fix this by adding a buffer parameter to the generated helpers, that the caller must supply, and updating all the call sites.

---
 tools/libxl/libxl_dm.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
tools/libxl/libxl_dm.c +11 -9
index 7caf471..a2ea95a 100644
@@ -762,9 +762,10 @@ libxl__detect_gfx_passthru_kind(libxl__gc *gc,
 #define DEFINE_USERLOOKUP_HELPER(NAME,SPEC_TYPE,STRUCTNAME,SYSCONF)     \
     static int userlookup_helper_##NAME(libxl__gc *gc,                  \
                                         SPEC_TYPE spec,                 \
-                                           struct STRUCTNAME **out)     \
+                                        struct STRUCTNAME *resultbuf,   \
+                                        struct STRUCTNAME **out)        \
     {                                                                   \
-        struct STRUCTNAME resultbuf, *resultp = NULL;                   \
+        struct STRUCTNAME *resultp = NULL;                              \
         char *buf = NULL;                                               \
         long buf_size;                                                  \
         int ret;                                                        \
@@ -779,7 +780,7 @@ libxl__detect_gfx_passthru_kind(libxl__gc *gc,
                                                                         \
         while (1) {                                                     \
             buf = libxl__realloc(gc, buf, buf_size);                    \
-            ret = NAME##_r(spec, &resultbuf, buf, buf_size, &resultp);  \
+            ret = NAME##_r(spec, resultbuf, buf, buf_size, &resultp);   \
             if (ret == ERANGE) {                                        \
                 buf_size += 128;                                        \
                 continue;                                               \
@@ -956,7 +957,7 @@ static int libxl__build_device_model_args_new(libxl__gc *gc,
     uint64_t ram_size;
     const char *path, *chardev;
     char *user = NULL;
-    struct passwd *user_base;
+    struct passwd *user_base, user_pwbuf;
 
     dm_args = flexarray_make(gc, 16, 1);
     dm_envs = flexarray_make(gc, 16, 1);
@@ -1660,20 +1661,21 @@ static int libxl__build_device_model_args_new(libxl__gc *gc,
         }
 
         user = GCSPRINTF("%s%d", LIBXL_QEMU_USER_BASE, guest_domid);
-        ret = userlookup_helper_getpwnam(gc, user, 0);
+        ret = userlookup_helper_getpwnam(gc, user, &user_pwbuf, 0);
         if (ret < 0)
             return ret;
         if (ret > 0)
             goto end_search;
 
         ret = userlookup_helper_getpwnam(gc, LIBXL_QEMU_USER_RANGE_BASE,
-                                         &user_base);
+                                         &user_pwbuf, &user_base);
         if (ret < 0)
             return ret;
         if (ret > 0) {
-            struct passwd *user_clash;
+            struct passwd *user_clash, user_clash_pwbuf;
             uid_t intended_uid = user_base->pw_uid + guest_domid;
-            ret = userlookup_helper_getpwuid(gc, intended_uid, &user_clash);
+            ret = userlookup_helper_getpwuid(gc, intended_uid,
+                                             &user_clash_pwbuf, &user_clash);
             if (ret < 0)
                 return ret;
             if (ret > 0) {
@@ -1693,7 +1695,7 @@ static int libxl__build_device_model_args_new(libxl__gc *gc,
         }
 
         user = LIBXL_QEMU_USER_SHARED;
-        ret = userlookup_helper_getpwnam(gc, user, 0);
+        ret = userlookup_helper_getpwnam(gc, user, &user_pwbuf, 0);
         if (ret < 0)
             return ret;
         if (ret > 0) {
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Acked-by on v1 1/1
#1 lore raw
Quoted 7 lines · Ian Jackson

When I converted the previous open-coded user lookup functionality into DEFINE_USERLOOKUP_HELPER, I moved the struct passwd buffer into the function generated by the macro. This is wrong because that buffer is used by get{pw,gr}* for its return value, so the helper function would contrive to return a pointer to the buffer on its own stack.

Quoted 6 lines

Fix this by adding a buffer parameter to the generated helpers, that the caller must supply, and updating all the call sites.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#2 lore raw

Wei Liu writes ("Re: [PATCH] libxl: dm_restrict: DEFINE_USERLOOKUP_HELPER returned a pointer to an auto"):

Quoted 10 lines · Ian Jackson
Quoted 9 lines

When I converted the previous open-coded user lookup functionality into DEFINE_USERLOOKUP_HELPER, I moved the struct passwd buffer into the function generated by the macro. This is wrong because that buffer is used by get{pw,gr}* for its return value, so the helper function would contrive to return a pointer to the buffer on its own stack.

Fix this by adding a buffer parameter to the generated helpers, that the caller must supply, and updating all the call sites.

...

Quoted 1 line

Thanks, pushed.

Ian.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel