@@ -45,53 +45,83 @@ void AutoCommissioner::SetOperationalCredentialsDelegate(OperationalCredentialsD
45
45
mOperationalCredentialsDelegate = operationalCredentialsDelegate;
46
46
}
47
47
48
- CHIP_ERROR AutoCommissioner::SetCommissioningParameters (const CommissioningParameters & params)
48
+ // Returns true if maybeUnsafeSpan is pointing to a buffer that we're not sure
49
+ // will live for long enough. knownSafeSpan, if it has a value, points to a
50
+ // buffer that we _are_ sure will live for long enough.
51
+ template <typename SpanType>
52
+ static bool IsUnsafeSpan (const Optional<SpanType> & maybeUnsafeSpan, const Optional<SpanType> & knownSafeSpan)
49
53
{
50
- mParams = params;
51
- if (params.GetFailsafeTimerSeconds ().HasValue ())
54
+ if (!maybeUnsafeSpan.HasValue ())
52
55
{
53
- ChipLogProgress (Controller, " Setting failsafe timer from parameters" );
54
- mParams .SetFailsafeTimerSeconds (params.GetFailsafeTimerSeconds ().Value ());
56
+ return false ;
55
57
}
56
58
57
- if (params. GetCASEFailsafeTimerSeconds () .HasValue ())
59
+ if (!knownSafeSpan .HasValue ())
58
60
{
59
- ChipLogProgress (Controller, " Setting CASE failsafe timer from parameters" );
60
- mParams .SetCASEFailsafeTimerSeconds (params.GetCASEFailsafeTimerSeconds ().Value ());
61
+ return true ;
61
62
}
62
63
63
- if (params.GetAdminSubject ().HasValue ())
64
+ return maybeUnsafeSpan.Value ().data () != knownSafeSpan.Value ().data ();
65
+ }
66
+
67
+ CHIP_ERROR AutoCommissioner::SetCommissioningParameters (const CommissioningParameters & params)
68
+ {
69
+ // Make sure any members that point to buffers that we are not pointing to
70
+ // our own buffers are not going to dangle. We can skip this step if all
71
+ // the buffers pointers that we don't plan to re-point to our own buffers
72
+ // below are already pointing to the same things as our own buffer pointers
73
+ // (so that we know they have to be safe somehow).
74
+ //
75
+ // The checks are a bit painful, because Span does not have a usable
76
+ // operator==, and in any case, we want to compare for pointer equality, not
77
+ // data equality.
78
+ bool haveMaybeDanglingBufferPointers =
79
+ ((params.GetNOCChainGenerationParameters ().HasValue () &&
80
+ (!mParams .GetNOCChainGenerationParameters ().HasValue () ||
81
+ params.GetNOCChainGenerationParameters ().Value ().nocsrElements .data () !=
82
+ mParams .GetNOCChainGenerationParameters ().Value ().nocsrElements .data () ||
83
+ params.GetNOCChainGenerationParameters ().Value ().signature .data () !=
84
+ mParams .GetNOCChainGenerationParameters ().Value ().signature .data ())) ||
85
+ IsUnsafeSpan (params.GetRootCert (), mParams .GetRootCert ()) || IsUnsafeSpan (params.GetNoc (), mParams .GetNoc ()) ||
86
+ IsUnsafeSpan (params.GetIcac (), mParams .GetIcac ()) || IsUnsafeSpan (params.GetIpk (), mParams .GetIpk ()) ||
87
+ IsUnsafeSpan (params.GetAttestationElements (), mParams .GetAttestationElements ()) ||
88
+ IsUnsafeSpan (params.GetAttestationSignature (), mParams .GetAttestationSignature ()) ||
89
+ IsUnsafeSpan (params.GetPAI (), mParams .GetPAI ()) || IsUnsafeSpan (params.GetDAC (), mParams .GetDAC ()));
90
+
91
+ mParams = params;
92
+
93
+ if (haveMaybeDanglingBufferPointers)
64
94
{
65
- ChipLogProgress (Controller, " Setting adminSubject from parameters" );
66
- mParams .SetAdminSubject (params.GetAdminSubject ().Value ());
95
+ mParams .ClearExternalBufferDependentValues ();
67
96
}
68
97
98
+ // For members of params that point to some sort of buffer, we have to copy
99
+ // the data over into our own buffers.
100
+
69
101
if (params.GetThreadOperationalDataset ().HasValue ())
70
102
{
71
103
ByteSpan dataset = params.GetThreadOperationalDataset ().Value ();
72
104
if (dataset.size () > CommissioningParameters::kMaxThreadDatasetLen )
73
105
{
74
106
ChipLogError (Controller, " Thread operational data set is too large" );
107
+ // Make sure our buffer pointers don't dangle.
108
+ mParams .ClearExternalBufferDependentValues ();
75
109
return CHIP_ERROR_INVALID_ARGUMENT;
76
110
}
77
111
memcpy (mThreadOperationalDataset , dataset.data (), dataset.size ());
78
112
ChipLogProgress (Controller, " Setting thread operational dataset from parameters" );
79
113
mParams .SetThreadOperationalDataset (ByteSpan (mThreadOperationalDataset , dataset.size ()));
80
114
}
81
115
82
- if (params.GetAttemptThreadNetworkScan ().HasValue ())
83
- {
84
- ChipLogProgress (Controller, " Setting attempt thread scan from parameters" );
85
- mParams .SetAttemptThreadNetworkScan (params.GetAttemptThreadNetworkScan ().Value ());
86
- }
87
-
88
116
if (params.GetWiFiCredentials ().HasValue ())
89
117
{
90
118
WiFiCredentials creds = params.GetWiFiCredentials ().Value ();
91
119
if (creds.ssid .size () > CommissioningParameters::kMaxSsidLen ||
92
120
creds.credentials .size () > CommissioningParameters::kMaxCredentialsLen )
93
121
{
94
122
ChipLogError (Controller, " Wifi credentials are too large" );
123
+ // Make sure our buffer pointers don't dangle.
124
+ mParams .ClearExternalBufferDependentValues ();
95
125
return CHIP_ERROR_INVALID_ARGUMENT;
96
126
}
97
127
memcpy (mSsid , creds.ssid .data (), creds.ssid .size ());
@@ -101,12 +131,6 @@ CHIP_ERROR AutoCommissioner::SetCommissioningParameters(const CommissioningParam
101
131
WiFiCredentials (ByteSpan (mSsid , creds.ssid .size ()), ByteSpan (mCredentials , creds.credentials .size ())));
102
132
}
103
133
104
- if (params.GetAttemptWiFiNetworkScan ().HasValue ())
105
- {
106
- ChipLogProgress (Controller, " Setting attempt wifi scan from parameters" );
107
- mParams .SetAttemptWiFiNetworkScan (params.GetAttemptWiFiNetworkScan ().Value ());
108
- }
109
-
110
134
if (params.GetCountryCode ().HasValue ())
111
135
{
112
136
auto code = params.GetCountryCode ().Value ();
@@ -118,6 +142,9 @@ CHIP_ERROR AutoCommissioner::SetCommissioningParameters(const CommissioningParam
118
142
else
119
143
{
120
144
ChipLogError (Controller, " Country code is too large: %u" , static_cast <unsigned >(code.size ()));
145
+ // Make sure our buffer pointers don't dangle.
146
+ mParams .ClearExternalBufferDependentValues ();
147
+ return CHIP_ERROR_INVALID_ARGUMENT;
121
148
}
122
149
}
123
150
@@ -148,12 +175,6 @@ CHIP_ERROR AutoCommissioner::SetCommissioningParameters(const CommissioningParam
148
175
}
149
176
mParams .SetCSRNonce (ByteSpan (mCSRNonce , sizeof (mCSRNonce )));
150
177
151
- if (params.GetSkipCommissioningComplete ().HasValue ())
152
- {
153
- ChipLogProgress (Controller, " Setting PASE-only commissioning from parameters" );
154
- mParams .SetSkipCommissioningComplete (params.GetSkipCommissioningComplete ().Value ());
155
- }
156
-
157
178
return CHIP_NO_ERROR;
158
179
}
159
180
0 commit comments