-
Notifications
You must be signed in to change notification settings - Fork 138
manager: Fix loosing iface options on CARRIER #548
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,6 +44,7 @@ | |
| #include <string.h> | ||
| #include <unistd.h> | ||
| #include <time.h> | ||
| #include <assert.h> | ||
|
|
||
| #include "config.h" | ||
| #include "common.h" | ||
|
|
@@ -2986,6 +2987,68 @@ add_options(struct dhcpcd_ctx *ctx, const char *ifname, | |
| return r; | ||
| } | ||
|
|
||
| #define ARGV_COPY_MAGIC ((char *)0x5a54292d273f3d34) | ||
| /*^ intentional truncation on 32bit arches */ | ||
|
|
||
| char **copy_argv(int argc, char **argv) | ||
| { | ||
| int i; | ||
| size_t strslen = 0; | ||
|
|
||
| for (i = 0; i < argc; i++) { | ||
DanielG marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| strslen += strlen(argv[i]) + 1; | ||
| } | ||
| if (strslen == 0) // also handles argc < 0 | ||
| return NULL; | ||
|
|
||
| unsigned nptrs = 1 + (unsigned)argc + 1; | ||
| size_t ptrslen = nptrs * sizeof(char *); | ||
| void *buf = malloc(ptrslen + strslen); | ||
| char **ptrs = buf; | ||
|
|
||
| if (!buf) | ||
| return NULL; | ||
|
|
||
| ptrs[0] = ARGV_COPY_MAGIC; | ||
| ptrs[nptrs - 1] = NULL; | ||
|
|
||
| if (argc == 0) | ||
| goto out; | ||
|
|
||
| char *strsp = (char *)&ptrs[nptrs]; | ||
|
|
||
| for (i = 0; i < argc; i++) { | ||
| size_t len = strlcpy(strsp, argv[i], strslen); | ||
| if (len >= strslen) // truncated | ||
| goto err; | ||
|
|
||
| ptrs[1 + i] = strsp; | ||
|
|
||
| strsp += len + 1; | ||
| if (strslen < len + 1) | ||
| goto err; | ||
| strslen -= len + 1; | ||
| } | ||
|
|
||
| assert(strslen == 0); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I find it interesting that you assert this error but allow impossible errors to pass with a failure in the loop.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What "impossible errors" are you talking about? |
||
| assert(ptrs[nptrs - 1] == NULL); | ||
| out: | ||
| return &ptrs[1]; | ||
|
|
||
| err: | ||
| free(buf); | ||
| return NULL; | ||
| } | ||
|
|
||
| void free_argv_copy(char **argv) | ||
| { | ||
| assert(argv[-1] == ARGV_COPY_MAGIC); | ||
| if (argv[-1] != ARGV_COPY_MAGIC) { | ||
| logerrx("%s: invalid argv", __func__); | ||
| } else | ||
| free(&argv[-1]); | ||
| } | ||
|
|
||
| void | ||
| free_options(struct dhcpcd_ctx *ctx, struct if_options *ifo) | ||
| { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is scary.
Why do we need a sentinel here? The scope of the variable isn't going outside any process so we can just malloc and free which makes the below code easier.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The sentinel is for memory safety. Think: double free or free or corrupted/wrong pointer being passed in. Always good manners for allocators IMO. Not sure what you mean by "going outside any process".
The sentinel could just as well be 32bit if you like, I just like to use the extra entropy modern machines can offer. The explicit cast should supress truncation warnings but I'm not 100% on that. Was hoping CI would show whether it raises a warning on on 32bit, but it seems there is no coverage.