msgutil.c 3.6 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 * linux/ipc/msgutil.c
L
Linus Torvalds 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15
 * Copyright (C) 1999, 2004 Manfred Spraul
 *
 * This file is released under GNU General Public Licence version 2 or
 * (at your option) any later version.
 *
 * See the file COPYING for more details.
 */

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

#include "util.h"

24 25
DEFINE_SPINLOCK(mq_lock);

26 27 28 29 30 31
/*
 * 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 = {
32
	.count		= ATOMIC_INIT(1),
33
	.user_ns = &init_user_ns,
34
	.ns.inum = PROC_IPC_INIT_INO,
35 36 37
#ifdef CONFIG_IPC_NS
	.ns.ops = &ipcns_operations,
#endif
38 39 40 41
};

atomic_t nr_ipc_ns = ATOMIC_INIT(1);

L
Linus Torvalds 已提交
42
struct msg_msgseg {
H
HoSung Jung 已提交
43
	struct msg_msgseg *next;
L
Linus Torvalds 已提交
44 45 46
	/* the next part of the message follows immediately */
};

47 48
#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 已提交
49

50

51
static struct msg_msg *alloc_msg(size_t len)
L
Linus Torvalds 已提交
52 53 54
{
	struct msg_msg *msg;
	struct msg_msgseg **pseg;
55
	size_t alen;
L
Linus Torvalds 已提交
56

P
Peter Hurley 已提交
57
	alen = min(len, DATALEN_MSG);
58
	msg = kmalloc(sizeof(*msg) + alen, GFP_KERNEL);
L
Linus Torvalds 已提交
59
	if (msg == NULL)
60
		return NULL;
L
Linus Torvalds 已提交
61 62 63 64

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

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
	len -= alen;
	pseg = &msg->next;
	while (len > 0) {
		struct msg_msgseg *seg;
		alen = min(len, DATALEN_SEG);
		seg = kmalloc(sizeof(*seg) + alen, GFP_KERNEL);
		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;
}

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

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

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

P
Peter Hurley 已提交
101 102 103
	for (seg = msg->next; seg != NULL; seg = seg->next) {
		len -= alen;
		src = (char __user *)src + alen;
P
Peter Hurley 已提交
104
		alen = min(len, DATALEN_SEG);
105
		if (copy_from_user(seg + 1, src, alen))
L
Linus Torvalds 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118
			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);
}
119 120 121 122
#ifdef CONFIG_CHECKPOINT_RESTORE
struct msg_msg *copy_msg(struct msg_msg *src, struct msg_msg *dst)
{
	struct msg_msgseg *dst_pseg, *src_pseg;
123 124
	size_t len = src->m_ts;
	size_t alen;
125 126 127 128

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

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

P
Peter Hurley 已提交
132 133 134 135 136
	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 已提交
137
		alen = min(len, DATALEN_SEG);
138 139 140 141 142 143 144 145
		memcpy(dst_pseg + 1, src_pseg + 1, alen);
	}

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

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

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

P
Peter Hurley 已提交
161 162 163
	for (seg = msg->next; seg != NULL; seg = seg->next) {
		len -= alen;
		dest = (char __user *)dest + alen;
P
Peter Hurley 已提交
164
		alen = min(len, DATALEN_SEG);
L
Linus Torvalds 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
		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;
		kfree(seg);
		seg = tmp;
	}
}