Skip to content

Commit 9cbce3e

Browse files
authored
Merge pull request #2714 from pqarmitage/updates
lib: check TMPDIR environment variable is a directory
2 parents 4ca45f1 + 0526a6f commit 9cbce3e

1 file changed

Lines changed: 18 additions & 3 deletions

File tree

lib/utils.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@
3737
#include <stdbool.h>
3838
#include <sys/prctl.h>
3939
#include <sys/resource.h>
40+
#include <sys/stat.h>
4041
#if defined _WITH_LVS_ || defined _HAVE_LIBIPSET_
4142
#include <sys/wait.h>
4243
#endif
4344
#ifdef _WITH_PERF_
4445
#include <stdio.h>
4546
#include <sys/types.h>
46-
#include <sys/stat.h>
4747
#include <sys/epoll.h>
4848
#include <sys/inotify.h>
4949
#endif
@@ -1468,8 +1468,23 @@ keepalived_modprobe(const char *mod_name)
14681468
void
14691469
set_tmp_dir(void)
14701470
{
1471-
if (!(tmp_dir = getenv("TMPDIR")) || tmp_dir[0] != '/')
1472-
tmp_dir = KA_TMP_DIR;
1471+
struct stat statbuf;
1472+
1473+
/* github-advanced-security identifies this as a high security risk.
1474+
* See https://github.com/acassen/keepalived/pull/2661#discussion_r3778043666
1475+
* https://github.com/acassen/keepalived/security/code-scanning/197
1476+
*
1477+
* Is this really an issue? root can open and write any writeable file.
1478+
*/
1479+
if ((tmp_dir = getenv("TMPDIR")) && tmp_dir[0] == '/') {
1480+
/* Check it is a directory */
1481+
if (!stat(tmp_dir, &statbuf) && S_ISDIR(statbuf.st_mode))
1482+
return;
1483+
1484+
log_message(LOG_INFO, "Environment TMPDIR=%s not a directory", tmp_dir);
1485+
}
1486+
1487+
tmp_dir = KA_TMP_DIR;
14731488
}
14741489

14751490
const char *

0 commit comments

Comments
 (0)