Skip to content

init/dhcp: only overwrite resolv.conf with DNS#698

Open
slp wants to merge 1 commit into
containers:mainfrom
slp:init-preserve-resolvconf
Open

init/dhcp: only overwrite resolv.conf with DNS#698
slp wants to merge 1 commit into
containers:mainfrom
slp:init-preserve-resolvconf

Conversation

@slp
Copy link
Copy Markdown
Collaborator

@slp slp commented May 29, 2026

Only overwrite resolv.conf when the DHCP server has provide us some nameservers. This allows users to keep the original resolv.conf in case it was populated beforehand.

@slp
Copy link
Copy Markdown
Collaborator Author

slp commented May 29, 2026

/gemini review

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request defers the opening of /etc/resolv.conf to only when DHCP Option 6 (DNS) is present in the response. The reviewer identified a potential file descriptor leak if multiple Option 6 fields are processed, as well as the loss of error reporting when fopen fails. A code suggestion was provided to conditionally open the file and restore the error logging.

Comment thread init/dhcp.c Outdated
Comment on lines 353 to 354
resolv = fopen("/etc/resolv.conf", "w");
if (resolv) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

If the DHCP response contains multiple Option 6 (DNS) fields, calling fopen repeatedly without checking if resolv is already open will leak the previous file descriptor. Additionally, the error reporting (perror) when fopen fails has been lost in this change. Checking if resolv is already open and restoring the error reporting solves both issues.

            if (!resolv) {
                resolv = fopen("/etc/resolv.conf", "w");
                if (!resolv) {
                    perror("Failed to open /etc/resolv.conf");
                }
            }
            if (resolv) {

@slp slp force-pushed the init-preserve-resolvconf branch from c221a45 to 516b6b0 Compare May 29, 2026 11:19
@slp
Copy link
Copy Markdown
Collaborator Author

slp commented May 29, 2026

/gemini review

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request defers the opening of /etc/resolv.conf in init/dhcp.c to be lazy, only attempting to open the file when a Domain Name Server option (Option 6) is actually processed. The review feedback points out that if this lazy fopen call fails, the error is now silently ignored, and recommends adding error handling that aligns with the file's existing logging patterns.

Comment thread init/dhcp.c
Comment on lines +354 to +357
if (!resolv && !tried_opening_resolv) {
tried_opening_resolv = true;
resolv = fopen("/etc/resolv.conf", "w");
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

When fopen fails to open /etc/resolv.conf, the error is silently ignored. We should report this error, but we must prioritize consistency with the existing error reporting patterns in this file (e.g., using stdout vs stderr or a specific logging function) to maintain a uniform codebase, rather than automatically using perror.

            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
  1. Prioritize consistency with existing error reporting patterns in a file (e.g., using stdout vs stderr) over standard external practices to maintain a uniform codebase.

Only overwrite resolv.conf when the DHCP server has provide us some
nameservers. This allows users to keep the original resolv.conf in case
it was populated beforehand.

Signed-off-by: Sergio Lopez <slp@redhat.com>
@slp slp force-pushed the init-preserve-resolvconf branch from 516b6b0 to 68c6e83 Compare May 29, 2026 11:24
@slp
Copy link
Copy Markdown
Collaborator Author

slp commented May 29, 2026

/gemini review

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request defers the opening of /etc/resolv.conf in handle_dhcp_ack so that it is only opened lazily when a DNS option (Option 6) is encountered in the DHCP response. The review feedback highlights two key areas for improvement: first, ensuring that fclose is guarded against NULL pointers and that early returns do not leak the file descriptor; second, simplifying resource management by localizing the file operations within the DNS option block and replacing the magic number 6 with a named constant.

Comment thread init/dhcp.c
Comment on lines +296 to +297
FILE *resolv = NULL;
bool tried_opening_resolv = false;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

Since resolv is now initialized to NULL and only opened conditionally when a DNS option is present, any subsequent call to fclose(resolv) at the end of handle_dhcp_ack must 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_ack contains any early return paths after the options loop, resolv must be closed before returning to prevent a file descriptor leak.

Comment thread init/dhcp.c
Comment on lines +354 to +360
if (!resolv && !tried_opening_resolv) {
tried_opening_resolv = true;
resolv = fopen("/etc/resolv.conf", "w");
if (!resolv) {
perror("Failed to open /etc/resolv.conf");
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

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 6 directly; define it as a named constant (e.g., DHCP_OPTION_DNS) for improved readability and maintainability. Since all DNS servers are typically provided within a single option block, you can open /etc/resolv.conf, write the nameservers, and close it immediately within this block. This avoids the need for the function-level resolv and tried_opening_resolv variables, and completely eliminates the risk of leaks on early returns or crashes from fclose(NULL).

References
  1. When using magic numbers for DHCP options, define them as named constants for improved readability and maintainability.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant