msgutil.c 3.6 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
L
Linus Torvalds 已提交
2
/*
3
 * linux/ipc/msgutil.c
L
Linus Torvalds 已提交
4 5 6 7 8 9 10 11
 * Copyright (C) 1999, 2004 Manfred Spraul
 */

#include <linux/spinlock.h>
#include <linux/init.h>
#include <linux/security.h>
#include <linux/slab.h>
#include <linux/ipc.h>
A
Al Viro 已提交
12
#include <linux/msg.h>
13
#include <linux/ipc_namespace.h>
A
Al Viro 已提交
14
#include <linux/utsname.h>
15
#include <linux/proc_ns.h>
H
HoSung Jung 已提交
16
#include <linux/uaccess.h>
17
#include <linux/sched.h>
L
Linus Torvalds 已提交
18 19 20

#include "util.h"

21 22
DEFINE_SPINLOCK(mq_lock);

23 24 25 26 27 28
/*
 * The next 2 defines are here bc this is the only file
 * compiled when either CONFIG_SYSVIPC and CONFIG_POSIX_MQUEUE
 * and not CONFIG_IPC_NS.
 */
struct ipc_namespace init_ipc_ns = {
29
	.count		= REFCOUNT_INIT(1),
30
	.user_ns = &init_user_ns,
31
	.ns.inum = PROC_IPC_INIT_INO,
32 33 34
#ifdef CONFIG_IPC_NS
	.ns.ops = &ipcns_operations,
#endif
35 36
};

L
Linus Torvalds 已提交
37
struct msg_msgseg {
H
HoSung Jung 已提交
38
	struct msg_msgseg *next;
L
Linus Torvalds 已提交
39 40 41
	/* the next part of the message follows immediately */
};

42 43
#define DATALEN_MSG	((size_t)PAGE_SIZE-sizeof(struct msg_msg))
#define DATALEN_SEG	((size_t)PAGE_SIZE-sizeof(struct msg_msgseg))
L
Linus Torvalds 已提交
44

45

46
static struct msg_msg *alloc_msg(size_t len)
L
Linus Torvalds 已提交
47 48 49
{
	struct msg_msg *msg;
	struct msg_msgseg **pseg;
50
	size_t alen;
L
Linus Torvalds 已提交
51

P
Peter Hurley 已提交
52
	alen = min(len, DATALEN_MSG);
53
	msg = kmalloc(sizeof(*msg) + alen, GFP_KERNEL_ACCOUNT);
L
Linus Torvalds 已提交
54
	if (msg == NULL)
55
		return NULL;
L
Linus Torvalds 已提交
56 57 58 59

	msg->next = NULL;
	msg->security = NULL;

60 61 62 63
	len -= alen;
	pseg = &msg->next;
	while (len > 0) {
		struct msg_msgseg *seg;
64 65 66

		cond_resched();

67
		alen = min(len, DATALEN_SEG);
68
		seg = kmalloc(sizeof(*seg) + alen, GFP_KERNEL_ACCOUNT);
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
		if (seg == NULL)
			goto out_err;
		*pseg = seg;
		seg->next = NULL;
		pseg = &seg->next;
		len -= alen;
	}

	return msg;

out_err:
	free_msg(msg);
	return NULL;
}

84
struct msg_msg *load_msg(const void __user *src, size_t len)
85 86 87
{
	struct msg_msg *msg;
	struct msg_msgseg *seg;
88
	int err = -EFAULT;
89
	size_t alen;
90 91 92 93 94 95

	msg = alloc_msg(len);
	if (msg == NULL)
		return ERR_PTR(-ENOMEM);

	alen = min(len, DATALEN_MSG);
96
	if (copy_from_user(msg + 1, src, alen))
L
Linus Torvalds 已提交
97 98
		goto out_err;

P
Peter Hurley 已提交
99 100 101
	for (seg = msg->next; seg != NULL; seg = seg->next) {
		len -= alen;
		src = (char __user *)src + alen;
P
Peter Hurley 已提交
102
		alen = min(len, DATALEN_SEG);
103
		if (copy_from_user(seg + 1, src, alen))
L
Linus Torvalds 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116
			goto out_err;
	}

	err = security_msg_msg_alloc(msg);
	if (err)
		goto out_err;

	return msg;

out_err:
	free_msg(msg);
	return ERR_PTR(err);
}
117 118 119 120
#ifdef CONFIG_CHECKPOINT_RESTORE
struct msg_msg *copy_msg(struct msg_msg *src, struct msg_msg *dst)
{
	struct msg_msgseg *dst_pseg, *src_pseg;
121 122
	size_t len = src->m_ts;
	size_t alen;
123 124 125 126

	if (src->m_ts > dst->m_ts)
		return ERR_PTR(-EINVAL);

P
Peter Hurley 已提交
127
	alen = min(len, DATALEN_MSG);
128 129
	memcpy(dst + 1, src + 1, alen);

P
Peter Hurley 已提交
130 131 132 133 134
	for (dst_pseg = dst->next, src_pseg = src->next;
	     src_pseg != NULL;
	     dst_pseg = dst_pseg->next, src_pseg = src_pseg->next) {

		len -= alen;
P
Peter Hurley 已提交
135
		alen = min(len, DATALEN_SEG);
136 137 138 139 140 141 142 143
		memcpy(dst_pseg + 1, src_pseg + 1, alen);
	}

	dst->m_type = src->m_type;
	dst->m_ts = src->m_ts;

	return dst;
}
144 145 146 147 148
#else
struct msg_msg *copy_msg(struct msg_msg *src, struct msg_msg *dst)
{
	return ERR_PTR(-ENOSYS);
}
149
#endif
150
int store_msg(void __user *dest, struct msg_msg *msg, size_t len)
L
Linus Torvalds 已提交
151
{
152
	size_t alen;
L
Linus Torvalds 已提交
153 154
	struct msg_msgseg *seg;

P
Peter Hurley 已提交
155
	alen = min(len, DATALEN_MSG);
L
Linus Torvalds 已提交
156 157 158
	if (copy_to_user(dest, msg + 1, alen))
		return -1;

P
Peter Hurley 已提交
159 160 161
	for (seg = msg->next; seg != NULL; seg = seg->next) {
		len -= alen;
		dest = (char __user *)dest + alen;
P
Peter Hurley 已提交
162
		alen = min(len, DATALEN_SEG);
L
Linus Torvalds 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
		if (copy_to_user(dest, seg + 1, alen))
			return -1;
	}
	return 0;
}

void free_msg(struct msg_msg *msg)
{
	struct msg_msgseg *seg;

	security_msg_msg_free(msg);

	seg = msg->next;
	kfree(msg);
	while (seg != NULL) {
		struct msg_msgseg *tmp = seg->next;
179 180

		cond_resched();
L
Linus Torvalds 已提交
181 182 183 184
		kfree(seg);
		seg = tmp;
	}
}