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
|
/*
* Copyright 2013 <James.Bottomley@HansenPartnership.com>
*
* see COPYING file
*/
#include <stdlib.h>
#include <stdint.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <efi.h>
#include <kernel_efivars.h>
#include <guid.h>
#include <sha256.h>
#include "efiauthenticated.h"
static char *kernel_efi_path = NULL;
void
kernel_variable_init(void)
{
FILE *mount_l_fp = NULL;
char *path = NULL;
char *type = NULL;
mount_l_fp = popen("mount -l", "r");
if (mount_l_fp == NULL) {
fprintf(stderr, "Failed to get output of mount -l\n");
exit(1);
}
while (fscanf(mount_l_fp, "%*s on %ms type %ms %*[^\n]\n", &path, &type) == 2) {
if (strcmp(type, "efivarfs") == 0) {
kernel_efi_path = strdup(path);
break;
}
free(path);
path = NULL;
free(type);
type = NULL;
}
if (mount_l_fp != NULL)
pclose(mount_l_fp);
if (path != NULL)
free(path);
if (type != NULL)
free(type);
if (kernel_efi_path == NULL) {
fprintf(stderr, "No efivarfs filesystem is mounted\n");
exit(1);
}
}
int
get_variable(const char *var, EFI_GUID *guid, uint32_t *attributes,
uint32_t *size, void *buf)
{
if (!kernel_efi_path)
return -EINVAL;
int varfs_len = strlen(var) + 48 + strlen(kernel_efi_path);
char *varfs = malloc(varfs_len);
uint32_t attr;
int fd;
struct stat st;
snprintf(varfs, varfs_len, "%s/%s-%s", kernel_efi_path,
var, guid_to_str(guid));
fd = open(varfs, O_RDONLY);
free(varfs);
if (fd < 0)
return errno;
if (fstat(fd, &st) < 0)
return errno;
if (size)
*size = st.st_size - sizeof(attr);
read(fd, &attr, sizeof(attr));
if (attributes)
*attributes = attr;
if (buf)
read(fd, buf, st.st_size - sizeof(attr));
close(fd);
return 0;
}
int
get_variable_alloc(const char *var, EFI_GUID *guid, uint32_t *attributes,
uint32_t *size, uint8_t **buf)
{
uint32_t len;
int ret = get_variable(var, guid, NULL, &len, NULL);
if (ret)
return ret;
*buf = malloc(len);
if (!buf)
return -ENOMEM;
return get_variable(var, guid, attributes, size, *buf);
}
int
variable_is_setupmode(void)
{
uint8_t setup_mode;
get_variable("SetupMode", &GV_GUID, NULL, NULL, &setup_mode);
return setup_mode;
}
int
variable_is_secureboot(void)
{
uint8_t secure_boot;
get_variable("SecureBoot", &GV_GUID, NULL, NULL, &secure_boot);
return secure_boot;
}
int
set_variable(const char *var, EFI_GUID *guid, uint32_t attributes,
uint32_t size, void *buf)
{
if (!kernel_efi_path)
return -EINVAL;
int varfs_len = strlen(var) + 48 + strlen(kernel_efi_path);
char *varfs = malloc(varfs_len),
*newbuf = malloc(size + sizeof(attributes));
int fd;
snprintf(varfs, varfs_len, "%s/%s-%s", kernel_efi_path,
var, guid_to_str(guid));
fd = open(varfs, O_RDWR|O_CREAT|O_TRUNC, 0644);
free(varfs);
if (fd < 0)
return errno;
memcpy(newbuf, &attributes, sizeof(attributes));
memcpy(newbuf + sizeof(attributes), buf, size);
if (write(fd, newbuf, size + sizeof(attributes)) != size + sizeof(attributes))
return errno;
close(fd);
return 0;
}
int
set_variable_esl(const char *var, EFI_GUID *guid, uint32_t attributes,
uint32_t size, void *buf)
{
if (!kernel_efi_path)
return -EINVAL;
int newsize = size + OFFSET_OF(EFI_VARIABLE_AUTHENTICATION_2, AuthInfo) + OFFSET_OF(WIN_CERTIFICATE_UEFI_GUID, CertData);
char *newdata = malloc(newsize);
EFI_VARIABLE_AUTHENTICATION_2 *DescriptorData;
EFI_TIME *Time;
struct tm tm;
time_t t;
time(&t);
memset(newdata, '\0', newsize);
memcpy(newdata + newsize - size, buf, size);
DescriptorData = (EFI_VARIABLE_AUTHENTICATION_2 *)newdata;
Time = &DescriptorData->TimeStamp;
gmtime_r(&t, &tm);
/* FIXME: currently timestamp is one year into future because of
* the way we set up the secure environment */
Time->Year = tm.tm_year + 1900 + 1;
/* EFI_TIME Month is 1-12; Unix tm_mon is 0-11 */
Time->Month = tm.tm_mon + 1;
Time->Day = tm.tm_mday;
Time->Hour = tm.tm_hour;
Time->Minute = tm.tm_min;
Time->Second = tm.tm_sec;
DescriptorData->AuthInfo.Hdr.dwLength = OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID, CertData);
DescriptorData->AuthInfo.Hdr.wRevision = 0x0200;
DescriptorData->AuthInfo.Hdr.wCertificateType = WIN_CERT_TYPE_EFI_GUID;
DescriptorData->AuthInfo.CertType = EFI_CERT_TYPE_PKCS7_GUID;
int ret = set_variable(var, guid, attributes, newsize, newdata);
free(newdata);
return ret;
}
uint8_t *
hash_to_esl(EFI_GUID *owner, int *len,
uint8_t hash[SHA256_DIGEST_SIZE])
{
const int siglen = sizeof(EFI_SIGNATURE_LIST) + sizeof(EFI_SIGNATURE_DATA) - 1 + SHA256_DIGEST_SIZE;
uint8_t *sig = malloc(siglen);
EFI_SIGNATURE_LIST *l = (void *)sig;
EFI_SIGNATURE_DATA *d = (void *)sig + sizeof(EFI_SIGNATURE_LIST);
if (len)
*len = siglen;
memset(sig, 0, siglen);
l->SignatureType = EFI_CERT_SHA256_GUID;
l->SignatureListSize = siglen;
l->SignatureSize = 16 +32; /* UEFI defined */
memcpy(&d->SignatureData, hash, SHA256_DIGEST_SIZE);
d->SignatureOwner = *owner;
return sig;
}
int
set_variable_hash(const char *var, EFI_GUID *owner, uint32_t attributes,
uint8_t hash[SHA256_DIGEST_SIZE])
{
int len;
uint8_t *sig = hash_to_esl(&MOK_OWNER, &len, hash);
int ret = set_variable_esl(var, owner, attributes, len, sig);
free(sig);
return ret;
}
|