1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2018 Samsung Electronics Co., Ltd.
6 #include <linux/list.h>
7 #include <linux/jhash.h>
8 #include <linux/slab.h>
9 #include <linux/rwsem.h>
10 #include <linux/parser.h>
11 #include <linux/namei.h>
12 #include <linux/sched.h>
15 #include "share_config.h"
16 #include "user_config.h"
17 #include "user_session.h"
18 #include "../transport_ipc.h"
21 #define SHARE_HASH_BITS 3
22 static DEFINE_HASHTABLE(shares_table, SHARE_HASH_BITS);
23 static DECLARE_RWSEM(shares_table_lock);
25 struct ksmbd_veto_pattern {
27 struct list_head list;
30 static unsigned int share_name_hash(const char *name)
32 return jhash(name, strlen(name), 0);
35 static void kill_share(struct ksmbd_share_config *share)
37 while (!list_empty(&share->veto_list)) {
38 struct ksmbd_veto_pattern *p;
40 p = list_entry(share->veto_list.next,
41 struct ksmbd_veto_pattern,
49 path_put(&share->vfs_path);
55 void ksmbd_share_config_del(struct ksmbd_share_config *share)
57 down_write(&shares_table_lock);
58 hash_del(&share->hlist);
59 up_write(&shares_table_lock);
62 void __ksmbd_share_config_put(struct ksmbd_share_config *share)
64 ksmbd_share_config_del(share);
68 static struct ksmbd_share_config *
69 __get_share_config(struct ksmbd_share_config *share)
71 if (!atomic_inc_not_zero(&share->refcount))
76 static struct ksmbd_share_config *__share_lookup(const char *name)
78 struct ksmbd_share_config *share;
79 unsigned int key = share_name_hash(name);
81 hash_for_each_possible(shares_table, share, hlist, key) {
82 if (!strcmp(name, share->name))
88 static int parse_veto_list(struct ksmbd_share_config *share,
97 while (veto_list_sz > 0) {
98 struct ksmbd_veto_pattern *p;
100 sz = strlen(veto_list);
104 p = kzalloc(sizeof(struct ksmbd_veto_pattern), GFP_KERNEL);
108 p->pattern = kstrdup(veto_list, GFP_KERNEL);
114 list_add(&p->list, &share->veto_list);
117 veto_list_sz -= (sz + 1);
123 static struct ksmbd_share_config *share_config_request(struct unicode_map *um,
126 struct ksmbd_share_config_response *resp;
127 struct ksmbd_share_config *share = NULL;
128 struct ksmbd_share_config *lookup;
131 resp = ksmbd_ipc_share_config_request(name);
135 if (resp->flags == KSMBD_SHARE_FLAG_INVALID)
138 if (*resp->share_name) {
142 cf_resp_name = ksmbd_casefold_sharename(um, resp->share_name);
143 if (IS_ERR(cf_resp_name))
145 equal = !strcmp(cf_resp_name, name);
151 share = kzalloc(sizeof(struct ksmbd_share_config), GFP_KERNEL);
155 share->flags = resp->flags;
156 atomic_set(&share->refcount, 1);
157 INIT_LIST_HEAD(&share->veto_list);
158 share->name = kstrdup(name, GFP_KERNEL);
160 if (!test_share_config_flag(share, KSMBD_SHARE_FLAG_PIPE)) {
161 share->path = kstrdup(ksmbd_share_config_path(resp),
164 share->path_sz = strlen(share->path);
165 share->create_mask = resp->create_mask;
166 share->directory_mask = resp->directory_mask;
167 share->force_create_mode = resp->force_create_mode;
168 share->force_directory_mode = resp->force_directory_mode;
169 share->force_uid = resp->force_uid;
170 share->force_gid = resp->force_gid;
171 ret = parse_veto_list(share,
172 KSMBD_SHARE_CONFIG_VETO_LIST(resp),
174 if (!ret && share->path) {
175 ret = kern_path(share->path, 0, &share->vfs_path);
177 ksmbd_debug(SMB, "failed to access '%s'\n",
179 /* Avoid put_path() */
184 if (ret || !share->name) {
191 down_write(&shares_table_lock);
192 lookup = __share_lookup(name);
194 lookup = __get_share_config(lookup);
196 hash_add(shares_table, &share->hlist, share_name_hash(name));
201 up_write(&shares_table_lock);
208 struct ksmbd_share_config *ksmbd_share_config_get(struct unicode_map *um,
211 struct ksmbd_share_config *share;
213 down_read(&shares_table_lock);
214 share = __share_lookup(name);
216 share = __get_share_config(share);
217 up_read(&shares_table_lock);
221 return share_config_request(um, name);
224 bool ksmbd_share_veto_filename(struct ksmbd_share_config *share,
225 const char *filename)
227 struct ksmbd_veto_pattern *p;
229 list_for_each_entry(p, &share->veto_list, list) {
230 if (match_wildcard(p->pattern, filename))