libsemanage: open lock_file with O_RDWR

man 2 flock:
    Since Linux 2.6.12, NFS clients support flock() locks by emulating
    them as fcntl(2) byte-range locks on the entire file.  This means
    that fcntl(2) and  flock()  locks  do  interact with one another
    over NFS.  It also means that in order to place an exclusive lock,
    the file must be opened for writing.

Fixes:
    # semanage fcontext -d -e /home /tmp/testdir
    libsemanage.semanage_get_lock: Error obtaining direct transaction lock at /var/lib/selinux/targeted/semanage.trans.LOCK. (Bad file descriptor).
    OSError: Bad file descriptor

Signed-off-by: Petr Lautrbach <lautrbach@redhat.com>
Acked-by: James Carter <jwcart2@gmail.com>
This commit is contained in:
Petr Lautrbach 2024-11-11 13:32:50 +01:00 committed by James Carter
parent 77da320e29
commit 267d4f9bf5
1 changed files with 6 additions and 8 deletions

View File

@ -1899,7 +1899,6 @@ static int semanage_get_lock(semanage_handle_t * sh,
struct timeval origtime, curtime; struct timeval origtime, curtime;
int got_lock = 0; int got_lock = 0;
if ((fd = open(lock_file, O_RDONLY)) == -1) {
if ((fd = if ((fd =
open(lock_file, O_RDWR | O_CREAT | O_TRUNC, open(lock_file, O_RDWR | O_CREAT | O_TRUNC,
S_IRUSR | S_IWUSR)) == -1) { S_IRUSR | S_IWUSR)) == -1) {
@ -1907,7 +1906,6 @@ static int semanage_get_lock(semanage_handle_t * sh,
lock_file); lock_file);
return -1; return -1;
} }
}
if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0) { if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0) {
ERR(sh, "Could not set close-on-exec for %s at %s.", lock_name, ERR(sh, "Could not set close-on-exec for %s at %s.", lock_name,
lock_file); lock_file);