@@ -65,23 +65,44 @@ bool asymmetric_key_id_same(const struct asymmetric_key_id *kid1,
6565}
6666EXPORT_SYMBOL_GPL (asymmetric_key_id_same );
6767
68+ /**
69+ * asymmetric_key_id_partial - Return true if two asymmetric keys IDs
70+ * partially match
71+ * @kid_1, @kid_2: The key IDs to compare
72+ */
73+ bool asymmetric_key_id_partial (const struct asymmetric_key_id * kid1 ,
74+ const struct asymmetric_key_id * kid2 )
75+ {
76+ if (!kid1 || !kid2 )
77+ return false;
78+ if (kid1 -> len < kid2 -> len )
79+ return false;
80+ return memcmp (kid1 -> data + (kid1 -> len - kid2 -> len ),
81+ kid2 -> data , kid2 -> len ) == 0 ;
82+ }
83+ EXPORT_SYMBOL_GPL (asymmetric_key_id_partial );
84+
6885/**
6986 * asymmetric_match_key_ids - Search asymmetric key IDs
7087 * @kids: The list of key IDs to check
7188 * @match_id: The key ID we're looking for
89+ * @match: The match function to use
7290 */
73- bool asymmetric_match_key_ids (const struct asymmetric_key_ids * kids ,
74- const struct asymmetric_key_id * match_id )
91+ static bool asymmetric_match_key_ids (
92+ const struct asymmetric_key_ids * kids ,
93+ const struct asymmetric_key_id * match_id ,
94+ bool (* match )(const struct asymmetric_key_id * kid1 ,
95+ const struct asymmetric_key_id * kid2 ))
7596{
97+ int i ;
98+
7699 if (!kids || !match_id )
77100 return false;
78- if (asymmetric_key_id_same (kids -> id [0 ], match_id ))
79- return true;
80- if (asymmetric_key_id_same (kids -> id [1 ], match_id ))
81- return true;
101+ for (i = 0 ; i < ARRAY_SIZE (kids -> id ); i ++ )
102+ if (match (kids -> id [i ], match_id ))
103+ return true;
82104 return false;
83105}
84- EXPORT_SYMBOL_GPL (asymmetric_match_key_ids );
85106
86107/**
87108 * asymmetric_key_hex_to_key_id - Convert a hex string into a key ID.
@@ -113,15 +134,29 @@ struct asymmetric_key_id *asymmetric_key_hex_to_key_id(const char *id)
113134}
114135
115136/*
116- * Match asymmetric keys by ID.
137+ * Match asymmetric keys by an exact match on an ID.
117138 */
118139static bool asymmetric_key_cmp (const struct key * key ,
119140 const struct key_match_data * match_data )
120141{
121142 const struct asymmetric_key_ids * kids = asymmetric_key_ids (key );
122143 const struct asymmetric_key_id * match_id = match_data -> preparsed ;
123144
124- return asymmetric_match_key_ids (kids , match_id );
145+ return asymmetric_match_key_ids (kids , match_id ,
146+ asymmetric_key_id_same );
147+ }
148+
149+ /*
150+ * Match asymmetric keys by a partial match on an IDs.
151+ */
152+ static bool asymmetric_key_cmp_partial (const struct key * key ,
153+ const struct key_match_data * match_data )
154+ {
155+ const struct asymmetric_key_ids * kids = asymmetric_key_ids (key );
156+ const struct asymmetric_key_id * match_id = match_data -> preparsed ;
157+
158+ return asymmetric_match_key_ids (kids , match_id ,
159+ asymmetric_key_id_partial );
125160}
126161
127162/*
@@ -131,7 +166,8 @@ static bool asymmetric_key_cmp(const struct key *key,
131166 * There are some specifiers for matching key IDs rather than by the key
132167 * description:
133168 *
134- * "id:<id>" - request a key by any available ID
169+ * "id:<id>" - find a key by partial match on any available ID
170+ * "ex:<id>" - find a key by exact match on any available ID
135171 *
136172 * These have to be searched by iteration rather than by direct lookup because
137173 * the key is hashed according to its description.
@@ -141,13 +177,20 @@ static int asymmetric_key_match_preparse(struct key_match_data *match_data)
141177 struct asymmetric_key_id * match_id ;
142178 const char * spec = match_data -> raw_data ;
143179 const char * id ;
180+ bool (* cmp )(const struct key * , const struct key_match_data * ) =
181+ asymmetric_key_cmp ;
144182
145183 if (!spec || !* spec )
146184 return - EINVAL ;
147185 if (spec [0 ] == 'i' &&
148186 spec [1 ] == 'd' &&
149187 spec [2 ] == ':' ) {
150188 id = spec + 3 ;
189+ cmp = asymmetric_key_cmp_partial ;
190+ } else if (spec [0 ] == 'e' &&
191+ spec [1 ] == 'x' &&
192+ spec [2 ] == ':' ) {
193+ id = spec + 3 ;
151194 } else {
152195 goto default_match ;
153196 }
@@ -157,7 +200,7 @@ static int asymmetric_key_match_preparse(struct key_match_data *match_data)
157200 return PTR_ERR (match_id );
158201
159202 match_data -> preparsed = match_id ;
160- match_data -> cmp = asymmetric_key_cmp ;
203+ match_data -> cmp = cmp ;
161204 match_data -> lookup_type = KEYRING_SEARCH_LOOKUP_ITERATE ;
162205 return 0 ;
163206
@@ -251,6 +294,7 @@ static void asymmetric_key_free_preparse(struct key_preparsed_payload *prep)
251294{
252295 struct asymmetric_key_subtype * subtype = prep -> type_data [0 ];
253296 struct asymmetric_key_ids * kids = prep -> type_data [1 ];
297+ int i ;
254298
255299 pr_devel ("==>%s()\n" , __func__ );
256300
@@ -259,8 +303,8 @@ static void asymmetric_key_free_preparse(struct key_preparsed_payload *prep)
259303 module_put (subtype -> owner );
260304 }
261305 if (kids ) {
262- kfree ( kids -> id [ 0 ]);
263- kfree (kids -> id [1 ]);
306+ for ( i = 0 ; i < ARRAY_SIZE ( kids -> id ); i ++ )
307+ kfree (kids -> id [i ]);
264308 kfree (kids );
265309 }
266310 kfree (prep -> description );
0 commit comments