aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Howells <dhowells@redhat.com>2012-10-19 20:19:29 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2012-10-19 20:30:40 -0400
commitcaabe240574aec05b2f5667414ce80f9075c2ba1 (patch)
treed92bf96b009bd0b0caec44c21348812b06805909
parentb6bb324dbddd704b4b9a85971e1f7ae79abb2e1d (diff)
MODSIGN: Move the magic string to the end of a module and eliminate the search
Emit the magic string that indicates a module has a signature after the signature data instead of before it. This allows module_sig_check() to be made simpler and faster by the elimination of the search for the magic string. Instead we just need to do a single memcmp(). This works because at the end of the signature data there is the fixed-length signature information block. This block then falls immediately prior to the magic number. From the contents of the information block, it is trivial to calculate the size of the signature data and thus the size of the actual module data. Signed-off-by: David Howells <dhowells@redhat.com> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
-rw-r--r--kernel/module-internal.h3
-rw-r--r--kernel/module.c26
-rw-r--r--kernel/module_signing.c24
-rwxr-xr-xscripts/sign-file6
4 files changed, 28 insertions, 31 deletions
diff --git a/kernel/module-internal.h b/kernel/module-internal.h
index 6114a13419bd..24f9247b7d02 100644
--- a/kernel/module-internal.h
+++ b/kernel/module-internal.h
@@ -11,5 +11,4 @@
11 11
12extern struct key *modsign_keyring; 12extern struct key *modsign_keyring;
13 13
14extern int mod_verify_sig(const void *mod, unsigned long modlen, 14extern int mod_verify_sig(const void *mod, unsigned long *_modlen);
15 const void *sig, unsigned long siglen);
diff --git a/kernel/module.c b/kernel/module.c
index 0e2da8695f8e..6085f5ef88ea 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -2421,25 +2421,17 @@ static inline void kmemleak_load_module(const struct module *mod,
2421 2421
2422#ifdef CONFIG_MODULE_SIG 2422#ifdef CONFIG_MODULE_SIG
2423static int module_sig_check(struct load_info *info, 2423static int module_sig_check(struct load_info *info,
2424 const void *mod, unsigned long *len) 2424 const void *mod, unsigned long *_len)
2425{ 2425{
2426 int err = -ENOKEY; 2426 int err = -ENOKEY;
2427 const unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1; 2427 unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1;
2428 const void *p = mod, *end = mod + *len; 2428 unsigned long len = *_len;
2429 2429
2430 /* Poor man's memmem. */ 2430 if (len > markerlen &&
2431 while ((p = memchr(p, MODULE_SIG_STRING[0], end - p))) { 2431 memcmp(mod + len - markerlen, MODULE_SIG_STRING, markerlen) == 0) {
2432 if (p + markerlen > end) 2432 /* We truncate the module to discard the signature */
2433 break; 2433 *_len -= markerlen;
2434 2434 err = mod_verify_sig(mod, _len);
2435 if (memcmp(p, MODULE_SIG_STRING, markerlen) == 0) {
2436 const void *sig = p + markerlen;
2437 /* Truncate module up to signature. */
2438 *len = p - mod;
2439 err = mod_verify_sig(mod, *len, sig, end - sig);
2440 break;
2441 }
2442 p++;
2443 } 2435 }
2444 2436
2445 if (!err) { 2437 if (!err) {
diff --git a/kernel/module_signing.c b/kernel/module_signing.c
index 6b09f6983ac0..d492a23df99c 100644
--- a/kernel/module_signing.c
+++ b/kernel/module_signing.c
@@ -183,27 +183,33 @@ static struct key *request_asymmetric_key(const char *signer, size_t signer_len,
183/* 183/*
184 * Verify the signature on a module. 184 * Verify the signature on a module.
185 */ 185 */
186int mod_verify_sig(const void *mod, unsigned long modlen, 186int mod_verify_sig(const void *mod, unsigned long *_modlen)
187 const void *sig, unsigned long siglen)
188{ 187{
189 struct public_key_signature *pks; 188 struct public_key_signature *pks;
190 struct module_signature ms; 189 struct module_signature ms;
191 struct key *key; 190 struct key *key;
192 size_t sig_len; 191 const void *sig;
192 size_t modlen = *_modlen, sig_len;
193 int ret; 193 int ret;
194 194
195 pr_devel("==>%s(,%lu,,%lu,)\n", __func__, modlen, siglen); 195 pr_devel("==>%s(,%lu)\n", __func__, modlen);
196 196
197 if (siglen <= sizeof(ms)) 197 if (modlen <= sizeof(ms))
198 return -EBADMSG; 198 return -EBADMSG;
199 199
200 memcpy(&ms, sig + (siglen - sizeof(ms)), sizeof(ms)); 200 memcpy(&ms, mod + (modlen - sizeof(ms)), sizeof(ms));
201 siglen -= sizeof(ms); 201 modlen -= sizeof(ms);
202 202
203 sig_len = be32_to_cpu(ms.sig_len); 203 sig_len = be32_to_cpu(ms.sig_len);
204 if (sig_len >= siglen || 204 if (sig_len >= modlen)
205 siglen - sig_len != (size_t)ms.signer_len + ms.key_id_len)
206 return -EBADMSG; 205 return -EBADMSG;
206 modlen -= sig_len;
207 if ((size_t)ms.signer_len + ms.key_id_len >= modlen)
208 return -EBADMSG;
209 modlen -= (size_t)ms.signer_len + ms.key_id_len;
210
211 *_modlen = modlen;
212 sig = mod + modlen;
207 213
208 /* For the moment, only support RSA and X.509 identifiers */ 214 /* For the moment, only support RSA and X.509 identifiers */
209 if (ms.algo != PKEY_ALGO_RSA || 215 if (ms.algo != PKEY_ALGO_RSA ||
diff --git a/scripts/sign-file b/scripts/sign-file
index d37d1309531e..87ca59d36e7e 100755
--- a/scripts/sign-file
+++ b/scripts/sign-file
@@ -403,11 +403,11 @@ my $info = pack("CCCCCxxxN",
403 403
404if ($verbose) { 404if ($verbose) {
405 print "Size of unsigned module: ", length($unsigned_module), "\n"; 405 print "Size of unsigned module: ", length($unsigned_module), "\n";
406 print "Size of magic number : ", length($magic_number), "\n";
407 print "Size of signer's name : ", length($signers_name), "\n"; 406 print "Size of signer's name : ", length($signers_name), "\n";
408 print "Size of key identifier : ", length($key_identifier), "\n"; 407 print "Size of key identifier : ", length($key_identifier), "\n";
409 print "Size of signature : ", length($signature), "\n"; 408 print "Size of signature : ", length($signature), "\n";
410 print "Size of informaton : ", length($info), "\n"; 409 print "Size of informaton : ", length($info), "\n";
410 print "Size of magic number : ", length($magic_number), "\n";
411 print "Signer's name : '", $signers_name, "'\n"; 411 print "Signer's name : '", $signers_name, "'\n";
412 print "Digest : $dgst\n"; 412 print "Digest : $dgst\n";
413} 413}
@@ -416,11 +416,11 @@ open(FD, ">$dest") || die $dest;
416binmode FD; 416binmode FD;
417print FD 417print FD
418 $unsigned_module, 418 $unsigned_module,
419 $magic_number,
420 $signers_name, 419 $signers_name,
421 $key_identifier, 420 $key_identifier,
422 $signature, 421 $signature,
423 $info 422 $info,
423 $magic_number
424 ; 424 ;
425close FD || die $dest; 425close FD || die $dest;
426 426