summaryrefslogtreecommitdiffstats
path: root/src/VBox/Runtime/common/ldr/ldrELF.cpp
blob: ad380088c05f6e0e95829a9976dc60e24d2167f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/* $Id: ldrELF.cpp $ */
/** @file
 * IPRT - Binary Image Loader, Executable and Linker Format (ELF).
 */

/*
 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * The contents of this file may alternatively be used under the terms
 * of the Common Development and Distribution License Version 1.0
 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
 * in the VirtualBox distribution, in which case the provisions of the
 * CDDL are applicable instead of those of the GPL.
 *
 * You may elect to license modified versions of this file under the
 * terms and conditions of either the GPL or the CDDL or both.
 *
 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#define LOG_GROUP RTLOGGROUP_LDR
#include <iprt/ldr.h>
#include "internal/iprt.h"

#include <iprt/alloca.h>
#include <iprt/asm.h>
#include <iprt/assert.h>
#include <iprt/dbg.h>
#include <iprt/string.h>
#include <iprt/log.h>
#include <iprt/mem.h>
#include <iprt/err.h>
#include <iprt/crypto/digest.h>
#include <iprt/formats/elf32.h>
#include <iprt/formats/elf64.h>
#include <iprt/formats/elf-i386.h>
#include <iprt/formats/elf-amd64.h>
#include "internal/ldr.h"
#include "internal/dbgmod.h"



/*********************************************************************************************************************************
*   Defined Constants And Macros                                                                                                 *
*********************************************************************************************************************************/
/** Finds an ELF symbol table string. */
#define ELF_STR(pHdrs, iStr)        ((pHdrs)->Rel.pStr + (iStr))
/** Finds an ELF symbol table string. */
#define ELF_DYN_STR(pHdrs, iStr)    ((pHdrs)->Dyn.pStr + (iStr))
/** Finds an ELF section header string. */
#define ELF_SH_STR(pHdrs, iStr)     ((pHdrs)->pShStr + (iStr))


/*********************************************************************************************************************************
*   Structures and Typedefs                                                                                                      *
*********************************************************************************************************************************/
/** Magic string for RTLDRLNXMODSIG::achMagic   */
#define RTLDRLNXMODSIG_MAGIC "~Module signature appended~\n"
AssertCompile(sizeof(RTLDRLNXMODSIG_MAGIC) == 29);

/**
 * Linux kernel module signature footer - found at the end of the file.
 */
typedef struct RTLDRLNXMODSIG
{
    /** Zero. */
    uint8_t         bAlgo;
    /** Zero. */
    uint8_t         bHash;
    /** Signature type (RTLDRLNXMODSIG_TYPE_PKCS7). */
    uint8_t         bType;
    /** Zero. */
    uint8_t         cbSignerName;
    /** Zero. */
    uint8_t         cbKeyId;
    /** Zero padding. */
    uint8_t         abReserved[3];
    /** The length of the signature preceeding this footer structure. */
    uint32_t        cbSignature;
    /** Magic value identifying this structure.   */
    char            achMagic[sizeof(RTLDRLNXMODSIG_MAGIC) - 1];
} RTLDRLNXMODSIG;
typedef RTLDRLNXMODSIG *PRTLDRLNXMODSIG;
typedef RTLDRLNXMODSIG const *PCRTLDRLNXMODSIG;
/** Signature type.   */
#define RTLDRLNXMODSIG_TYPE_PKCS7   2



/*********************************************************************************************************************************
*   Internal Functions                                                                                                           *
*********************************************************************************************************************************/
static int rtLdrELFLnxKModQueryPropIsSigned(PRTLDRREADER pReader, bool *pfRet);
static int rtLdrELFLnxKModQueryPropPkcs7SignedData(PRTLDRREADER pReader, void *pvBuf, size_t cbBuf, size_t *pcbRet);
static DECLCALLBACK(int) rtldrELFLnxKModHashImage(PRTLDRMODINTERNAL pMod, RTDIGESTTYPE enmDigest, uint8_t *pabHash, size_t cbHash);
#ifdef LOG_ENABLED
static const char *rtldrElfGetShdrType(uint32_t iType);
static const char *rtldrElfGetPhdrType(uint32_t iType);
#endif


/* Select ELF mode and include the template. */
#define ELF_MODE            32
#define Elf_Reloc           Elf_Rel
#include "ldrELFRelocatable.cpp.h"
#undef ELF_MODE
#undef Elf_Reloc


#define ELF_MODE            64
#define Elf_Reloc           Elf_Rela
#include "ldrELFRelocatable.cpp.h"
#undef ELF_MODE
#undef Elf_Reloc


#ifdef LOG_ENABLED

/**
 * Gets the section type.
 *
 * @returns Pointer to read only string.
 * @param   iType       The section type index.
 */
static const char *rtldrElfGetShdrType(uint32_t iType)
{
    switch (iType)
    {
        RT_CASE_RET_STR(SHT_NULL);
        RT_CASE_RET_STR(SHT_PROGBITS);
        RT_CASE_RET_STR(SHT_SYMTAB);
        RT_CASE_RET_STR(SHT_STRTAB);
        RT_CASE_RET_STR(SHT_RELA);
        RT_CASE_RET_STR(SHT_HASH);
        RT_CASE_RET_STR(SHT_DYNAMIC);
        RT_CASE_RET_STR(SHT_NOTE);
        RT_CASE_RET_STR(SHT_NOBITS);
        RT_CASE_RET_STR(SHT_REL);
        RT_CASE_RET_STR(SHT_SHLIB);
        RT_CASE_RET_STR(SHT_DYNSYM);
        default:
            return "";
    }
}

/**
 * Gets the program header type.
 *
 * @returns Pointer to read only string.
 * @param   iType       The section type index.
 */
static const char *rtldrElfGetPhdrType(uint32_t iType)
{
    switch (iType)
    {
        RT_CASE_RET_STR(PT_NULL);
        RT_CASE_RET_STR(PT_LOAD);
        RT_CASE_RET_STR(PT_DYNAMIC);
        RT_CASE_RET_STR(PT_INTERP);
        RT_CASE_RET_STR(PT_NOTE);
        RT_CASE_RET_STR(PT_SHLIB);
        RT_CASE_RET_STR(PT_PHDR);
        RT_CASE_RET_STR(PT_TLS);
        RT_CASE_RET_STR(PT_GNU_EH_FRAME);
        RT_CASE_RET_STR(PT_GNU_STACK);
        RT_CASE_RET_STR(PT_GNU_RELRO);
        RT_CASE_RET_STR(PT_GNU_PROPERTY);
        default:
            return "";
    }
}

#endif /* LOG_ENABLED*/

/**
 * Reads in what migt be a linux kernel module signature footer.
 */
static int rtLdrELFLnxKModReadFooter(PRTLDRREADER pReader, PRTLDRLNXMODSIG pSigFooter, uint64_t *pcbFile)
{
    /*
     * Look for the linux module signature at the end of the file.
     * This should be safe to read w/o any size checking as it is smaller than the elf header.
     */
    uint64_t cbFile = pReader->pfnSize(pReader);
    *pcbFile = cbFile;

    AssertCompile(sizeof(*pSigFooter) <= sizeof(Elf32_Ehdr));
    return pReader->pfnRead(pReader, pSigFooter, sizeof(*pSigFooter), cbFile - sizeof(*pSigFooter));
}


/**
 * Check that a linux kernel module signature footer is valid.
 */
static bool rtLdrELFLnxKModIsFooterValid(PCRTLDRLNXMODSIG pSigFooter, uint64_t cbFile)
{
    if (memcmp(pSigFooter->achMagic, RTLDRLNXMODSIG_MAGIC, sizeof(pSigFooter->achMagic)) == 0)
    {
        uint32_t const cbSignature = RT_N2H_U32(pSigFooter->cbSignature);
        if (cbSignature > 32 && cbSignature + sizeof(*pSigFooter) < cbFile)
            return pSigFooter->bAlgo        == 0
                && pSigFooter->bHash        == 0
                && pSigFooter->cbSignerName == 0
                && pSigFooter->cbKeyId      == 0;
    }
    return false;
}


/**
 * Handles the linux kernel module signature part of RTLDRPROP_IS_SIGNED
 * queries.
 */
static int rtLdrELFLnxKModQueryPropIsSigned(PRTLDRREADER pReader, bool *pfRet)
{
    *pfRet = false;
    AssertReturn(pReader, VERR_INVALID_STATE);

    uint64_t       cbFile;
    RTLDRLNXMODSIG SigFooter;
    int rc = rtLdrELFLnxKModReadFooter(pReader, &SigFooter, &cbFile);
    if (RT_SUCCESS(rc))
        *pfRet = rtLdrELFLnxKModIsFooterValid(&SigFooter, cbFile);
    return rc;
}


/**
 * Handles the linux kernel module signature part of RTLDRPROP_IS_SIGNED
 * queries.
 */
static int rtLdrELFLnxKModQueryPropPkcs7SignedData(PRTLDRREADER pReader, void *pvBuf, size_t cbBuf, size_t *pcbRet)
{
    AssertReturn(pReader, VERR_INVALID_STATE);

    uint64_t       cbFile;
    RTLDRLNXMODSIG SigFooter;
    int rc = rtLdrELFLnxKModReadFooter(pReader, &SigFooter, &cbFile);
    if (RT_SUCCESS(rc))
    {
        if (   rtLdrELFLnxKModIsFooterValid(&SigFooter, cbFile)
            && SigFooter.bType == RTLDRLNXMODSIG_TYPE_PKCS7)
        {
            uint32_t const cbSignature = RT_N2H_U32(SigFooter.cbSignature);
            *pcbRet = cbSignature;
            if (cbSignature <= cbBuf)
                rc = pReader->pfnRead(pReader, pvBuf, cbSignature, cbFile - sizeof(SigFooter) - cbSignature);
            else
                rc = VERR_BUFFER_OVERFLOW;
        }
        else
            rc = VERR_NOT_FOUND;
    }
    return rc;
}


/**
 * @interface_method_impl{RTLDROPS,pfnHashImage,
 * Handles the linux kernel module signatures.}
 */
static DECLCALLBACK(int) rtldrELFLnxKModHashImage(PRTLDRMODINTERNAL pMod, RTDIGESTTYPE enmDigest, uint8_t *pabHash, size_t cbHash)
{
    PRTLDRREADER pReader = pMod->pReader;
    AssertReturn(pReader, VERR_INVALID_STATE);

    /*
     * Get the file size and subtract any linux kernel module signature from it
     * since it's not part of the hash.
     */
    uint64_t       cbFile;
    RTLDRLNXMODSIG SigFooter;
    int rc = rtLdrELFLnxKModReadFooter(pReader, &SigFooter, &cbFile);
    if (RT_SUCCESS(rc))
    {
        if (rtLdrELFLnxKModIsFooterValid(&SigFooter, cbFile))
            cbFile -= sizeof(SigFooter) + RT_N2H_U32(SigFooter.cbSignature);

        /*
         * Now hash the file.
         */
        RTCRDIGEST hDigest;
        rc = RTCrDigestCreateByType(&hDigest, enmDigest);
        if (RT_SUCCESS(rc))
        {
            uint32_t cbBuf = _64K;
            void    *pvBuf = RTMemTmpAlloc(_64K);
            void    *pvBufFree = pvBuf;
            if (!pvBuf)
            {
                cbBuf = _4K;
                pvBuf = alloca(_4K);
            }

            for (uint64_t offFile = 0; offFile < cbFile; )
            {
                uint64_t cbLeft = cbFile - offFile;
                uint32_t cbToRead = cbLeft >= cbBuf ? cbBuf : (uint32_t)cbLeft;
                rc = pReader->pfnRead(pReader, pvBuf, cbToRead, offFile);
                AssertRCBreak(rc);

                rc = RTCrDigestUpdate(hDigest, pvBuf, cbToRead);
                offFile += cbToRead;
                AssertRCBreak(rc);
            }

            RTMemTmpFree(pvBufFree);

            if (RT_SUCCESS(rc))
                rc = RTCrDigestFinal(hDigest, pabHash, cbHash);
            RTCrDigestRelease(hDigest);
        }
    }
    return rc;
}


/**
 * Open an ELF image.
 *
 * @returns iprt status code.
 * @param   pReader     The loader reader instance which will provide the raw image bits.
 * @param   fFlags      Reserved, MBZ.
 * @param   enmArch     Architecture specifier.
 * @param   phLdrMod    Where to store the handle.
 * @param   pErrInfo    Where to return extended error information. Optional.
 */
DECLHIDDEN(int) rtldrELFOpen(PRTLDRREADER pReader, uint32_t fFlags, RTLDRARCH enmArch, PRTLDRMOD phLdrMod, PRTERRINFO pErrInfo)
{
    const char *pszLogName = pReader->pfnLogName(pReader); NOREF(pszLogName);

    /*
     * Read the ident to decide if this is 32-bit or 64-bit
     * and worth dealing with.
     */
    uint8_t e_ident[EI_NIDENT];
    int rc = pReader->pfnRead(pReader, &e_ident, sizeof(e_ident), 0);
    if (RT_FAILURE(rc))
        return rc;

    if (    e_ident[EI_MAG0] != ELFMAG0
        ||  e_ident[EI_MAG1] != ELFMAG1
        ||  e_ident[EI_MAG2] != ELFMAG2
        ||  e_ident[EI_MAG3] != ELFMAG3
        ||  (   e_ident[EI_CLASS] != ELFCLASS32
             && e_ident[EI_CLASS] != ELFCLASS64)
       )
        return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT,
                                   "%s: Unsupported/invalid ident %.*Rhxs", pszLogName, sizeof(e_ident), e_ident);

    if (e_ident[EI_DATA] != ELFDATA2LSB)
        return RTERRINFO_LOG_SET_F(pErrInfo, VERR_LDRELF_ODD_ENDIAN,
                                   "%s: ELF endian %x is unsupported", pszLogName, e_ident[EI_DATA]);

    if (e_ident[EI_CLASS] == ELFCLASS32)
        rc = rtldrELF32Open(pReader, fFlags, enmArch, phLdrMod, pErrInfo);
    else
        rc = rtldrELF64Open(pReader, fFlags, enmArch, phLdrMod, pErrInfo);
    return rc;
}