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
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include "fd-util.h"
#include "fileio.h"
#include "env-file.h"
#include "kernel-image.h"
#include "os-util.h"
#include "parse-util.h"
#include "pe-binary.h"
#include "string-table.h"
#define PE_SECTION_READ_MAX (16U*1024U)
static const char * const kernel_image_type_table[_KERNEL_IMAGE_TYPE_MAX] = {
[KERNEL_IMAGE_TYPE_UNKNOWN] = "unknown",
[KERNEL_IMAGE_TYPE_UKI] = "uki",
[KERNEL_IMAGE_TYPE_PE] = "pe",
};
DEFINE_STRING_TABLE_LOOKUP_TO_STRING(kernel_image_type, KernelImageType);
static int uki_read_pretty_name(
int fd,
const PeHeader *pe_header,
const IMAGE_SECTION_HEADER *sections,
char **ret) {
_cleanup_free_ char *pname = NULL, *name = NULL;
_cleanup_fclose_ FILE *f = NULL;
_cleanup_free_ void *osrel = NULL;
size_t osrel_size;
int r;
assert(fd >= 0);
assert(pe_header);
assert(sections || le16toh(pe_header->pe.NumberOfSections) == 0);
assert(ret);
r = pe_read_section_data(
fd,
pe_header,
sections,
".osrel",
/* max_size=*/ PE_SECTION_READ_MAX,
&osrel,
&osrel_size);
if (r == -ENXIO) { /* Section not found */
*ret = NULL;
return 0;
}
f = fmemopen(osrel, osrel_size, "r");
if (!f)
return log_error_errno(errno, "Failed to open embedded os-release file: %m");
r = parse_env_file(
f, NULL,
"PRETTY_NAME", &pname,
"NAME", &name);
if (r < 0)
return log_error_errno(r, "Failed to parse embedded os-release file: %m");
/* follow the same logic as os_release_pretty_name() */
if (!isempty(pname))
*ret = TAKE_PTR(pname);
else if (!isempty(name))
*ret = TAKE_PTR(name);
else {
char *n = strdup("Linux");
if (!n)
return log_oom();
*ret = n;
}
return 0;
}
static int inspect_uki(
int fd,
const PeHeader *pe_header,
const IMAGE_SECTION_HEADER *sections,
char **ret_cmdline,
char **ret_uname,
char **ret_pretty_name) {
_cleanup_free_ char *cmdline = NULL, *uname = NULL, *pname = NULL;
int r;
assert(fd >= 0);
assert(sections || le16toh(pe_header->pe.NumberOfSections) == 0);
if (ret_cmdline) {
r = pe_read_section_data(fd, pe_header, sections, ".cmdline", PE_SECTION_READ_MAX, (void**) &cmdline, NULL);
if (r < 0 && r != -ENXIO) /* If the section doesn't exist, that's fine */
return r;
}
if (ret_uname) {
r = pe_read_section_data(fd, pe_header, sections, ".uname", PE_SECTION_READ_MAX, (void**) &uname, NULL);
if (r < 0 && r != -ENXIO) /* If the section doesn't exist, that's fine */
return r;
}
if (ret_pretty_name) {
r = uki_read_pretty_name(fd, pe_header, sections, &pname);
if (r < 0)
return r;
}
if (ret_cmdline)
*ret_cmdline = TAKE_PTR(cmdline);
if (ret_uname)
*ret_uname = TAKE_PTR(uname);
if (ret_pretty_name)
*ret_pretty_name = TAKE_PTR(pname);
return 0;
}
int inspect_kernel(
int dir_fd,
const char *filename,
KernelImageType *ret_type,
char **ret_cmdline,
char **ret_uname,
char **ret_pretty_name) {
_cleanup_free_ IMAGE_SECTION_HEADER *sections = NULL;
_cleanup_free_ IMAGE_DOS_HEADER *dos_header = NULL;
KernelImageType t = KERNEL_IMAGE_TYPE_UNKNOWN;
_cleanup_free_ PeHeader *pe_header = NULL;
_cleanup_close_ int fd = -EBADF;
int r;
assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
assert(filename);
fd = openat(dir_fd, filename, O_RDONLY|O_CLOEXEC);
if (fd < 0)
return log_error_errno(errno, "Failed to open kernel image file '%s': %m", filename);
r = pe_load_headers(fd, &dos_header, &pe_header);
if (r == -EBADMSG) /* not a valid PE file */
goto not_uki;
if (r < 0)
return log_error_errno(r, "Failed to parse kernel image file '%s': %m", filename);
r = pe_load_sections(fd, dos_header, pe_header, §ions);
if (r == -EBADMSG) /* not a valid PE file */
goto not_uki;
if (r < 0)
return log_error_errno(r, "Failed to load PE sections from kernel image file '%s': %m", filename);
if (pe_is_uki(pe_header, sections)) {
r = inspect_uki(fd, pe_header, sections, ret_cmdline, ret_uname, ret_pretty_name);
if (r < 0)
return r;
t = KERNEL_IMAGE_TYPE_UKI;
goto done;
} else
t = KERNEL_IMAGE_TYPE_PE;
not_uki:
if (ret_cmdline)
*ret_cmdline = NULL;
if (ret_uname)
*ret_uname = NULL;
if (ret_pretty_name)
*ret_pretty_name = NULL;
done:
if (ret_type)
*ret_type = t;
return 0;
}
|