msgutil.c 3.61 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
/*
2
 * linux/ipc/msgutil.c
Linus Torvalds's avatar
Linus Torvalds committed
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>
Al Viro's avatar
Al Viro committed
16
#include <linux/msg.h>
17
#include <linux/ipc_namespace.h>
Al Viro's avatar
Al Viro committed
18
#include <linux/utsname.h>
19
#include <linux/proc_ns.h>
20
#include <linux/uaccess.h>
Linus Torvalds's avatar
Linus Torvalds committed
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		= REFCOUNT_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
};

Linus Torvalds's avatar
Linus Torvalds committed
40
struct msg_msgseg {
41
	struct msg_msgseg *next;
Linus Torvalds's avatar
Linus Torvalds committed
42 43 44
	/* the next part of the message follows immediately */
};

45 46
#define DATALEN_MSG	((size_t)PAGE_SIZE-sizeof(struct msg_msg))
#define DATALEN_SEG	((size_t)PAGE_SIZE-sizeof(struct msg_msgseg))
Linus Torvalds's avatar
Linus Torvalds committed
47

48

49
static struct msg_msg *alloc_msg(size_t len)
Linus Torvalds's avatar
Linus Torvalds committed
50 51 52
{
	struct msg_msg *msg;
	struct msg_msgseg **pseg;
53
	size_t alen;
Linus Torvalds's avatar
Linus Torvalds committed
54

Peter Hurley's avatar
Peter Hurley committed
55
	alen = min(len, DATALEN_MSG);
56
	msg = kmalloc(sizeof(*msg) + alen, GFP_KERNEL_ACCOUNT);
Linus Torvalds's avatar
Linus Torvalds committed
57
	if (msg == NULL)
58
		return NULL;
Linus Torvalds's avatar
Linus Torvalds committed
59 60 61 62

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

63 64 65 66 67
	len -= alen;
	pseg = &msg->next;
	while (len > 0) {
		struct msg_msgseg *seg;
		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))
Linus Torvalds's avatar
Linus Torvalds committed
97 98
		goto out_err;

99 100 101
	for (seg = msg->next; seg != NULL; seg = seg->next) {
		len -= alen;
		src = (char __user *)src + alen;
Peter Hurley's avatar
Peter Hurley committed
102
		alen = min(len, DATALEN_SEG);
103
		if (copy_from_user(seg + 1, src, alen))
Linus Torvalds's avatar
Linus Torvalds committed
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);

Peter Hurley's avatar
Peter Hurley committed
127
	alen = min(len, DATALEN_MSG);
128 129
	memcpy(dst + 1, src + 1, alen);

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;
Peter Hurley's avatar
Peter Hurley committed
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)
Linus Torvalds's avatar
Linus Torvalds committed
151
{
152
	size_t alen;
Linus Torvalds's avatar
Linus Torvalds committed
153 154
	struct msg_msgseg *seg;

Peter Hurley's avatar
Peter Hurley committed
155
	alen = min(len, DATALEN_MSG);
Linus Torvalds's avatar
Linus Torvalds committed
156 157 158
	if (copy_to_user(dest, msg + 1, alen))
		return -1;

159 160 161
	for (seg = msg->next; seg != NULL; seg = seg->next) {
		len -= alen;
		dest = (char __user *)dest + alen;
Peter Hurley's avatar
Peter Hurley committed
162
		alen = min(len, DATALEN_SEG);
Linus Torvalds's avatar
Linus Torvalds committed
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
		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;
	}
}