cifs-utils: fix problems with credcaches with overridden process creds
Subsystems (2)
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
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
@@ -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
PATCH 2/8 spengo.c/asn1.c: remove need for replace.h
In reply to a message not in the archiveJust need stdbool.h instead.
--- asn1.c | 2 +- spnego.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
asn1.c +1 -1
@@ -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
@@ -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
PATCH 3/8 cifs.upcall: remove need for replace.h
In reply to a message not in the archiveTake 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
@@ -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
PATCH 4/8 replace.h: remove it
In reply to a message not in the archiveNothing 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
@@ -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
PATCH 5/8 treewide: Eliminate SAFE_FREE
In reply to a message not in the archiveIt 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
@@ -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
@@ -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
PATCH 6/8 data_blob: Eliminate _PUBLIC_
In reply to a message not in the archiveIt'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
@@ -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
@@ -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
PATCH 7/8 cifs.upcall: unset $KRB5CCNAME when creating new credcache from keytab
In reply to a message not in the archiveWe 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
@@ -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
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.
Signed-off-by Jeff Layton <[email protected]>--- cifs.upcall.c | 6 ++++++ 1 file changed, 6 insertions(+)cifs.upcall.c +0
-0@@ -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
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.
Signed-off-by Jeff Layton <[email protected]>--- cifs.upcall.c | 6 ++++++ 1 file changed, 6 insertions(+)cifs.upcall.c +0
-0@@ -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...
PATCH 8/8 cifs.upcall: don't do env scraping when uid is 0
In reply to a message not in the archiveSetuid 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
@@ -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
@@ -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
[cifs-utils PATCH 9/8] cifs.upcall: use a MEMORY: ccache when instantiating from a keytab
In reply to a message not in the archiveUsing 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
@@ -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
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 forreplace.h
spengo.c/asn1.c: remove need forreplace.h
cifs.upcall: remove need forreplace.h
replace.h: remove it
treewide: EliminateSAFE_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!