Blame SOURCES/sudo-1.8.6p7-digest-backport.patch

110381
From c8a6eecf768d8102a9a77f5fdb5b516e571d462e Mon Sep 17 00:00:00 2001
110381
From: Radovan Sroka <rsroka@redhat.com>
110381
Date: Tue, 23 Aug 2016 13:43:08 +0200
110381
Subject: [PATCH] Using libgcrypt
110381
110381
Using libgcrypt and not sudo implementation of SHA...
110381
110381
Rebased patch of digest backport.
110381
Added option --with-gcrypt
110381
110381
Rebased from:
110381
Patch35: sudo-1.8.6p7-digest-backport.patch
110381
110381
Resolves:
110381
rhbz#1183818
110381
---
110381
 configure.ac                 |  16 +++++++
110381
 plugins/sudoers/Makefile.in  |   9 +++-
110381
 plugins/sudoers/filedigest.c | 104 +++++++++++++++++++++++++++++++++++++++++++
110381
 plugins/sudoers/filedigest.h |  17 +++++++
110381
 plugins/sudoers/match.c      |  94 ++++++++++++++++++++++++++++++--------
110381
 5 files changed, 219 insertions(+), 21 deletions(-)
110381
 create mode 100644 plugins/sudoers/filedigest.c
110381
 create mode 100644 plugins/sudoers/filedigest.h
110381
110381
diff --git a/configure.ac b/configure.ac
110381
index 13c3c1b..54929b2 100644
110381
--- a/configure.ac
110381
+++ b/configure.ac
110381
@@ -35,6 +35,7 @@ AC_SUBST([SUDO_OBJS])
110381
 AC_SUBST([LIBS])
110381
 AC_SUBST([SUDO_LIBS])
110381
 AC_SUBST([SUDOERS_LIBS])
110381
+AC_SUBST([LIBPARSESUDOERS_LIBS])
110381
 AC_SUBST([STATIC_SUDOERS])
110381
 AC_SUBST([NET_LIBS])
110381
 AC_SUBST([AFS_LIBS])
110381
@@ -1517,6 +1518,19 @@ AC_ARG_WITH(selinux, [AS_HELP_STRING([--with-selinux], [enable SELinux support])
110381
 		;;
110381
 esac], [with_selinux=no])
110381
 
110381
+AC_ARG_WITH(gcrypt, [AS_HELP_STRING([--with-gcrypt], [enable libgcrypt support])],
110381
+[case $with_gcrypt in
110381
+    yes)
110381
+               AC_DEFINE(HAVE_LIBGCRYPT)
110381
+               LIBPARSESUDOERS_LIBS="${LIBPARSESUDOERS_LIBS} -lgcrypt"
110381
+               AC_CHECK_LIB([gcrypt], [gcry_md_open],
110381
+                   [AC_DEFINE(HAVE_GCRY_MD_OPEN)])
110381
+               ;;
110381
+    no)                ;;
110381
+    *)         AC_MSG_ERROR(["--with-gcrypt does not take an argument."])
110381
+               ;;
110381
+esac])
110381
+
110381
 dnl
110381
 dnl gss_krb5_ccache_name() may not work on Heimdal so we don't use it by default
110381
 dnl
110381
@@ -4344,6 +4358,8 @@ AH_TEMPLATE(HAVE_PROJECT_H, [Define to 1 if you have the <project.h> header file
110381
 AH_TEMPLATE(HAVE_SECURID, [Define to 1 if you use SecurID for authentication.])
110381
 AH_TEMPLATE(HAVE_SELINUX, [Define to 1 to enable SELinux RBAC support.])
110381
 AH_TEMPLATE(HAVE_SETKEYCREATECON, [Define to 1 if you have the `setkeycreatecon' function.])
110381
+AH_TEMPLATE(HAVE_LIBGCRYPT, [Define to 1 to enable libgcrypt support.])
110381
+AH_TEMPLATE(HAVE_GCRY_MD_OPEN, [Define to 1 if you have the `gcry_md_open' function.])
110381
 AH_TEMPLATE(HAVE_SHL_LOAD, [Define to 1 if you have the `shl_load' function.])
110381
 AH_TEMPLATE(HAVE_SKEY, [Define to 1 if you use S/Key.])
110381
 AH_TEMPLATE(HAVE_SKEYACCESS, [Define to 1 if your S/Key library has skeyaccess().])
110381
diff --git a/plugins/sudoers/Makefile.in b/plugins/sudoers/Makefile.in
110381
index f36f9ef..32c0ed0 100644
110381
--- a/plugins/sudoers/Makefile.in
110381
+++ b/plugins/sudoers/Makefile.in
110381
@@ -55,6 +55,7 @@ LT_LIBS = $(top_builddir)/lib/util/libsudo_util.la
110381
 LIBS = $(LT_LIBS)
110381
 NET_LIBS = @NET_LIBS@
110381
 SUDOERS_LIBS = @SUDOERS_LIBS@ @AFS_LIBS@ @GETGROUPS_LIB@ $(LIBS) $(NET_LIBS) @ZLIB@ @LIBMD@
110381
+LIBPARSESUDOERS_LIBS = @LIBPARSESUDOERS_LIBS@
110381
 REPLAY_LIBS = @REPLAY_LIBS@ @ZLIB@
110381
 VISUDO_LIBS = $(NET_LIBS) @LIBMD@
110381
 TESTSUDOERS_LIBS = $(NET_LIBS) @LIBMD@
110381
@@ -153,7 +154,7 @@ AUTH_OBJS = sudo_auth.lo @AUTH_OBJS@
110381
 LIBPARSESUDOERS_OBJS = alias.lo audit.lo base64.lo defaults.lo hexchar.lo \
110381
 		       gram.lo match.lo match_addr.lo pwutil.lo pwutil_impl.lo \
110381
 		       rcstr.lo redblack.lo sudoers_debug.lo timestr.lo \
110381
-		       toke.lo toke_util.lo
110381
+		       toke.lo toke_util.lo filedigest.lo
110381
 
110381
 SUDOERS_OBJS = $(AUTH_OBJS) boottime.lo check.lo editor.lo env.lo find_path.lo \
110381
 	       gc.lo goodpath.lo group_plugin.lo interfaces.lo iolog.lo \
110381
@@ -217,7 +218,7 @@ Makefile: $(srcdir)/Makefile.in
110381
 	(cd $(top_builddir) && ./config.status --file plugins/sudoers/Makefile)
110381
 
110381
 libparsesudoers.la: $(LIBPARSESUDOERS_OBJS)
110381
-	$(LIBTOOL) $(LTFLAGS) --mode=link $(CC) -o $@ $(LIBPARSESUDOERS_OBJS) -no-install
110381
+	$(LIBTOOL) --mode=link $(CC) -o $@ $(LIBPARSESUDOERS_OBJS) $(LIBPARSESUDOERS_LIBS) -no-install
110381
 
110381
 sudoers.la: $(SUDOERS_OBJS) $(LT_LIBS) libparsesudoers.la @LT_LDDEP@
110381
 	case "$(LT_LDFLAGS)" in \
110381
@@ -656,6 +657,10 @@ env.lo: $(srcdir)/env.c $(devdir)/def_data.h $(incdir)/compat/stdbool.h \
110381
         $(srcdir)/sudoers.h $(srcdir)/sudoers_debug.h $(top_builddir)/config.h \
110381
         $(top_builddir)/pathnames.h
110381
 	$(LIBTOOL) $(LTFLAGS) --mode=compile $(CC) -c $(CPPFLAGS) $(CFLAGS) $(ASAN_CFLAGS) $(PIE_CFLAGS) $(SSP_CFLAGS) $(srcdir)/env.c
110381
+filedigest.lo: $(srcdir)/filedigest.c $(top_builddir)/config.h \
110381
+        $(incdir)/sudo_debug.h
110381
+	$(LIBTOOL) --mode=compile $(CC) -c $(CPPFLAGS) $(CFLAGS) $(ASAN_CFLAGS) $(PIE_CFLAGS) $(SSP_CFLAGS) $(DEFS) $(srcdir)/filedigest.c
110381
+filedigest.o: filedigest.lo
110381
 find_path.lo: $(srcdir)/find_path.c $(devdir)/def_data.h \
110381
               $(incdir)/compat/stdbool.h $(incdir)/sudo_compat.h \
110381
               $(incdir)/sudo_conf.h $(incdir)/sudo_debug.h \
110381
diff --git a/plugins/sudoers/filedigest.c b/plugins/sudoers/filedigest.c
110381
new file mode 100644
110381
index 0000000..c173741
110381
--- /dev/null
110381
+++ b/plugins/sudoers/filedigest.c
110381
@@ -0,0 +1,104 @@
110381
+#include <config.h>
110381
+#include <errno.h>
110381
+#include <stddef.h>
110381
+#include <sys/types.h>
110381
+#include <sys/stat.h>
110381
+#include <fcntl.h>
110381
+#include <unistd.h>
110381
+#include "filedigest.h"
110381
+#include "sudo_compat.h"
110381
+#include "sudo_debug.h"
110381
+
110381
+#if defined(HAVE_LIBGCRYPT)
110381
+#include <gcrypt.h>
110381
+
110381
+static int sudo_filedigest_gcrypt(int fd, int algo, unsigned char **dvalue, size_t *dvalue_size)
110381
+{
110381
+  char buffer[4096];
110381
+  gcry_md_hd_t ctx;
110381
+  int gcry_algo;
110381
+  debug_decl(sudo_filedigest_gcrypt, SUDO_DEBUG_UTIL);
110381
+
110381
+  switch(algo) {
110381
+  case SUDO_DIGEST_SHA224:
110381
+    gcry_algo = GCRY_MD_SHA224; break;
110381
+  case SUDO_DIGEST_SHA256:
110381
+    gcry_algo = GCRY_MD_SHA256; break;
110381
+  case SUDO_DIGEST_SHA384:
110381
+    gcry_algo = GCRY_MD_SHA384; break;
110381
+  case SUDO_DIGEST_SHA512:
110381
+    gcry_algo = GCRY_MD_SHA512; break;
110381
+  default:
110381
+    debug_return_int(-1);
110381
+  }
110381
+
110381
+  gcry_md_open(&ctx, gcry_algo, 0);
110381
+
110381
+  /* Read block of data from fd and digest them */
110381
+  while (1) {
110381
+    const ssize_t read_bytes = read(fd, buffer, sizeof buffer);
110381
+
110381
+    if (read_bytes < 0) {
110381
+      /* Error */
110381
+      gcry_md_close(ctx);
110381
+      debug_return_int(-1);
110381
+    }
110381
+    else if (read_bytes > 0) {
110381
+      /* Some data read -- update the digest */
110381
+      gcry_md_write(ctx, buffer, (size_t)read_bytes);
110381
+    }
110381
+    else {
110381
+      /* EOF */
110381
+      break;
110381
+    }
110381
+  }
110381
+
110381
+  /*
110381
+   * All data digested. Finalize the digest value.
110381
+   */
110381
+  const unsigned char *value = gcry_md_read(ctx, gcry_algo);
110381
+
110381
+  if (value == NULL) {
110381
+    debug_return_int(-1);
110381
+  }
110381
+
110381
+  /*
110381
+   * Make a copy of the digest value. The pointer
110381
+   * returned from gcry_md_read cannot be used after
110381
+   * gcry_md_close was called
110381
+   */
110381
+  (*dvalue_size) = gcry_md_get_algo_dlen(gcry_algo);
110381
+  (*dvalue) = malloc(*dvalue_size);
110381
+
110381
+  if (*dvalue == NULL) {
110381
+    debug_return_int(-1);
110381
+  }
110381
+
110381
+  memcpy(*dvalue, value, *dvalue_size);
110381
+  gcry_md_close(ctx);
110381
+
110381
+  debug_return_int(0);
110381
+}
110381
+#endif
110381
+
110381
+#include <stdio.h>
110381
+
110381
+int sudo_filedigest(const char *path, int algo, unsigned char **dvalue, size_t *dvalue_size)
110381
+{
110381
+  int rc = -1;
110381
+  int fd = -1;
110381
+  debug_decl(sudo_filedigest, SUDO_DEBUG_UTIL);
110381
+
110381
+  if ((fd = open(path, O_RDONLY)) < 0) {
110381
+    debug_return_int(rc);
110381
+  }
110381
+
110381
+#if defined(HAVE_LIBGCRYPT)
110381
+  rc = sudo_filedigest_gcrypt(fd, algo, dvalue, dvalue_size);
110381
+  close(fd);
110381
+#else
110381
+  rc = -1;
110381
+  errno = ENOTSUP;
110381
+#endif
110381
+  debug_return_int(rc);
110381
+}
110381
diff --git a/plugins/sudoers/filedigest.h b/plugins/sudoers/filedigest.h
110381
new file mode 100644
110381
index 0000000..437f02f
110381
--- /dev/null
110381
+++ b/plugins/sudoers/filedigest.h
110381
@@ -0,0 +1,17 @@
110381
+#include <stddef.h>
110381
+
110381
+#define SUDO_DIGEST_SHA224	0
110381
+#define SUDO_DIGEST_SHA256	1
110381
+#define SUDO_DIGEST_SHA384	2
110381
+#define SUDO_DIGEST_SHA512	3
110381
+#define SUDO_DIGEST_INVALID     4
110381
+
110381
+#define SUDO_SHA224_DIGEST_LENGTH 28
110381
+#define SUDO_SHA256_DIGEST_LENGTH 32
110381
+#define SUDO_SHA384_DIGEST_LENGTH 48
110381
+#define SUDO_SHA512_DIGEST_LENGTH 64
110381
+
110381
+/*
110381
+ * Compute a digest of a given file. Returns 0 on success, -1 otherwise.
110381
+ */
110381
+int sudo_filedigest(const char *path, int algo, unsigned char **dvalue, size_t *dvalue_size);
110381
diff --git a/plugins/sudoers/match.c b/plugins/sudoers/match.c
110381
index 1916bde..2a9ea4b 100644
110381
--- a/plugins/sudoers/match.c
110381
+++ b/plugins/sudoers/match.c
110381
@@ -62,6 +62,7 @@
110381
 
110381
 #include "sudoers.h"
110381
 #include "parse.h"
110381
+#include "filedigest.h"
110381
 #include <gram.h>
110381
 
110381
 #ifdef HAVE_FNMATCH
110381
@@ -576,6 +577,7 @@ command_matches_normal(const char *sudoers_cmnd, const char *sudoers_args, const
110381
 }
110381
 #else /* !SUDOERS_NAME_MATCH */
110381
 
110381
+#ifndef HAVE_LIBGCRYPT /* !!! */
110381
 static struct digest_function {
110381
     const char *digest_name;
110381
     const unsigned int digest_len;
110381
@@ -616,24 +618,43 @@ static struct digest_function {
110381
 	NULL
110381
     }
110381
 };
110381
+#endif /* !HAVE_LIBGCRYPT */
110381
+
110381
+static const char *digesttype2str(int digest_type)
110381
+{
110381
+    switch(digest_type) {
110381
+        case SUDO_DIGEST_SHA224:
110381
+            return "SHA224";
110381
+        case SUDO_DIGEST_SHA256:
110381
+            return "SHA256";
110381
+        case SUDO_DIGEST_SHA384:
110381
+            return "SHA384";
110381
+        case SUDO_DIGEST_SHA512:
110381
+            return "SHA512";
110381
+    }
110381
+    return "<INVALID>";
110381
+}
110381
 
110381
 static bool
110381
 digest_matches(const char *file, const struct sudo_digest *sd, int *fd)
110381
 {
110381
-    unsigned char file_digest[SHA512_DIGEST_LENGTH];
110381
-    unsigned char sudoers_digest[SHA512_DIGEST_LENGTH];
110381
+    unsigned char * file_digest = NULL;
110381
+    unsigned char * sudoers_digest = NULL;
110381
+    size_t digest_size;
110381
     unsigned char buf[32 * 1024];
110381
-    struct digest_function *func = NULL;
110381
 #ifdef HAVE_FEXECVE
110381
     bool first = true;
110381
     bool is_script = false;
110381
 #endif /* HAVE_FEXECVE */
110381
     size_t nread;
110381
-    SHA2_CTX ctx;
110381
     FILE *fp;
110381
     unsigned int i;
110381
     debug_decl(digest_matches, SUDOERS_DEBUG_MATCH)
110381
 
110381
+#ifndef HAVE_LIBGCRYPT /* !!! */
110381
+
110381
+    SHA2_CTX ctx;
110381
+    struct digest_function *func = NULL;
110381
     for (i = 0; digest_functions[i].digest_name != NULL; i++) {
110381
 	if (sd->digest_type == i) {
110381
 	    func = &digest_functions[i];
110381
@@ -644,9 +665,33 @@ digest_matches(const char *file, const struct sudo_digest *sd, int *fd)
110381
 	sudo_warnx(U_("unsupported digest type %d for %s"), sd->digest_type, file);
110381
 	debug_return_bool(false);
110381
     }
110381
-    if (strlen(sd->digest_str) == func->digest_len * 2) {
110381
+
110381
+    digest_size = func->digest_len;
110381
+
110381
+    file_digest = malloc(digest_size);
110381
+    if (file_digest == NULL) {
110381
+	debug_return_bool(false);
110381
+    }
110381
+
110381
+#elif HAVE_LIBGCRYPT
110381
+
110381
+    if (sudo_filedigest(file, sd->digest_type,
110381
+        &file_digest, &digest_size) != 0) {
110381
+        sudo_warnx(U_("Cannot compute digest type %d for %s"), sd->digest_type, file);
110381
+        goto clean_up;
110381
+    }
110381
+
110381
+#endif /* !HAVE_LIBGCRYPT */
110381
+
110381
+    sudoers_digest = malloc(digest_size);
110381
+    if (sudoers_digest == NULL) {
110381
+        free(file_digest);
110381
+	debug_return_bool(false);
110381
+    }
110381
+
110381
+    if (strlen(sd->digest_str) == digest_size * 2) {
110381
 	/* Convert the command digest from ascii hex to binary. */
110381
-	for (i = 0; i < func->digest_len; i++) {
110381
+	for (i = 0; i < digest_size ; i++) {
110381
 	    const int h = hexchar(&sd->digest_str[i + i]);
110381
 	    if (h == -1)
110381
 		goto bad_format;
110381
@@ -654,11 +699,11 @@ digest_matches(const char *file, const struct sudo_digest *sd, int *fd)
110381
 	}
110381
     } else {
110381
 	size_t len = base64_decode(sd->digest_str, sudoers_digest,
110381
-	    sizeof(sudoers_digest));
110381
-	if (len != func->digest_len) {
110381
+	    digest_size);
110381
+	if (len != digest_size) {
110381
 	    sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
110381
-		"incorrect length for digest, expected %u, got %zu",
110381
-		func->digest_len, len);
110381
+		"incorrect length for digest, expected %zu, got %zu",
110381
+		digest_size, len);
110381
 	    goto bad_format;
110381
 	}
110381
     }
110381
@@ -666,10 +711,11 @@ digest_matches(const char *file, const struct sudo_digest *sd, int *fd)
110381
     if ((fp = fopen(file, "r")) == NULL) {
110381
 	sudo_debug_printf(SUDO_DEBUG_INFO, "unable to open %s: %s",
110381
 	    file, strerror(errno));
110381
-	debug_return_bool(false);
110381
+        goto clean_up;
110381
     }
110381
-
110381
+#ifndef HAVE_LIBGCRYPT
110381
     func->init(&ctx;;
110381
+#endif /* !HAVE_LIBGCRYPT */
110381
     while ((nread = fread(buf, 1, sizeof(buf), fp)) != 0) {
110381
 #ifdef HAVE_FEXECVE
110381
 	/* Check for #! cookie and set is_script. */
110381
@@ -679,21 +725,24 @@ digest_matches(const char *file, const struct sudo_digest *sd, int *fd)
110381
 		is_script = true;
110381
 	}
110381
 #endif /* HAVE_FEXECVE */
110381
+#ifndef HAVE_LIBGCRYPT
110381
 	func->update(&ctx, buf, nread);
110381
+#endif /* !HAVE_LIBGCRYPT */
110381
     }
110381
     if (ferror(fp)) {
110381
 	sudo_warnx(U_("%s: read error"), file);
110381
 	fclose(fp);
110381
-	debug_return_bool(false);
110381
+        goto clean_up;
110381
     }
110381
+#ifndef HAVE_LIBGCRYPT
110381
     func->final(file_digest, &ctx;;
110381
-
110381
-    if (memcmp(file_digest, sudoers_digest, func->digest_len) != 0) {
110381
+#endif /* !HAVE_LIBGCRYPT */
110381
+    if (memcmp(file_digest, sudoers_digest, digest_size) != 0) {
110381
 	fclose(fp);
110381
 	sudo_debug_printf(SUDO_DEBUG_DIAG|SUDO_DEBUG_LINENO,
110381
 	    "%s digest mismatch for %s, expecting %s",
110381
-	    func->digest_name, file, sd->digest_str);
110381
-	debug_return_bool(false);
110381
+	    digesttype2str(sd->digest_type), file, sd->digest_str);
110381
+        goto clean_up;
110381
     }
110381
 
110381
 #ifdef HAVE_FEXECVE
110381
@@ -705,7 +754,7 @@ digest_matches(const char *file, const struct sudo_digest *sd, int *fd)
110381
 	sudo_debug_printf(SUDO_DEBUG_INFO, "unable to dup %s: %s",
110381
 	    file, strerror(errno));
110381
 	fclose(fp);
110381
-	debug_return_bool(false);
110381
+        goto clean_up;
110381
     }
110381
     /*
110381
      * Shell scripts go through namei twice and so we can't set the close
110381
@@ -715,10 +764,17 @@ digest_matches(const char *file, const struct sudo_digest *sd, int *fd)
110381
 	(void)fcntl(*fd, F_SETFD, FD_CLOEXEC);
110381
 #endif /* HAVE_FEXECVE */
110381
     fclose(fp);
110381
+    free(file_digest);
110381
+    free(sudoers_digest);
110381
     debug_return_bool(true);
110381
 bad_format:
110381
     sudo_warnx(U_("digest for %s (%s) is not in %s form"), file,
110381
-	sd->digest_str, func->digest_name);
110381
+	sd->digest_str, digesttype2str(sd->digest_type));
110381
+clean_up:
110381
+    if (file_digest)
110381
+        free(file_digest);
110381
+    if (sudoers_digest)
110381
+        free(sudoers_digest);
110381
     debug_return_bool(false);
110381
 }
110381
 
110381
-- 
110381
2.7.4
110381