@@ -83,11 +83,43 @@ static struct key_type key_type_blacklist = {
8383 .describe = blacklist_describe ,
8484};
8585
86+ static char * get_raw_hash (const u8 * hash , size_t hash_len ,
87+ enum blacklist_hash_type hash_type )
88+ {
89+ size_t type_len ;
90+ const char * type_prefix ;
91+ char * buffer , * p ;
92+
93+ switch (hash_type ) {
94+ case BLACKLIST_HASH_X509_TBS :
95+ type_len = sizeof (tbs_prefix ) - 1 ;
96+ type_prefix = tbs_prefix ;
97+ break ;
98+ case BLACKLIST_HASH_BINARY :
99+ type_len = sizeof (bin_prefix ) - 1 ;
100+ type_prefix = bin_prefix ;
101+ break ;
102+ default :
103+ WARN_ON_ONCE (1 );
104+ return ERR_PTR (- EINVAL );
105+ }
106+ buffer = kmalloc (type_len + 1 + hash_len * 2 + 1 , GFP_KERNEL );
107+ if (!buffer )
108+ return ERR_PTR (- ENOMEM );
109+ p = memcpy (buffer , type_prefix , type_len );
110+ p += type_len ;
111+ * p ++ = ':' ;
112+ bin2hex (p , hash , hash_len );
113+ p += hash_len * 2 ;
114+ * p = '\0' ;
115+ return buffer ;
116+ }
117+
86118/**
87- * mark_hash_blacklisted - Add a hash to the system blacklist
119+ * mark_raw_hash_blacklisted - Add a hash to the system blacklist
88120 * @hash: The hash as a hex string with a type prefix (eg. "tbs:23aa429783")
89121 */
90- int mark_hash_blacklisted (const char * hash )
122+ static int mark_raw_hash_blacklisted (const char * hash )
91123{
92124 key_ref_t key ;
93125
@@ -107,29 +139,36 @@ int mark_hash_blacklisted(const char *hash)
107139 return 0 ;
108140}
109141
142+ int mark_hash_blacklisted (const u8 * hash , size_t hash_len ,
143+ enum blacklist_hash_type hash_type )
144+ {
145+ const char * buffer ;
146+ int err ;
147+
148+ buffer = get_raw_hash (hash , hash_len , hash_type );
149+ if (IS_ERR (buffer ))
150+ return PTR_ERR (buffer );
151+ err = mark_raw_hash_blacklisted (buffer );
152+ kfree (buffer );
153+ return err ;
154+ }
155+
110156/**
111157 * is_hash_blacklisted - Determine if a hash is blacklisted
112158 * @hash: The hash to be checked as a binary blob
113159 * @hash_len: The length of the binary hash
114- * @type : Type of hash
160+ * @hash_type : Type of hash
115161 */
116- int is_hash_blacklisted (const u8 * hash , size_t hash_len , const char * type )
162+ int is_hash_blacklisted (const u8 * hash , size_t hash_len ,
163+ enum blacklist_hash_type hash_type )
117164{
118165 key_ref_t kref ;
119- size_t type_len = strlen (type );
120- char * buffer , * p ;
166+ const char * buffer ;
121167 int ret = 0 ;
122168
123- buffer = kmalloc (type_len + 1 + hash_len * 2 + 1 , GFP_KERNEL );
124- if (!buffer )
125- return - ENOMEM ;
126- p = memcpy (buffer , type , type_len );
127- p += type_len ;
128- * p ++ = ':' ;
129- bin2hex (p , hash , hash_len );
130- p += hash_len * 2 ;
131- * p = 0 ;
132-
169+ buffer = get_raw_hash (hash , hash_len , hash_type );
170+ if (IS_ERR (buffer ))
171+ return PTR_ERR (buffer );
133172 kref = keyring_search (make_key_ref (blacklist_keyring , true),
134173 & key_type_blacklist , buffer , false);
135174 if (!IS_ERR (kref )) {
@@ -144,7 +183,8 @@ EXPORT_SYMBOL_GPL(is_hash_blacklisted);
144183
145184int is_binary_blacklisted (const u8 * hash , size_t hash_len )
146185{
147- if (is_hash_blacklisted (hash , hash_len , "bin" ) == - EKEYREJECTED )
186+ if (is_hash_blacklisted (hash , hash_len , BLACKLIST_HASH_BINARY ) ==
187+ - EKEYREJECTED )
148188 return - EPERM ;
149189
150190 return 0 ;
@@ -217,7 +257,7 @@ static int __init blacklist_init(void)
217257 panic ("Can't allocate system blacklist keyring\n" );
218258
219259 for (bl = blacklist_hashes ; * bl ; bl ++ )
220- if (mark_hash_blacklisted (* bl ) < 0 )
260+ if (mark_raw_hash_blacklisted (* bl ) < 0 )
221261 pr_err ("- blacklisting failed\n" );
222262 return 0 ;
223263}
0 commit comments