From 448b18de2b0211bc8d6e4e13a6e7bd1e20ff3911 Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Mon, 7 Sep 2015 06:06:08 -0600 Subject: [PATCH] Set errno to EINVAL if sudo_lock_* is called with a bad type. --- lib/util/locking.c | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/lib/util/locking.c b/lib/util/locking.c index b33845759..99ba2bfe4 100644 --- a/lib/util/locking.c +++ b/lib/util/locking.c @@ -29,14 +29,15 @@ #ifdef HAVE_STRINGS_H # include #endif /* HAVE_STRING_H */ -#include -#include -#include #ifdef HAVE_STDBOOL_H # include #else # include "compat/stdbool.h" #endif +#include +#include +#include +#include #include "sudo_compat.h" #include "sudo_util.h" @@ -69,6 +70,7 @@ sudo_lock_region_v1(int fd, int type, off_t len) op = F_ULOCK; break; default: + errno = EINVAL; debug_return_bool(false); } debug_return_bool(lockf(fd, op, len) == 0); @@ -83,16 +85,31 @@ sudo_lock_file_v1(int fd, int type) bool sudo_lock_region_v1(int fd, int type, off_t len) { - int func; struct flock lock; + int func; debug_decl(sudo_lock_file, SUDO_DEBUG_UTIL) + switch (type) { + case SUDO_LOCK: + lock.l_type = F_WRLCK; + func = F_SETLKW; + break; + case SUDO_TLOCK: + lock.l_type = F_WRLCK; + func = F_SETLK; + break; + case SUDO_UNLOCK: + lock.l_type = F_UNLCK; + func = F_SETLK; + break; + default: + errno = EINVAL; + debug_return_bool(false); + } lock.l_start = 0; lock.l_len = len; lock.l_pid = 0; - lock.l_type = (type == SUDO_UNLOCK) ? F_UNLCK : F_WRLCK; lock.l_whence = len ? SEEK_CUR : SEEK_SET; - func = (type == SUDO_LOCK) ? F_SETLKW : F_SETLK; debug_return_bool(fcntl(fd, func, &lock) == 0); } -- 2.40.0