Skip to content

utils: Handle close_range more gracefully#4596

Merged
kolyshkin merged 2 commits into
opencontainers:mainfrom
evanphx:evanphx/b-close-range
Feb 12, 2025
Merged

utils: Handle close_range more gracefully#4596
kolyshkin merged 2 commits into
opencontainers:mainfrom
evanphx:evanphx/b-close-range

Conversation

@evanphx

@evanphx evanphx commented Jan 20, 2025

Copy link
Copy Markdown
Contributor

This fixes 2 things:

  1. The last argument needs to be at most a int32, so be sure to pass only that as the max (discovered under gvisor)
  2. If close_range fails, don't error out, instead just close the fds the long way.

@evanphx
evanphx force-pushed the evanphx/b-close-range branch 2 times, most recently from d212348 to 0bdf924 Compare January 20, 2025 04:45
return nil
}

logrus.Debugf("close_range failed, closing range one at a time (error: %v)", err)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not sure I get this. If close range fails and we have detected closeRange is available, what does this fallback add?

The errors it can return don't seem like something using the regular close fallback will fix: https://man7.org/linux/man-pages/man2/close_range.2.html.

Am I missing something? Do you see where this new fallback can help?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It's more defensive, given the existing defensive attempts to see if close_range is available at all. We probe if we can use it, and thusly if for some reason the probe was wrong, we've got the fallback functionality at hand so seems prudent to use it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I agree with @evanphx here -- if we can use a fallback, it makes more sense to use it rather than give up and fail right away. Yes, we might still fail later, but oh well, we tried.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Cool! Should be all ready now.

Comment thread libcontainer/utils/utils_unix.go

@kolyshkin kolyshkin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

While the fallback makes sense, the call to CloseRange is guarded by haveCloseRangeCloexec, meaning if the former fails, the latter is not an adequate check.

Or is it failing only because gvisor doesn't like MaxUint? Maybe it makes sense to add const closeRangeMaxFD and use it in both CloseExecFrom and haveCloseRangeCloexec?

Also, the first and last arguments of close_range are of unsigned int type in both glibc and the kernel. From a cursory look at the kernel code I don't see how specifying MaxUint64 can break things in the kernel.

@kolyshkin

Copy link
Copy Markdown
Contributor

Also, the first and last arguments of close_range are of unsigned int type in both glibc and the kernel. From a cursory look at the kernel code I don't see how specifying MaxUint64 can break things in the kernel.

My bad, I forgot that C int is 32-bit even on a 64-bit platform.

@rata

rata commented Jan 22, 2025

Copy link
Copy Markdown
Member

@kolyshkin how are these PRs related to gvisor? I thought gvisor was used as an OCI runtime, as a runc replacement. But from your comment, it seems that is not what is happening here, is it? How is that runc and gvisor are run together in this scenario?

@evanphx

evanphx commented Jan 22, 2025 via email

Copy link
Copy Markdown
Contributor Author

@evanphx
evanphx force-pushed the evanphx/b-close-range branch from 0bdf924 to 33315a0 Compare January 24, 2025 19:55

@rata rata left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, thanks!

@kolyshkin kolyshkin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@cyphar PTAL

@kolyshkin
kolyshkin requested a review from cyphar January 27, 2025 20:39
@kolyshkin

Copy link
Copy Markdown
Contributor

@cyphar PTAL

@evanphx is this something that you'd like to be backported to runc v1.2.x, or is it OK for you to wait for runc v1.3?

@evanphx

evanphx commented Feb 7, 2025 via email

Copy link
Copy Markdown
Contributor Author

@kolyshkin
kolyshkin merged commit 35a28ad into opencontainers:main Feb 12, 2025
@cyphar cyphar mentioned this pull request Mar 4, 2025
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.

3 participants