已开启
fix: add null pointer check for profile content buffer #345
wangtiantian创建于 7月14日
fix: add null pointer check for profile content buffer #345
已开启
wangtiantian创建于 7月14日
2 个文件变更+68-1
@@ -47,7 +47,14 @@ bool HapProfileVerifyUtils::ParseProfile(Pkcs7Context& profilePkcs7Context, cons
47 return false;47 return false;
48 }48 }
49 49 
50- profile = std::string(profilePkcs7Context.content.GetBufferPtr(), profilePkcs7Context.content.GetCapacity());50+ const char* contentBuffer = profilePkcs7Context.content.GetBufferPtr();
51+ int32_t contentLen = profilePkcs7Context.content.GetCapacity();
52+ if (contentBuffer == nullptr || contentLen <= 0) {
53+ HAPVERIFY_LOG_ERROR("profile content is invalid after pkcs7 parsing");
54+ return false;
55+ }
56+ 
57+ profile = std::string(contentBuffer, static_cast<size_t>(contentLen));
51 return true;58 return true;
52}59}
53 60 
@@ -97,4 +97,64 @@ HWTEST_F(HapProfileVerifyUtilsTest, ParseProfileTest003, TestSize.Level1)
97 bool ret = HapProfileVerifyUtils::ParseProfile(profilePkcs7Context, hapPkcs7Context, pkcs7ProfileBlock, profile);97 bool ret = HapProfileVerifyUtils::ParseProfile(profilePkcs7Context, hapPkcs7Context, pkcs7ProfileBlock, profile);
98 EXPECT_FALSE(ret);98 EXPECT_FALSE(ret);
99}99}
100+ 
101+/**
102+ * @tc.name: ParseProfileTest004
103+ * @tc.desc: Test ParseProfile with invalid PKCS7 data that might result in null content
104+ * @tc.type: FUNC
105+ */
106+HWTEST_F(HapProfileVerifyUtilsTest, ParseProfileTest004, TestSize.Level1)
107+{
108+ Pkcs7Context profilePkcs7Context = {};
109+ Pkcs7Context hapPkcs7Context = {};
110+ std::string mockProfile = "invalid_pkcs7_data_with_no_valid_structure";
111+ HapByteBuffer pkcs7ProfileBlock(mockProfile.size());
112+ pkcs7ProfileBlock.PutData(0, mockProfile.c_str(), mockProfile.size());
113+ std::string profile = "";
114+ bool ret = HapProfileVerifyUtils::ParseProfile(profilePkcs7Context, hapPkcs7Context, pkcs7ProfileBlock, profile);
115+ // Should fail due to ParsePkcs7Package failure
116+ EXPECT_FALSE(ret);
117+ // Verify that profile is not modified when parsing fails
118+ EXPECT_TRUE(profile.empty());
119+}
120+ 
121+/**
122+ * @tc.name: ParseProfileTest005
123+ * @tc.desc: Test ParseProfile with malformed PKCS7 data to test content validation
124+ * @tc.type: FUNC
125+ */
126+HWTEST_F(HapProfileVerifyUtilsTest, ParseProfileTest005, TestSize.Level1)
127+{
128+ Pkcs7Context profilePkcs7Context = {};
129+ Pkcs7Context hapPkcs7Context = {};
130+ // Create test data that might trigger edge cases
131+ std::string mockProfile = "\x00\x01\x02\x03\x04\x05";
132+ HapByteBuffer pkcs7ProfileBlock(mockProfile.size());
133+ pkcs7ProfileBlock.PutData(0, mockProfile.c_str(), mockProfile.size());
134+ std::string profile = "";
135+ bool ret = HapProfileVerifyUtils::ParseProfile(profilePkcs7Context, hapPkcs7Context, pkcs7ProfileBlock, profile);
136+ // Should fail due to invalid PKCS7 structure or content validation
137+ EXPECT_FALSE(ret);
138+}
139+ 
140+/**
141+ * @tc.name: ParseProfileTest006
142+ * @tc.desc: Test ParseProfile with APP_GALLARY source but empty content validation
143+ * @tc.type: FUNC
144+ */
145+HWTEST_F(HapProfileVerifyUtilsTest, ParseProfileTest006, TestSize.Level1)
146+{
147+ Pkcs7Context profilePkcs7Context = {};
148+ Pkcs7Context hapPkcs7Context = {};
149+ // Set APP_GALLARY source to trigger the early return path
150+ hapPkcs7Context.matchResult.matchState = MATCH_WITH_SIGN;
151+ hapPkcs7Context.matchResult.source = APP_GALLARY;
152+ 
153+ // Test with empty profile block - should fail the initial validation
154+ HapByteBuffer emptyPkcs7ProfileBlock(0);
155+ std::string profile = "";
156+ bool ret = HapProfileVerifyUtils::ParseProfile(profilePkcs7Context, hapPkcs7Context, emptyPkcs7ProfileBlock, profile);
157+ EXPECT_FALSE(ret);
158+ EXPECT_TRUE(profile.empty());
159+}
100}160}