Skip to content

MWI: Fix missing O_CLOEXEC in botfs.openSecure() and other issues#55411

Merged
timothyb89 merged 4 commits intomasterfrom
timothyb89/botfs-cloexec
Jun 6, 2025
Merged

MWI: Fix missing O_CLOEXEC in botfs.openSecure() and other issues#55411
timothyb89 merged 4 commits intomasterfrom
timothyb89/botfs-cloexec

Conversation

@timothyb89
Copy link
Copy Markdown
Contributor

This fixes various issues in botfs's openSecure implementation for Linux, particularly:

  • Added missing O_CLOEXEC flag to openat2() flags
  • Added EINTR retry loop to follow the stdlib's OpenFile() implementation.
  • Remove O_CREATE from ReadFlags, and convert it to a pseudo-enum so we can accurately predict intent (see next item)
  • Pass 0 mode to openat2() when reading (see above)
  • Renamed confusingly named OpenMode to OpenFlags, because it corresponds to flags, not file modes.
  • Fixed coincidentally correct misuse of unix.O_RDONLY

Note that while removing O_CREATE from read flags is technically a breaking change, we still automatically files downstream as needed. This results in a "new" debug-level log, but not any actual behavior change.

changelog: The tbot client now ensures the O_CLOEXEC flag is used when opening files on Linux hosts

This fixes various issues in botfs's `openSecure` implementation for
Linux, particularly:
- Added missing `O_CLOEXEC` flag to `openat2()` flags
- Added EINTR retry loop to follow the stdlib's `OpenFile()`
  implementation.
- Remove `O_CREATE` from `ReadFlags`, and convert it to a pseudo-enum
  so we can accurately predict intent (see next item)
- Pass `0` mode to `openat2()` when reading (see above)
- Renamed confusingly named `OpenMode` to `OpenFlags`, because it
  corresponds to flags, not file modes.
- Fixed coincidentally correct misuse of `unix.O_RDONLY`

Note that while removing `O_CREATE` from read flags is technically a
breaking change, we still automatically files downstream as needed.
This results in a "new" debug-level log, but not any actual behavior
change.
Comment thread lib/tbot/botfs/fs_linux.go Outdated
return os.NewFile(uintptr(fd), filepath.Base(path)), nil
// os.File.Close() appears to close wrapped files sanely, so rely on that
// rather than relying on callers to use unix.Close(fd)
return os.NewFile(uintptr(fd), filepath.Base(path)), nil
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.

nit: the .Name() of a file opened with os.OpenFile (and thus openStandard, I guess) is the unmodified path passed to OpenFile, do we have to modify the name here?

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.

Good catch, I don't think name is actually used at all so it's a harmless change. I also caught a note in the godoc for os.NewFile() about it potentially returning a nil in some cases, so I've also added a check to convert that into an error.

Comment thread lib/tbot/botfs/fs_linux.go
Comment thread lib/tbot/botfs/botfs.go Outdated
@timothyb89 timothyb89 added this pull request to the merge queue Jun 6, 2025
Merged via the queue into master with commit 4a185cd Jun 6, 2025
40 checks passed
@timothyb89 timothyb89 deleted the timothyb89/botfs-cloexec branch June 6, 2025 02:52
@backport-bot-workflows
Copy link
Copy Markdown
Contributor

@timothyb89 See the table below for backport results.

Branch Result
branch/v16 Failed
branch/v17 Create PR
branch/v18 Create PR

timothyb89 added a commit that referenced this pull request Jun 6, 2025
…sues (#55411)

Backport of #55411 for branch/v16

---

* MWI: Fix missing O_CLOEXEC in `botfs.openSecure()` and other issues

This fixes various issues in botfs's `openSecure` implementation for
Linux, particularly:
- Added missing `O_CLOEXEC` flag to `openat2()` flags
- Added EINTR retry loop to follow the stdlib's `OpenFile()`
  implementation.
- Remove `O_CREATE` from `ReadFlags`, and convert it to a pseudo-enum
  so we can accurately predict intent (see next item)
- Pass `0` mode to `openat2()` when reading (see above)
- Renamed confusingly named `OpenMode` to `OpenFlags`, because it
  corresponds to flags, not file modes.
- Fixed coincidentally correct misuse of `unix.O_RDONLY`

Note that while removing `O_CREATE` from read flags is technically a
breaking change, we still automatically files downstream as needed.
This results in a "new" debug-level log, but not any actual behavior
change.

* Fix lint

* Simplify mode conditional, fix comments

* Handle potentially nil file returns from `os.OpenFile`
github-merge-queue Bot pushed a commit that referenced this pull request Jun 10, 2025
…sues (#55411) (#55504)

Backport of #55411 for branch/v16

---

* MWI: Fix missing O_CLOEXEC in `botfs.openSecure()` and other issues

This fixes various issues in botfs's `openSecure` implementation for
Linux, particularly:
- Added missing `O_CLOEXEC` flag to `openat2()` flags
- Added EINTR retry loop to follow the stdlib's `OpenFile()`
  implementation.
- Remove `O_CREATE` from `ReadFlags`, and convert it to a pseudo-enum
  so we can accurately predict intent (see next item)
- Pass `0` mode to `openat2()` when reading (see above)
- Renamed confusingly named `OpenMode` to `OpenFlags`, because it
  corresponds to flags, not file modes.
- Fixed coincidentally correct misuse of `unix.O_RDONLY`

Note that while removing `O_CREATE` from read flags is technically a
breaking change, we still automatically files downstream as needed.
This results in a "new" debug-level log, but not any actual behavior
change.

* Fix lint

* Simplify mode conditional, fix comments

* Handle potentially nil file returns from `os.OpenFile`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants