Skip to contentSkip to conversation

cifs: cifsacl cleanup, pile #3

PATCH In review Jeff Layton 2 participants 6 messages Oct 19, 2012 – Oct 30, 2012 1 message not in the archive

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

cover cifs: cifsacl cleanup, pile #3

A few more fixes for the cifsacl set. Steve, I can roll these into the other patches if you'd prefer...

Jeff Layton (3):
cifs: extra sanity checking for cifs.idmap keys
cifs: fix hardcoded default security descriptor length
cifs: deal with id_to_sid embedded sid reply corner case

 fs/cifs/cifsacl.c | 35 ++++++++++++++++++++++++++++++-----
 fs/cifs/cifsacl.h | 11 ++++++++---
 2 files changed, 38 insertions(+), 8 deletions(-)
Signature · 1 line
1.7.11.7
#2 lore raw

PATCH 1/3 cifs: extra sanity checking for cifs.idmap keys

In reply to a message not in the archive

Now that we aren't so rigid about the length of the key being passed in, we need to be a bit more rigorous about checking the length of the actual data against the claimed length (a'la num_subauths field).

Check for the case where userspace sends us a seemingly valid key with a num_subauths field that goes beyond the end of the array. If that happens, return -EIO and invalidate the key.

Also change the other places where we check for malformed keys in this code to invalidate the key as well.

---
 fs/cifs/cifsacl.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)
fs/cifs/cifsacl.c +18 -2
index 0c0a594..bd18723 100644
@@ -197,6 +197,8 @@ id_to_sid(unsigned int cid, uint sidtype, struct cifs_sid *ssid)
 {
 	int rc;
 	struct key *sidkey;
+	struct cifs_sid *ksid;
+	unsigned int ksid_size;
 	char desc[3 + 10 + 1]; /* 3 byte prefix + 10 bytes for value + NULL */
 	const struct cred *saved_cred;
 
@@ -217,15 +219,28 @@ id_to_sid(unsigned int cid, uint sidtype, struct cifs_sid *ssid)
 		rc = -EIO;
 		cFYI(1, "%s: Downcall contained malformed key "
 			"(datalen=%hu)", __func__, sidkey->datalen);
-		goto out_key_put;
+		goto invalidate_key;
 	}
-	cifs_copy_sid(ssid, (struct cifs_sid *)sidkey->payload.data);
+
+	ksid = (struct cifs_sid *)sidkey->payload.data;
+	ksid_size = CIFS_SID_BASE_SIZE + (ksid->num_subauth * sizeof(__le32));
+	if (ksid_size > sidkey->datalen) {
+		rc = -EIO;
+		cFYI(1, "%s: Downcall contained malformed key (datalen=%hu, "
+			"ksid_size=%u)", __func__, sidkey->datalen, ksid_size);
+		goto invalidate_key;
+	}
+	cifs_copy_sid(ssid, ksid);
 	key_set_timeout(sidkey, cifs_idmap_cache_timeout);
 out_key_put:
 	key_put(sidkey);
 out_revert_creds:
 	revert_creds(saved_cred);
 	return rc;
+
+invalidate_key:
+	key_invalidate(sidkey);
+	goto out_key_put;
 }
 
 static int
@@ -271,6 +286,7 @@ sid_to_id(struct cifs_sb_info *cifs_sb, struct cifs_sid *psid,
 		rc = -EIO;
 		cFYI(1, "%s: Downcall contained malformed key "
 			"(datalen=%hu)", __func__, sidkey->datalen);
+		key_invalidate(sidkey);
 		goto out_key_put;
 	}
 
Signature · 1 line
1.7.11.7
#3 lore raw

PATCH 2/3 cifs: fix hardcoded default security descriptor length

In reply to a message not in the archive

It was hardcoded to 192 bytes, which was not enough when the max number of subauthorities went to 15. Redefine this constant in terms of sizeof the structs involved, and rename it for better clarity.

While we're at it, remove a couple more unused constants from cifsacl.h.

---
 fs/cifs/cifsacl.c |  2 +-
 fs/cifs/cifsacl.h | 11 ++++++++---
 2 files changed, 9 insertions(+), 4 deletions(-)
fs/cifs/cifsacl.c +1 -1
index bd18723..664abc3 100644
@@ -1016,7 +1016,7 @@ id_mode_to_cifs_acl(struct inode *inode, const char *path, __u64 nmode,
 	 * memory for the smb header, set security descriptor request security
 	 * descriptor parameters, and secuirty descriptor itself
 	 */
-	secdesclen = max_t(u32, secdesclen, DEFSECDESCLEN);
+	secdesclen = max_t(u32, secdesclen, DEFAULT_SEC_DESC_LEN);
 	pnntsd = kmalloc(secdesclen, GFP_KERNEL);
 	if (!pnntsd) {
 		cERROR(1, "Unable to allocate security descriptor");
fs/cifs/cifsacl.h +8 -3
index 46cd444..a445405 100644
@@ -25,9 +25,6 @@
 
 #define NUM_AUTHS (6)	/* number of authority fields */
 #define SID_MAX_SUB_AUTHORITIES (15) /* max number of sub authority fields */
-#define NUM_WK_SIDS 7 /* number of well known sids */
-#define SIDNAMELENGTH 20 /* long enough for the ones we care about */
-#define DEFSECDESCLEN 192 /* sec desc len contaiting a dacl with three aces */
 
 #define READ_BIT        0x4
 #define WRITE_BIT       0x2
@@ -43,6 +40,14 @@
 #define SIDGROUP 2
 
 /*
+ * Security Descriptor length containing DACL with 3 ACEs (one each for
+ * owner, group and world).
+ */
+#define DEFAULT_SEC_DESC_LEN (sizeof(struct cifs_ntsd) + \
+			      sizeof(struct cifs_acl) + \
+			      (sizeof(struct cifs_ace) * 3))
+
+/*
  * Maximum size of a string representation of a SID:
  *
  * The fields are unsigned values in decimal. So:
Signature · 1 line
1.7.11.7
#4 lore raw

PATCH 3/3 cifs: deal with id_to_sid embedded sid reply corner case

In reply to a message not in the archive

A SID could potentially be embedded inside of payload.value if there are no subauthorities, and the arch has 8 byte pointers. Allow for that possibility there.

While we're at it, rephrase the "embedding" check in terms of key->payload to allow for the possibility that the union might change size in the future.

---
 fs/cifs/cifsacl.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
fs/cifs/cifsacl.c +12 -3
index 664abc3..bbe7928 100644
@@ -63,7 +63,7 @@ cifs_idmap_key_instantiate(struct key *key, struct key_preparsed_payload *prep)
 	 * With this however, you must check the datalen before trying to
 	 * dereference payload.data!
 	 */
-	if (prep->datalen <= sizeof(void *)) {
+	if (prep->datalen <= sizeof(key->payload)) {
 		key->payload.value = 0;
 		memcpy(&key->payload.value, prep->data, prep->datalen);
 		key->datalen = prep->datalen;
@@ -82,7 +82,7 @@ cifs_idmap_key_instantiate(struct key *key, struct key_preparsed_payload *prep)
 static inline void
 cifs_idmap_key_destroy(struct key *key)
 {
-	if (key->datalen > sizeof(void *))
+	if (key->datalen > sizeof(key->payload))
 		kfree(key->payload.data);
 }
 
@@ -222,7 +222,15 @@ id_to_sid(unsigned int cid, uint sidtype, struct cifs_sid *ssid)
 		goto invalidate_key;
 	}
 
-	ksid = (struct cifs_sid *)sidkey->payload.data;
+	/*
+	 * A sid is usually too large to be embedded in payload.value, but if
+	 * there are no subauthorities and the host has 8-byte pointers, then
+	 * it could be.
+	 */
+	ksid = sidkey->datalen <= sizeof(sidkey->payload) ?
+		(struct cifs_sid *)&sidkey->payload.value :
+		(struct cifs_sid *)sidkey->payload.data;
+
 	ksid_size = CIFS_SID_BASE_SIZE + (ksid->num_subauth * sizeof(__le32));
 	if (ksid_size > sidkey->datalen) {
 		rc = -EIO;
@@ -230,6 +238,7 @@ id_to_sid(unsigned int cid, uint sidtype, struct cifs_sid *ssid)
 			"ksid_size=%u)", __func__, sidkey->datalen, ksid_size);
 		goto invalidate_key;
 	}
+
 	cifs_copy_sid(ssid, ksid);
 	key_set_timeout(sidkey, cifs_idmap_cache_timeout);
 out_key_put:
Signature · 1 line
1.7.11.7
Reviewed-by
#5 lore raw
In reply to a message not in the archive
Quoted 8 lines · Jeff Layton

A few more fixes for the cifsacl set. Steve, I can roll these into the other patches if you'd prefer...

Jeff Layton (3):
cifs: extra sanity checking for cifs.idmap keys
cifs: fix hardcoded default security descriptor length
cifs: deal with id_to_sid embedded sid reply corner case

Quoted 7 lines
 fs/cifs/cifsacl.c | 35 ++++++++++++++++++++++++++++++-----
 fs/cifs/cifsacl.h | 11 ++++++++---
 2 files changed, 38 insertions(+), 8 deletions(-)

--
1.7.11.7

All three patches look correct.

#6 lore raw

[PATCH 4/3] cifs: fix SID binary to string conversion

In reply to a message not in the archive

The authority fields are supposed to be represented by a single 48-bit big-endian value. It's also supposed to represent the value as hex if it's equal to or greater than 2^32. This is documented in MS-DTYP, section 2.4.2.1.

Also, fix up the max string length to account for this fix.

Cc Shirish Pargaonkar <[email protected]>
---
 fs/cifs/cifsacl.c | 25 +++++++++++++++++++------
 fs/cifs/cifsacl.h |  8 +++++---
 2 files changed, 24 insertions(+), 9 deletions(-)
fs/cifs/cifsacl.c +19 -6
index c15699b..6c8ea82 100644
@@ -94,6 +94,7 @@ sid_to_key_str(struct cifs_sid *sidptr, unsigned int type)
 	int i, len;
 	unsigned int saval;
 	char *sidstr, *strptr;
+	unsigned long long id_auth_val;
 
 	/* 3 bytes for prefix */
 	sidstr = kmalloc(3 + SID_STRING_BASE_SIZE +
@@ -107,12 +108,24 @@ sid_to_key_str(struct cifs_sid *sidptr, unsigned int type)
 			sidptr->revision);
 	strptr += len;
 
-	for (i = 0; i < NUM_AUTHS; ++i) {
-		if (sidptr->authority[i]) {
-			len = sprintf(strptr, "-%hhu", sidptr->authority[i]);
-			strptr += len;
-		}
-	}
+	/* The authority field is a single 48-bit number */
+	id_auth_val = (unsigned long long)sidptr->authority[5];
+	id_auth_val += (unsigned long long)sidptr->authority[4] << 8;
+	id_auth_val += (unsigned long long)sidptr->authority[3] << 16;
+	id_auth_val += (unsigned long long)sidptr->authority[2] << 24;
+	id_auth_val += (unsigned long long)sidptr->authority[1] << 32;
+	id_auth_val += (unsigned long long)sidptr->authority[0] << 48;
+
+        /*
+	 * MS-DTYP states that if the authority is >= 2^32, then it should be
+	 * expressed as a hex value.
+	 */
+	if (id_auth_val <= UINT_MAX)
+		len = sprintf(strptr, "-%llu", id_auth_val);
+	else
+		len = sprintf(strptr, "-0x%llx", id_auth_val);
+
+	strptr += len;
 
 	for (i = 0; i < sidptr->num_subauth; ++i) {
 		saval = le32_to_cpu(sidptr->sub_auth[i]);
fs/cifs/cifsacl.h +5 -3
index a445405..4f38848 100644
@@ -55,12 +55,14 @@
  * u8:  max 3 bytes in decimal
  * u32: max 10 bytes in decimal
  *
- * "S-" + 3 bytes for version field + 4 bytes for each authority field (3 bytes
- * per number + 1 for '-') + NULL terminator.
+ * "S-" + 3 bytes for version field + 15 for authority field + NULL terminator
+ *
+ * For authority field, max is when all 6 values are non-zero and it must be
+ * represented in hex. So "-0x" + 12 hex digits.
  *
  * Add 11 bytes for each subauthority field (10 bytes each + 1 for '-')
  */
-#define SID_STRING_BASE_SIZE (2 + 3 + (4 * NUM_AUTHS) + 1)
+#define SID_STRING_BASE_SIZE (2 + 3 + 15 + 1)
 #define SID_STRING_SUBAUTH_SIZE (11) /* size of a single subauth string */
 
 struct cifs_ntsd {
Signature · 1 line
1.7.11.7