8000 fn: fix TestWriteFile when running as root by starius · Pull Request #10186 · lightningnetwork/lnd · GitHub
[go: up one dir, main page]

Skip to content

Conversation

starius
Copy link
Collaborator
@starius starius commented Sep 1, 2025

Change Description

Root can bypass read-only file permissions, which made the test failure confusing: a file was overwritten even though it shouldn't have been. This is caused by process capability CAP_DAC_OVERRIDE which root processes often have in Linux. To workaround this, the owner of the file is changed to uid=1000 and gid=1000 (root can do it). If a file belongs to a world-writable sticky dir (like /tmp where the tested file is created) and fs.protected_regular kernel feature is enabled (a common practice on modern Linux distros), such file can't be overwritten by root.

Steps to Test

$ cd fn/

$ go test -v -run TestWriteFile
=== RUN   TestWriteFile
=== PAUSE TestWriteFile
=== RUN   TestWriteFileRemove
=== PAUSE TestWriteFileRemove
=== CONT  TestWriteFile
=== CONT  TestWriteFileRemove
--- PASS: TestWriteFileRemove (0.03s)
--- PASS: TestWriteFile (0.04s)
PASS
ok      github.com/lightningnetwork/lnd/fn/v2   0.040s

$ sudo su
# go test -v -run TestWriteFile
=== RUN   TestWriteFile
=== PAUSE TestWriteFile
=== RUN   TestWriteFileRemove
=== PAUSE TestWriteFileRemove
=== CONT  TestWriteFile
=== CONT  TestWriteFileRemove
--- PASS: TestWriteFile (0.01s)
--- PASS: TestWriteFileRemove (0.01s)
PASS
ok      github.com/lightningnetwork/lnd/fn/v2   0.017s
# 

Pull Request Checklist

Testing

  • Your PR passes all CI checks.
  • Tests covering the positive and negative (error paths) are included.
  • Bug fixes contain tests triggering the bug to prevent regressions.

Code Style and Documentation

📝 Please see our Contribution Guidelines for further guidance.

Root can bypass read-only file permissions, which made the test failure
confusing: a file was overwritten even though it shouldn't have been. This is
caused by process capability CAP_DAC_OVERRIDE which root processes often have in
Linux. To workaround this, the owner of the file is changed to uid=1000 and
gid=1000 (root can do it). If a file belongs to a world-writable sticky dir
(like /tmp where the tested file is created) and fs.protected_regular kernel
feature is enabled (a common practice on modern Linux distros), such file can't
be overwritten by root.
@starius starius marked this pull request as ready for review September 2, 2025 01:53
@Abdulkbk
Copy link
Contributor

Tested this, but it still throws the error. I tested it on a mac though.

Screenshot 2025-09-23 at 15 08 58

@starius
Copy link
Collaborator Author
starius commented Sep 24, 2025

@Abdulkbk Thanks for testing!

I don't know how to address it reliably.

One option I tried is to use immutable files via github.com/g0rbe/go-chattr package (chattr +i). This approach works on Linux for fn.WriteFile (and I hope it would work on Mac too via unix.Chflags), but it makes the file completely immutable which prevent its removal by fn.WriteFileRemove.

Do you know an alternative way of making write fail which we could use in the test?

@Abdulkbk
Copy link
Contributor
Abdulkbk commented Oct 1, 2025

I don't know a reliable way to handle this either. My first thought was that if this causes issues with checks, we could skip it when root is used, but that just hides the problem instead of fixing it.

@starius starius marked this pull request as draft October 4, 2025 01:23
@starius
Copy link
Collaborator Author
starius commented Oct 4, 2025

Converting this to draft until a reliable solution is found.

7365

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.

2 participants
0