-
Notifications
You must be signed in to change notification settings - Fork 197
init/dhcp: only overwrite resolv.conf with DNS #698
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: main
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 |
|---|---|---|
|
|
@@ -293,6 +293,9 @@ static unsigned char get_dhcp_msg_type(const unsigned char *response, | |
| static int handle_dhcp_ack(int nl_sock, int iface_index, | ||
| const unsigned char *response, ssize_t len) | ||
| { | ||
| FILE *resolv = NULL; | ||
| bool tried_opening_resolv = false; | ||
|
|
||
| /* Need at least 240 bytes (DHCP header + magic cookie) + 1 for options */ | ||
| if (len < 241) { | ||
| printf("DHCPACK too short (%zd bytes)\n", len); | ||
|
|
@@ -314,11 +317,6 @@ static int handle_dhcp_ack(int nl_sock, int iface_index, | |
| /* Clamp MTU to passt's limit */ | ||
| uint16_t mtu = 65520; | ||
|
|
||
| FILE *resolv = fopen("/etc/resolv.conf", "w"); | ||
| if (!resolv) { | ||
| perror("Failed to open /etc/resolv.conf"); | ||
| } | ||
|
|
||
| /* Parse DHCP options (start at offset 240 after magic cookie) */ | ||
| size_t p = 240; | ||
| while (p < (size_t)len) { | ||
|
|
@@ -353,6 +351,14 @@ static int handle_dhcp_ack(int nl_sock, int iface_index, | |
| memcpy(&router.s_addr, &response[p], sizeof(router.s_addr)); | ||
| } else if (opt == 6 && opt_len >= 4) { | ||
| /* Option 6: Domain Name Server */ | ||
| if (!resolv && !tried_opening_resolv) { | ||
| tried_opening_resolv = true; | ||
| resolv = fopen("/etc/resolv.conf", "w"); | ||
| if (!resolv) { | ||
| perror("Failed to open /etc/resolv.conf"); | ||
| } | ||
| } | ||
|
Comment on lines
+354
to
+360
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. When if (!resolv && !tried_opening_resolv) {
tried_opening_resolv = true;
resolv = fopen("/etc/resolv.conf", "w");
if (!resolv) {
// Use the consistent error reporting pattern of this file
}
}References
Comment on lines
+354
to
+360
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. To simplify resource management and eliminate function-level state, consider localizing the file opening and closing directly within the DNS option block. Additionally, avoid using the magic number References
|
||
|
|
||
| if (resolv) { | ||
| for (int dns_p = p; dns_p + 4 <= p + opt_len; dns_p += 4) { | ||
| fprintf(resolv, "nameserver %d.%d.%d.%d\n", response[dns_p], | ||
|
|
||
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.
Since
resolvis now initialized toNULLand only opened conditionally when a DNS option is present, any subsequent call tofclose(resolv)at the end ofhandle_dhcp_ackmust be guarded with a NULL check (i.e.,if (resolv) fclose(resolv);) to prevent a segmentation fault when no DNS options are provided in the DHCP response.Additionally, if
handle_dhcp_ackcontains any early return paths after the options loop,resolvmust be closed before returning to prevent a file descriptor leak.