summaryrefslogtreecommitdiff
path: root/ipc/kdbus/handle.h
blob: 5dde2c10bed4cab1a981f4ce8c9adde4d6c30e7b (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
/*
 * Copyright (C) 2013-2015 Kay Sievers
 * Copyright (C) 2013-2015 Greg Kroah-Hartman <gregkh@linuxfoundation.org>
 * Copyright (C) 2013-2015 Daniel Mack <daniel@zonque.org>
 * Copyright (C) 2013-2015 David Herrmann <dh.herrmann@gmail.com>
 * Copyright (C) 2013-2015 Linux Foundation
 *
 * kdbus is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation; either version 2.1 of the License, or (at
 * your option) any later version.
 */

#ifndef __KDBUS_HANDLE_H
#define __KDBUS_HANDLE_H

#include <linux/fs.h>
#include <uapi/linux/kdbus.h>

extern const struct file_operations kdbus_handle_ops;

/**
 * kdbus_arg - information and state of a single ioctl command item
 * @type:		item type
 * @item:		set by the parser to the first found item of this type
 * @multiple:		whether multiple items of this type are allowed
 * @mandatory:		whether at least one item of this type is required
 *
 * This structure describes a single item in an ioctl command payload. The
 * caller has to pre-fill the type and flags, the parser will then use this
 * information to verify the ioctl payload. @item is set by the parser to point
 * to the first occurrence of the item.
 */
struct kdbus_arg {
	u64 type;
	struct kdbus_item *item;
	bool multiple : 1;
	bool mandatory : 1;
};

/**
 * kdbus_args - information and state of ioctl command parser
 * @allowed_flags:	set of flags this command supports
 * @argc:		number of items in @argv
 * @argv:		array of items this command supports
 * @user:		set by parser to user-space location of current command
 * @cmd:		set by parser to kernel copy of command payload
 * @cmd_buf:		inline buf to avoid kmalloc() on small cmds
 * @items:		points to item array in @cmd
 * @items_size:		size of @items in bytes
 * @is_cmd:		whether this is a command-payload or msg-payload
 *
 * This structure is used to parse ioctl command payloads on each invocation.
 * The ioctl handler has to pre-fill the flags and allowed items before passing
 * the object to kdbus_args_parse(). The parser will copy the command payload
 * into kernel-space and verify the correctness of the data.
 *
 * We use a 256 bytes buffer for small command payloads, to be allocated on
 * stack on syscall entrance.
 */
struct kdbus_args {
	u64 allowed_flags;
	size_t argc;
	struct kdbus_arg *argv;

	struct kdbus_cmd __user *user;
	struct kdbus_cmd *cmd;
	u8 cmd_buf[256];

	struct kdbus_item *items;
	size_t items_size;
	bool is_cmd : 1;
};

int __kdbus_args_parse(struct kdbus_args *args, bool is_cmd, void __user *argp,
		       size_t type_size, size_t items_offset, void **out);
int kdbus_args_clear(struct kdbus_args *args, int ret);

#define kdbus_args_parse(_args, _argp, _v)                              \
	({                                                              \
		BUILD_BUG_ON(offsetof(typeof(**(_v)), size) !=          \
			     offsetof(struct kdbus_cmd, size));         \
		BUILD_BUG_ON(offsetof(typeof(**(_v)), flags) !=         \
			     offsetof(struct kdbus_cmd, flags));        \
		BUILD_BUG_ON(offsetof(typeof(**(_v)), return_flags) !=  \
			     offsetof(struct kdbus_cmd, return_flags)); \
		__kdbus_args_parse((_args), 1, (_argp), sizeof(**(_v)), \
				   offsetof(typeof(**(_v)), items),     \
				   (void **)(_v));                      \
	})

#define kdbus_args_parse_msg(_args, _argp, _v)                          \
	({                                                              \
		BUILD_BUG_ON(offsetof(typeof(**(_v)), size) !=          \
			     offsetof(struct kdbus_cmd, size));         \
		BUILD_BUG_ON(offsetof(typeof(**(_v)), flags) !=         \
			     offsetof(struct kdbus_cmd, flags));        \
		__kdbus_args_parse((_args), 0, (_argp), sizeof(**(_v)), \
				   offsetof(typeof(**(_v)), items),     \
				   (void **)(_v));                      \
	})

#endif