Skip to content

Commit bf776ba

Browse files
committed
Add FD_CLOEXEC flag to config files, pipes and sockets
File descriptors are leaking to processes spawned by upsmod and upssched, leading to SELinux errors when (for example) sendmail attempts to read from fd #4.
1 parent 0deb6a6 commit bf776ba

3 files changed

Lines changed: 20 additions & 0 deletions

File tree

clients/upsmon.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#include <sys/stat.h>
2525
#include <sys/wait.h>
2626
#include <sys/socket.h>
27+
#include <unistd.h>
28+
#include <fcntl.h>
2729

2830
#include "upsclient.h"
2931
#include "upsmon.h"
@@ -1432,6 +1434,9 @@ static int try_connect(utype_t *ups)
14321434
/* we're definitely connected now */
14331435
setflag(&ups->status, ST_CONNECTED);
14341436

1437+
/* prevent connection leaking to NOTIFYCMD */
1438+
fcntl(upscli_fd(&ups->conn), F_SETFD, FD_CLOEXEC);
1439+
14351440
/* now try to authenticate to upsd */
14361441

14371442
ret = do_upsd_auth(ups);
@@ -1714,6 +1719,9 @@ static void start_pipe(void)
17141719
}
17151720

17161721
close(pipefd[0]);
1722+
1723+
/* prevent pipe leaking to NOTIFYCMD */
1724+
fcntl(pipefd[1], F_SETFD, FD_CLOEXEC);
17171725
}
17181726

17191727
static void delete_ups(utype_t *target)

clients/upssched.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
#include <sys/socket.h>
4747
#include <sys/un.h>
4848
#include <netinet/in.h>
49+
#include <unistd.h>
50+
#include <fcntl.h>
4951

5052
#include "upssched.h"
5153
#include "timehead.h"
@@ -297,6 +299,9 @@ static int open_sock(void)
297299
if (ret < 0)
298300
fatal_with_errno(EXIT_FAILURE, "listen(%d, %d) failed", fd, US_LISTEN_BACKLOG);
299301

302+
/* don't leak socket to CMDSCRIPT */
303+
fcntl(fd, F_SETFD, FD_CLOEXEC);
304+
300305
return fd;
301306
}
302307

@@ -370,6 +375,9 @@ static void conn_add(int sockfd)
370375
return;
371376
}
372377

378+
/* don't leak connection to CMDSCRIPT */
379+
fcntl(acc, F_SETFD, FD_CLOEXEC);
380+
373381
/* enable nonblocking I/O */
374382

375383
ret = fcntl(acc, F_GETFL, 0);

common/parseconf.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
#include <stdlib.h>
8484
#include <string.h>
8585
#include <unistd.h>
86+
#include <fcntl.h>
8687

8788
#include "parseconf.h"
8889

@@ -443,6 +444,9 @@ int pconf_file_begin(PCONF_CTX_t *ctx, const char *fn)
443444
return 0;
444445
}
445446

447+
/* prevent fd leaking to child processes */
448+
fcntl(fileno(ctx->f), F_SETFD, FD_CLOEXEC);
449+
446450
return 1; /* OK */
447451
}
448452

0 commit comments

Comments
 (0)