Skip to contentSkip to conversation

fs/smb/server/tests/secdesc_kunit.c:742 ksmbd_sd_decode_offset_in_header_test() error: dereferencing freed memory 'sd' (line 738)

PATCH kernel test robot 1 participant 1 message Nov 27

lore
Share & tools
lore

kernel test robot

author build bot
#0 lore raw

Unfiltered warning: this smatch report is sent as-is and has not been reviewed for false positives, so please treat it with due skepticism.

BCC: [email protected]
CC: [email protected]
TO: Ian Bridges <[email protected]>
CC: Namjae Jeon <[email protected]>

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git master
head:   e5aad2bdbbee783611d32599f89cea3f12f9593d
commit: 67e9363105151861dc8ffafb407d322d19b0b374 [11360/13037] ksmbd: add a security descriptor decoder and encoder

:::::: branch date: 5 hours ago
:::::: commit date: 2 days ago
config: hexagon-randconfig-2200-20261127 (https://download.01.org/0day-ci/archive/20261127/[email protected]/config)
compiler: clang version 24.0.0git (https://github.com/llvm/llvm-project 578b1f2d6a9776b63cbb6b86f4cca6208d429f48)
smatch: v0.5.0-9187-g5189e3fb

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags

| Reported-by: kernel test robot <[email protected]>
| Reported-by: Dan Carpenter <[email protected]>
| Closes: https://lore.kernel.org/r/[email protected]/

New smatch warnings:
fs/smb/server/tests/secdesc_kunit.c:742 ksmbd_sd_decode_offset_in_header_test() error: dereferencing freed memory 'sd' (line 738) fs/smb/server/tests/secdesc_kunit.c:770 t_one_ace() warn: returning freed memory 'sd' (line 769) fs/smb/server/tests/secdesc_kunit.c:874 ksmbd_sd_decode_encode_max_dacl_test() error: dereferencing freed memory 'sd' (line 869) fs/smb/server/tests/secdesc_kunit.c:876 ksmbd_sd_decode_encode_max_dacl_test() error: dereferencing freed memory 'more' (line 875)

Old smatch warnings:
fs/smb/server/tests/secdesc_kunit.c:750 ksmbd_sd_decode_offset_in_header_test() error: dereferencing freed memory 'sd' (line 745)

vim +/sd +742 fs/smb/server/tests/secdesc_kunit.c

67e936310515186 Ian Bridges 2026-11-25  722  
67e936310515186 Ian Bridges 2026-11-25  723  /*
67e936310515186 Ian Bridges 2026-11-25  724   * An owner or a group whose offset points into the header of the SD
67e936310515186 Ian Bridges 2026-11-25  725   * (MS-DTYP 2.4.6) is left out, even when the bytes there read as a SID. With
67e936310515186 Ian Bridges 2026-11-25  726   * SACL_PRESENT clear OffsetSacl is not read, so here it is set to 1. The
67e936310515186 Ian Bridges 2026-11-25  727   * header then holds the bytes of a SID with no sub-authority at offset 12.
67e936310515186 Ian Bridges 2026-11-25  728   */
67e936310515186 Ian Bridges 2026-11-25  729  static void ksmbd_sd_decode_offset_in_header_test(struct kunit *test)
67e936310515186 Ian Bridges 2026-11-25  730  {
67e936310515186 Ian Bridges 2026-11-25  731  	const size_t len = sizeof(base_sd);
67e936310515186 Ian Bridges 2026-11-25  732  	struct ksmbd_sd *sd = NULL;
67e936310515186 Ian Bridges 2026-11-25  733  	u8 *buf = t_base_copy(test);
67e936310515186 Ian Bridges 2026-11-25  734  
67e936310515186 Ian Bridges 2026-11-25  735  	buf[O_SD_CONTROL] &= ~SACL_PRESENT;
67e936310515186 Ian Bridges 2026-11-25  736  	put_unaligned_le32(1, &buf[O_SD_SACL]);
67e936310515186 Ian Bridges 2026-11-25  737  	KUNIT_ASSERT_EQ(test, ksmbd_sd_decode(buf, len, &sd), 0);
67e936310515186 Ian Bridges 2026-11-25 @738  	kvfree(sd);
67e936310515186 Ian Bridges 2026-11-25  739  
67e936310515186 Ian Bridges 2026-11-25  740  	buf[O_SD_OWNER] = O_SD_SACL;
67e936310515186 Ian Bridges 2026-11-25  741  	KUNIT_ASSERT_EQ(test, ksmbd_sd_decode(buf, len, &sd), 0);
67e936310515186 Ian Bridges 2026-11-25 @742  	KUNIT_EXPECT_NULL(test, sd->owner);
67e936310515186 Ian Bridges 2026-11-25  743  	KUNIT_EXPECT_NOT_NULL(test, sd->group);
67e936310515186 Ian Bridges 2026-11-25  744  	KUNIT_EXPECT_TRUE(test, sd->part_dropped);
67e936310515186 Ian Bridges 2026-11-25  745  	kvfree(sd);
67e936310515186 Ian Bridges 2026-11-25  746  
67e936310515186 Ian Bridges 2026-11-25  747  	buf[O_SD_OWNER] = B_OWNER;
67e936310515186 Ian Bridges 2026-11-25  748  	buf[O_SD_GROUP] = O_SD_SACL;
67e936310515186 Ian Bridges 2026-11-25  749  	KUNIT_ASSERT_EQ(test, ksmbd_sd_decode(buf, len, &sd), 0);
67e936310515186 Ian Bridges 2026-11-25  750  	KUNIT_EXPECT_NOT_NULL(test, sd->owner);
67e936310515186 Ian Bridges 2026-11-25  751  	KUNIT_EXPECT_NULL(test, sd->group);
67e936310515186 Ian Bridges 2026-11-25  752  	KUNIT_EXPECT_TRUE(test, sd->part_dropped);
67e936310515186 Ian Bridges 2026-11-25  753  	kvfree(sd);
67e936310515186 Ian Bridges 2026-11-25  754  
67e936310515186 Ian Bridges 2026-11-25  755  	/* An SD shorter than its header is refused */
67e936310515186 Ian Bridges 2026-11-25  756  	KUNIT_EXPECT_EQ(test, ksmbd_sd_decode(buf, O_SD_HDR_LEN - 1, &sd), -EBADMSG);
67e936310515186 Ian Bridges 2026-11-25  757  }
67e936310515186 Ian Bridges 2026-11-25  758  
67e936310515186 Ian Bridges 2026-11-25  759  /* Returns an SD with one ACE, which the encoder accepts */
67e936310515186 Ian Bridges 2026-11-25  760  static struct ksmbd_sd *t_one_ace(struct kunit *test)
67e936310515186 Ian Bridges 2026-11-25  761  {
67e936310515186 Ian Bridges 2026-11-25  762  	struct ksmbd_sd *sd = t_alloc(test, 1);
67e936310515186 Ian Bridges 2026-11-25  763  
67e936310515186 Ian Bridges 2026-11-25  764  	sd->dacl = KSMBD_ACL_PRESENT;
67e936310515186 Ian Bridges 2026-11-25  765  	sd->aces[0] = (struct ksmbd_ace){
67e936310515186 Ian Bridges 2026-11-25  766  		.type = ACCESS_DENIED_ACE_TYPE, .flags = 0x13, .mask = 0x1f01ff,
67e936310515186 Ian Bridges 2026-11-25  767  		.sid = &everyone,
67e936310515186 Ian Bridges 2026-11-25  768  	};
67e936310515186 Ian Bridges 2026-11-25 @769  	KUNIT_ASSERT_EQ(test, t_try_encode(test, sd), 0);
67e936310515186 Ian Bridges 2026-11-25 @770  	return sd;
67e936310515186 Ian Bridges 2026-11-25  771  }
67e936310515186 Ian Bridges 2026-11-25  772  
67e936310515186 Ian Bridges 2026-11-25  773  /* Makes one change to an SD the encoder accepts, and expects -EINVAL */
67e936310515186 Ian Bridges 2026-11-25  774  #define T_REFUSES(what, change)						\
67e936310515186 Ian Bridges 2026-11-25  775  	do {								\
67e936310515186 Ian Bridges 2026-11-25  776  		struct ksmbd_sd *sd = t_one_ace(test);			\
67e936310515186 Ian Bridges 2026-11-25  777  		struct ksmbd_ace *ace = &sd->aces[0];			\
67e936310515186 Ian Bridges 2026-11-25  778  									\
67e936310515186 Ian Bridges 2026-11-25  779  		(void)ace;						\
67e936310515186 Ian Bridges 2026-11-25  780  		change;							\
67e936310515186 Ian Bridges 2026-11-25  781  		KUNIT_EXPECT_EQ_MSG(test, t_try_encode(test, sd), -EINVAL, what); \
67e936310515186 Ian Bridges 2026-11-25  782  	} while (0)
67e936310515186 Ian Bridges 2026-11-25  783  
67e936310515186 Ian Bridges 2026-11-25  784  /*
67e936310515186 Ian Bridges 2026-11-25  785   * The encoder returns -EINVAL for a struct ksmbd_sd that would encode to a
67e936310515186 Ian Bridges 2026-11-25  786   * malformed SD. Each T_REFUSES line makes one such change to a valid SD.
67e936310515186 Ian Bridges 2026-11-25  787   */
67e936310515186 Ian Bridges 2026-11-25  788  static void ksmbd_sd_encode_refuse_test(struct kunit *test)
67e936310515186 Ian Bridges 2026-11-25  789  {
67e936310515186 Ian Bridges 2026-11-25  790  	static const u8 flags_undefined[4] = { 4 }, flags_one_guid[4] = { 1 };
67e936310515186 Ian Bridges 2026-11-25  791  	static const u8 acl_rev3[8] = { 3, 0, 8 }, acl_and_more[12] = { 2, 0, 8 };
67e936310515186 Ian Bridges 2026-11-25  792  	static const u8 data[4];
67e936310515186 Ian Bridges 2026-11-25  793  	struct smb_sid rev2 = { .revision = 2, .num_subauth = 1 };
67e936310515186 Ian Bridges 2026-11-25  794  	struct smb_sid sub16 = { .revision = 1, .num_subauth = 16 };
67e936310515186 Ian Bridges 2026-11-25  795  
67e936310515186 Ian Bridges 2026-11-25  796  	T_REFUSES("a Control bit the encoder derives", sd->control = DACL_PRESENT);
67e936310515186 Ian Bridges 2026-11-25  797  	T_REFUSES("an owner SID of revision 2", sd->owner = &rev2);
67e936310515186 Ian Bridges 2026-11-25  798  	T_REFUSES("a group SID of 16 sub-authorities", sd->group = &sub16);
67e936310515186 Ian Bridges 2026-11-25  799  	T_REFUSES("a DACL state that is not one of the three", sd->dacl = 3);
67e936310515186 Ian Bridges 2026-11-25  800  	T_REFUSES("ACEs with a NULL DACL", sd->dacl = KSMBD_ACL_NULL);
67e936310515186 Ian Bridges 2026-11-25  801  	T_REFUSES("a DACL revision of 3", sd->dacl_revision = 3);
67e936310515186 Ian Bridges 2026-11-25  802  	T_REFUSES("a SACL state that is not one of the three", sd->sacl = 3);
67e936310515186 Ian Bridges 2026-11-25  803  	T_REFUSES("SACL bytes with no SACL",
67e936310515186 Ian Bridges 2026-11-25  804  		  sd->sacl_data = acl_and_more; sd->sacl_len = 8);
67e936310515186 Ian Bridges 2026-11-25  805  	T_REFUSES("a SACL with no bytes", sd->sacl = KSMBD_ACL_PRESENT);
67e936310515186 Ian Bridges 2026-11-25  806  	T_REFUSES("a SACL that the decoder would refuse",
67e936310515186 Ian Bridges 2026-11-25  807  		  sd->sacl = KSMBD_ACL_PRESENT; sd->sacl_data = acl_rev3;
67e936310515186 Ian Bridges 2026-11-25  808  		  sd->sacl_len = sizeof(acl_rev3));
67e936310515186 Ian Bridges 2026-11-25  809  	T_REFUSES("bytes after the SACL",
67e936310515186 Ian Bridges 2026-11-25  810  		  sd->sacl = KSMBD_ACL_PRESENT; sd->sacl_data = acl_and_more;
67e936310515186 Ian Bridges 2026-11-25  811  		  sd->sacl_len = sizeof(acl_and_more));
67e936310515186 Ian Bridges 2026-11-25  812  
67e936310515186 Ian Bridges 2026-11-25  813  	T_REFUSES("an ACE type with no layout", ace->type = 0x03);
67e936310515186 Ian Bridges 2026-11-25  814  	T_REFUSES("an ACE with no SID", ace->sid = NULL);
67e936310515186 Ian Bridges 2026-11-25  815  	T_REFUSES("an ACE SID of revision 2", ace->sid = &rev2);
67e936310515186 Ian Bridges 2026-11-25  816  	T_REFUSES("ACE data that is not a multiple of 4 bytes",
67e936310515186 Ian Bridges 2026-11-25  817  		  ace->data = data; ace->data_len = 3);
67e936310515186 Ian Bridges 2026-11-25  818  	T_REFUSES("an ACE data length with no data", ace->data_len = 4);
67e936310515186 Ian Bridges 2026-11-25  819  	T_REFUSES("an object section in an ACE that is not an object ACE",
67e936310515186 Ian Bridges 2026-11-25  820  		  ace->obj = flags_one_guid; ace->obj_len = 4);
67e936310515186 Ian Bridges 2026-11-25  821  	T_REFUSES("an object ACE with no object section",
67e936310515186 Ian Bridges 2026-11-25  822  		  ace->type = ACCESS_ALLOWED_OBJECT_ACE_TYPE; ace->obj_len = 20);
67e936310515186 Ian Bridges 2026-11-25  823  	T_REFUSES("an object ACE with an undefined flag",
67e936310515186 Ian Bridges 2026-11-25  824  		  ace->type = ACCESS_ALLOWED_OBJECT_ACE_TYPE;
67e936310515186 Ian Bridges 2026-11-25  825  		  ace->obj = flags_undefined; ace->obj_len = 4);
67e936310515186 Ian Bridges 2026-11-25  826  	T_REFUSES("an object section shorter than its Flags say",
67e936310515186 Ian Bridges 2026-11-25  827  		  ace->type = ACCESS_ALLOWED_OBJECT_ACE_TYPE;
67e936310515186 Ian Bridges 2026-11-25  828  		  ace->obj = flags_one_guid; ace->obj_len = 4);
67e936310515186 Ian Bridges 2026-11-25  829  }
67e936310515186 Ian Bridges 2026-11-25  830  
67e936310515186 Ian Bridges 2026-11-25  831  #define T_MAX_ACES	((U16_MAX - 8) / 16)
67e936310515186 Ian Bridges 2026-11-25  832  
67e936310515186 Ian Bridges 2026-11-25  833  /*
67e936310515186 Ian Bridges 2026-11-25  834   * AclSize is 16 bits wide (MS-DTYP 2.4.5), so a DACL holds at most U16_MAX
67e936310515186 Ian Bridges 2026-11-25  835   * bytes, and at most 4095 ACEs since the smallest ACE is 16 bytes. The decoder
67e936310515186 Ian Bridges 2026-11-25  836   * accepts a DACL of 4095 ACEs and the encoder writes it again. The encoder
67e936310515186 Ian Bridges 2026-11-25  837   * refuses a DACL with one ACE more, which would be too large.
67e936310515186 Ian Bridges 2026-11-25  838   */
67e936310515186 Ian Bridges 2026-11-25  839  static void ksmbd_sd_decode_encode_max_dacl_test(struct kunit *test)
67e936310515186 Ian Bridges 2026-11-25  840  {
67e936310515186 Ian Bridges 2026-11-25  841  	const size_t n = T_MAX_ACES, len = O_SD_HDR_LEN + U16_MAX;
67e936310515186 Ian Bridges 2026-11-25  842  	struct ksmbd_sd *sd = NULL, *more;
67e936310515186 Ian Bridges 2026-11-25  843  	size_t i;
67e936310515186 Ian Bridges 2026-11-25  844  	u8 *buf;
67e936310515186 Ian Bridges 2026-11-25  845  
67e936310515186 Ian Bridges 2026-11-25  846  	/*
67e936310515186 Ian Bridges 2026-11-25  847  	 * A DACL of 4095 ACEs whose SIDs have no sub-authority, followed by
67e936310515186 Ian Bridges 2026-11-25  848  	 * 7 unused bytes
67e936310515186 Ian Bridges 2026-11-25  849  	 */
67e936310515186 Ian Bridges 2026-11-25  850  	buf = kunit_kzalloc(test, len, GFP_KERNEL);
67e936310515186 Ian Bridges 2026-11-25  851  	KUNIT_ASSERT_NOT_NULL(test, buf);
67e936310515186 Ian Bridges 2026-11-25  852  	buf[O_SD_REVISION] = 1;
67e936310515186 Ian Bridges 2026-11-25  853  	put_unaligned_le16(SELF_RELATIVE | DACL_PRESENT, &buf[O_SD_CONTROL]);
67e936310515186 Ian Bridges 2026-11-25  854  	put_unaligned_le32(O_SD_HDR_LEN, &buf[O_SD_DACL]);
67e936310515186 Ian Bridges 2026-11-25  855  	buf[O_SD_HDR_LEN + O_ACL_REVISION] = 2;
67e936310515186 Ian Bridges 2026-11-25  856  	put_unaligned_le16(U16_MAX, &buf[O_SD_HDR_LEN + O_ACL_SIZE]);
67e936310515186 Ian Bridges 2026-11-25  857  	put_unaligned_le16(n, &buf[O_SD_HDR_LEN + O_ACL_COUNT]);
67e936310515186 Ian Bridges 2026-11-25  858  	for (i = 0; i < n; i++) {
67e936310515186 Ian Bridges 2026-11-25  859  		u8 *ace = &buf[O_SD_HDR_LEN + O_ACL_HDR_LEN + 16 * i];
67e936310515186 Ian Bridges 2026-11-25  860  
67e936310515186 Ian Bridges 2026-11-25  861  		put_unaligned_le16(16, &ace[O_ACE_SIZE]);
67e936310515186 Ian Bridges 2026-11-25  862  		put_unaligned_le32(i + 1, &ace[O_ACE_MASK]);
67e936310515186 Ian Bridges 2026-11-25  863  		ace[8 + O_SID_REVISION] = 1;
67e936310515186 Ian Bridges 2026-11-25  864  		ace[8 + O_SID_AUTHORITY] = 5;
67e936310515186 Ian Bridges 2026-11-25  865  	}
67e936310515186 Ian Bridges 2026-11-25  866  	sd = t_decode(test, buf, len);
67e936310515186 Ian Bridges 2026-11-25  867  	KUNIT_ASSERT_EQ(test, sd->num_aces, n);
67e936310515186 Ian Bridges 2026-11-25  868  	KUNIT_EXPECT_EQ(test, sd->aces[n - 1].mask, n);
67e936310515186 Ian Bridges 2026-11-25 @869  	KUNIT_EXPECT_EQ(test, t_try_encode(test, sd), 0);
67e936310515186 Ian Bridges 2026-11-25  870  	/* One ACE more makes the DACL too large for the encoder */
67e936310515186 Ian Bridges 2026-11-25  871  	more = t_alloc(test, n + 1);
67e936310515186 Ian Bridges 2026-11-25  872  	more->dacl = KSMBD_ACL_PRESENT;
67e936310515186 Ian Bridges 2026-11-25  873  	memcpy(more->aces, sd->aces, flex_array_size(sd, aces, n));
67e936310515186 Ian Bridges 2026-11-25 @874  	more->aces[n] = sd->aces[0];
67e936310515186 Ian Bridges 2026-11-25 @875  	KUNIT_EXPECT_EQ(test, t_try_encode(test, more), -E2BIG);
67e936310515186 Ian Bridges 2026-11-25 @876  	more->num_aces = n;
67e936310515186 Ian Bridges 2026-11-25  877  	KUNIT_EXPECT_EQ(test, t_try_encode(test, more), 0);
67e936310515186 Ian Bridges 2026-11-25  878  }

67e936310515186 Ian Bridges 2026-11-25 879

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki