summaryrefslogtreecommitdiff
path: root/fs/smb/server/mgmt/share_config.c
blob: cc9f18ede80df209bd58b6f4928efa52670a406b (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 *   Copyright (C) 2018 Samsung Electronics Co., Ltd.
 */

#include <linux/list.h>
#include <linux/jhash.h>
#include <linux/slab.h>
#include <linux/rwsem.h>
#include <linux/parser.h>
#include <linux/namei.h>
#include <linux/fs_struct.h>
#include <linux/sched.h>
#include <linux/mm.h>

#include "share_config.h"
#include "user_config.h"
#include "user_session.h"
#include "../connection.h"
#include "../transport_ipc.h"
#include "../misc.h"

#define SHARE_HASH_BITS		12
static DEFINE_HASHTABLE(shares_table, SHARE_HASH_BITS);
static DECLARE_RWSEM(shares_table_lock);

struct ksmbd_veto_pattern {
	char			*pattern;
	struct list_head	list;
};

#ifdef CONFIG_PROC_FS
static const struct ksmbd_const_name ksmbd_share_flag_names[] = {
	{KSMBD_SHARE_FLAG_AVAILABLE, "available"},
	{KSMBD_SHARE_FLAG_BROWSEABLE, "browseable"},
	{KSMBD_SHARE_FLAG_WRITEABLE, "writeable"},
	{KSMBD_SHARE_FLAG_READONLY, "read-only"},
	{KSMBD_SHARE_FLAG_GUEST_OK, "guest-ok"},
	{KSMBD_SHARE_FLAG_GUEST_ONLY, "guest-only"},
	{KSMBD_SHARE_FLAG_STORE_DOS_ATTRS, "store-dos-attrs"},
	{KSMBD_SHARE_FLAG_OPLOCKS, "oplocks"},
	{KSMBD_SHARE_FLAG_PIPE, "pipe"},
	{KSMBD_SHARE_FLAG_HIDE_DOT_FILES, "hide-dot-files"},
	{KSMBD_SHARE_FLAG_INHERIT_OWNER, "inherit-owner"},
	{KSMBD_SHARE_FLAG_STREAMS, "streams"},
	{KSMBD_SHARE_FLAG_FOLLOW_SYMLINKS, "follow-symlinks"},
	{KSMBD_SHARE_FLAG_ACL_XATTR, "acl-xattr"},
	{KSMBD_SHARE_FLAG_UPDATE, "update"},
	{KSMBD_SHARE_FLAG_CROSSMNT, "crossmnt"},
	{KSMBD_SHARE_FLAG_CONTINUOUS_AVAILABILITY, "continuous-availability"},
	{KSMBD_SHARE_FLAG_ENCRYPT_DATA, "encrypt-data"},
};

static int proc_show_shares(struct seq_file *m, void *v)
{
	struct ksmbd_share_config *share;
	int i;

	down_read(&shares_table_lock);
	hash_for_each(shares_table, i, share, hlist) {
		seq_printf(m, "name:\t%s\n", share->name);
		seq_printf(m, "type:\t%s\n",
			   test_share_config_flag(share, KSMBD_SHARE_FLAG_PIPE) ?
			   "pipe" : "disk");
		seq_printf(m, "tree_connects:\t%d\n",
			   atomic_read(&share->tree_connections));
		seq_printf(m, "file_mask:\t0%07o\n", share->create_mask);
		seq_printf(m, "directory_mask:\t0%07o\n", share->directory_mask);
		seq_puts(m, "flags:\t");
		ksmbd_proc_show_flag_names(m, ksmbd_share_flag_names,
					   ARRAY_SIZE(ksmbd_share_flag_names),
					   share->flags);
		seq_puts(m, "\n\n");
	}
	up_read(&shares_table_lock);
	return 0;
}

int create_proc_shares(void)
{
	if (!ksmbd_proc_create("shares", proc_show_shares, NULL))
		return -ENOMEM;
	return 0;
}
#else
int create_proc_shares(void) { return 0; }
#endif

static unsigned int share_name_hash(const char *name)
{
	return jhash(name, strlen(name), 0);
}

static void kill_share(struct ksmbd_share_config *share)
{
	while (!list_empty(&share->veto_list)) {
		struct ksmbd_veto_pattern *p;

		p = list_entry(share->veto_list.next,
			       struct ksmbd_veto_pattern,
			       list);
		list_del(&p->list);
		kfree(p->pattern);
		kfree(p);
	}

	if (share->path)
		path_put(&share->vfs_path);
	kfree(share->name);
	kfree(share->path);
	kfree(share);
}

void ksmbd_share_config_del(struct ksmbd_share_config *share)
{
	down_write(&shares_table_lock);
	hash_del(&share->hlist);
	up_write(&shares_table_lock);
}

void __ksmbd_share_config_put(struct ksmbd_share_config *share)
{
	ksmbd_share_config_del(share);
	kill_share(share);
}

static struct ksmbd_share_config *
__get_share_config(struct ksmbd_share_config *share)
{
	if (!atomic_inc_not_zero(&share->refcount))
		return NULL;
	return share;
}

static struct ksmbd_share_config *__share_lookup(const char *name)
{
	struct ksmbd_share_config *share;
	unsigned int key = share_name_hash(name);

	hash_for_each_possible(shares_table, share, hlist, key) {
		if (!strcmp(name, share->name))
			return share;
	}
	return NULL;
}

static int parse_veto_list(struct ksmbd_share_config *share,
			   char *veto_list,
			   size_t veto_list_sz)
{
	size_t sz;

	if (!veto_list_sz)
		return 0;

	while (veto_list_sz > 0) {
		struct ksmbd_veto_pattern *p;

		sz = strnlen(veto_list, veto_list_sz);
		if (!sz)
			break;

		p = kzalloc_obj(struct ksmbd_veto_pattern, KSMBD_DEFAULT_GFP);
		if (!p)
			return -ENOMEM;

		p->pattern = kstrndup(veto_list, sz, KSMBD_DEFAULT_GFP);
		if (!p->pattern) {
			kfree(p);
			return -ENOMEM;
		}

		list_add(&p->list, &share->veto_list);

		if (sz == veto_list_sz)
			break;

		veto_list += sz + 1;
		veto_list_sz -= (sz + 1);
	}

	return 0;
}

static struct ksmbd_share_config *share_config_request(struct ksmbd_work *work,
						       const char *name)
{
	struct ksmbd_share_config_response *resp;
	struct ksmbd_share_config *share = NULL;
	struct ksmbd_share_config *lookup;
	struct unicode_map *um = work->conn->um;
	int ret;

	resp = ksmbd_ipc_share_config_request(name);
	if (!resp)
		return NULL;

	if (resp->flags == KSMBD_SHARE_FLAG_INVALID)
		goto out;

	if (*resp->share_name) {
		char *cf_resp_name;
		bool equal;

		cf_resp_name = ksmbd_casefold_sharename(um, resp->share_name);
		if (IS_ERR(cf_resp_name))
			goto out;
		equal = !strcmp(cf_resp_name, name);
		kfree(cf_resp_name);
		if (!equal)
			goto out;
	}

	share = kzalloc_obj(struct ksmbd_share_config, KSMBD_DEFAULT_GFP);
	if (!share)
		goto out;

	share->flags = resp->flags;
	atomic_set(&share->refcount, 1);
	ksmbd_share_tree_conn_init(share);
	INIT_LIST_HEAD(&share->veto_list);
	share->name = kstrdup(name, KSMBD_DEFAULT_GFP);
	if (!share->name) {
		kill_share(share);
		share = NULL;
		goto out;
	}

	if (!test_share_config_flag(share, KSMBD_SHARE_FLAG_PIPE)) {
		size_t path_len;

		if (resp->payload_sz <= resp->veto_list_sz) {
			ret = -EINVAL;
		} else {
			path_len = resp->payload_sz - resp->veto_list_sz;
			if (resp->veto_list_sz)
				path_len--;

			if (!path_len) {
				ret = -EINVAL;
			} else {
				share->path = kstrndup(
					ksmbd_share_config_path(resp),
					path_len, KSMBD_DEFAULT_GFP);
				if (!share->path)
					ret = -ENOMEM;
				else
					ret = 0;
			}
		}
		if (share->path) {
			share->path_sz = strlen(share->path);
			while (share->path_sz > 1 &&
			       share->path[share->path_sz - 1] == '/')
				share->path[--share->path_sz] = '\0';
		}
		share->create_mask = resp->create_mask;
		share->directory_mask = resp->directory_mask;
		share->force_create_mode = resp->force_create_mode;
		share->force_directory_mode = resp->force_directory_mode;
		share->force_uid = resp->force_uid;
		share->force_gid = resp->force_gid;
		if (!ret)
			ret = parse_veto_list(share,
					      KSMBD_SHARE_CONFIG_VETO_LIST(resp),
					      resp->veto_list_sz);
		if (!ret && share->path) {
			if (__ksmbd_override_fsids(work, share)) {
				kill_share(share);
				share = NULL;
				goto out;
			}

			scoped_with_init_fs()
				ret = kern_path(share->path, 0, &share->vfs_path);
			ksmbd_revert_fsids(work);
			if (ret) {
				ksmbd_debug(SMB, "failed to access '%s'\n",
					    share->path);
				/* Avoid put_path() */
				kfree(share->path);
				share->path = NULL;
			}
		}
		if (ret) {
			kill_share(share);
			share = NULL;
			goto out;
		}
	}

	down_write(&shares_table_lock);
	lookup = __share_lookup(name);
	if (lookup)
		lookup = __get_share_config(lookup);
	if (!lookup) {
		hash_add(shares_table, &share->hlist, share_name_hash(name));
	} else {
		kill_share(share);
		share = lookup;
	}
	up_write(&shares_table_lock);

out:
	kvfree(resp);
	return share;
}

struct ksmbd_share_config *ksmbd_share_config_get(struct ksmbd_work *work,
						  const char *name)
{
	struct ksmbd_share_config *share;

	down_read(&shares_table_lock);
	share = __share_lookup(name);
	if (share)
		share = __get_share_config(share);
	up_read(&shares_table_lock);

	if (share)
		return share;
	return share_config_request(work, name);
}

bool ksmbd_share_veto_filename(struct ksmbd_share_config *share,
			       const char *filename)
{
	struct ksmbd_veto_pattern *p;

	list_for_each_entry(p, &share->veto_list, list) {
		if (match_wildcard(p->pattern, filename))
			return true;
	}
	return false;
}