1*839529caSEd Maste /*-
2*839529caSEd Maste * Copyright (c) 2016 Kai Wang
3*839529caSEd Maste * All rights reserved.
4*839529caSEd Maste *
5*839529caSEd Maste * Redistribution and use in source and binary forms, with or without
6*839529caSEd Maste * modification, are permitted provided that the following conditions
7*839529caSEd Maste * are met:
8*839529caSEd Maste * 1. Redistributions of source code must retain the above copyright
9*839529caSEd Maste * notice, this list of conditions and the following disclaimer.
10*839529caSEd Maste * 2. Redistributions in binary form must reproduce the above copyright
11*839529caSEd Maste * notice, this list of conditions and the following disclaimer in the
12*839529caSEd Maste * documentation and/or other materials provided with the distribution.
13*839529caSEd Maste *
14*839529caSEd Maste * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*839529caSEd Maste * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*839529caSEd Maste * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*839529caSEd Maste * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*839529caSEd Maste * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*839529caSEd Maste * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*839529caSEd Maste * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*839529caSEd Maste * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*839529caSEd Maste * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*839529caSEd Maste * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*839529caSEd Maste * SUCH DAMAGE.
25*839529caSEd Maste */
26*839529caSEd Maste
27*839529caSEd Maste #include <assert.h>
28*839529caSEd Maste #include <errno.h>
29*839529caSEd Maste
30*839529caSEd Maste #include "_libpe.h"
31*839529caSEd Maste
32*839529caSEd Maste ELFTC_VCSID("$Id: pe_rich.c 3312 2016-01-10 09:23:51Z kaiwang27 $");
33*839529caSEd Maste
34*839529caSEd Maste PE_RichHdr *
pe_rich_header(PE * pe)35*839529caSEd Maste pe_rich_header(PE *pe)
36*839529caSEd Maste {
37*839529caSEd Maste
38*839529caSEd Maste if (pe == NULL) {
39*839529caSEd Maste errno = EINVAL;
40*839529caSEd Maste return (NULL);
41*839529caSEd Maste }
42*839529caSEd Maste
43*839529caSEd Maste if (pe->pe_rh == NULL && pe->pe_stub_ex > 0 &&
44*839529caSEd Maste (pe->pe_flags & LIBPE_F_LOAD_DOS_STUB) == 0) {
45*839529caSEd Maste assert((pe->pe_flags & LIBPE_F_SPECIAL_FILE) == 0);
46*839529caSEd Maste (void) libpe_read_msdos_stub(pe);
47*839529caSEd Maste }
48*839529caSEd Maste
49*839529caSEd Maste if (pe->pe_rh == NULL) {
50*839529caSEd Maste errno = ENOENT;
51*839529caSEd Maste return (NULL);
52*839529caSEd Maste }
53*839529caSEd Maste
54*839529caSEd Maste return (pe->pe_rh);
55*839529caSEd Maste }
56*839529caSEd Maste
57*839529caSEd Maste static uint32_t
rol32(uint32_t n,int c)58*839529caSEd Maste rol32(uint32_t n, int c)
59*839529caSEd Maste {
60*839529caSEd Maste
61*839529caSEd Maste c &= 0x1f;
62*839529caSEd Maste
63*839529caSEd Maste return ((n << c) | (n >> (0x20 - c)));
64*839529caSEd Maste }
65*839529caSEd Maste
66*839529caSEd Maste int
pe_rich_header_validate(PE * pe)67*839529caSEd Maste pe_rich_header_validate(PE *pe)
68*839529caSEd Maste {
69*839529caSEd Maste PE_RichHdr *rh;
70*839529caSEd Maste uint32_t cksum;
71*839529caSEd Maste char *p;
72*839529caSEd Maste int i, off;
73*839529caSEd Maste
74*839529caSEd Maste if (pe_rich_header(pe) == NULL)
75*839529caSEd Maste return (-1);
76*839529caSEd Maste
77*839529caSEd Maste assert(pe->pe_rh_start != NULL);
78*839529caSEd Maste
79*839529caSEd Maste /*
80*839529caSEd Maste * Initial value of the checksum is the offset to the begin of
81*839529caSEd Maste * the Rich header.
82*839529caSEd Maste */
83*839529caSEd Maste cksum = pe->pe_rh_start - pe->pe_stub;
84*839529caSEd Maste
85*839529caSEd Maste /*
86*839529caSEd Maste * Add the bytes before the Rich header to the checksum, rotated
87*839529caSEd Maste * left by the offset.
88*839529caSEd Maste */
89*839529caSEd Maste for (p = pe->pe_stub; p < pe->pe_rh_start; p++) {
90*839529caSEd Maste /* Skip dh_lfanew. */
91*839529caSEd Maste off = p - pe->pe_stub;
92*839529caSEd Maste if (off >= 0x3c && off < 0x40)
93*839529caSEd Maste continue;
94*839529caSEd Maste cksum += rol32((unsigned char) *p, off);
95*839529caSEd Maste }
96*839529caSEd Maste
97*839529caSEd Maste /* Add each compid rotated left by its count to the checksum. */
98*839529caSEd Maste rh = pe->pe_rh;
99*839529caSEd Maste for (i = 0; (uint32_t) i < rh->rh_total; i++)
100*839529caSEd Maste cksum += rol32(rh->rh_compid[i], rh->rh_cnt[i]);
101*839529caSEd Maste
102*839529caSEd Maste /* Validate the checksum with the XOR mask stored after "Rich". */
103*839529caSEd Maste if (cksum == rh->rh_xor)
104*839529caSEd Maste return (1);
105*839529caSEd Maste
106*839529caSEd Maste return (0);
107*839529caSEd Maste }
108