Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nsexec: Check for errors in write_log() #3712

Merged
merged 1 commit into from
Jan 31, 2023

Conversation

rata
Copy link
Member

@rata rata commented Jan 27, 2023

First, check if strdup() fails and error out.

While we are there, the else case was missing brackets, as we only need to check ret in the else case. Fix that too

Signed-off-by: Rodrigo Campos rodrigoca@microsoft.com

First, check if strdup() fails and error out.

While we are there, the else case was missing brackets, as we only need
to check ret in the else case. Fix that too

Signed-off-by: Rodrigo Campos <rodrigoca@microsoft.com>
Copy link
Contributor

@kolyshkin kolyshkin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comes from commit 64bb59f. The fix makes sense, so LGTM.

@kolyshkin
Copy link
Contributor

@cyphar PTAL

The alternative would be something like this:

@@ -151,7 +152,8 @@ int setns(int fd, int nstype)
 
 static void write_log(int level, const char *format, ...)
 {
-       char *message = NULL, *stage = NULL, *json = NULL;
+       char *message = NULL, *json = NULL;
+       char stage[] = "nsexec\0 ";
        va_list args;
        int ret;
 
@@ -168,13 +170,16 @@ static void write_log(int level, const char *format, ...)
 
        message = escape_json_string(message);
 
-       if (current_stage == STAGE_SETUP)
-               stage = strdup("nsexec");
-       else
-               ret = asprintf(&stage, "nsexec-%d", current_stage);
-       if (ret < 0) {
-               stage = NULL;
-               goto out;
+       if (current_stage != STAGE_SETUP) {
+               char s = '?';
+               switch (current_stage) {
+               case STAGE_PARENT:
+               case STAGE_CHILD:
+               case STAGE_INIT:
+                       s = '0' + current_stage;
+               }
+               stage[sizeof(stage) - 3] = '-';
+               stage[sizeof(stage) - 2] = s;
        }
 
        ret = asprintf(&json, "{\"level\":\"%s\", \"msg\": \"%s[%d]: %s\"}\n",
@@ -191,22 +196,20 @@ static void write_log(int level, const char *format, ...)
 
 out:
        free(message);
-       free(stage);
        free(json);
 }
 
 /* XXX: This is ugly. */
 static int syncfd = -1;
 

(and I don't like it as it's kind of long)

@rata
Copy link
Member Author

rata commented Jan 30, 2023

@kolyshkin I agree, I don't like that either. This PR seems more readable IMHO.

Once this is merged, do we want to backport it? I have not triggered it, but I haven't run runc in mem constrained envs, so strdup can fail. I think it is safe to backport and we can avoid problems (pointer is uninitialized pointing who knows where, better to backport it and not think about it IMHO).

@AkihiroSuda AkihiroSuda merged commit 8c5d3f0 into opencontainers:main Jan 31, 2023
@AkihiroSuda AkihiroSuda added the backport/todo/1.1 A PR in main branch which needs to be backported to release-1.1 label Jan 31, 2023
@rata rata deleted the rata/nsexec-fixes branch January 31, 2023 10:18
@rata
Copy link
Member Author

rata commented Jan 31, 2023

Thanks! Let me know if I should create the backport PR or how that should work

@kolyshkin
Copy link
Contributor

@kolyshkin I agree, I don't like that either. This PR seems more readable IMHO.

Once this is merged, do we want to backport it? I have not triggered it, but I haven't run runc in mem constrained envs, so strdup can fail. I think it is safe to backport and we can avoid problems (pointer is uninitialized pointing who knows where, better to backport it and not think about it IMHO).

Hmm, I don't see where do we have an uninitialized pointer. When strdup fails, the pointer is NULL. Theoretically, it could be a problem for printf-style functions. Practically, in glibc it's OK to pass NULL as %s positional argument.

Adding curly braces remove the redundant ret check but with the current code it's just that -- a redundant check.

Perhaps I am missing something here (trying to cut on my espresso intake)?

@rata
Copy link
Member Author

rata commented Feb 1, 2023

@kolyshkin Oh, I didn't know passing NULL to %s in printf works fine. If we only support glibc, it might be ok, then. The other case, the code that was outside the else clause is executed anyways, so even without this patch if asnprintf fails, stage will be set to NULL. I was thinking too fast.

I'd prefer to backport it, just in case some other lib is used or some version doesn't behave correctly, because it is a very simple patch to backport and we need to do other backports anyways. But as you and others prefer :)

@kolyshkin
Copy link
Contributor

You are right that this is undefined behavior and thus needs to be fixed.

Practically, though, the function we're using here (asprintf) is a gnu glibc extension, and glibc prints a %s NULL pointer as (null).

I checked that this is also implemented in musl, and they do the same thing (since BlankOn/musl@5f81468).

The only issue I know is puts(NULL) segfaults in both glibc and musl.

@kolyshkin
Copy link
Contributor

@rata anyway, please do open a backport against the release-1.1 branch, it won't hurt.

@rata
Copy link
Member Author

rata commented Feb 3, 2023

@kolyshkin done!

@kolyshkin kolyshkin added backport/done/1.1 A PR in main branch which was backported to release-1.1 and removed backport/todo/1.1 A PR in main branch which needs to be backported to release-1.1 labels Apr 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport/done/1.1 A PR in main branch which was backported to release-1.1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants