Skip to contentSkip to conversation

cifs-utils: fix problems with credcaches with overridden process creds

PATCH In review Jeff Layton 3 participants 13 messages Feb 24, 2017 – Feb 28, 2017 3 messages not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover cifs-utils: fix problems with credcaches with overridden process creds

The main impetus for this set is patch #7, which is a one-liner. #8 may be overkill, but I do think that it's the right approach, at least for now.

In order to get all of this to build however, we need to eliminate the unsetenv override in replace.h, or add a test for unsetenv. I chose the former, so the real patches are preceded by a number of cleanup patches.

Jeff Layton (8):
data_blob: remove need for replace.h
spengo.c/asn1.c: remove need for replace.h
cifs.upcall: remove need for replace.h
replace.h: remove it
treewide: Eliminate SAFE_FREE
data_blob: Eliminate _PUBLIC_
cifs.upcall: unset $KRB5CCNAME when creating new credcache from keytab cifs.upcall: don't do env scraping when uid is 0

 asn1.c           |   2 +-
 cifs.upcall.8.in |   5 +-
 cifs.upcall.c    |  56 +++--
 configure.ac     |   2 +-
 data_blob.c      |  11 +-
 data_blob.h      |  10 +-
 mount.cifs.c     |  16 +-
 replace.h        | 674 -------------------------------------------------------
 spnego.c         |   2 +-
 9 files changed, 64 insertions(+), 714 deletions(-)
 delete mode 100644 replace.h
Signature · 1 line
2.9.3

A message that is not in the archive (sent off-list or not yet received): 20170224142750.4151-1-jlayton-eUNUBHrolfbYtjvyW6yDsg@public.gmane.org

#2 lore raw

PATCH 1/8 data_blob: remove need for replace.h

We only need ZERO_STRUCT there.

---
 data_blob.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
data_blob.c +4 -1
index 834d810c9d46..a884f132bcde 100644
@@ -20,9 +20,12 @@
 
 #include <string.h>
 
-#include "replace.h"
 #include "data_blob.h"
 
+#ifndef ZERO_STRUCT
+#define ZERO_STRUCT(x) memset((char *)&(x), 0, sizeof(x))
+#endif
+
 const DATA_BLOB data_blob_null = { NULL, 0 };
 
 /**
Signature · 1 line
2.9.3
#3 lore raw

PATCH 2/8 spengo.c/asn1.c: remove need for replace.h

In reply to a message not in the archive

Just need stdbool.h instead.

---
 asn1.c   | 2 +-
 spnego.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
asn1.c +1 -1
index 4c0e48037f14..dd96d2c0e595 100644
@@ -20,8 +20,8 @@
 #include <string.h>
 #include <talloc.h>
 #include <stdint.h>
+#include <stdbool.h>
 
-#include "replace.h"
 #include "data_blob.h"
 #include "asn1.h"
 
spnego.c +1 -1
index 7f32d7d2efa9..e483e6e1c526 100644
@@ -21,8 +21,8 @@
 
 #include <talloc.h>
 #include <stdint.h>
+#include <stdbool.h>
 
-#include "replace.h"
 #include "data_blob.h"
 #include "asn1.h"
 #include "spnego.h"
Signature · 1 line
2.9.3
#4 lore raw

PATCH 3/8 cifs.upcall: remove need for replace.h

In reply to a message not in the archive

Take just what we need from replace.h and move it to cifs.upcall.c.

---
 cifs.upcall.c | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)
cifs.upcall.c +19 -3
index f766a8b5799e..cfc926cd9d09 100644
@@ -49,8 +49,9 @@
 #include <ctype.h>
 #include <pwd.h>
 #include <grp.h>
+#include <stdbool.h>
+#include <errno.h>
 
-#include "replace.h"
 #include "data_blob.h"
 #include "spnego.h"
 #include "cifs_spnego.h"
@@ -68,6 +69,21 @@ typedef enum _sectype {
 	MS_KRB5
 } sectype_t;
 
+/* These macros unify the keyblock handling of Heimdal and MIT somewhat */
+#ifdef HAVE_KRB5_KEYBLOCK_KEYVALUE /* Heimdal */
+#define KRB5_KEY_TYPE(k)        ((k)->keytype)
+#define KRB5_KEY_LENGTH(k)      ((k)->keyvalue.length)
+#define KRB5_KEY_DATA(k)        ((k)->keyvalue.data)
+#define KRB5_KEY_DATA_CAST      void
+#else /* MIT */
+#define KRB5_KEY_TYPE(k)        ((k)->enctype)
+#define KRB5_KEY_LENGTH(k)      ((k)->length)
+#define KRB5_KEY_DATA(k)        ((k)->contents)
+#define KRB5_KEY_DATA_CAST      krb5_octet
+#endif
+
+#define SAFE_FREE(x) do { if ((x) != NULL) {free(x); x = NULL; } } while (0)
+
 #ifdef HAVE_LIBCAP_NG
 static int
 trim_capabilities(bool need_environ)
@@ -434,7 +450,7 @@ cifs_krb5_get_req(const char *host, krb5_ccache ccache,
 	krb5_data apreq_pkt, in_data;
 	krb5_auth_context auth_context = NULL;
 #if defined(HAVE_KRB5_AUTH_CON_SETADDRS) && defined(HAVE_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE)
-	static const uint8_t gss_cksum[24] = { 0x10, 0x00, /* ... */};
+	static char gss_cksum[24] = { 0x10, 0x00, /* ... */};
 #endif
 	memset(&in_creds, 0, sizeof(in_creds));
 
@@ -498,7 +514,7 @@ cifs_krb5_get_req(const char *host, krb5_ccache ccache,
 	 *
 	 * See https://bugzilla.samba.org/show_bug.cgi?id=7890
 	 */
-	in_data.data = discard_const_p(char, gss_cksum);
+	in_data.data = gss_cksum;
 	in_data.length = 24;
 
 	/* MIT krb5 < 1.7 is missing the prototype, but still has the symbol */
Signature · 1 line
2.9.3
#5 lore raw

PATCH 4/8 replace.h: remove it

In reply to a message not in the archive

Nothing uses it now.

---
 configure.ac |   2 +-
 replace.h    | 674 -----------------------------------------------------------
 2 files changed, 1 insertion(+), 675 deletions(-)
 delete mode 100644 replace.h
configure.ac +1 -1
index 12098e2bda41..78a1215f849d 100644
@@ -2,7 +2,7 @@
 # Process this file with autoconf to produce a configure script.
 
 AC_INIT([cifs-utils],[6.6.1],[[email protected]],[cifs-utils],[https://wiki.samba.org/index.php/LinuxCIFS_utils])
-AC_CONFIG_SRCDIR([replace.h])
+AC_CONFIG_SRCDIR([data_blob.h])
 AC_CONFIG_HEADERS([config.h])
 AC_CONFIG_FILES([Makefile contrib/Makefile contrib/request-key.d/Makefile])
 AC_CONFIG_MACRO_DIR(aclocal)
replace.h deleted +0 -674

674 lines not on this page · raw message

Signature · 1 line
2.9.3
#6 lore raw

PATCH 5/8 treewide: Eliminate SAFE_FREE

In reply to a message not in the archive

It just frees and then zeroes out the pointer. That's of dubious value in the places where it's currently being used. Just use free() instead.

---
 cifs.upcall.c | 22 ++++++++++------------
 mount.cifs.c  | 16 +++++-----------
 2 files changed, 15 insertions(+), 23 deletions(-)
cifs.upcall.c +10 -12
index cfc926cd9d09..15e1e0f91c22 100644
@@ -82,8 +82,6 @@ typedef enum _sectype {
 #define KRB5_KEY_DATA_CAST      krb5_octet
 #endif
 
-#define SAFE_FREE(x) do { if ((x) != NULL) {free(x); x = NULL; } } while (0)
-
 #ifdef HAVE_LIBCAP_NG
 static int
 trim_capabilities(bool need_environ)
@@ -162,7 +160,7 @@ static char *cifs_krb5_principal_get_realm(krb5_principal principal)
 #if !defined(HAVE_KRB5_FREE_UNPARSED_NAME)
 static void krb5_free_unparsed_name(krb5_context context, char *val)
 {
-	SAFE_FREE(val);
+	free(val);
 }
 #endif
 
@@ -650,7 +648,7 @@ decode_key_description(const char *desc, struct decoded_args *arg)
 				len = pos - tkn;
 
 			len -= 5;
-			SAFE_FREE(arg->hostname);
+			free(arg->hostname);
 			arg->hostname = strndup(tkn + 5, len);
 			if (arg->hostname == NULL) {
 				syslog(LOG_ERR, "Unable to allocate memory");
@@ -665,7 +663,7 @@ decode_key_description(const char *desc, struct decoded_args *arg)
 				len = pos - tkn;
 
 			len -= 4;
-			SAFE_FREE(arg->ip);
+			free(arg->ip);
 			arg->ip = strndup(tkn + 4, len);
 			if (arg->ip == NULL) {
 				syslog(LOG_ERR, "Unable to allocate memory");
@@ -680,7 +678,7 @@ decode_key_description(const char *desc, struct decoded_args *arg)
 				len = pos - tkn;
 
 			len -= 5;
-			SAFE_FREE(arg->username);
+			free(arg->username);
 			arg->username = strndup(tkn + 5, len);
 			if (arg->username == NULL) {
 				syslog(LOG_ERR, "Unable to allocate memory");
@@ -968,7 +966,7 @@ int main(const int argc, char *const argv[])
 	}
 
 	have = decode_key_description(buf, &arg);
-	SAFE_FREE(buf);
+	free(buf);
 	if ((have & DKD_MUSTHAVE_SET) != DKD_MUSTHAVE_SET) {
 		syslog(LOG_ERR, "unable to get necessary params from key "
 		       "description (0x%x)", have);
@@ -1207,11 +1205,11 @@ out:
 		krb5_cc_close(context, ccache);
 	if (context)
 		krb5_free_context(context);
-	SAFE_FREE(arg.hostname);
-	SAFE_FREE(arg.ip);
-	SAFE_FREE(arg.username);
-	SAFE_FREE(keydata);
-	SAFE_FREE(env_cachename);
+	free(arg.hostname);
+	free(arg.ip);
+	free(arg.username);
+	free(keydata);
+	free(env_cachename);
 	syslog(LOG_DEBUG, "Exit status %ld", rc);
 	return rc;
 }
mount.cifs.c +5 -11
index b2bcb7f16b9f..13b71ef50303 100644
@@ -61,6 +61,7 @@
 #include "mount.h"
 #include "util.h"
 #include "resolve_host.h"
+#include "data_blob.h"
 
 #ifndef MS_MOVE 
 #define MS_MOVE 8192 
@@ -101,13 +102,6 @@
 /* Max password size. */
 #define MOUNT_PASSWD_SIZE 512
 
-
-
-#ifndef SAFE_FREE
-#define SAFE_FREE(x) do { if ((x) != NULL) {free(x); x = NULL; } } while (0)
-#endif
-
-
 /*
  * mount.cifs has been the subject of many "security" bugs that have arisen
  * because of users and distributions installing it as a setuid root program
@@ -621,7 +615,7 @@ return_i:
 	/* make sure passwords are scrubbed from memory */
 	if (line_buf != NULL)
 		memset(line_buf, 0, line_buf_size);
-	SAFE_FREE(line_buf);
+	free(line_buf);
 	return i;
 }
 
@@ -1516,7 +1510,7 @@ add_mtab(char *devname, char *mountpoint, unsigned long flags, const char *fstyp
 		rc = EX_FILEIO;
 	}
 	unlock_mtab();
-	SAFE_FREE(mountent.mnt_opts);
+	free(mountent.mnt_opts);
 add_mtab_exit:
 	toggle_dac_capability(1, 0);
 	sigprocmask(SIG_SETMASK, &oldmask, NULL);
@@ -2152,7 +2146,7 @@ mount_exit:
 		memset(parsed_info->password, 0, sizeof(parsed_info->password));
 		munmap(parsed_info, sizeof(*parsed_info));
 	}
-	SAFE_FREE(options);
-	SAFE_FREE(orgoptions);
+	free(options);
+	free(orgoptions);
 	return rc;
 }
Signature · 1 line
2.9.3
#7 lore raw

PATCH 6/8 data_blob: Eliminate _PUBLIC_

In reply to a message not in the archive

It's defined to nothing anyway.

---
 data_blob.c |  6 +++---
 data_blob.h | 10 +++-------
 2 files changed, 6 insertions(+), 10 deletions(-)
data_blob.c +3 -3
index a884f132bcde..07e54e2c2bcd 100644
@@ -37,7 +37,7 @@ const DATA_BLOB data_blob_null = { NULL, 0 };
  construct a data blob, must be freed with data_blob_free()
  you can pass NULL for p and get a blank data blob
 **/
-_PUBLIC_ DATA_BLOB data_blob_named(const void *p, size_t length, const char *name)
+DATA_BLOB data_blob_named(const void *p, size_t length, const char *name)
 {
 	DATA_BLOB ret;
 
@@ -63,7 +63,7 @@ _PUBLIC_ DATA_BLOB data_blob_named(const void *p, size_t length, const char *nam
 /**
  construct a data blob, using supplied TALLOC_CTX
 **/
-_PUBLIC_ DATA_BLOB data_blob_talloc_named(TALLOC_CTX *mem_ctx, const void *p, size_t length, const char *name)
+DATA_BLOB data_blob_talloc_named(TALLOC_CTX *mem_ctx, const void *p, size_t length, const char *name)
 {
 	DATA_BLOB ret = data_blob_named(p, length, name);
 
@@ -76,7 +76,7 @@ _PUBLIC_ DATA_BLOB data_blob_talloc_named(TALLOC_CTX *mem_ctx, const void *p, si
 /**
 free a data blob
 **/
-_PUBLIC_ void data_blob_free(DATA_BLOB *d)
+void data_blob_free(DATA_BLOB *d)
 {
 	if (d) {
 		talloc_free(d->data);
data_blob.h +3 -7
index ccdf30d110af..3d961475e9ff 100644
@@ -23,10 +23,6 @@
 #ifndef _SAMBA_DATABLOB_H_
 #define _SAMBA_DATABLOB_H_
 
-#ifndef _PUBLIC_
-#define _PUBLIC_
-#endif
-
 #include <talloc.h>
 #include <stdint.h>
 
@@ -53,17 +49,17 @@ struct data_blob_list_item {
  construct a data blob, must be freed with data_blob_free()
  you can pass NULL for p and get a blank data blob
 **/
-_PUBLIC_ DATA_BLOB data_blob_named(const void *p, size_t length, const char *name);
+DATA_BLOB data_blob_named(const void *p, size_t length, const char *name);
 
 /**
  construct a data blob, using supplied TALLOC_CTX
 **/
-_PUBLIC_ DATA_BLOB data_blob_talloc_named(TALLOC_CTX *mem_ctx, const void *p, size_t length, const char *name);
+DATA_BLOB data_blob_talloc_named(TALLOC_CTX *mem_ctx, const void *p, size_t length, const char *name);
 
 /**
 free a data blob
 **/
-_PUBLIC_ void data_blob_free(DATA_BLOB *d);
+void data_blob_free(DATA_BLOB *d);
 
 extern const DATA_BLOB data_blob_null;
 
Signature · 1 line
2.9.3
#8 lore raw

PATCH 7/8 cifs.upcall: unset $KRB5CCNAME when creating new credcache from keytab

In reply to a message not in the archive

We don't want to trust $KRB5CCNAME when creating or updating a new credcache since we could be operating under the wrong credentials. Always create new credcaches in the default location instead.

---
 cifs.upcall.c | 6 ++++++
 1 file changed, 6 insertions(+)
cifs.upcall.c +6 -0
index 15e1e0f91c22..0c89d7cf40d7 100644
@@ -379,6 +379,12 @@ init_cc_from_keytab(const char *keytab_name, const char *user)
 
 	memset((char *) &my_creds, 0, sizeof(my_creds));
 
+	/*
+	 * Unset the environment variable, if any. If we're creating our own
+	 * credcache here, stick it in the default location.
+	 */
+	unsetenv(ENV_NAME);
+
 	if (keytab_name)
 		ret = krb5_kt_resolve(context, keytab_name, &keytab);
 	else
Signature · 1 line
2.9.3

A message that is not in the archive (sent off-list or not yet received): 20170224142750.4151-8-jlayton-eUNUBHrolfbYtjvyW6yDsg@public.gmane.org

#10 lore raw

Re: [cifs-utils PATCH 7/8] cifs.upcall: unset $KRB5CCNAME when creating new credcache from keytab

Quoted 17 lines · Jeff Layton

We don't want to trust $KRB5CCNAME when creating or updating a new credcache since we could be operating under the wrong credentials. Always create new credcaches in the default location instead.

---
 cifs.upcall.c | 6 ++++++
 1 file changed, 6 insertions(+)
cifs.upcall.c +0 -0
index 15e1e0f91c22..0c89d7cf40d7 100644
@@ -379,6 +379,12 @@ init_cc_from_keytab(const char *keytab_name, const char *user)
 
 	memset((char *) &my_creds, 0, sizeof(my_creds));

cifs.upcall.c init_cc_from_keytab(const char *keytab_name, const char *user) · line 390

Quoted 10 lines
cifs.upcall.c +6 -0
 
+	/*
+	 * Unset the environment variable, if any. If we're creating our own
+	 * credcache here, stick it in the default location.
+	 */
+	unsetenv(ENV_NAME);
+
 	if (keytab_name)
 		ret = krb5_kt_resolve(context, keytab_name, &keytab);
 	else

How long do you need these credentials around for ? I wonder if using a memory ccache would work here.

Simo.

A message that is not in the archive (sent off-list or not yet received): 1487947130.1893.127.camel-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org

#12 lore raw

Re: [cifs-utils PATCH 7/8] cifs.upcall: unset $KRB5CCNAME when creating new credcache from keytab

Quoted 19 lines · Simo Sorce
Quoted 18 lines · Jeff Layton

We don't want to trust $KRB5CCNAME when creating or updating a new credcache since we could be operating under the wrong credentials. Always create new credcaches in the default location instead.

---
 cifs.upcall.c | 6 ++++++
 1 file changed, 6 insertions(+)
cifs.upcall.c +0 -0
index 15e1e0f91c22..0c89d7cf40d7 100644
@@ -379,6 +379,12 @@ init_cc_from_keytab(const char *keytab_name, const char *user)
 
 	memset((char *) &my_creds, 0, sizeof(my_creds));
 
Quoted 14 lines

cifs.upcall.c init_cc_from_keytab(const char *keytab_name, const char *user) · line 390

Quoted 9 lines
cifs.upcall.c +6 -0
+	/*
+	 * Unset the environment variable, if any. If we're creating our own
+	 * credcache here, stick it in the default location.
+	 */
+	unsetenv(ENV_NAME);
+
 	if (keytab_name)
 		ret = krb5_kt_resolve(context, keytab_name, &keytab);
 	else

How long do you need these credentials around for ? I wonder if using a memory ccache would work here.

Only for as long as the upcall program lasts. A memory cache sounds like a good idea, actually. Let me ponder that...

#13 lore raw

PATCH 8/8 cifs.upcall: don't do env scraping when uid is 0

In reply to a message not in the archive

Setuid programs triggering upcalls could trick the program here. Also, the d_automount method is done with credentials overridden so if you can end up with mismatched creds and env vars due to that as well.

It's a hack, but the only recourse I can see is to avoid doing this when the uid is 0. That means we can't rely on finding root credcaches in alternate locations using $KRB5CCNAME, but I think that's the best we can do.

---
 cifs.upcall.8.in |  5 ++++-
 cifs.upcall.c    | 10 +++++++++-
 2 files changed, 13 insertions(+), 2 deletions(-)
cifs.upcall.8.in +4 -1
index e1f3956e176a..81481a482fb4 100644
@@ -44,7 +44,10 @@ Normally, cifs.upcall will probe the environment variable space of the process
 that initiated the upcall in order to fetch the value of $KRB5CCNAME. This can
 assist the program with finding credential caches in non-default locations. If
 this option is set, then the program won't do this and will rely on finding
-credcaches in the default locations specified in krb5.conf.
+credcaches in the default locations specified in krb5.conf. Note that this is
+never performed when the uid is 0. The default credcache location is always
+used when the uid is 0, regardless of the environment variable setting in the
+process.
 .RE
 .PP
 \--krb5conf=/path/to/krb5.conf|-k /path/to/krb5.conf
cifs.upcall.c +9 -1
index 0c89d7cf40d7..8545e09611ad 100644
@@ -1038,11 +1038,19 @@ int main(const int argc, char *const argv[])
 	}
 
 	/*
+	 * We can't reasonably do this for root. Mounting a DFS share, for
+	 * instance we can end up with creds being overridden, but the env
+	 * variable left intact.
+	 */
+	if (uid == 0)
+		env_probe = false;
+
+	/*
 	 * Must do this before setuid, as we need elevated capabilities to
 	 * look at the environ file.
 	 */
 	env_cachename =
-		get_cachename_from_process_env(env_probe ?  arg.pid : 0);
+		get_cachename_from_process_env(env_probe ? arg.pid : 0);
 
 	rc = setuid(uid);
 	if (rc == -1) {
Signature · 1 line
2.9.3
#14 lore raw

[cifs-utils PATCH 9/8] cifs.upcall: use a MEMORY: ccache when instantiating from a keytab

In reply to a message not in the archive

Using a more permanent ccache is potentially problematic when we're instantiating a new one. We might be operating under different creds than expected. Just use a MEMORY: ccache since we don't need it to last longer than the life of the upcall anyway.

---
 cifs.upcall.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
cifs.upcall.c +2 -2
index e0d372408233..89563fd42adc 100644
@@ -410,9 +410,9 @@ init_cc_from_keytab(const char *keytab_name, const char *user)
 		goto icfk_cleanup;
 	}
 
-	ret = krb5_cc_default(context, &cc);
+	ret = krb5_cc_resolve(context, "MEMORY:", &cc);
 	if (ret) {
-		syslog(LOG_DEBUG, "krb5_cc_default: %d", (int)ret);
+		syslog(LOG_DEBUG, "krb5_cc_resolve: %d", (int)ret);
 		goto icfk_cleanup;
 	}
 
Signature · 1 line
2.9.3
#15 lore raw
In reply to Jeff Layton
Quoted 18 lines · Jeff Layton

The main impetus for this set is patch #7, which is a one-liner. #8 may be overkill, but I do think that it's the right approach, at least for now.

In order to get all of this to build however, we need to eliminate the unsetenv override in replace.h, or add a test for unsetenv. I chose the former, so the real patches are preceded by a number of cleanup patches.

Jeff Layton (8):
data_blob: remove need for replace.h
spengo.c/asn1.c: remove need for replace.h
cifs.upcall: remove need for replace.h
replace.h: remove it
treewide: Eliminate SAFE_FREE
data_blob: Eliminate _PUBLIC_
cifs.upcall: unset $KRB5CCNAME when creating new credcache from keytab cifs.upcall: don't do env scraping when uid is 0

Quoted 12 lines
 asn1.c           |   2 +-
 cifs.upcall.8.in |   5 +-
 cifs.upcall.c    |  56 +++--
 configure.ac     |   2 +-
 data_blob.c      |  11 +-
 data_blob.h      |  10 +-
 mount.cifs.c     |  16 +-
 replace.h        | 674 -------------------------------------------------------
 spnego.c         |   2 +-
 9 files changed, 64 insertions(+), 714 deletions(-)
 delete mode 100644 replace.h

Chad messaged me privately and stated that these patches fixed the issue he was having. Merged into cifs-utils master branch. This should make v6.7.

Thanks!