Debian Codegraph @

staprun/modverify.c

raw
1 /*
2 This program verifies the given file using the given signature, the named
3 certificate and public key in the given certificate database.
4
5 Copyright (C) 2009-2013, 2018 Red Hat Inc.
6
7 This file is part of systemtap, and is free software. You can
8 redistribute it and/or modify it under the terms of the GNU General Public
9 License as published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "../config.h"
22 #include "staprun.h"
23
24 #include <stdio.h>
25
26 #include <nspr.h>
27 #include <nss.h>
28 #include <pk11pub.h>
29 #include <cryptohi.h>
30 #include <cert.h>
31 #include <certt.h>
32
33 #include <sys/stat.h>
34 #include <sys/types.h>
35 #include <errno.h>
36
37 #include "../nsscommon.h"
38 #include "modverify.h"
39
40 // Called by some of the functions in nsscommon.cxx.
41 void
42 nsscommon_error (const char *msg, int logit __attribute ((unused)))
43 {
44 fprintf (stderr, "%s\n", msg);
45 fflush (stderr);
46 }
47
48 /* Function: int check_cert_db_permissions (const char *cert_db_path);
49 *
50 * Check that the given certificate directory and its contents have
51 * the correct permissions.
52 *
53 * Returns 0 if there is an error, 1 otherwise.
54 */
55 static int
56 check_db_file_permissions (const char *cert_db_file) {
57 struct stat info;
58 int rc;
59
60 rc = stat (cert_db_file, & info);
61 if (rc)
62 {
63 fprintf (stderr, "Could not obtain information on certificate database file %s.\n",
64 cert_db_file);
65 perror ("");
66 return 0;
67 }
68
69 rc = 1; /* ok */
70
71 /* The owner of the file must be root. */
72 if (info.st_uid != 0)
73 {
74 fprintf (stderr, "Certificate database file %s must be owned by root.\n",
75 cert_db_file);
76 rc = 0;
77 }
78
79 /* Check the access permissions of the file. */
80 if ((info.st_mode & S_IRUSR) == 0)
81 fprintf (stderr, "Certificate database file %s should be readable by the owner.\n", cert_db_file);
82 if ((info.st_mode & S_IWUSR) == 0)
83 fprintf (stderr, "Certificate database file %s should be writable by the owner.\n", cert_db_file);
84 if ((info.st_mode & S_IXUSR) != 0)
85 {
86 fprintf (stderr, "Certificate database file %s must not be executable by the owner.\n", cert_db_file);
87 rc = 0;
88 }
89 if ((info.st_mode & S_IRGRP) == 0)
90 {
91 fprintf (stderr, "Certificate database file %s should be readable by the group.\n", cert_db_file);
92 rc = 0;
93 }
94 if ((info.st_mode & S_IWGRP) != 0)
95 {
96 fprintf (stderr, "Certificate database file %s must not be writable by the group.\n", cert_db_file);
97 rc = 0;
98 }
99 if ((info.st_mode & S_IXGRP) != 0)
100 {
101 fprintf (stderr, "Certificate database file %s must not be executable by the group.\n", cert_db_file);
102 rc = 0;
103 }
104 if ((info.st_mode & S_IROTH) == 0)
105 {
106 fprintf (stderr, "Certificate database file %s should be readable by others.\n", cert_db_file);
107 rc = 0;
108 }
109 if ((info.st_mode & S_IWOTH) != 0)
110 {
111 fprintf (stderr, "Certificate database file %s must not be writable by others.\n", cert_db_file);
112 rc = 0;
113 }
114 if ((info.st_mode & S_IXOTH) != 0)
115 {
116 fprintf (stderr, "Certificate database file %s must not be executable by others.\n", cert_db_file);
117 rc = 0;
118 }
119
120 return rc;
121 }
122
123 /* Function: int check_cert_db_permissions (const char *cert_db_path);
124 *
125 * Check that the given certificate directory and its contents have
126 * the correct permissions.
127 *
128 * Returns 0 if there is an error, 1 otherwise.
129 */
130 static int
131 check_cert_db_permissions (const char *cert_db_path) {
132 struct stat info;
133 char *fileName;
134 int rc;
135
136 rc = stat (cert_db_path, & info);
137 if (rc)
138 {
139 /* It is ok if the directory does not exist. This simply means that no signing
140 certificates have been authorized yet. */
141 if (errno == ENOENT)
142 return 0;
143 fprintf (stderr, "Could not obtain information on certificate database directory %s.\n",
144 cert_db_path);
145 perror ("");
146 return 0;
147 }
148
149 if (! S_ISDIR (info.st_mode))
150 {
151 fprintf (stderr, "Certificate database %s is not a directory.\n", cert_db_path);
152 return 0;
153 }
154
155 /* The owner of the database must be root. */
156 if (info.st_uid != 0)
157 {
158 fprintf (stderr, "Certificate database directory %s must be owned by root.\n", cert_db_path);
159 return 0;
160 }
161
162 rc = 1; /* ok */
163
164 /* Check the database directory access permissions */
165 if ((info.st_mode & S_IRUSR) == 0)
166 fprintf (stderr, "Certificate database %s should be readable by the owner.\n", cert_db_path);
167 if ((info.st_mode & S_IWUSR) == 0)
168 fprintf (stderr, "Certificate database %s should be writable by the owner.\n", cert_db_path);
169 if ((info.st_mode & S_IXUSR) == 0)
170 fprintf (stderr, "Certificate database %s should be searchable by the owner.\n", cert_db_path);
171 if ((info.st_mode & S_IRGRP) == 0)
172 fprintf (stderr, "Certificate database %s should be readable by the group.\n", cert_db_path);
173 if ((info.st_mode & S_IWGRP) != 0)
174 {
175 fprintf (stderr, "Certificate database %s must not be writable by the group.\n", cert_db_path);
176 rc = 0;
177 }
178 if ((info.st_mode & S_IXGRP) == 0)
179 fprintf (stderr, "Certificate database %s should be searchable by the group.\n", cert_db_path);
180 if ((info.st_mode & S_IROTH) == 0)
181 fprintf (stderr, "Certificate database %s should be readable by others.\n", cert_db_path);
182 if ((info.st_mode & S_IWOTH) != 0)
183 {
184 fprintf (stderr, "Certificate database %s must not be writable by others.\n", cert_db_path);
185 rc = 0;
186 }
187 if ((info.st_mode & S_IXOTH) == 0)
188 fprintf (stderr, "Certificate database %s should be searchable by others.\n", cert_db_path);
189
190 /* Now check the permissions of the critical files. */
191 fileName = PORT_Alloc (strlen (cert_db_path) + 11);
192 if (! fileName)
193 {
194 fprintf (stderr, "Unable to allocate memory for certificate database file names\n");
195 return 0;
196 }
197
198 /* These uses of sprintf() are OK, since we just allocated the
199 * string to be the correct length. */
200 sprintf (fileName, "%s/cert8.db", cert_db_path);
201 rc &= check_db_file_permissions (fileName);
202 sprintf (fileName, "%s/key3.db", cert_db_path);
203 rc &= check_db_file_permissions (fileName);
204 sprintf (fileName, "%s/secmod.db", cert_db_path);
205 rc &= check_db_file_permissions (fileName);
206
207 PORT_Free (fileName);
208
209 if (rc == 0)
210 fprintf (stderr, "Unable to use certificate database %s due to errors.\n", cert_db_path);
211
212 return rc;
213 }
214
215 static int
216 verify_it (const char *signatureName, const SECItem *signature,
217 const char *module_name, const void *module_data, off_t module_size,
218 const SECKEYPublicKey *pubKey)
219 {
220 VFYContext *vfy;
221 SECStatus secStatus;
222 int rc = MODULE_OK;
223
224 /* Create a verification context. */
225 vfy = VFY_CreateContextDirect (pubKey, signature, SEC_OID_PKCS1_RSA_ENCRYPTION,
226 SEC_OID_UNKNOWN, NULL, NULL);
227 if (! vfy)
228 {
229 /* The key does not match the signature. This is not an error. It just
230 means we are currently trying the wrong certificate/key. i.e. the
231 module remains untrusted for now. */
232 rc = MODULE_UNTRUSTED;
233 goto done;
234 }
235
236 /* Begin the verification process. */
237 secStatus = VFY_Begin(vfy);
238 if (secStatus != SECSuccess)
239 {
240 fprintf (stderr, "Unable to initialize verification context while verifying %s using the signature in %s.\n",
241 module_name, signatureName);
242 nssError ();
243 rc = MODULE_CHECK_ERROR;
244 goto done;
245 }
246
247 /* Add the data to be verified. */
248 secStatus = VFY_Update (vfy, module_data, module_size);
249 if (secStatus != SECSuccess)
250 {
251 fprintf (stderr, "Error while verifying %s using the signature in %s.\n",
252 module_name, signatureName);
253 nssError ();
254 rc = MODULE_CHECK_ERROR;
255 goto done;
256 }
257
258 /* Complete the verification. */
259 secStatus = VFY_End (vfy);
260 if (secStatus != SECSuccess) {
261 fprintf (stderr, "Unable to verify the signed module %s. It may have been altered since it was created.\n",
262 module_name);
263 nssError ();
264 rc = MODULE_ALTERED;
265 }
266
267 done:
268 if (vfy)
269 VFY_DestroyContext(vfy, PR_TRUE /*freeit*/);
270 return rc;
271 }
272
273 int verify_module (const char *signatureName, const char* module_name,
274 const void *module_data, off_t module_size)
275 {
276 const char *dbdir = SYSCONFDIR "/systemtap/staprun";
277 SECKEYPublicKey *pubKey;
278 CERTCertList *certList;
279 CERTCertListNode *certListNode;
280 CERTCertificate *cert;
281 PRStatus prStatus;
282 PRFileInfo info;
283 PRInt32 numBytes;
284 PRFileDesc *local_file_fd;
285 SECItem signature;
286 int rc = 0;
287 NSSInitContext *context;
288
289 /* Call the NSPR initialization routines. */
290 /* XXX: We shouldn't be using NSPR for a lot of this. */
291 PR_Init (PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1);
292
293 /* Get the size of the signature file. */
294 prStatus = PR_GetFileInfo (signatureName, &info);
295 if (prStatus != PR_SUCCESS || info.type != PR_FILE_FILE || info.size < 0)
296 {
297 if (verbose>1) fprintf (stderr, "Signature file %s not found\n", signatureName);
298 PR_Cleanup ();
299 return MODULE_UNTRUSTED; /* Not signed */
300 }
301
302 /* Open the signature file. */
303 local_file_fd = PR_Open (signatureName, PR_RDONLY, 0);
304 if (local_file_fd == NULL)
305 {
306 fprintf (stderr, "Could not open the signature file %s\n.", signatureName);
307 nssError ();
308 PR_Cleanup ();
309 return MODULE_CHECK_ERROR;
310 }
311
312 /* Allocate space to read the signature file. */
313 signature.data = PORT_Alloc (info.size);
314 if (! signature.data)
315 {
316 fprintf (stderr, "Unable to allocate memory for the signature in %s.\n", signatureName);
317 nssError ();
318 PR_Cleanup ();
319 return MODULE_CHECK_ERROR;
320 }
321
322 numBytes = PR_Read_Complete (local_file_fd, signature.data, info.size);
323 if (numBytes == 0) /* EOF */
324 {
325 fprintf (stderr, "EOF reading signature file %s.\n", signatureName);
326 PR_Cleanup ();
327 return MODULE_CHECK_ERROR;
328 }
329 if (numBytes < 0)
330 {
331 fprintf (stderr, "Error reading signature file %s.\n", signatureName);
332 nssError ();
333 PR_Cleanup ();
334 return MODULE_CHECK_ERROR;
335 }
336 if (numBytes != info.size)
337 {
338 fprintf (stderr, "Incomplete data while reading signature file %s.\n", signatureName);
339 PR_Cleanup ();
340 return MODULE_CHECK_ERROR;
341 }
342 signature.len = info.size;
343
344 /* Done with the signature file. */
345 PR_Close (local_file_fd);
346
347 /* Verify the permissions of the certificate database and its files. */
348 if (! check_cert_db_permissions (dbdir))
349 {
350 if (verbose>1) fprintf (stderr, "Certificate db %s permissions too loose\n", dbdir);
351 PR_Cleanup ();
352 return MODULE_UNTRUSTED;
353 }
354
355 /* Initialize NSS. */
356 context = nssInitContext (dbdir, 0/*readwrite*/, 1/*issueMessage*/);
357 if (context == NULL)
358 {
359 // Message already issued.
360 return MODULE_CHECK_ERROR;
361 }
362
363 certList = PK11_ListCerts (PK11CertListAll, NULL);
364 if (certList == NULL)
365 {
366 fprintf (stderr, "Unable to find certificates in the certificate database in %s.\n",
367 dbdir);
368 nssError ();
369 nssCleanup (dbdir, context);
370 return MODULE_UNTRUSTED;
371 }
372
373 /* We need to look at each certificate in the database. */
374 for (certListNode = CERT_LIST_HEAD (certList);
375 ! CERT_LIST_END (certListNode, certList);
376 certListNode = CERT_LIST_NEXT (certListNode))
377 {
378 cert = certListNode->cert;
379
380 pubKey = CERT_ExtractPublicKey (cert);
381 if (pubKey == NULL)
382 {
383 fprintf (stderr, "Unable to extract public key from the certificate with nickname %s from the certificate database in %s.\n",
384 cert->nickname, dbdir);
385 nssError ();
386 rc = MODULE_CHECK_ERROR;
387 break;
388 }
389
390 /* Verify the file. */
391 rc = verify_it (signatureName, & signature,
392 module_name, module_data, module_size, pubKey);
393 if (rc == MODULE_OK || rc == MODULE_ALTERED || rc == MODULE_CHECK_ERROR)
394 break; /* resolved or error */
395 }
396
397 CERT_DestroyCertList (certList);
398
399 /* Shutdown NSS and exit NSPR gracefully. */
400 nssCleanup (dbdir, context);
401 PR_Cleanup ();
402
403 return rc;
404 }
405
406 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */