1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24 */
25
26 #include <ctype.h>
27 #include <unistd.h>
28 #include <sys/types.h>
29 #include <fcntl.h>
30 #include <stdio.h>
31 #include <libelf.h>
32 #include <gelf.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <sha1.h>
36 #include <sys/elf_SPARC.h>
37 #include <fips/fips_checksum.h>
38
39
40 #define FAIL_EXIT \
41 (void) fprintf(stderr, "failure at line %d\n", __LINE__); \
42 return (-1)
43
44 static const char fips_section_name[] = ".SUNW_fips";
45
46 static int
add_fips_section(int fd)47 add_fips_section(int fd)
48 {
49 Elf64_Ehdr *ehdrp;
50 Elf64_Shdr *section;
51 Elf *elf;
52 Elf_Scn *scn, *shstrtab_scn, *fips_scn = NULL;
53 Elf_Data *shstrtab_data;
54 Elf_Data *sdata;
55 unsigned int cnt, old_size, new_size;
56 char *sname, *newbuf;
57
58 /* Obtain the ELF descriptor */
59 (void) elf_version(EV_CURRENT);
60 if ((elf = elf_begin(fd, ELF_C_RDWR, NULL)) == NULL) {
61 FAIL_EXIT;
62 }
63
64 if ((ehdrp = elf64_getehdr(elf)) == NULL) {
65 FAIL_EXIT;
66 } else if ((shstrtab_scn = elf_getscn(elf, ehdrp->e_shstrndx)) ==
67 NULL) {
68 FAIL_EXIT;
69 } else if ((shstrtab_data = elf_getdata(shstrtab_scn, NULL)) == NULL) {
70 FAIL_EXIT;
71 }
72
73 /* Traverse input file to see if the fips section already exists */
74 for (cnt = 1, scn = NULL; scn = elf_nextscn(elf, scn); cnt++) {
75 if ((section = elf64_getshdr(scn)) == NULL) {
76 FAIL_EXIT;
77 }
78 sname = (char *)shstrtab_data->d_buf + section->sh_name;
79 if (strcmp(sname, fips_section_name) == 0) {
80 /*
81 * If the fips section already exists, make sure that
82 * the section is large enough.
83 */
84 fips_scn = scn;
85 if ((sdata = elf_getdata(scn, NULL)) == NULL) {
86 FAIL_EXIT;
87 }
88 if (sdata->d_size < SHA1_DIGEST_LENGTH) {
89 newbuf = malloc(SHA1_DIGEST_LENGTH);
90 sdata->d_size = SHA1_DIGEST_LENGTH;
91 sdata->d_buf = newbuf;
92 }
93 (void) elf_flagdata(sdata, ELF_C_SET, ELF_F_DIRTY);
94 (void) elf_flagscn(scn, ELF_C_SET, ELF_F_DIRTY);
95 (void) elf_flagelf(elf, ELF_C_SET, ELF_F_DIRTY);
96 }
97 }
98
99 /* If the fips section does not exist, allocate the section. */
100 if (fips_scn == NULL) {
101 Elf64_Shdr *shdr;
102
103 /* add the section name at the end of the .shstrtab section */
104 old_size = shstrtab_data->d_size;
105 new_size = old_size + strlen(fips_section_name) + 1;
106 if ((newbuf = malloc(new_size)) == NULL) {
107 FAIL_EXIT;
108 }
109
110 (void) memcpy(newbuf, shstrtab_data->d_buf, old_size);
111 (void) strlcpy(newbuf + old_size, fips_section_name,
112 new_size - old_size);
113 shstrtab_data->d_buf = newbuf;
114 shstrtab_data->d_size = new_size;
115 shstrtab_data->d_align = 1;
116 if ((fips_scn = elf_newscn(elf)) == 0) {
117 FAIL_EXIT;
118 }
119
120 /* Initialize the fips section */
121 if ((shdr = elf64_getshdr(fips_scn)) == NULL) {
122 FAIL_EXIT;
123 }
124 /*
125 * sh_name is the starting position of the name
126 * within the shstrtab_data->d_buf buffer
127 */
128 shdr->sh_name = old_size;
129 shdr->sh_type = SHT_SUNW_SIGNATURE;
130 shdr->sh_flags = SHF_EXCLUDE;
131 shdr->sh_addr = 0;
132 shdr->sh_link = 0;
133 shdr->sh_info = 0;
134 shdr->sh_size = 0;
135 shdr->sh_offset = 0;
136 shdr->sh_addralign = 1;
137
138 if ((sdata = elf_newdata(fips_scn)) == NULL) {
139 FAIL_EXIT;
140 }
141 if (sdata->d_size < SHA1_DIGEST_LENGTH) {
142 newbuf = malloc(SHA1_DIGEST_LENGTH);
143 sdata->d_size = SHA1_DIGEST_LENGTH;
144 sdata->d_buf = newbuf;
145 }
146 (void) elf_flagdata(sdata, ELF_C_SET, ELF_F_DIRTY);
147 (void) elf_flagscn(fips_scn, ELF_C_SET, ELF_F_DIRTY);
148 (void) elf_flagelf(elf, ELF_C_SET, ELF_F_DIRTY);
149 }
150 (void) elf_update(elf, ELF_C_WRITE);
151 (void) elf_end(elf);
152 (void) close(fd);
153
154 return (0);
155 }
156
157 int
main(int argc,char ** argv)158 main(int argc, char **argv)
159 {
160 Elf64_Ehdr ehdr;
161 Elf64_Ehdr *ehdrp;
162 Elf64_Shdr *section;
163 Elf *elf;
164 Elf_Scn *scn, *shstrtab_scn;
165 Elf_Data *shstrtab_data, *sdata;
166 int fd;
167 unsigned int size, i, cnt;
168 char sha1buf[SHA1_DIGEST_LENGTH];
169 char *sname, *newbuf;
170
171 if (argc != 2) {
172 (void) fprintf(stderr, "Usage: %s [filename]\n", argv[0]);
173 return (-1);
174 }
175
176 /* Open the driver file */
177 if ((fd = open(argv[1], O_RDWR)) == -1) {
178 goto errorexit;
179 }
180
181 /* Read the ELF header */
182 size = sizeof (ehdr);
183 if (fips_read_file(fd, (char *)(&ehdr), size, 0) < 0) {
184 goto errorexit;
185 }
186
187 /* check if it is an ELF file */
188 for (i = 0; i < SELFMAG; i++) {
189 if (ehdr.e_ident[i] != ELFMAG[i]) {
190 (void) fprintf(stderr, "%s not an elf file\n", argv[1]);
191 goto errorexit;
192 }
193 }
194
195 if (add_fips_section(fd) < 0) { /* closes fd on success */
196 goto errorexit;
197 }
198
199 if ((fd = open(argv[1], O_RDWR)) == -1) {
200 FAIL_EXIT;
201 }
202 if (fips_read_file(fd, (char *)(&ehdr), size, 0) < 0) {
203 goto errorexit;
204 }
205
206 /* calculate the file checksum */
207 if (fips_calc_checksum(fd, &ehdr, sha1buf) < 0) {
208 goto errorexit;
209 }
210
211 (void) elf_version(EV_CURRENT);
212 if ((elf = elf_begin(fd, ELF_C_RDWR, NULL)) == NULL) {
213 goto errorexit;
214 }
215
216 if ((ehdrp = elf64_getehdr(elf)) == NULL) {
217 goto errorexit;
218 } else if ((shstrtab_scn = elf_getscn(elf, ehdrp->e_shstrndx)) ==
219 NULL) {
220 goto errorexit;
221 } else if ((shstrtab_data = elf_getdata(shstrtab_scn, NULL)) == NULL) {
222 goto errorexit;
223 }
224
225 /* Add the checksum to the fips section */
226 for (cnt = 1, scn = NULL; scn = elf_nextscn(elf, scn); cnt++) {
227 if ((section = elf64_getshdr(scn)) == NULL) {
228 goto errorexit;
229 }
230
231 sname = (char *)shstrtab_data->d_buf + section->sh_name;
232 if (strcmp(sname, fips_section_name) == 0) {
233 if ((sdata = elf_getdata(scn, NULL)) == NULL) {
234 goto errorexit;
235 }
236
237 newbuf = sdata->d_buf;
238 (void) memcpy(newbuf, sha1buf, SHA1_DIGEST_LENGTH);
239 (void) elf_flagdata(sdata, ELF_C_SET, ELF_F_DIRTY);
240 (void) elf_flagscn(scn, ELF_C_SET, ELF_F_DIRTY);
241 (void) elf_flagelf(elf, ELF_C_SET, ELF_F_DIRTY);
242 }
243 }
244 (void) elf_update(elf, ELF_C_WRITE);
245 (void) elf_end(elf);
246 (void) close(fd);
247
248 return (0);
249
250
251 errorexit:
252
253 (void) close(fd);
254
255 FAIL_EXIT;
256 }
257