Skip to content

Commit

Permalink
lock-util: do not expect EACCES when it cannot happen
Browse files Browse the repository at this point in the history
As per the documentation, EACCES is only returned when F_SETLK is
used, and only on some platforms, which doesn't seem to include
Linux:

https://github.com/torvalds/linux/blob/master/fs/locks.c

F_OFD_SETLK is documented to only return EAGAIN, and F_SETLKW/F_OFD_SETLKW
are blocking operations so this logic doesn't apply to them in the
first place.

Hence, only automatically convert EACCES into EAGAIN for F_SETLK
operations, and propagate the original error in the other cases.

This is important because in some cases we catch permission errors
and gracefully fallback, which is not possible if the original error
is lost.

This is an issue in practice because, due to a kernel bug present
before v6.2, AppArmor denies locking on file descriptors to LXC
containers. We support all currently maintained LTS kernels,
including v6.1, where despite a lot of effort and attempts over almost
a year, the bugfix still hasn't been backported, as it is complex and
requires large changes to AppArmor.
On affected kernels, all services running with PrivateNetwork=yes
fail and do not recover, instead of the normal behaviour of gracefully
downgrading to PrivateNetwork=no.

The integration tests in the Debian CI fail due to this issue:

https://ci.debian.net/packages/s/systemd/testing/arm64/46828037/
  • Loading branch information
bluca committed May 22, 2024
1 parent 87ed87e commit 06384eb
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/basic/lock-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,14 @@ static int fcntl_lock(int fd, int operation, bool ofd) {
.l_len = 0,
}));

if (r == -EACCES) /* Treat EACCESS/EAGAIN the same as per man page. */
/* If we are doing non-blocking operations, treat EACCES/EAGAIN the same as per man page. But if
* not, propagate EACCES back, as it will likely be due to an LSM denying the operation (for example
* LXC with AppArmor when running on kernel < 6.2), and in some cases we want to gracefully
* fallback (e.g.: PrivateNetwork=yes). As per documentation, it's only the non-blocking operation
* F_SETLK that might return EACCES on some platforms (although the Linux implementation doesn't
* seem to), as F_SETLKW and F_OFD_SETLKW block so this is not an issue, and F_OFD_SETLK is documented
* to only return EAGAIN if the lock is already held. */
if ((operation & LOCK_NB) && r == -EACCES)
r = -EAGAIN;

return r;
Expand Down

0 comments on commit 06384eb

Please sign in to comment.