summaryrefslogtreecommitdiff
path: root/lib/libc/gen/shm_open.c
blob: 59aca4b10a840a31bca43369b354cdbfdd837441 (plain)
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
/* $OpenBSD: shm_open.c,v 1.7 2015/09/12 15:01:33 guenther Exp $ */
/*
 * Copyright (c) 2013 Ted Unangst <tedu@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>

#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <sha2.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

/* SHA256_DIGEST_STRING_LENGTH includes nul */
/* "/tmp/" + sha256 + ".shm" */
#define SHM_PATH_SIZE (5 + SHA256_DIGEST_STRING_LENGTH + 4)

/* O_CLOEXEC and O_NOFOLLOW are extensions to POSIX */
#define OK_FLAGS	(O_CREAT | O_EXCL | O_TRUNC | O_CLOEXEC | O_NOFOLLOW)

static void
makeshmpath(const char *origpath, char *shmpath, size_t len)
{
	char buf[SHA256_DIGEST_STRING_LENGTH];

	SHA256Data(origpath, strlen(origpath), buf);
	snprintf(shmpath, len, "/tmp/%s.shm", buf);
}

int
shm_open(const char *path, int flags, mode_t mode)
{
	char shmpath[SHM_PATH_SIZE];
	struct stat sb;
	int fd;

	if (((flags & O_ACCMODE) != O_RDONLY && (flags & O_ACCMODE) != O_RDWR)
	    || (flags & ~(O_ACCMODE | OK_FLAGS))) {
		errno = EINVAL;
		return -1;
	}

	flags |= O_CLOEXEC | O_NOFOLLOW;
	mode = mode & 0600;

	makeshmpath(path, shmpath, sizeof(shmpath));

	fd = open(shmpath, flags, mode);
	if (fd == -1)
		return -1;
	if (fstat(fd, &sb) == -1 || !S_ISREG(sb.st_mode)) {
		close(fd);
		errno = EINVAL;
		return -1;
	}
	if (sb.st_uid != getuid()) {
		close(fd);
		errno = EPERM;
		return -1;
	}
	return fd;
}
DEF_WEAK(shm_open);

int
shm_unlink(const char *path)
{
	char shmpath[SHM_PATH_SIZE];

	makeshmpath(path, shmpath, sizeof(shmpath));
	return unlink(shmpath);
}

int
shm_mkstemp(char *template)
{
	size_t templatelen;
	char *t;
	int i, fd;

	templatelen = strlen(template);
	t = malloc(templatelen + 1);
	if (!t)
		return -1;
	t[templatelen] = '\0';

	fd = -1;
	for (i = 0; i < INT_MAX; i++) {
		memcpy(t, template, templatelen);
		if (!_mktemp(t))
			break;
		fd = shm_open(t, O_RDWR | O_EXCL | O_CREAT, 0600);
		if (fd != -1) {
			memcpy(template, t, templatelen);
			break;
		}
	}

	free(t);
	return fd;
}