]>
git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - crypto/asymmetric_keys/verify_pefile.c
1 /* Parse a signed PE binary
3 * Copyright (C) 2014 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
12 #define pr_fmt(fmt) "PEFILE: "fmt
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/err.h>
18 #include <crypto/pkcs7.h>
19 #include <crypto/hash.h>
20 #include "verify_pefile.h"
25 static int pefile_parse_binary(const void *pebuf
, unsigned int pelen
,
26 struct pefile_context
*ctx
)
28 const struct mz_hdr
*mz
= pebuf
;
29 const struct pe_hdr
*pe
;
30 const struct pe32_opt_hdr
*pe32
;
31 const struct pe32plus_opt_hdr
*pe64
;
32 const struct data_directory
*ddir
;
33 const struct data_dirent
*dde
;
34 const struct section_header
*secs
, *sec
;
35 size_t cursor
, datalen
= pelen
;
39 #define chkaddr(base, x, s) \
41 if ((x) < base || (s) >= datalen || (x) > datalen - (s)) \
45 chkaddr(0, 0, sizeof(*mz
));
46 if (mz
->magic
!= MZ_MAGIC
)
50 chkaddr(cursor
, mz
->peaddr
, sizeof(*pe
));
51 pe
= pebuf
+ mz
->peaddr
;
52 if (pe
->magic
!= PE_MAGIC
)
54 cursor
= mz
->peaddr
+ sizeof(*pe
);
56 chkaddr(0, cursor
, sizeof(pe32
->magic
));
57 pe32
= pebuf
+ cursor
;
58 pe64
= pebuf
+ cursor
;
60 switch (pe32
->magic
) {
61 case PE_OPT_MAGIC_PE32
:
62 chkaddr(0, cursor
, sizeof(*pe32
));
63 ctx
->image_checksum_offset
=
64 (unsigned long)&pe32
->csum
- (unsigned long)pebuf
;
65 ctx
->header_size
= pe32
->header_size
;
66 cursor
+= sizeof(*pe32
);
67 ctx
->n_data_dirents
= pe32
->data_dirs
;
70 case PE_OPT_MAGIC_PE32PLUS
:
71 chkaddr(0, cursor
, sizeof(*pe64
));
72 ctx
->image_checksum_offset
=
73 (unsigned long)&pe64
->csum
- (unsigned long)pebuf
;
74 ctx
->header_size
= pe64
->header_size
;
75 cursor
+= sizeof(*pe64
);
76 ctx
->n_data_dirents
= pe64
->data_dirs
;
80 pr_debug("Unknown PEOPT magic = %04hx\n", pe32
->magic
);
84 pr_debug("checksum @ %x\n", ctx
->image_checksum_offset
);
85 pr_debug("header size = %x\n", ctx
->header_size
);
87 if (cursor
>= ctx
->header_size
|| ctx
->header_size
>= datalen
)
90 if (ctx
->n_data_dirents
> (ctx
->header_size
- cursor
) / sizeof(*dde
))
93 ddir
= pebuf
+ cursor
;
94 cursor
+= sizeof(*dde
) * ctx
->n_data_dirents
;
96 ctx
->cert_dirent_offset
=
97 (unsigned long)&ddir
->certs
- (unsigned long)pebuf
;
98 ctx
->certs_size
= ddir
->certs
.size
;
100 if (!ddir
->certs
.virtual_address
|| !ddir
->certs
.size
) {
101 pr_debug("Unsigned PE binary\n");
102 return -EKEYREJECTED
;
105 chkaddr(ctx
->header_size
, ddir
->certs
.virtual_address
,
107 ctx
->sig_offset
= ddir
->certs
.virtual_address
;
108 ctx
->sig_len
= ddir
->certs
.size
;
109 pr_debug("cert = %x @%x [%*ph]\n",
110 ctx
->sig_len
, ctx
->sig_offset
,
111 ctx
->sig_len
, pebuf
+ ctx
->sig_offset
);
113 ctx
->n_sections
= pe
->sections
;
114 if (ctx
->n_sections
> (ctx
->header_size
- cursor
) / sizeof(*sec
))
116 ctx
->secs
= secs
= pebuf
+ cursor
;
122 * verify_pefile_signature - Verify the signature on a PE binary image
123 * @pebuf: Buffer containing the PE binary image
124 * @pelen: Length of the binary image
125 * @trust_keyring: Signing certificates to use as starting points
126 * @_trusted: Set to true if trustworth, false otherwise
128 * Validate that the certificate chain inside the PKCS#7 message inside the PE
129 * binary image intersects keys we already know and trust.
131 * Returns, in order of descending priority:
133 * (*) -ELIBBAD if the image cannot be parsed, or:
135 * (*) -EKEYREJECTED if a signature failed to match for which we have a valid
138 * (*) 0 if at least one signature chain intersects with the keys in the trust
141 * (*) -ENOPKG if a suitable crypto module couldn't be found for a check on a
144 * (*) -ENOKEY if we couldn't find a match for any of the signature chains in
147 * May also return -ENOMEM.
149 int verify_pefile_signature(const void *pebuf
, unsigned pelen
,
150 struct key
*trusted_keyring
, bool *_trusted
)
152 struct pefile_context ctx
;
157 memset(&ctx
, 0, sizeof(ctx
));
158 ret
= pefile_parse_binary(pebuf
, pelen
, &ctx
);
162 return -ENOANO
; // Not yet complete