Skip to content

Commit 4fe3f15

Browse files
committed
Lock I/O log timing file when opening for writing.
1 parent c6141eb commit 4fe3f15

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

include/sudo_iolog.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ struct timing_closure {
8282
struct iolog_file {
8383
bool enabled;
8484
bool compressed;
85+
bool locked;
8586
bool writable;
8687
union {
8788
FILE *f;

lib/iolog/iolog_open.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@ static unsigned char const gzip_magic[2] = {0x1f, 0x8b};
4040
/*
4141
* Open the specified I/O log file and store in iol.
4242
* Stores the open file handle which has the close-on-exec flag set.
43+
* Also locks the file if iofd is IOFD_TIMING and mode is writable.
4344
*/
4445
bool
4546
iolog_open(struct iolog_file *iol, int dfd, int iofd, const char *mode)
4647
{
4748
int flags;
4849
const char *file;
50+
bool lockit = false;
4951
unsigned char magic[2];
5052
const uid_t iolog_uid = iolog_get_uid();
5153
const gid_t iolog_gid = iolog_get_gid();
@@ -66,11 +68,25 @@ iolog_open(struct iolog_file *iol, int dfd, int iofd, const char *mode)
6668
"%s: invalid iofd %d", __func__, iofd);
6769
debug_return_bool(false);
6870
}
71+
if (iofd == IOFD_TIMING && ISSET(flags, O_WRONLY|O_RDWR)) {
72+
lockit = true;
73+
}
6974

7075
iol->writable = false;
7176
iol->compressed = false;
77+
iol->locked = false;
7278
if (iol->enabled) {
7379
int fd = iolog_openat(dfd, file, flags);
80+
if (lockit && fd != -1) {
81+
if (sudo_lock_file(fd, SUDO_TLOCK)) {
82+
iol->locked = true;
83+
} else {
84+
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO,
85+
"%s: unable to lock %s", __func__, file);
86+
close(fd);
87+
fd = -1;
88+
}
89+
}
7490
if (fd != -1) {
7591
if (*mode == 'w') {
7692
if (fchown(fd, iolog_uid, iolog_gid) != 0) {

logsrvd/logsrv_util.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ iolog_open_all(int dfd, const char *iolog_dir, struct iolog_file *iolog_files,
9999
int iofd;
100100
debug_decl(iolog_open_all, SUDO_DEBUG_UTIL);
101101

102-
for (iofd = 0; iofd < IOFD_MAX; iofd++) {
102+
/* Iterate in reverse order so we open/lock IOFD_TIMING first. */
103+
for (iofd = IOFD_MAX - 1; iofd >= 0; iofd--) {
103104
iolog_files[iofd].enabled = true;
104105
if (!iolog_open(&iolog_files[iofd], dfd, iofd, mode)) {
105106
if (errno != ENOENT) {

0 commit comments

Comments
 (0)