@@ -49,25 +49,6 @@ void AutoCommissioner::SetOperationalCredentialsDelegate(OperationalCredentialsD
49
49
mOperationalCredentialsDelegate = operationalCredentialsDelegate;
50
50
}
51
51
52
- // Returns true if maybeUnsafeSpan is pointing to a buffer that we're not sure
53
- // will live for long enough. knownSafeSpan, if it has a value, points to a
54
- // buffer that we _are_ sure will live for long enough.
55
- template <typename SpanType>
56
- static bool IsUnsafeSpan (const Optional<SpanType> & maybeUnsafeSpan, const Optional<SpanType> & knownSafeSpan)
57
- {
58
- if (!maybeUnsafeSpan.HasValue ())
59
- {
60
- return false ;
61
- }
62
-
63
- if (!knownSafeSpan.HasValue ())
64
- {
65
- return true ;
66
- }
67
-
68
- return maybeUnsafeSpan.Value ().data () != knownSafeSpan.Value ().data ();
69
- }
70
-
71
52
CHIP_ERROR AutoCommissioner::VerifyICDRegistrationInfo (const CommissioningParameters & params)
72
53
{
73
54
ChipLogProgress (Controller, " Checking ICD registration parameters" );
@@ -101,56 +82,26 @@ CHIP_ERROR AutoCommissioner::VerifyICDRegistrationInfo(const CommissioningParame
101
82
102
83
CHIP_ERROR AutoCommissioner::SetCommissioningParameters (const CommissioningParameters & params)
103
84
{
104
- // Make sure any members that point to buffers that we are not pointing to
105
- // our own buffers are not going to dangle. We can skip this step if all
106
- // the buffers pointers that we don't plan to re-point to our own buffers
107
- // below are already pointing to the same things as our own buffer pointers
108
- // (so that we know they have to be safe somehow).
109
- //
110
- // The checks are a bit painful, because Span does not have a usable
111
- // operator==, and in any case, we want to compare for pointer equality, not
112
- // data equality.
113
- bool haveMaybeDanglingBufferPointers =
114
- ((params.GetNOCChainGenerationParameters ().HasValue () &&
115
- (!mParams .GetNOCChainGenerationParameters ().HasValue () ||
116
- params.GetNOCChainGenerationParameters ().Value ().nocsrElements .data () !=
117
- mParams .GetNOCChainGenerationParameters ().Value ().nocsrElements .data () ||
118
- params.GetNOCChainGenerationParameters ().Value ().signature .data () !=
119
- mParams .GetNOCChainGenerationParameters ().Value ().signature .data ())) ||
120
- IsUnsafeSpan (params.GetRootCert (), mParams .GetRootCert ()) || IsUnsafeSpan (params.GetNoc (), mParams .GetNoc ()) ||
121
- IsUnsafeSpan (params.GetIcac (), mParams .GetIcac ()) || IsUnsafeSpan (params.GetIpk (), mParams .GetIpk ()) ||
122
- IsUnsafeSpan (params.GetAttestationElements (), mParams .GetAttestationElements ()) ||
123
- IsUnsafeSpan (params.GetAttestationSignature (), mParams .GetAttestationSignature ()) ||
124
- IsUnsafeSpan (params.GetPAI (), mParams .GetPAI ()) || IsUnsafeSpan (params.GetDAC (), mParams .GetDAC ()) ||
125
- IsUnsafeSpan (params.GetTimeZone (), mParams .GetTimeZone ()) ||
126
- IsUnsafeSpan (params.GetDSTOffsets (), mParams .GetDSTOffsets ()) ||
127
- IsUnsafeSpan (params.GetICDSymmetricKey (), mParams .GetICDSymmetricKey ()) ||
128
- (params.GetDefaultNTP ().HasValue () && !params.GetDefaultNTP ().Value ().IsNull () &&
129
- params.GetDefaultNTP ().Value ().Value ().data () != mDefaultNtp ));
130
-
85
+ // Our logic below assumes that we can modify mParams without affecting params.
86
+ VerifyOrReturnError (¶ms != &mParams , CHIP_NO_ERROR);
87
+
88
+ // Copy the whole struct (scalars and pointers), but clear any members that might point to
89
+ // external buffers. For those members we have to copy the data over into our own buffers below.
90
+ // Note that all of the copy operations use memmove() instead of memcpy(), because the caller
91
+ // may be passing a modified shallow copy of our CommissioningParmeters, i.e. where various spans
92
+ // already point into the buffers we're copying into, and memcpy() with overlapping buffers is UB.
131
93
mParams = params;
132
-
133
- mNeedIcdRegistration = false ;
134
-
135
- if (haveMaybeDanglingBufferPointers)
136
- {
137
- mParams .ClearExternalBufferDependentValues ();
138
- }
139
-
140
- // For members of params that point to some sort of buffer, we have to copy
141
- // the data over into our own buffers.
94
+ mParams .ClearExternalBufferDependentValues ();
142
95
143
96
if (params.GetThreadOperationalDataset ().HasValue ())
144
97
{
145
98
ByteSpan dataset = params.GetThreadOperationalDataset ().Value ();
146
99
if (dataset.size () > CommissioningParameters::kMaxThreadDatasetLen )
147
100
{
148
101
ChipLogError (Controller, " Thread operational data set is too large" );
149
- // Make sure our buffer pointers don't dangle.
150
- mParams .ClearExternalBufferDependentValues ();
151
102
return CHIP_ERROR_INVALID_ARGUMENT;
152
103
}
153
- memcpy (mThreadOperationalDataset , dataset.data (), dataset.size ());
104
+ memmove (mThreadOperationalDataset , dataset.data (), dataset.size ());
154
105
ChipLogProgress (Controller, " Setting thread operational dataset from parameters" );
155
106
mParams .SetThreadOperationalDataset (ByteSpan (mThreadOperationalDataset , dataset.size ()));
156
107
}
@@ -162,12 +113,10 @@ CHIP_ERROR AutoCommissioner::SetCommissioningParameters(const CommissioningParam
162
113
creds.credentials .size () > CommissioningParameters::kMaxCredentialsLen )
163
114
{
164
115
ChipLogError (Controller, " Wifi credentials are too large" );
165
- // Make sure our buffer pointers don't dangle.
166
- mParams .ClearExternalBufferDependentValues ();
167
116
return CHIP_ERROR_INVALID_ARGUMENT;
168
117
}
169
- memcpy (mSsid , creds.ssid .data (), creds.ssid .size ());
170
- memcpy (mCredentials , creds.credentials .data (), creds.credentials .size ());
118
+ memmove (mSsid , creds.ssid .data (), creds.ssid .size ());
119
+ memmove (mCredentials , creds.credentials .data (), creds.credentials .size ());
171
120
ChipLogProgress (Controller, " Setting wifi credentials from parameters" );
172
121
mParams .SetWiFiCredentials (
173
122
WiFiCredentials (ByteSpan (mSsid , creds.ssid .size ()), ByteSpan (mCredentials , creds.credentials .size ())));
@@ -184,8 +133,6 @@ CHIP_ERROR AutoCommissioner::SetCommissioningParameters(const CommissioningParam
184
133
else
185
134
{
186
135
ChipLogError (Controller, " Country code is too large: %u" , static_cast <unsigned >(code.size ()));
187
- // Make sure our buffer pointers don't dangle.
188
- mParams .ClearExternalBufferDependentValues ();
189
136
return CHIP_ERROR_INVALID_ARGUMENT;
190
137
}
191
138
}
@@ -195,7 +142,7 @@ CHIP_ERROR AutoCommissioner::SetCommissioningParameters(const CommissioningParam
195
142
{
196
143
ChipLogProgress (Controller, " Setting attestation nonce from parameters" );
197
144
VerifyOrReturnError (params.GetAttestationNonce ().Value ().size () == sizeof (mAttestationNonce ), CHIP_ERROR_INVALID_ARGUMENT);
198
- memcpy (mAttestationNonce , params.GetAttestationNonce ().Value ().data (), params.GetAttestationNonce ().Value ().size ());
145
+ memmove (mAttestationNonce , params.GetAttestationNonce ().Value ().data (), params.GetAttestationNonce ().Value ().size ());
199
146
}
200
147
else
201
148
{
@@ -208,7 +155,7 @@ CHIP_ERROR AutoCommissioner::SetCommissioningParameters(const CommissioningParam
208
155
{
209
156
ChipLogProgress (Controller, " Setting CSR nonce from parameters" );
210
157
VerifyOrReturnError (params.GetCSRNonce ().Value ().size () == sizeof (mCSRNonce ), CHIP_ERROR_INVALID_ARGUMENT);
211
- memcpy (mCSRNonce , params.GetCSRNonce ().Value ().data (), params.GetCSRNonce ().Value ().size ());
158
+ memmove (mCSRNonce , params.GetCSRNonce ().Value ().data (), params.GetCSRNonce ().Value ().size ());
212
159
}
213
160
else
214
161
{
@@ -271,7 +218,7 @@ CHIP_ERROR AutoCommissioner::SetCommissioningParameters(const CommissioningParam
271
218
ReturnErrorOnFailure (VerifyICDRegistrationInfo (params));
272
219
273
220
// The values must be valid now.
274
- memcpy (mICDSymmetricKey , params.GetICDSymmetricKey ().Value ().data (), params.GetICDSymmetricKey ().Value ().size ());
221
+ memmove (mICDSymmetricKey , params.GetICDSymmetricKey ().Value ().data (), params.GetICDSymmetricKey ().Value ().size ());
275
222
mParams .SetICDSymmetricKey (ByteSpan (mICDSymmetricKey ));
276
223
mParams .SetICDCheckInNodeId (params.GetICDCheckInNodeId ().Value ());
277
224
mParams .SetICDMonitoredSubject (params.GetICDMonitoredSubject ().Value ());
@@ -787,6 +734,7 @@ CHIP_ERROR AutoCommissioner::CommissioningStepFinished(CHIP_ERROR err, Commissio
787
734
}
788
735
}
789
736
737
+ mNeedIcdRegistration = false ;
790
738
if (mParams .GetICDRegistrationStrategy () != ICDRegistrationStrategy::kIgnore )
791
739
{
792
740
if (mDeviceCommissioningInfo .icd .isLIT && mDeviceCommissioningInfo .icd .checkInProtocolSupport )
0 commit comments