Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit 4284f7a

Browse files
hansendcsuryasaimadhu
authored andcommitted
selftests/sgx: Improve error detection and messages
The SGX device file (/dev/sgx_enclave) is unusual in that it requires execute permissions. It has to be both "chmod +x" *and* be on a filesystem without 'noexec'. In the future, udev and systemd should get updates to set up systems automatically. But, for now, nobody's systems do this automatically, and everybody gets error messages like this when running ./test_sgx: 0x0000000000000000 0x0000000000002000 0x03 0x0000000000002000 0x0000000000001000 0x05 0x0000000000003000 0x0000000000003000 0x03 mmap() failed, errno=1. That isn't very user friendly, even for forgetful kernel developers. Further, the test case is rather haphazard about its use of fprintf() versus perror(). Improve the error messages. Use perror() where possible. Lastly, do some sanity checks on opening and mmap()ing the device file so that we can get a decent error message out to the user. Now, if your user doesn't have permission, you'll get the following: $ ls -l /dev/sgx_enclave crw------- 1 root root 10, 126 Mar 18 11:29 /dev/sgx_enclave $ ./test_sgx Unable to open /dev/sgx_enclave: Permission denied If you then 'chown dave:dave /dev/sgx_enclave' (or whatever), but you leave execute permissions off, you'll get: $ ls -l /dev/sgx_enclave crw------- 1 dave dave 10, 126 Mar 18 11:29 /dev/sgx_enclave $ ./test_sgx no execute permissions on device file If you fix that with "chmod ug+x /dev/sgx" but you leave /dev as noexec, you'll get this: $ mount | grep "/dev .*noexec" udev on /dev type devtmpfs (rw,nosuid,noexec,...) $ ./test_sgx ERROR: mmap for exec: Operation not permitted mmap() succeeded for PROT_READ, but failed for PROT_EXEC check that user has execute permissions on /dev/sgx_enclave and that /dev does not have noexec set: 'mount | grep "/dev .*noexec"' That can be fixed with: mount -o remount,noexec /devESC Hopefully, the combination of better error messages and the search engines indexing this message will help people fix their systems until we do this properly. [ bp: Improve error messages more. ] Signed-off-by: Dave Hansen <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Signed-off-by: Borislav Petkov <[email protected]> Reviewed-by: Jarkko Sakkinen <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 901ddbb commit 4284f7a

File tree

2 files changed

+56
-15
lines changed

2 files changed

+56
-15
lines changed

tools/testing/selftests/sgx/load.c

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,19 @@ static bool encl_map_bin(const char *path, struct encl *encl)
4545

4646
fd = open(path, O_RDONLY);
4747
if (fd == -1) {
48-
perror("open()");
48+
perror("enclave executable open()");
4949
return false;
5050
}
5151

5252
ret = stat(path, &sb);
5353
if (ret) {
54-
perror("stat()");
54+
perror("enclave executable stat()");
5555
goto err;
5656
}
5757

5858
bin = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
5959
if (bin == MAP_FAILED) {
60-
perror("mmap()");
60+
perror("enclave executable mmap()");
6161
goto err;
6262
}
6363

@@ -90,8 +90,7 @@ static bool encl_ioc_create(struct encl *encl)
9090
ioc.src = (unsigned long)secs;
9191
rc = ioctl(encl->fd, SGX_IOC_ENCLAVE_CREATE, &ioc);
9292
if (rc) {
93-
fprintf(stderr, "SGX_IOC_ENCLAVE_CREATE failed: errno=%d\n",
94-
errno);
93+
perror("SGX_IOC_ENCLAVE_CREATE failed");
9594
munmap((void *)secs->base, encl->encl_size);
9695
return false;
9796
}
@@ -116,31 +115,72 @@ static bool encl_ioc_add_pages(struct encl *encl, struct encl_segment *seg)
116115

117116
rc = ioctl(encl->fd, SGX_IOC_ENCLAVE_ADD_PAGES, &ioc);
118117
if (rc < 0) {
119-
fprintf(stderr, "SGX_IOC_ENCLAVE_ADD_PAGES failed: errno=%d.\n",
120-
errno);
118+
perror("SGX_IOC_ENCLAVE_ADD_PAGES failed");
121119
return false;
122120
}
123121

124122
return true;
125123
}
126124

125+
126+
127127
bool encl_load(const char *path, struct encl *encl)
128128
{
129+
const char device_path[] = "/dev/sgx_enclave";
129130
Elf64_Phdr *phdr_tbl;
130131
off_t src_offset;
131132
Elf64_Ehdr *ehdr;
133+
struct stat sb;
134+
void *ptr;
132135
int i, j;
133136
int ret;
137+
int fd = -1;
134138

135139
memset(encl, 0, sizeof(*encl));
136140

137-
ret = open("/dev/sgx_enclave", O_RDWR);
138-
if (ret < 0) {
139-
fprintf(stderr, "Unable to open /dev/sgx_enclave\n");
141+
fd = open(device_path, O_RDWR);
142+
if (fd < 0) {
143+
perror("Unable to open /dev/sgx_enclave");
144+
goto err;
145+
}
146+
147+
ret = stat(device_path, &sb);
148+
if (ret) {
149+
perror("device file stat()");
150+
goto err;
151+
}
152+
153+
/*
154+
* This just checks if the /dev file has these permission
155+
* bits set. It does not check that the current user is
156+
* the owner or in the owning group.
157+
*/
158+
if (!(sb.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) {
159+
fprintf(stderr, "no execute permissions on device file %s\n", device_path);
160+
goto err;
161+
}
162+
163+
ptr = mmap(NULL, PAGE_SIZE, PROT_READ, MAP_SHARED, fd, 0);
164+
if (ptr == (void *)-1) {
165+
perror("mmap for read");
166+
goto err;
167+
}
168+
munmap(ptr, PAGE_SIZE);
169+
170+
#define ERR_MSG \
171+
"mmap() succeeded for PROT_READ, but failed for PROT_EXEC.\n" \
172+
" Check that current user has execute permissions on %s and \n" \
173+
" that /dev does not have noexec set: mount | grep \"/dev .*noexec\"\n" \
174+
" If so, remount it executable: mount -o remount,exec /dev\n\n"
175+
176+
ptr = mmap(NULL, PAGE_SIZE, PROT_EXEC, MAP_SHARED, fd, 0);
177+
if (ptr == (void *)-1) {
178+
fprintf(stderr, ERR_MSG, device_path);
140179
goto err;
141180
}
181+
munmap(ptr, PAGE_SIZE);
142182

143-
encl->fd = ret;
183+
encl->fd = fd;
144184

145185
if (!encl_map_bin(path, encl))
146186
goto err;
@@ -217,6 +257,8 @@ bool encl_load(const char *path, struct encl *encl)
217257
return true;
218258

219259
err:
260+
if (fd != -1)
261+
close(fd);
220262
encl_delete(encl);
221263
return false;
222264
}
@@ -229,7 +271,7 @@ static bool encl_map_area(struct encl *encl)
229271
area = mmap(NULL, encl_size * 2, PROT_NONE,
230272
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
231273
if (area == MAP_FAILED) {
232-
perror("mmap");
274+
perror("reservation mmap()");
233275
return false;
234276
}
235277

@@ -268,8 +310,7 @@ bool encl_build(struct encl *encl)
268310
ioc.sigstruct = (uint64_t)&encl->sigstruct;
269311
ret = ioctl(encl->fd, SGX_IOC_ENCLAVE_INIT, &ioc);
270312
if (ret) {
271-
fprintf(stderr, "SGX_IOC_ENCLAVE_INIT failed: errno=%d\n",
272-
errno);
313+
perror("SGX_IOC_ENCLAVE_INIT failed");
273314
return false;
274315
}
275316

tools/testing/selftests/sgx/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ int main(int argc, char *argv[], char *envp[])
195195
addr = mmap((void *)encl.encl_base + seg->offset, seg->size,
196196
seg->prot, MAP_SHARED | MAP_FIXED, encl.fd, 0);
197197
if (addr == MAP_FAILED) {
198-
fprintf(stderr, "mmap() failed, errno=%d.\n", errno);
198+
perror("mmap() segment failed");
199199
exit(KSFT_FAIL);
200200
}
201201
}

0 commit comments

Comments
 (0)