Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Copyright (C) 2016 Namjae Jeon <linkinjeon@kernel.org> |
| 4 | * Copyright (C) 2018 Samsung Electronics Co., Ltd. |
| 5 | */ |
| 6 | |
| 7 | #include <linux/inetdevice.h> |
| 8 | #include <net/addrconf.h> |
| 9 | #include <linux/syscalls.h> |
| 10 | #include <linux/namei.h> |
| 11 | #include <linux/statfs.h> |
| 12 | #include <linux/ethtool.h> |
Namjae Jeon | e8c0619 | 2021-06-22 11:06:11 +0900 | [diff] [blame] | 13 | #include <linux/falloc.h> |
Namjae Jeon | 02655a7 | 2022-04-13 10:01:36 +0900 | [diff] [blame] | 14 | #include <linux/mount.h> |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 15 | |
| 16 | #include "glob.h" |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 17 | #include "smbfsctl.h" |
| 18 | #include "oplock.h" |
| 19 | #include "smbacl.h" |
| 20 | |
| 21 | #include "auth.h" |
| 22 | #include "asn1.h" |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 23 | #include "connection.h" |
| 24 | #include "transport_ipc.h" |
Hyunchul Lee | 03d8d4f | 2021-07-13 16:09:34 +0900 | [diff] [blame] | 25 | #include "transport_rdma.h" |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 26 | #include "vfs.h" |
| 27 | #include "vfs_cache.h" |
| 28 | #include "misc.h" |
| 29 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 30 | #include "server.h" |
| 31 | #include "smb_common.h" |
| 32 | #include "smbstatus.h" |
| 33 | #include "ksmbd_work.h" |
| 34 | #include "mgmt/user_config.h" |
| 35 | #include "mgmt/share_config.h" |
| 36 | #include "mgmt/tree_connect.h" |
| 37 | #include "mgmt/user_session.h" |
| 38 | #include "mgmt/ksmbd_ida.h" |
| 39 | #include "ndr.h" |
| 40 | |
| 41 | static void __wbuf(struct ksmbd_work *work, void **req, void **rsp) |
| 42 | { |
| 43 | if (work->next_smb2_rcv_hdr_off) { |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 44 | *req = ksmbd_req_buf_next(work); |
| 45 | *rsp = ksmbd_resp_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 46 | } else { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 47 | *req = smb2_get_msg(work->request_buf); |
| 48 | *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
| 52 | #define WORK_BUFFERS(w, rq, rs) __wbuf((w), (void **)&(rq), (void **)&(rs)) |
| 53 | |
| 54 | /** |
| 55 | * check_session_id() - check for valid session id in smb header |
| 56 | * @conn: connection instance |
| 57 | * @id: session id from smb header |
| 58 | * |
| 59 | * Return: 1 if valid session id, otherwise 0 |
| 60 | */ |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 61 | static inline bool check_session_id(struct ksmbd_conn *conn, u64 id) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 62 | { |
| 63 | struct ksmbd_session *sess; |
| 64 | |
| 65 | if (id == 0 || id == -1) |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 66 | return false; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 67 | |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 68 | sess = ksmbd_session_lookup_all(conn, id); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 69 | if (sess) |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 70 | return true; |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 71 | pr_err("Invalid user session id: %llu\n", id); |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 72 | return false; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 73 | } |
| 74 | |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 75 | struct channel *lookup_chann_list(struct ksmbd_session *sess, struct ksmbd_conn *conn) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 76 | { |
| 77 | struct channel *chann; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 78 | |
Namjae Jeon | 6f3d5ee | 2021-06-18 10:28:52 +0900 | [diff] [blame] | 79 | list_for_each_entry(chann, &sess->ksmbd_chann_list, chann_list) { |
Namjae Jeon | 560ac05 | 2021-06-22 16:16:45 +0900 | [diff] [blame] | 80 | if (chann->conn == conn) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 81 | return chann; |
| 82 | } |
| 83 | |
| 84 | return NULL; |
| 85 | } |
| 86 | |
| 87 | /** |
Namjae Jeon | 5ec3df8e | 2021-08-12 10:17:39 +0900 | [diff] [blame] | 88 | * smb2_get_ksmbd_tcon() - get tree connection information using a tree id. |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 89 | * @work: smb work |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 90 | * |
Namjae Jeon | 5ec3df8e | 2021-08-12 10:17:39 +0900 | [diff] [blame] | 91 | * Return: 0 if there is a tree connection matched or these are |
| 92 | * skipable commands, otherwise error |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 93 | */ |
| 94 | int smb2_get_ksmbd_tcon(struct ksmbd_work *work) |
| 95 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 96 | struct smb2_hdr *req_hdr = smb2_get_msg(work->request_buf); |
Ralph Boehme | 341b160 | 2021-10-05 07:03:40 +0200 | [diff] [blame] | 97 | unsigned int cmd = le16_to_cpu(req_hdr->Command); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 98 | int tree_id; |
| 99 | |
| 100 | work->tcon = NULL; |
Ralph Boehme | 341b160 | 2021-10-05 07:03:40 +0200 | [diff] [blame] | 101 | if (cmd == SMB2_TREE_CONNECT_HE || |
| 102 | cmd == SMB2_CANCEL_HE || |
| 103 | cmd == SMB2_LOGOFF_HE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 104 | ksmbd_debug(SMB, "skip to check tree connect request\n"); |
| 105 | return 0; |
| 106 | } |
| 107 | |
Namjae Jeon | 02b68b2 | 2021-04-01 17:45:33 +0900 | [diff] [blame] | 108 | if (xa_empty(&work->sess->tree_conns)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 109 | ksmbd_debug(SMB, "NO tree connected\n"); |
Namjae Jeon | c6ce2b5 | 2021-08-12 10:18:18 +0900 | [diff] [blame] | 110 | return -ENOENT; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | tree_id = le32_to_cpu(req_hdr->Id.SyncId.TreeId); |
| 114 | work->tcon = ksmbd_tree_conn_lookup(work->sess, tree_id); |
| 115 | if (!work->tcon) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 116 | pr_err("Invalid tid %d\n", tree_id); |
Namjae Jeon | c6ce2b5 | 2021-08-12 10:18:18 +0900 | [diff] [blame] | 117 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | return 1; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * smb2_set_err_rsp() - set error response code on smb response |
| 125 | * @work: smb work containing response buffer |
| 126 | */ |
| 127 | void smb2_set_err_rsp(struct ksmbd_work *work) |
| 128 | { |
| 129 | struct smb2_err_rsp *err_rsp; |
| 130 | |
| 131 | if (work->next_smb2_rcv_hdr_off) |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 132 | err_rsp = ksmbd_resp_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 133 | else |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 134 | err_rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 135 | |
| 136 | if (err_rsp->hdr.Status != STATUS_STOPPED_ON_SYMLINK) { |
| 137 | err_rsp->StructureSize = SMB2_ERROR_STRUCTURE_SIZE2_LE; |
| 138 | err_rsp->ErrorContextCount = 0; |
| 139 | err_rsp->Reserved = 0; |
| 140 | err_rsp->ByteCount = 0; |
| 141 | err_rsp->ErrorData[0] = 0; |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 142 | inc_rfc1001_len(work->response_buf, SMB2_ERROR_STRUCTURE_SIZE2); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * is_smb2_neg_cmd() - is it smb2 negotiation command |
| 148 | * @work: smb work containing smb header |
| 149 | * |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 150 | * Return: true if smb2 negotiation command, otherwise false |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 151 | */ |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 152 | bool is_smb2_neg_cmd(struct ksmbd_work *work) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 153 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 154 | struct smb2_hdr *hdr = smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 155 | |
| 156 | /* is it SMB2 header ? */ |
| 157 | if (hdr->ProtocolId != SMB2_PROTO_NUMBER) |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 158 | return false; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 159 | |
| 160 | /* make sure it is request not response message */ |
| 161 | if (hdr->Flags & SMB2_FLAGS_SERVER_TO_REDIR) |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 162 | return false; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 163 | |
| 164 | if (hdr->Command != SMB2_NEGOTIATE) |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 165 | return false; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 166 | |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 167 | return true; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /** |
| 171 | * is_smb2_rsp() - is it smb2 response |
| 172 | * @work: smb work containing smb response buffer |
| 173 | * |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 174 | * Return: true if smb2 response, otherwise false |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 175 | */ |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 176 | bool is_smb2_rsp(struct ksmbd_work *work) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 177 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 178 | struct smb2_hdr *hdr = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 179 | |
| 180 | /* is it SMB2 header ? */ |
| 181 | if (hdr->ProtocolId != SMB2_PROTO_NUMBER) |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 182 | return false; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 183 | |
| 184 | /* make sure it is response not request message */ |
| 185 | if (!(hdr->Flags & SMB2_FLAGS_SERVER_TO_REDIR)) |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 186 | return false; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 187 | |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 188 | return true; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /** |
| 192 | * get_smb2_cmd_val() - get smb command code from smb header |
| 193 | * @work: smb work containing smb request buffer |
| 194 | * |
| 195 | * Return: smb2 request command value |
| 196 | */ |
Namjae Jeon | fc2d1b5 | 2021-05-26 18:01:08 +0900 | [diff] [blame] | 197 | u16 get_smb2_cmd_val(struct ksmbd_work *work) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 198 | { |
| 199 | struct smb2_hdr *rcv_hdr; |
| 200 | |
| 201 | if (work->next_smb2_rcv_hdr_off) |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 202 | rcv_hdr = ksmbd_req_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 203 | else |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 204 | rcv_hdr = smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 205 | return le16_to_cpu(rcv_hdr->Command); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * set_smb2_rsp_status() - set error response code on smb2 header |
| 210 | * @work: smb work containing response buffer |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 211 | * @err: error response code |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 212 | */ |
| 213 | void set_smb2_rsp_status(struct ksmbd_work *work, __le32 err) |
| 214 | { |
| 215 | struct smb2_hdr *rsp_hdr; |
| 216 | |
| 217 | if (work->next_smb2_rcv_hdr_off) |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 218 | rsp_hdr = ksmbd_resp_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 219 | else |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 220 | rsp_hdr = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 221 | rsp_hdr->Status = err; |
| 222 | smb2_set_err_rsp(work); |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * init_smb2_neg_rsp() - initialize smb2 response for negotiate command |
| 227 | * @work: smb work containing smb request buffer |
| 228 | * |
| 229 | * smb2 negotiate response is sent in reply of smb1 negotiate command for |
| 230 | * dialect auto-negotiation. |
| 231 | */ |
| 232 | int init_smb2_neg_rsp(struct ksmbd_work *work) |
| 233 | { |
| 234 | struct smb2_hdr *rsp_hdr; |
| 235 | struct smb2_negotiate_rsp *rsp; |
| 236 | struct ksmbd_conn *conn = work->conn; |
| 237 | |
| 238 | if (conn->need_neg == false) |
| 239 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 240 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 241 | *(__be32 *)work->response_buf = |
| 242 | cpu_to_be32(conn->vals->header_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 243 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 244 | rsp_hdr = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 245 | memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 246 | rsp_hdr->ProtocolId = SMB2_PROTO_NUMBER; |
| 247 | rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE; |
| 248 | rsp_hdr->CreditRequest = cpu_to_le16(2); |
| 249 | rsp_hdr->Command = SMB2_NEGOTIATE; |
| 250 | rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR); |
| 251 | rsp_hdr->NextCommand = 0; |
| 252 | rsp_hdr->MessageId = 0; |
| 253 | rsp_hdr->Id.SyncId.ProcessId = 0; |
| 254 | rsp_hdr->Id.SyncId.TreeId = 0; |
| 255 | rsp_hdr->SessionId = 0; |
| 256 | memset(rsp_hdr->Signature, 0, 16); |
| 257 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 258 | rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 259 | |
| 260 | WARN_ON(ksmbd_conn_good(work)); |
| 261 | |
| 262 | rsp->StructureSize = cpu_to_le16(65); |
| 263 | ksmbd_debug(SMB, "conn->dialect 0x%x\n", conn->dialect); |
| 264 | rsp->DialectRevision = cpu_to_le16(conn->dialect); |
| 265 | /* Not setting conn guid rsp->ServerGUID, as it |
| 266 | * not used by client for identifying connection |
| 267 | */ |
| 268 | rsp->Capabilities = cpu_to_le32(conn->vals->capabilities); |
| 269 | /* Default Max Message Size till SMB2.0, 64K*/ |
| 270 | rsp->MaxTransactSize = cpu_to_le32(conn->vals->max_trans_size); |
| 271 | rsp->MaxReadSize = cpu_to_le32(conn->vals->max_read_size); |
| 272 | rsp->MaxWriteSize = cpu_to_le32(conn->vals->max_write_size); |
| 273 | |
| 274 | rsp->SystemTime = cpu_to_le64(ksmbd_systime()); |
| 275 | rsp->ServerStartTime = 0; |
| 276 | |
| 277 | rsp->SecurityBufferOffset = cpu_to_le16(128); |
| 278 | rsp->SecurityBufferLength = cpu_to_le16(AUTH_GSS_LENGTH); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 279 | ksmbd_copy_gss_neg_header((char *)(&rsp->hdr) + |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 280 | le16_to_cpu(rsp->SecurityBufferOffset)); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 281 | inc_rfc1001_len(work->response_buf, |
| 282 | sizeof(struct smb2_negotiate_rsp) - |
| 283 | sizeof(struct smb2_hdr) - sizeof(rsp->Buffer) + |
| 284 | AUTH_GSS_LENGTH); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 285 | rsp->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED_LE; |
| 286 | if (server_conf.signing == KSMBD_CONFIG_OPT_MANDATORY) |
| 287 | rsp->SecurityMode |= SMB2_NEGOTIATE_SIGNING_REQUIRED_LE; |
| 288 | conn->use_spnego = true; |
| 289 | |
| 290 | ksmbd_conn_set_need_negotiate(work); |
| 291 | return 0; |
| 292 | } |
| 293 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 294 | /** |
| 295 | * smb2_set_rsp_credits() - set number of credits in response buffer |
| 296 | * @work: smb work containing smb response buffer |
| 297 | */ |
| 298 | int smb2_set_rsp_credits(struct ksmbd_work *work) |
| 299 | { |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 300 | struct smb2_hdr *req_hdr = ksmbd_req_buf_next(work); |
| 301 | struct smb2_hdr *hdr = ksmbd_resp_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 302 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | 914d7e5 | 2021-12-29 23:10:03 +0900 | [diff] [blame] | 303 | unsigned short credits_requested, aux_max; |
Hyunchul Lee | bf8acc9 | 2021-10-07 16:26:58 +0900 | [diff] [blame] | 304 | unsigned short credit_charge, credits_granted = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 305 | |
Hyunchul Lee | bf8acc9 | 2021-10-07 16:26:58 +0900 | [diff] [blame] | 306 | if (work->send_no_response) |
| 307 | return 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 308 | |
Hyunchul Lee | bf8acc9 | 2021-10-07 16:26:58 +0900 | [diff] [blame] | 309 | hdr->CreditCharge = req_hdr->CreditCharge; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 310 | |
Namjae Jeon | 004443b | 2021-12-29 23:08:46 +0900 | [diff] [blame] | 311 | if (conn->total_credits > conn->vals->max_credits) { |
Hyunchul Lee | bf8acc9 | 2021-10-07 16:26:58 +0900 | [diff] [blame] | 312 | hdr->CreditRequest = 0; |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 313 | pr_err("Total credits overflow: %d\n", conn->total_credits); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 314 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 315 | } |
| 316 | |
Hyunchul Lee | bf8acc9 | 2021-10-07 16:26:58 +0900 | [diff] [blame] | 317 | credit_charge = max_t(unsigned short, |
| 318 | le16_to_cpu(req_hdr->CreditCharge), 1); |
Namjae Jeon | 914d7e5 | 2021-12-29 23:10:03 +0900 | [diff] [blame] | 319 | if (credit_charge > conn->total_credits) { |
| 320 | ksmbd_debug(SMB, "Insufficient credits granted, given: %u, granted: %u\n", |
| 321 | credit_charge, conn->total_credits); |
| 322 | return -EINVAL; |
| 323 | } |
| 324 | |
| 325 | conn->total_credits -= credit_charge; |
Namjae Jeon | b589f5d | 2021-12-31 09:26:25 +0900 | [diff] [blame] | 326 | conn->outstanding_credits -= credit_charge; |
Hyunchul Lee | bf8acc9 | 2021-10-07 16:26:58 +0900 | [diff] [blame] | 327 | credits_requested = max_t(unsigned short, |
| 328 | le16_to_cpu(req_hdr->CreditRequest), 1); |
| 329 | |
| 330 | /* according to smb2.credits smbtorture, Windows server |
| 331 | * 2016 or later grant up to 8192 credits at once. |
| 332 | * |
| 333 | * TODO: Need to adjuct CreditRequest value according to |
| 334 | * current cpu load |
| 335 | */ |
Hyunchul Lee | bf8acc9 | 2021-10-07 16:26:58 +0900 | [diff] [blame] | 336 | if (hdr->Command == SMB2_NEGOTIATE) |
Namjae Jeon | 914d7e5 | 2021-12-29 23:10:03 +0900 | [diff] [blame] | 337 | aux_max = 1; |
Hyunchul Lee | bf8acc9 | 2021-10-07 16:26:58 +0900 | [diff] [blame] | 338 | else |
Namjae Jeon | 004443b | 2021-12-29 23:08:46 +0900 | [diff] [blame] | 339 | aux_max = conn->vals->max_credits - credit_charge; |
Namjae Jeon | 914d7e5 | 2021-12-29 23:10:03 +0900 | [diff] [blame] | 340 | credits_granted = min_t(unsigned short, credits_requested, aux_max); |
Hyunchul Lee | bf8acc9 | 2021-10-07 16:26:58 +0900 | [diff] [blame] | 341 | |
Namjae Jeon | 004443b | 2021-12-29 23:08:46 +0900 | [diff] [blame] | 342 | if (conn->vals->max_credits - conn->total_credits < credits_granted) |
| 343 | credits_granted = conn->vals->max_credits - |
Hyunchul Lee | bf8acc9 | 2021-10-07 16:26:58 +0900 | [diff] [blame] | 344 | conn->total_credits; |
| 345 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 346 | conn->total_credits += credits_granted; |
| 347 | work->credits_granted += credits_granted; |
| 348 | |
| 349 | if (!req_hdr->NextCommand) { |
| 350 | /* Update CreditRequest in last request */ |
| 351 | hdr->CreditRequest = cpu_to_le16(work->credits_granted); |
| 352 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 353 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 354 | "credits: requested[%d] granted[%d] total_granted[%d]\n", |
| 355 | credits_requested, credits_granted, |
| 356 | conn->total_credits); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 357 | return 0; |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * init_chained_smb2_rsp() - initialize smb2 chained response |
| 362 | * @work: smb work containing smb response buffer |
| 363 | */ |
| 364 | static void init_chained_smb2_rsp(struct ksmbd_work *work) |
| 365 | { |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 366 | struct smb2_hdr *req = ksmbd_req_buf_next(work); |
| 367 | struct smb2_hdr *rsp = ksmbd_resp_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 368 | struct smb2_hdr *rsp_hdr; |
| 369 | struct smb2_hdr *rcv_hdr; |
| 370 | int next_hdr_offset = 0; |
| 371 | int len, new_len; |
| 372 | |
| 373 | /* Len of this response = updated RFC len - offset of previous cmd |
| 374 | * in the compound rsp |
| 375 | */ |
| 376 | |
| 377 | /* Storing the current local FID which may be needed by subsequent |
| 378 | * command in the compound request |
| 379 | */ |
| 380 | if (req->Command == SMB2_CREATE && rsp->Status == STATUS_SUCCESS) { |
Paulo Alcantara | 2d004c6 | 2022-03-21 13:08:26 -0300 | [diff] [blame] | 381 | work->compound_fid = ((struct smb2_create_rsp *)rsp)->VolatileFileId; |
| 382 | work->compound_pfid = ((struct smb2_create_rsp *)rsp)->PersistentFileId; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 383 | work->compound_sid = le64_to_cpu(rsp->SessionId); |
| 384 | } |
| 385 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 386 | len = get_rfc1002_len(work->response_buf) - work->next_smb2_rsp_hdr_off; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 387 | next_hdr_offset = le32_to_cpu(req->NextCommand); |
| 388 | |
| 389 | new_len = ALIGN(len, 8); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 390 | inc_rfc1001_len(work->response_buf, |
| 391 | sizeof(struct smb2_hdr) + new_len - len); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 392 | rsp->NextCommand = cpu_to_le32(new_len); |
| 393 | |
| 394 | work->next_smb2_rcv_hdr_off += next_hdr_offset; |
| 395 | work->next_smb2_rsp_hdr_off += new_len; |
| 396 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 397 | "Compound req new_len = %d rcv off = %d rsp off = %d\n", |
| 398 | new_len, work->next_smb2_rcv_hdr_off, |
| 399 | work->next_smb2_rsp_hdr_off); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 400 | |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 401 | rsp_hdr = ksmbd_resp_buf_next(work); |
| 402 | rcv_hdr = ksmbd_req_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 403 | |
| 404 | if (!(rcv_hdr->Flags & SMB2_FLAGS_RELATED_OPERATIONS)) { |
| 405 | ksmbd_debug(SMB, "related flag should be set\n"); |
| 406 | work->compound_fid = KSMBD_NO_FID; |
| 407 | work->compound_pfid = KSMBD_NO_FID; |
| 408 | } |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 409 | memset((char *)rsp_hdr, 0, sizeof(struct smb2_hdr) + 2); |
Namjae Jeon | 18a015b | 2021-09-22 21:00:57 +0900 | [diff] [blame] | 410 | rsp_hdr->ProtocolId = SMB2_PROTO_NUMBER; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 411 | rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE; |
| 412 | rsp_hdr->Command = rcv_hdr->Command; |
| 413 | |
| 414 | /* |
| 415 | * Message is response. We don't grant oplock yet. |
| 416 | */ |
| 417 | rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR | |
| 418 | SMB2_FLAGS_RELATED_OPERATIONS); |
| 419 | rsp_hdr->NextCommand = 0; |
| 420 | rsp_hdr->MessageId = rcv_hdr->MessageId; |
| 421 | rsp_hdr->Id.SyncId.ProcessId = rcv_hdr->Id.SyncId.ProcessId; |
| 422 | rsp_hdr->Id.SyncId.TreeId = rcv_hdr->Id.SyncId.TreeId; |
| 423 | rsp_hdr->SessionId = rcv_hdr->SessionId; |
| 424 | memcpy(rsp_hdr->Signature, rcv_hdr->Signature, 16); |
| 425 | } |
| 426 | |
| 427 | /** |
| 428 | * is_chained_smb2_message() - check for chained command |
| 429 | * @work: smb work containing smb request buffer |
| 430 | * |
| 431 | * Return: true if chained request, otherwise false |
| 432 | */ |
| 433 | bool is_chained_smb2_message(struct ksmbd_work *work) |
| 434 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 435 | struct smb2_hdr *hdr = smb2_get_msg(work->request_buf); |
Namjae Jeon | d72a9c1 | 2021-09-24 09:24:08 +0900 | [diff] [blame] | 436 | unsigned int len, next_cmd; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 437 | |
| 438 | if (hdr->ProtocolId != SMB2_PROTO_NUMBER) |
| 439 | return false; |
| 440 | |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 441 | hdr = ksmbd_req_buf_next(work); |
Namjae Jeon | d72a9c1 | 2021-09-24 09:24:08 +0900 | [diff] [blame] | 442 | next_cmd = le32_to_cpu(hdr->NextCommand); |
| 443 | if (next_cmd > 0) { |
| 444 | if ((u64)work->next_smb2_rcv_hdr_off + next_cmd + |
| 445 | __SMB2_HEADER_STRUCTURE_SIZE > |
| 446 | get_rfc1002_len(work->request_buf)) { |
| 447 | pr_err("next command(%u) offset exceeds smb msg size\n", |
| 448 | next_cmd); |
| 449 | return false; |
| 450 | } |
| 451 | |
Namjae Jeon | dbad630 | 2021-10-11 19:15:25 +0900 | [diff] [blame] | 452 | if ((u64)get_rfc1002_len(work->response_buf) + MAX_CIFS_SMALL_BUFFER_SIZE > |
| 453 | work->response_sz) { |
| 454 | pr_err("next response offset exceeds response buffer size\n"); |
| 455 | return false; |
| 456 | } |
| 457 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 458 | ksmbd_debug(SMB, "got SMB2 chained command\n"); |
| 459 | init_chained_smb2_rsp(work); |
| 460 | return true; |
| 461 | } else if (work->next_smb2_rcv_hdr_off) { |
| 462 | /* |
| 463 | * This is last request in chained command, |
| 464 | * align response to 8 byte |
| 465 | */ |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 466 | len = ALIGN(get_rfc1002_len(work->response_buf), 8); |
| 467 | len = len - get_rfc1002_len(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 468 | if (len) { |
| 469 | ksmbd_debug(SMB, "padding len %u\n", len); |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 470 | inc_rfc1001_len(work->response_buf, len); |
| 471 | if (work->aux_payload_sz) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 472 | work->aux_payload_sz += len; |
| 473 | } |
| 474 | } |
| 475 | return false; |
| 476 | } |
| 477 | |
| 478 | /** |
| 479 | * init_smb2_rsp_hdr() - initialize smb2 response |
| 480 | * @work: smb work containing smb request buffer |
| 481 | * |
| 482 | * Return: 0 |
| 483 | */ |
| 484 | int init_smb2_rsp_hdr(struct ksmbd_work *work) |
| 485 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 486 | struct smb2_hdr *rsp_hdr = smb2_get_msg(work->response_buf); |
| 487 | struct smb2_hdr *rcv_hdr = smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 488 | struct ksmbd_conn *conn = work->conn; |
| 489 | |
| 490 | memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 491 | *(__be32 *)work->response_buf = |
| 492 | cpu_to_be32(conn->vals->header_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 493 | rsp_hdr->ProtocolId = rcv_hdr->ProtocolId; |
| 494 | rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE; |
| 495 | rsp_hdr->Command = rcv_hdr->Command; |
| 496 | |
| 497 | /* |
| 498 | * Message is response. We don't grant oplock yet. |
| 499 | */ |
| 500 | rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR); |
| 501 | rsp_hdr->NextCommand = 0; |
| 502 | rsp_hdr->MessageId = rcv_hdr->MessageId; |
| 503 | rsp_hdr->Id.SyncId.ProcessId = rcv_hdr->Id.SyncId.ProcessId; |
| 504 | rsp_hdr->Id.SyncId.TreeId = rcv_hdr->Id.SyncId.TreeId; |
| 505 | rsp_hdr->SessionId = rcv_hdr->SessionId; |
| 506 | memcpy(rsp_hdr->Signature, rcv_hdr->Signature, 16); |
| 507 | |
| 508 | work->syncronous = true; |
| 509 | if (work->async_id) { |
Namjae Jeon | d40012a | 2021-04-13 13:06:30 +0900 | [diff] [blame] | 510 | ksmbd_release_id(&conn->async_ida, work->async_id); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 511 | work->async_id = 0; |
| 512 | } |
| 513 | |
| 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | /** |
| 518 | * smb2_allocate_rsp_buf() - allocate smb2 response buffer |
| 519 | * @work: smb work containing smb request buffer |
| 520 | * |
| 521 | * Return: 0 on success, otherwise -ENOMEM |
| 522 | */ |
| 523 | int smb2_allocate_rsp_buf(struct ksmbd_work *work) |
| 524 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 525 | struct smb2_hdr *hdr = smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 526 | size_t small_sz = MAX_CIFS_SMALL_BUFFER_SIZE; |
Namjae Jeon | 4bc5947 | 2021-10-15 17:14:02 +0900 | [diff] [blame] | 527 | size_t large_sz = small_sz + work->conn->vals->max_trans_size; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 528 | size_t sz = small_sz; |
| 529 | int cmd = le16_to_cpu(hdr->Command); |
| 530 | |
Namjae Jeon | c30f4eb | 2021-06-18 10:17:37 +0900 | [diff] [blame] | 531 | if (cmd == SMB2_IOCTL_HE || cmd == SMB2_QUERY_DIRECTORY_HE) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 532 | sz = large_sz; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 533 | |
| 534 | if (cmd == SMB2_QUERY_INFO_HE) { |
| 535 | struct smb2_query_info_req *req; |
| 536 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 537 | req = smb2_get_msg(work->request_buf); |
Namjae Jeon | 8f05411 | 2022-08-02 07:28:51 +0900 | [diff] [blame] | 538 | if ((req->InfoType == SMB2_O_INFO_FILE && |
| 539 | (req->FileInfoClass == FILE_FULL_EA_INFORMATION || |
| 540 | req->FileInfoClass == FILE_ALL_INFORMATION)) || |
| 541 | req->InfoType == SMB2_O_INFO_SECURITY) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 542 | sz = large_sz; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | /* allocate large response buf for chained commands */ |
| 546 | if (le32_to_cpu(hdr->NextCommand) > 0) |
| 547 | sz = large_sz; |
| 548 | |
Namjae Jeon | c30f4eb | 2021-06-18 10:17:37 +0900 | [diff] [blame] | 549 | work->response_buf = kvmalloc(sz, GFP_KERNEL | __GFP_ZERO); |
Namjae Jeon | 63c454f | 2021-04-20 14:24:28 +0900 | [diff] [blame] | 550 | if (!work->response_buf) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 551 | return -ENOMEM; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 552 | |
| 553 | work->response_sz = sz; |
| 554 | return 0; |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * smb2_check_user_session() - check for valid session for a user |
| 559 | * @work: smb work containing smb request buffer |
| 560 | * |
| 561 | * Return: 0 on success, otherwise error |
| 562 | */ |
| 563 | int smb2_check_user_session(struct ksmbd_work *work) |
| 564 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 565 | struct smb2_hdr *req_hdr = smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 566 | struct ksmbd_conn *conn = work->conn; |
| 567 | unsigned int cmd = conn->ops->get_cmd_val(work); |
| 568 | unsigned long long sess_id; |
| 569 | |
| 570 | work->sess = NULL; |
| 571 | /* |
| 572 | * SMB2_ECHO, SMB2_NEGOTIATE, SMB2_SESSION_SETUP command do not |
| 573 | * require a session id, so no need to validate user session's for |
| 574 | * these commands. |
| 575 | */ |
| 576 | if (cmd == SMB2_ECHO_HE || cmd == SMB2_NEGOTIATE_HE || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 577 | cmd == SMB2_SESSION_SETUP_HE) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 578 | return 0; |
| 579 | |
| 580 | if (!ksmbd_conn_good(work)) |
| 581 | return -EINVAL; |
| 582 | |
| 583 | sess_id = le64_to_cpu(req_hdr->SessionId); |
| 584 | /* Check for validity of user session */ |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 585 | work->sess = ksmbd_session_lookup_all(conn, sess_id); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 586 | if (work->sess) |
| 587 | return 1; |
| 588 | ksmbd_debug(SMB, "Invalid user session, Uid %llu\n", sess_id); |
| 589 | return -EINVAL; |
| 590 | } |
| 591 | |
Namjae Jeon | e4d3e6b | 2022-07-22 10:15:10 +0900 | [diff] [blame] | 592 | static void destroy_previous_session(struct ksmbd_conn *conn, |
| 593 | struct ksmbd_user *user, u64 id) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 594 | { |
| 595 | struct ksmbd_session *prev_sess = ksmbd_session_lookup_slowpath(id); |
| 596 | struct ksmbd_user *prev_user; |
Namjae Jeon | 17ea92a | 2022-07-22 10:17:06 +0900 | [diff] [blame] | 597 | struct channel *chann; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 598 | |
| 599 | if (!prev_sess) |
| 600 | return; |
| 601 | |
| 602 | prev_user = prev_sess->user; |
| 603 | |
Marios Makassikis | 1fca803 | 2021-05-06 11:41:54 +0900 | [diff] [blame] | 604 | if (!prev_user || |
| 605 | strcmp(user->name, prev_user->name) || |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 606 | user->passkey_sz != prev_user->passkey_sz || |
Namjae Jeon | af7c39d | 2022-07-25 13:36:52 +0900 | [diff] [blame] | 607 | memcmp(user->passkey, prev_user->passkey, user->passkey_sz)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 608 | return; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 609 | |
Namjae Jeon | 17ea92a | 2022-07-22 10:17:06 +0900 | [diff] [blame] | 610 | prev_sess->state = SMB2_SESSION_EXPIRED; |
| 611 | write_lock(&prev_sess->chann_lock); |
| 612 | list_for_each_entry(chann, &prev_sess->ksmbd_chann_list, chann_list) |
| 613 | chann->conn->status = KSMBD_SESS_EXITING; |
| 614 | write_unlock(&prev_sess->chann_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | /** |
| 618 | * smb2_get_name() - get filename string from on the wire smb format |
| 619 | * @src: source buffer |
| 620 | * @maxlen: maxlen of source string |
Yang Li | d4eeb82 | 2021-12-21 20:48:57 +0900 | [diff] [blame] | 621 | * @local_nls: nls_table pointer |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 622 | * |
| 623 | * Return: matching converted filename on success, otherwise error ptr |
| 624 | */ |
| 625 | static char * |
Marios Makassikis | 80917f1 | 2021-12-01 21:41:19 +0100 | [diff] [blame] | 626 | smb2_get_name(const char *src, const int maxlen, struct nls_table *local_nls) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 627 | { |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 628 | char *name; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 629 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 630 | name = smb_strndup_from_utf16(src, maxlen, 1, local_nls); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 631 | if (IS_ERR(name)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 632 | pr_err("failed to get name %ld\n", PTR_ERR(name)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 633 | return name; |
| 634 | } |
| 635 | |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 636 | ksmbd_conv_path_to_unix(name); |
| 637 | ksmbd_strip_last_slash(name); |
| 638 | return name; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 639 | } |
| 640 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 641 | int setup_async_work(struct ksmbd_work *work, void (*fn)(void **), void **arg) |
| 642 | { |
| 643 | struct smb2_hdr *rsp_hdr; |
| 644 | struct ksmbd_conn *conn = work->conn; |
| 645 | int id; |
| 646 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 647 | rsp_hdr = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 648 | rsp_hdr->Flags |= SMB2_FLAGS_ASYNC_COMMAND; |
| 649 | |
Namjae Jeon | d40012a | 2021-04-13 13:06:30 +0900 | [diff] [blame] | 650 | id = ksmbd_acquire_async_msg_id(&conn->async_ida); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 651 | if (id < 0) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 652 | pr_err("Failed to alloc async message id\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 653 | return id; |
| 654 | } |
| 655 | work->syncronous = false; |
| 656 | work->async_id = id; |
| 657 | rsp_hdr->Id.AsyncId = cpu_to_le64(id); |
| 658 | |
| 659 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 660 | "Send interim Response to inform async request id : %d\n", |
| 661 | work->async_id); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 662 | |
| 663 | work->cancel_fn = fn; |
| 664 | work->cancel_argv = arg; |
| 665 | |
Namjae Jeon | 6c4e675 | 2021-06-07 09:08:45 +0900 | [diff] [blame] | 666 | if (list_empty(&work->async_request_entry)) { |
| 667 | spin_lock(&conn->request_lock); |
| 668 | list_add_tail(&work->async_request_entry, &conn->async_requests); |
| 669 | spin_unlock(&conn->request_lock); |
| 670 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 671 | |
| 672 | return 0; |
| 673 | } |
| 674 | |
| 675 | void smb2_send_interim_resp(struct ksmbd_work *work, __le32 status) |
| 676 | { |
| 677 | struct smb2_hdr *rsp_hdr; |
| 678 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 679 | rsp_hdr = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 680 | smb2_set_err_rsp(work); |
| 681 | rsp_hdr->Status = status; |
| 682 | |
| 683 | work->multiRsp = 1; |
| 684 | ksmbd_conn_write(work); |
| 685 | rsp_hdr->Status = 0; |
| 686 | work->multiRsp = 0; |
| 687 | } |
| 688 | |
| 689 | static __le32 smb2_get_reparse_tag_special_file(umode_t mode) |
| 690 | { |
| 691 | if (S_ISDIR(mode) || S_ISREG(mode)) |
| 692 | return 0; |
| 693 | |
| 694 | if (S_ISLNK(mode)) |
| 695 | return IO_REPARSE_TAG_LX_SYMLINK_LE; |
| 696 | else if (S_ISFIFO(mode)) |
| 697 | return IO_REPARSE_TAG_LX_FIFO_LE; |
| 698 | else if (S_ISSOCK(mode)) |
| 699 | return IO_REPARSE_TAG_AF_UNIX_LE; |
| 700 | else if (S_ISCHR(mode)) |
| 701 | return IO_REPARSE_TAG_LX_CHR_LE; |
| 702 | else if (S_ISBLK(mode)) |
| 703 | return IO_REPARSE_TAG_LX_BLK_LE; |
| 704 | |
| 705 | return 0; |
| 706 | } |
| 707 | |
| 708 | /** |
| 709 | * smb2_get_dos_mode() - get file mode in dos format from unix mode |
| 710 | * @stat: kstat containing file mode |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 711 | * @attribute: attribute flags |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 712 | * |
| 713 | * Return: converted dos mode |
| 714 | */ |
| 715 | static int smb2_get_dos_mode(struct kstat *stat, int attribute) |
| 716 | { |
| 717 | int attr = 0; |
| 718 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 719 | if (S_ISDIR(stat->mode)) { |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 720 | attr = FILE_ATTRIBUTE_DIRECTORY | |
| 721 | (attribute & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 722 | } else { |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 723 | attr = (attribute & 0x00005137) | FILE_ATTRIBUTE_ARCHIVE; |
| 724 | attr &= ~(FILE_ATTRIBUTE_DIRECTORY); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 725 | if (S_ISREG(stat->mode) && (server_conf.share_fake_fscaps & |
| 726 | FILE_SUPPORTS_SPARSE_FILES)) |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 727 | attr |= FILE_ATTRIBUTE_SPARSE_FILE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 728 | |
| 729 | if (smb2_get_reparse_tag_special_file(stat->mode)) |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 730 | attr |= FILE_ATTRIBUTE_REPARSE_POINT; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | return attr; |
| 734 | } |
| 735 | |
| 736 | static void build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 737 | __le16 hash_id) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 738 | { |
| 739 | pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES; |
| 740 | pneg_ctxt->DataLength = cpu_to_le16(38); |
| 741 | pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1); |
| 742 | pneg_ctxt->Reserved = cpu_to_le32(0); |
| 743 | pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE); |
| 744 | get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE); |
| 745 | pneg_ctxt->HashAlgorithms = hash_id; |
| 746 | } |
| 747 | |
| 748 | static void build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 749 | __le16 cipher_type) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 750 | { |
| 751 | pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES; |
| 752 | pneg_ctxt->DataLength = cpu_to_le16(4); |
| 753 | pneg_ctxt->Reserved = cpu_to_le32(0); |
| 754 | pneg_ctxt->CipherCount = cpu_to_le16(1); |
| 755 | pneg_ctxt->Ciphers[0] = cipher_type; |
| 756 | } |
| 757 | |
Ronnie Sahlberg | d6c9ad23b | 2021-11-03 08:44:38 +0900 | [diff] [blame] | 758 | static void build_compression_ctxt(struct smb2_compression_capabilities_context *pneg_ctxt, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 759 | __le16 comp_algo) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 760 | { |
| 761 | pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES; |
| 762 | pneg_ctxt->DataLength = |
Ronnie Sahlberg | d6c9ad23b | 2021-11-03 08:44:38 +0900 | [diff] [blame] | 763 | cpu_to_le16(sizeof(struct smb2_compression_capabilities_context) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 764 | - sizeof(struct smb2_neg_context)); |
| 765 | pneg_ctxt->Reserved = cpu_to_le32(0); |
| 766 | pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(1); |
Ronnie Sahlberg | d6c9ad23b | 2021-11-03 08:44:38 +0900 | [diff] [blame] | 767 | pneg_ctxt->Flags = cpu_to_le32(0); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 768 | pneg_ctxt->CompressionAlgorithms[0] = comp_algo; |
| 769 | } |
| 770 | |
Namjae Jeon | 378087c | 2021-07-21 10:05:53 +0900 | [diff] [blame] | 771 | static void build_sign_cap_ctxt(struct smb2_signing_capabilities *pneg_ctxt, |
| 772 | __le16 sign_algo) |
| 773 | { |
| 774 | pneg_ctxt->ContextType = SMB2_SIGNING_CAPABILITIES; |
| 775 | pneg_ctxt->DataLength = |
| 776 | cpu_to_le16((sizeof(struct smb2_signing_capabilities) + 2) |
| 777 | - sizeof(struct smb2_neg_context)); |
| 778 | pneg_ctxt->Reserved = cpu_to_le32(0); |
| 779 | pneg_ctxt->SigningAlgorithmCount = cpu_to_le16(1); |
| 780 | pneg_ctxt->SigningAlgorithms[0] = sign_algo; |
| 781 | } |
| 782 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 783 | static void build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 784 | { |
| 785 | pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE; |
| 786 | pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN); |
| 787 | /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */ |
| 788 | pneg_ctxt->Name[0] = 0x93; |
| 789 | pneg_ctxt->Name[1] = 0xAD; |
| 790 | pneg_ctxt->Name[2] = 0x25; |
| 791 | pneg_ctxt->Name[3] = 0x50; |
| 792 | pneg_ctxt->Name[4] = 0x9C; |
| 793 | pneg_ctxt->Name[5] = 0xB4; |
| 794 | pneg_ctxt->Name[6] = 0x11; |
| 795 | pneg_ctxt->Name[7] = 0xE7; |
| 796 | pneg_ctxt->Name[8] = 0xB4; |
| 797 | pneg_ctxt->Name[9] = 0x23; |
| 798 | pneg_ctxt->Name[10] = 0x83; |
| 799 | pneg_ctxt->Name[11] = 0xDE; |
| 800 | pneg_ctxt->Name[12] = 0x96; |
| 801 | pneg_ctxt->Name[13] = 0x8B; |
| 802 | pneg_ctxt->Name[14] = 0xCD; |
| 803 | pneg_ctxt->Name[15] = 0x7C; |
| 804 | } |
| 805 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 806 | static void assemble_neg_contexts(struct ksmbd_conn *conn, |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 807 | struct smb2_negotiate_rsp *rsp, |
| 808 | void *smb2_buf_len) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 809 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 810 | char *pneg_ctxt = (char *)rsp + |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 811 | le32_to_cpu(rsp->NegotiateContextOffset); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 812 | int neg_ctxt_cnt = 1; |
| 813 | int ctxt_size; |
| 814 | |
| 815 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 816 | "assemble SMB2_PREAUTH_INTEGRITY_CAPABILITIES context\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 817 | build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 818 | conn->preauth_info->Preauth_HashId); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 819 | rsp->NegotiateContextCount = cpu_to_le16(neg_ctxt_cnt); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 820 | inc_rfc1001_len(smb2_buf_len, AUTH_GSS_PADDING); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 821 | ctxt_size = sizeof(struct smb2_preauth_neg_context); |
| 822 | /* Round to 8 byte boundary */ |
| 823 | pneg_ctxt += round_up(sizeof(struct smb2_preauth_neg_context), 8); |
| 824 | |
| 825 | if (conn->cipher_type) { |
| 826 | ctxt_size = round_up(ctxt_size, 8); |
| 827 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 828 | "assemble SMB2_ENCRYPTION_CAPABILITIES context\n"); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 829 | build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 830 | conn->cipher_type); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 831 | rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt); |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 832 | ctxt_size += sizeof(struct smb2_encryption_neg_context) + 2; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 833 | /* Round to 8 byte boundary */ |
| 834 | pneg_ctxt += |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 835 | round_up(sizeof(struct smb2_encryption_neg_context) + 2, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 836 | 8); |
| 837 | } |
| 838 | |
| 839 | if (conn->compress_algorithm) { |
| 840 | ctxt_size = round_up(ctxt_size, 8); |
| 841 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 842 | "assemble SMB2_COMPRESSION_CAPABILITIES context\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 843 | /* Temporarily set to SMB3_COMPRESS_NONE */ |
Ronnie Sahlberg | d6c9ad23b | 2021-11-03 08:44:38 +0900 | [diff] [blame] | 844 | build_compression_ctxt((struct smb2_compression_capabilities_context *)pneg_ctxt, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 845 | conn->compress_algorithm); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 846 | rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt); |
Ronnie Sahlberg | d6c9ad23b | 2021-11-03 08:44:38 +0900 | [diff] [blame] | 847 | ctxt_size += sizeof(struct smb2_compression_capabilities_context) + 2; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 848 | /* Round to 8 byte boundary */ |
Ronnie Sahlberg | d6c9ad23b | 2021-11-03 08:44:38 +0900 | [diff] [blame] | 849 | pneg_ctxt += round_up(sizeof(struct smb2_compression_capabilities_context) + 2, |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 850 | 8); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | if (conn->posix_ext_supported) { |
| 854 | ctxt_size = round_up(ctxt_size, 8); |
| 855 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 856 | "assemble SMB2_POSIX_EXTENSIONS_AVAILABLE context\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 857 | build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt); |
| 858 | rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt); |
| 859 | ctxt_size += sizeof(struct smb2_posix_neg_context); |
Namjae Jeon | 378087c | 2021-07-21 10:05:53 +0900 | [diff] [blame] | 860 | /* Round to 8 byte boundary */ |
| 861 | pneg_ctxt += round_up(sizeof(struct smb2_posix_neg_context), 8); |
| 862 | } |
| 863 | |
| 864 | if (conn->signing_negotiated) { |
| 865 | ctxt_size = round_up(ctxt_size, 8); |
| 866 | ksmbd_debug(SMB, |
| 867 | "assemble SMB2_SIGNING_CAPABILITIES context\n"); |
| 868 | build_sign_cap_ctxt((struct smb2_signing_capabilities *)pneg_ctxt, |
| 869 | conn->signing_algorithm); |
| 870 | rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt); |
| 871 | ctxt_size += sizeof(struct smb2_signing_capabilities) + 2; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 872 | } |
| 873 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 874 | inc_rfc1001_len(smb2_buf_len, ctxt_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 875 | } |
| 876 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 877 | static __le32 decode_preauth_ctxt(struct ksmbd_conn *conn, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 878 | struct smb2_preauth_neg_context *pneg_ctxt) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 879 | { |
| 880 | __le32 err = STATUS_NO_PREAUTH_INTEGRITY_HASH_OVERLAP; |
| 881 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 882 | if (pneg_ctxt->HashAlgorithms == SMB2_PREAUTH_INTEGRITY_SHA512) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 883 | conn->preauth_info->Preauth_HashId = |
| 884 | SMB2_PREAUTH_INTEGRITY_SHA512; |
| 885 | err = STATUS_SUCCESS; |
| 886 | } |
| 887 | |
| 888 | return err; |
| 889 | } |
| 890 | |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 891 | static void decode_encrypt_ctxt(struct ksmbd_conn *conn, |
| 892 | struct smb2_encryption_neg_context *pneg_ctxt, |
| 893 | int len_of_ctxts) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 894 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 895 | int cph_cnt = le16_to_cpu(pneg_ctxt->CipherCount); |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 896 | int i, cphs_size = cph_cnt * sizeof(__le16); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 897 | |
| 898 | conn->cipher_type = 0; |
| 899 | |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 900 | if (sizeof(struct smb2_encryption_neg_context) + cphs_size > |
| 901 | len_of_ctxts) { |
| 902 | pr_err("Invalid cipher count(%d)\n", cph_cnt); |
| 903 | return; |
| 904 | } |
| 905 | |
Namjae Jeon | 37ba7b0 | 2022-10-29 00:01:38 +0900 | [diff] [blame] | 906 | if (server_conf.flags & KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION_OFF) |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 907 | return; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 908 | |
| 909 | for (i = 0; i < cph_cnt; i++) { |
| 910 | if (pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES128_GCM || |
Namjae Jeon | 5a0ca77 | 2021-05-06 11:43:37 +0900 | [diff] [blame] | 911 | pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES128_CCM || |
| 912 | pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES256_CCM || |
| 913 | pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES256_GCM) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 914 | ksmbd_debug(SMB, "Cipher ID = 0x%x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 915 | pneg_ctxt->Ciphers[i]); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 916 | conn->cipher_type = pneg_ctxt->Ciphers[i]; |
| 917 | break; |
| 918 | } |
| 919 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 920 | } |
| 921 | |
Marcos Del Sol Vives | 83912d6 | 2021-12-16 11:37:22 +0100 | [diff] [blame] | 922 | /** |
| 923 | * smb3_encryption_negotiated() - checks if server and client agreed on enabling encryption |
| 924 | * @conn: smb connection |
| 925 | * |
| 926 | * Return: true if connection should be encrypted, else false |
| 927 | */ |
Namjae Jeon | 5bedae9 | 2022-09-22 23:37:41 +0900 | [diff] [blame] | 928 | bool smb3_encryption_negotiated(struct ksmbd_conn *conn) |
Marcos Del Sol Vives | 83912d6 | 2021-12-16 11:37:22 +0100 | [diff] [blame] | 929 | { |
| 930 | if (!conn->ops->generate_encryptionkey) |
| 931 | return false; |
| 932 | |
| 933 | /* |
| 934 | * SMB 3.0 and 3.0.2 dialects use the SMB2_GLOBAL_CAP_ENCRYPTION flag. |
| 935 | * SMB 3.1.1 uses the cipher_type field. |
| 936 | */ |
| 937 | return (conn->vals->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION) || |
| 938 | conn->cipher_type; |
| 939 | } |
| 940 | |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 941 | static void decode_compress_ctxt(struct ksmbd_conn *conn, |
Ronnie Sahlberg | d6c9ad23b | 2021-11-03 08:44:38 +0900 | [diff] [blame] | 942 | struct smb2_compression_capabilities_context *pneg_ctxt) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 943 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 944 | conn->compress_algorithm = SMB3_COMPRESS_NONE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 945 | } |
| 946 | |
Namjae Jeon | 378087c | 2021-07-21 10:05:53 +0900 | [diff] [blame] | 947 | static void decode_sign_cap_ctxt(struct ksmbd_conn *conn, |
| 948 | struct smb2_signing_capabilities *pneg_ctxt, |
| 949 | int len_of_ctxts) |
| 950 | { |
| 951 | int sign_algo_cnt = le16_to_cpu(pneg_ctxt->SigningAlgorithmCount); |
| 952 | int i, sign_alos_size = sign_algo_cnt * sizeof(__le16); |
| 953 | |
| 954 | conn->signing_negotiated = false; |
| 955 | |
| 956 | if (sizeof(struct smb2_signing_capabilities) + sign_alos_size > |
| 957 | len_of_ctxts) { |
| 958 | pr_err("Invalid signing algorithm count(%d)\n", sign_algo_cnt); |
| 959 | return; |
| 960 | } |
| 961 | |
| 962 | for (i = 0; i < sign_algo_cnt; i++) { |
Ronnie Sahlberg | d6c9ad23b | 2021-11-03 08:44:38 +0900 | [diff] [blame] | 963 | if (pneg_ctxt->SigningAlgorithms[i] == SIGNING_ALG_HMAC_SHA256_LE || |
| 964 | pneg_ctxt->SigningAlgorithms[i] == SIGNING_ALG_AES_CMAC_LE) { |
Namjae Jeon | 378087c | 2021-07-21 10:05:53 +0900 | [diff] [blame] | 965 | ksmbd_debug(SMB, "Signing Algorithm ID = 0x%x\n", |
| 966 | pneg_ctxt->SigningAlgorithms[i]); |
| 967 | conn->signing_negotiated = true; |
| 968 | conn->signing_algorithm = |
| 969 | pneg_ctxt->SigningAlgorithms[i]; |
| 970 | break; |
| 971 | } |
| 972 | } |
| 973 | } |
| 974 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 975 | static __le32 deassemble_neg_contexts(struct ksmbd_conn *conn, |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 976 | struct smb2_negotiate_req *req, |
| 977 | int len_of_smb) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 978 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 979 | /* +4 is to account for the RFC1001 len field */ |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 980 | struct smb2_neg_context *pctx = (struct smb2_neg_context *)req; |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 981 | int i = 0, len_of_ctxts; |
| 982 | int offset = le32_to_cpu(req->NegotiateContextOffset); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 983 | int neg_ctxt_cnt = le16_to_cpu(req->NegotiateContextCount); |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 984 | __le32 status = STATUS_INVALID_PARAMETER; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 985 | |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 986 | ksmbd_debug(SMB, "decoding %d negotiate contexts\n", neg_ctxt_cnt); |
| 987 | if (len_of_smb <= offset) { |
| 988 | ksmbd_debug(SMB, "Invalid response: negotiate context offset\n"); |
| 989 | return status; |
| 990 | } |
| 991 | |
| 992 | len_of_ctxts = len_of_smb - offset; |
| 993 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 994 | while (i++ < neg_ctxt_cnt) { |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 995 | int clen; |
| 996 | |
| 997 | /* check that offset is not beyond end of SMB */ |
| 998 | if (len_of_ctxts == 0) |
| 999 | break; |
| 1000 | |
| 1001 | if (len_of_ctxts < sizeof(struct smb2_neg_context)) |
| 1002 | break; |
| 1003 | |
| 1004 | pctx = (struct smb2_neg_context *)((char *)pctx + offset); |
| 1005 | clen = le16_to_cpu(pctx->DataLength); |
| 1006 | if (clen + sizeof(struct smb2_neg_context) > len_of_ctxts) |
| 1007 | break; |
| 1008 | |
| 1009 | if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1010 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1011 | "deassemble SMB2_PREAUTH_INTEGRITY_CAPABILITIES context\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1012 | if (conn->preauth_info->Preauth_HashId) |
| 1013 | break; |
| 1014 | |
| 1015 | status = decode_preauth_ctxt(conn, |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 1016 | (struct smb2_preauth_neg_context *)pctx); |
| 1017 | if (status != STATUS_SUCCESS) |
| 1018 | break; |
| 1019 | } else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1020 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1021 | "deassemble SMB2_ENCRYPTION_CAPABILITIES context\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1022 | if (conn->cipher_type) |
| 1023 | break; |
| 1024 | |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 1025 | decode_encrypt_ctxt(conn, |
| 1026 | (struct smb2_encryption_neg_context *)pctx, |
| 1027 | len_of_ctxts); |
| 1028 | } else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1029 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1030 | "deassemble SMB2_COMPRESSION_CAPABILITIES context\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1031 | if (conn->compress_algorithm) |
| 1032 | break; |
| 1033 | |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 1034 | decode_compress_ctxt(conn, |
Ronnie Sahlberg | d6c9ad23b | 2021-11-03 08:44:38 +0900 | [diff] [blame] | 1035 | (struct smb2_compression_capabilities_context *)pctx); |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 1036 | } else if (pctx->ContextType == SMB2_NETNAME_NEGOTIATE_CONTEXT_ID) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1037 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1038 | "deassemble SMB2_NETNAME_NEGOTIATE_CONTEXT_ID context\n"); |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 1039 | } else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1040 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1041 | "deassemble SMB2_POSIX_EXTENSIONS_AVAILABLE context\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1042 | conn->posix_ext_supported = true; |
Namjae Jeon | 378087c | 2021-07-21 10:05:53 +0900 | [diff] [blame] | 1043 | } else if (pctx->ContextType == SMB2_SIGNING_CAPABILITIES) { |
| 1044 | ksmbd_debug(SMB, |
| 1045 | "deassemble SMB2_SIGNING_CAPABILITIES context\n"); |
| 1046 | decode_sign_cap_ctxt(conn, |
| 1047 | (struct smb2_signing_capabilities *)pctx, |
| 1048 | len_of_ctxts); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1049 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1050 | |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 1051 | /* offsets must be 8 byte aligned */ |
| 1052 | clen = (clen + 7) & ~0x7; |
| 1053 | offset = clen + sizeof(struct smb2_neg_context); |
| 1054 | len_of_ctxts -= clen + sizeof(struct smb2_neg_context); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1055 | } |
| 1056 | return status; |
| 1057 | } |
| 1058 | |
| 1059 | /** |
| 1060 | * smb2_handle_negotiate() - handler for smb2 negotiate command |
| 1061 | * @work: smb work containing smb request buffer |
| 1062 | * |
| 1063 | * Return: 0 |
| 1064 | */ |
| 1065 | int smb2_handle_negotiate(struct ksmbd_work *work) |
| 1066 | { |
| 1067 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1068 | struct smb2_negotiate_req *req = smb2_get_msg(work->request_buf); |
| 1069 | struct smb2_negotiate_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1070 | int rc = 0; |
Namjae Jeon | 442ff9e | 2021-09-29 15:44:32 +0900 | [diff] [blame] | 1071 | unsigned int smb2_buf_len, smb2_neg_size; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1072 | __le32 status; |
| 1073 | |
| 1074 | ksmbd_debug(SMB, "Received negotiate request\n"); |
| 1075 | conn->need_neg = false; |
| 1076 | if (ksmbd_conn_good(work)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 1077 | pr_err("conn->tcp_status is already in CifsGood State\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1078 | work->send_no_response = 1; |
| 1079 | return rc; |
| 1080 | } |
| 1081 | |
| 1082 | if (req->DialectCount == 0) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 1083 | pr_err("malformed packet\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1084 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 1085 | rc = -EINVAL; |
| 1086 | goto err_out; |
| 1087 | } |
| 1088 | |
Namjae Jeon | 442ff9e | 2021-09-29 15:44:32 +0900 | [diff] [blame] | 1089 | smb2_buf_len = get_rfc1002_len(work->request_buf); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1090 | smb2_neg_size = offsetof(struct smb2_negotiate_req, Dialects); |
Namjae Jeon | 442ff9e | 2021-09-29 15:44:32 +0900 | [diff] [blame] | 1091 | if (smb2_neg_size > smb2_buf_len) { |
| 1092 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 1093 | rc = -EINVAL; |
| 1094 | goto err_out; |
| 1095 | } |
| 1096 | |
| 1097 | if (conn->dialect == SMB311_PROT_ID) { |
| 1098 | unsigned int nego_ctxt_off = le32_to_cpu(req->NegotiateContextOffset); |
| 1099 | |
| 1100 | if (smb2_buf_len < nego_ctxt_off) { |
| 1101 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 1102 | rc = -EINVAL; |
| 1103 | goto err_out; |
| 1104 | } |
| 1105 | |
| 1106 | if (smb2_neg_size > nego_ctxt_off) { |
| 1107 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 1108 | rc = -EINVAL; |
| 1109 | goto err_out; |
| 1110 | } |
| 1111 | |
| 1112 | if (smb2_neg_size + le16_to_cpu(req->DialectCount) * sizeof(__le16) > |
| 1113 | nego_ctxt_off) { |
| 1114 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 1115 | rc = -EINVAL; |
| 1116 | goto err_out; |
| 1117 | } |
| 1118 | } else { |
| 1119 | if (smb2_neg_size + le16_to_cpu(req->DialectCount) * sizeof(__le16) > |
| 1120 | smb2_buf_len) { |
| 1121 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 1122 | rc = -EINVAL; |
| 1123 | goto err_out; |
| 1124 | } |
| 1125 | } |
| 1126 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1127 | conn->cli_cap = le32_to_cpu(req->Capabilities); |
| 1128 | switch (conn->dialect) { |
| 1129 | case SMB311_PROT_ID: |
| 1130 | conn->preauth_info = |
| 1131 | kzalloc(sizeof(struct preauth_integrity_info), |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1132 | GFP_KERNEL); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1133 | if (!conn->preauth_info) { |
| 1134 | rc = -ENOMEM; |
| 1135 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 1136 | goto err_out; |
| 1137 | } |
| 1138 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1139 | status = deassemble_neg_contexts(conn, req, |
| 1140 | get_rfc1002_len(work->request_buf)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1141 | if (status != STATUS_SUCCESS) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 1142 | pr_err("deassemble_neg_contexts error(0x%x)\n", |
| 1143 | status); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1144 | rsp->hdr.Status = status; |
| 1145 | rc = -EINVAL; |
Namjae Jeon | aa7253c | 2022-07-28 21:56:19 +0900 | [diff] [blame] | 1146 | kfree(conn->preauth_info); |
| 1147 | conn->preauth_info = NULL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1148 | goto err_out; |
| 1149 | } |
| 1150 | |
| 1151 | rc = init_smb3_11_server(conn); |
| 1152 | if (rc < 0) { |
| 1153 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
Namjae Jeon | aa7253c | 2022-07-28 21:56:19 +0900 | [diff] [blame] | 1154 | kfree(conn->preauth_info); |
| 1155 | conn->preauth_info = NULL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1156 | goto err_out; |
| 1157 | } |
| 1158 | |
| 1159 | ksmbd_gen_preauth_integrity_hash(conn, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1160 | work->request_buf, |
| 1161 | conn->preauth_info->Preauth_HashValue); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1162 | rsp->NegotiateContextOffset = |
| 1163 | cpu_to_le32(OFFSET_OF_NEG_CONTEXT); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1164 | assemble_neg_contexts(conn, rsp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1165 | break; |
| 1166 | case SMB302_PROT_ID: |
| 1167 | init_smb3_02_server(conn); |
| 1168 | break; |
| 1169 | case SMB30_PROT_ID: |
| 1170 | init_smb3_0_server(conn); |
| 1171 | break; |
| 1172 | case SMB21_PROT_ID: |
| 1173 | init_smb2_1_server(conn); |
| 1174 | break; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1175 | case SMB2X_PROT_ID: |
| 1176 | case BAD_PROT_ID: |
| 1177 | default: |
| 1178 | ksmbd_debug(SMB, "Server dialect :0x%x not supported\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1179 | conn->dialect); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1180 | rsp->hdr.Status = STATUS_NOT_SUPPORTED; |
| 1181 | rc = -EINVAL; |
| 1182 | goto err_out; |
| 1183 | } |
| 1184 | rsp->Capabilities = cpu_to_le32(conn->vals->capabilities); |
| 1185 | |
| 1186 | /* For stats */ |
| 1187 | conn->connection_type = conn->dialect; |
| 1188 | |
| 1189 | rsp->MaxTransactSize = cpu_to_le32(conn->vals->max_trans_size); |
| 1190 | rsp->MaxReadSize = cpu_to_le32(conn->vals->max_read_size); |
| 1191 | rsp->MaxWriteSize = cpu_to_le32(conn->vals->max_write_size); |
| 1192 | |
Namjae Jeon | 51a1387 | 2021-09-29 13:09:24 +0900 | [diff] [blame] | 1193 | memcpy(conn->ClientGUID, req->ClientGUID, |
| 1194 | SMB2_CLIENT_GUID_SIZE); |
| 1195 | conn->cli_sec_mode = le16_to_cpu(req->SecurityMode); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1196 | |
| 1197 | rsp->StructureSize = cpu_to_le16(65); |
| 1198 | rsp->DialectRevision = cpu_to_le16(conn->dialect); |
| 1199 | /* Not setting conn guid rsp->ServerGUID, as it |
| 1200 | * not used by client for identifying server |
| 1201 | */ |
| 1202 | memset(rsp->ServerGUID, 0, SMB2_CLIENT_GUID_SIZE); |
| 1203 | |
| 1204 | rsp->SystemTime = cpu_to_le64(ksmbd_systime()); |
| 1205 | rsp->ServerStartTime = 0; |
| 1206 | ksmbd_debug(SMB, "negotiate context offset %d, count %d\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1207 | le32_to_cpu(rsp->NegotiateContextOffset), |
| 1208 | le16_to_cpu(rsp->NegotiateContextCount)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1209 | |
| 1210 | rsp->SecurityBufferOffset = cpu_to_le16(128); |
| 1211 | rsp->SecurityBufferLength = cpu_to_le16(AUTH_GSS_LENGTH); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1212 | ksmbd_copy_gss_neg_header((char *)(&rsp->hdr) + |
| 1213 | le16_to_cpu(rsp->SecurityBufferOffset)); |
| 1214 | inc_rfc1001_len(work->response_buf, sizeof(struct smb2_negotiate_rsp) - |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1215 | sizeof(struct smb2_hdr) - sizeof(rsp->Buffer) + |
| 1216 | AUTH_GSS_LENGTH); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1217 | rsp->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED_LE; |
| 1218 | conn->use_spnego = true; |
| 1219 | |
| 1220 | if ((server_conf.signing == KSMBD_CONFIG_OPT_AUTO || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1221 | server_conf.signing == KSMBD_CONFIG_OPT_DISABLED) && |
| 1222 | req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED_LE) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1223 | conn->sign = true; |
| 1224 | else if (server_conf.signing == KSMBD_CONFIG_OPT_MANDATORY) { |
| 1225 | server_conf.enforced_signing = true; |
| 1226 | rsp->SecurityMode |= SMB2_NEGOTIATE_SIGNING_REQUIRED_LE; |
| 1227 | conn->sign = true; |
| 1228 | } |
| 1229 | |
| 1230 | conn->srv_sec_mode = le16_to_cpu(rsp->SecurityMode); |
| 1231 | ksmbd_conn_set_need_negotiate(work); |
| 1232 | |
| 1233 | err_out: |
| 1234 | if (rc < 0) |
| 1235 | smb2_set_err_rsp(work); |
| 1236 | |
| 1237 | return rc; |
| 1238 | } |
| 1239 | |
| 1240 | static int alloc_preauth_hash(struct ksmbd_session *sess, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1241 | struct ksmbd_conn *conn) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1242 | { |
| 1243 | if (sess->Preauth_HashValue) |
| 1244 | return 0; |
| 1245 | |
kernel test robot | 86f5297 | 2021-04-02 12:17:24 +0900 | [diff] [blame] | 1246 | sess->Preauth_HashValue = kmemdup(conn->preauth_info->Preauth_HashValue, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1247 | PREAUTH_HASHVALUE_SIZE, GFP_KERNEL); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1248 | if (!sess->Preauth_HashValue) |
| 1249 | return -ENOMEM; |
| 1250 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1251 | return 0; |
| 1252 | } |
| 1253 | |
| 1254 | static int generate_preauth_hash(struct ksmbd_work *work) |
| 1255 | { |
| 1256 | struct ksmbd_conn *conn = work->conn; |
| 1257 | struct ksmbd_session *sess = work->sess; |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1258 | u8 *preauth_hash; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1259 | |
| 1260 | if (conn->dialect != SMB311_PROT_ID) |
| 1261 | return 0; |
| 1262 | |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1263 | if (conn->binding) { |
| 1264 | struct preauth_session *preauth_sess; |
| 1265 | |
| 1266 | preauth_sess = ksmbd_preauth_session_lookup(conn, sess->id); |
| 1267 | if (!preauth_sess) { |
| 1268 | preauth_sess = ksmbd_preauth_session_alloc(conn, sess->id); |
| 1269 | if (!preauth_sess) |
| 1270 | return -ENOMEM; |
| 1271 | } |
| 1272 | |
| 1273 | preauth_hash = preauth_sess->Preauth_HashValue; |
| 1274 | } else { |
| 1275 | if (!sess->Preauth_HashValue) |
| 1276 | if (alloc_preauth_hash(sess, conn)) |
| 1277 | return -ENOMEM; |
| 1278 | preauth_hash = sess->Preauth_HashValue; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1279 | } |
| 1280 | |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1281 | ksmbd_gen_preauth_integrity_hash(conn, work->request_buf, preauth_hash); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1282 | return 0; |
| 1283 | } |
| 1284 | |
Marios Makassikis | 0d994cd | 2021-10-19 17:39:38 +0200 | [diff] [blame] | 1285 | static int decode_negotiation_token(struct ksmbd_conn *conn, |
| 1286 | struct negotiate_message *negblob, |
| 1287 | size_t sz) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1288 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1289 | if (!conn->use_spnego) |
| 1290 | return -EINVAL; |
| 1291 | |
Hyunchul Lee | fad4161 | 2021-04-19 17:26:15 +0900 | [diff] [blame] | 1292 | if (ksmbd_decode_negTokenInit((char *)negblob, sz, conn)) { |
| 1293 | if (ksmbd_decode_negTokenTarg((char *)negblob, sz, conn)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1294 | conn->auth_mechs |= KSMBD_AUTH_NTLMSSP; |
| 1295 | conn->preferred_auth_mech = KSMBD_AUTH_NTLMSSP; |
| 1296 | conn->use_spnego = false; |
| 1297 | } |
| 1298 | } |
| 1299 | return 0; |
| 1300 | } |
| 1301 | |
| 1302 | static int ntlm_negotiate(struct ksmbd_work *work, |
Marios Makassikis | 0d994cd | 2021-10-19 17:39:38 +0200 | [diff] [blame] | 1303 | struct negotiate_message *negblob, |
| 1304 | size_t negblob_len) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1305 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1306 | struct smb2_sess_setup_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1307 | struct challenge_message *chgblob; |
| 1308 | unsigned char *spnego_blob = NULL; |
| 1309 | u16 spnego_blob_len; |
| 1310 | char *neg_blob; |
| 1311 | int sz, rc; |
| 1312 | |
| 1313 | ksmbd_debug(SMB, "negotiate phase\n"); |
Namjae Jeon | ce53d36 | 2021-12-15 14:57:27 +0900 | [diff] [blame] | 1314 | rc = ksmbd_decode_ntlmssp_neg_blob(negblob, negblob_len, work->conn); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1315 | if (rc) |
| 1316 | return rc; |
| 1317 | |
| 1318 | sz = le16_to_cpu(rsp->SecurityBufferOffset); |
| 1319 | chgblob = |
| 1320 | (struct challenge_message *)((char *)&rsp->hdr.ProtocolId + sz); |
| 1321 | memset(chgblob, 0, sizeof(struct challenge_message)); |
| 1322 | |
| 1323 | if (!work->conn->use_spnego) { |
Namjae Jeon | ce53d36 | 2021-12-15 14:57:27 +0900 | [diff] [blame] | 1324 | sz = ksmbd_build_ntlmssp_challenge_blob(chgblob, work->conn); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1325 | if (sz < 0) |
| 1326 | return -ENOMEM; |
| 1327 | |
| 1328 | rsp->SecurityBufferLength = cpu_to_le16(sz); |
| 1329 | return 0; |
| 1330 | } |
| 1331 | |
| 1332 | sz = sizeof(struct challenge_message); |
| 1333 | sz += (strlen(ksmbd_netbios_name()) * 2 + 1 + 4) * 6; |
| 1334 | |
| 1335 | neg_blob = kzalloc(sz, GFP_KERNEL); |
| 1336 | if (!neg_blob) |
| 1337 | return -ENOMEM; |
| 1338 | |
| 1339 | chgblob = (struct challenge_message *)neg_blob; |
Namjae Jeon | ce53d36 | 2021-12-15 14:57:27 +0900 | [diff] [blame] | 1340 | sz = ksmbd_build_ntlmssp_challenge_blob(chgblob, work->conn); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1341 | if (sz < 0) { |
| 1342 | rc = -ENOMEM; |
| 1343 | goto out; |
| 1344 | } |
| 1345 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1346 | rc = build_spnego_ntlmssp_neg_blob(&spnego_blob, &spnego_blob_len, |
| 1347 | neg_blob, sz); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1348 | if (rc) { |
| 1349 | rc = -ENOMEM; |
| 1350 | goto out; |
| 1351 | } |
| 1352 | |
| 1353 | sz = le16_to_cpu(rsp->SecurityBufferOffset); |
| 1354 | memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len); |
| 1355 | rsp->SecurityBufferLength = cpu_to_le16(spnego_blob_len); |
| 1356 | |
| 1357 | out: |
| 1358 | kfree(spnego_blob); |
| 1359 | kfree(neg_blob); |
| 1360 | return rc; |
| 1361 | } |
| 1362 | |
| 1363 | static struct authenticate_message *user_authblob(struct ksmbd_conn *conn, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1364 | struct smb2_sess_setup_req *req) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1365 | { |
| 1366 | int sz; |
| 1367 | |
| 1368 | if (conn->use_spnego && conn->mechToken) |
| 1369 | return (struct authenticate_message *)conn->mechToken; |
| 1370 | |
| 1371 | sz = le16_to_cpu(req->SecurityBufferOffset); |
| 1372 | return (struct authenticate_message *)((char *)&req->hdr.ProtocolId |
| 1373 | + sz); |
| 1374 | } |
| 1375 | |
| 1376 | static struct ksmbd_user *session_user(struct ksmbd_conn *conn, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1377 | struct smb2_sess_setup_req *req) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1378 | { |
| 1379 | struct authenticate_message *authblob; |
| 1380 | struct ksmbd_user *user; |
| 1381 | char *name; |
Marios Makassikis | 0d994cd | 2021-10-19 17:39:38 +0200 | [diff] [blame] | 1382 | unsigned int auth_msg_len, name_off, name_len, secbuf_len; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1383 | |
Marios Makassikis | 0d994cd | 2021-10-19 17:39:38 +0200 | [diff] [blame] | 1384 | secbuf_len = le16_to_cpu(req->SecurityBufferLength); |
| 1385 | if (secbuf_len < sizeof(struct authenticate_message)) { |
| 1386 | ksmbd_debug(SMB, "blob len %d too small\n", secbuf_len); |
| 1387 | return NULL; |
| 1388 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1389 | authblob = user_authblob(conn, req); |
Marios Makassikis | 0d994cd | 2021-10-19 17:39:38 +0200 | [diff] [blame] | 1390 | name_off = le32_to_cpu(authblob->UserName.BufferOffset); |
| 1391 | name_len = le16_to_cpu(authblob->UserName.Length); |
| 1392 | auth_msg_len = le16_to_cpu(req->SecurityBufferOffset) + secbuf_len; |
| 1393 | |
| 1394 | if (auth_msg_len < (u64)name_off + name_len) |
| 1395 | return NULL; |
| 1396 | |
| 1397 | name = smb_strndup_from_utf16((const char *)authblob + name_off, |
| 1398 | name_len, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1399 | true, |
| 1400 | conn->local_nls); |
| 1401 | if (IS_ERR(name)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 1402 | pr_err("cannot allocate memory\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1403 | return NULL; |
| 1404 | } |
| 1405 | |
| 1406 | ksmbd_debug(SMB, "session setup request for user %s\n", name); |
| 1407 | user = ksmbd_login_user(name); |
| 1408 | kfree(name); |
| 1409 | return user; |
| 1410 | } |
| 1411 | |
| 1412 | static int ntlm_authenticate(struct ksmbd_work *work) |
| 1413 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1414 | struct smb2_sess_setup_req *req = smb2_get_msg(work->request_buf); |
| 1415 | struct smb2_sess_setup_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1416 | struct ksmbd_conn *conn = work->conn; |
| 1417 | struct ksmbd_session *sess = work->sess; |
| 1418 | struct channel *chann = NULL; |
| 1419 | struct ksmbd_user *user; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1420 | u64 prev_id; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1421 | int sz, rc; |
| 1422 | |
| 1423 | ksmbd_debug(SMB, "authenticate phase\n"); |
| 1424 | if (conn->use_spnego) { |
| 1425 | unsigned char *spnego_blob; |
| 1426 | u16 spnego_blob_len; |
| 1427 | |
| 1428 | rc = build_spnego_ntlmssp_auth_blob(&spnego_blob, |
| 1429 | &spnego_blob_len, |
| 1430 | 0); |
| 1431 | if (rc) |
| 1432 | return -ENOMEM; |
| 1433 | |
| 1434 | sz = le16_to_cpu(rsp->SecurityBufferOffset); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1435 | memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1436 | rsp->SecurityBufferLength = cpu_to_le16(spnego_blob_len); |
| 1437 | kfree(spnego_blob); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1438 | inc_rfc1001_len(work->response_buf, spnego_blob_len - 1); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1439 | } |
| 1440 | |
| 1441 | user = session_user(conn, req); |
| 1442 | if (!user) { |
| 1443 | ksmbd_debug(SMB, "Unknown user name or an error\n"); |
Namjae Jeon | 58090b1 | 2021-07-16 14:52:09 +0900 | [diff] [blame] | 1444 | return -EPERM; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1445 | } |
| 1446 | |
| 1447 | /* Check for previous session */ |
| 1448 | prev_id = le64_to_cpu(req->PreviousSessionId); |
| 1449 | if (prev_id && prev_id != sess->id) |
Namjae Jeon | e4d3e6b | 2022-07-22 10:15:10 +0900 | [diff] [blame] | 1450 | destroy_previous_session(conn, user, prev_id); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1451 | |
| 1452 | if (sess->state == SMB2_SESSION_VALID) { |
| 1453 | /* |
| 1454 | * Reuse session if anonymous try to connect |
| 1455 | * on reauthetication. |
| 1456 | */ |
| 1457 | if (ksmbd_anonymous_user(user)) { |
| 1458 | ksmbd_free_user(user); |
| 1459 | return 0; |
| 1460 | } |
Namjae Jeon | a58b45a | 2021-12-16 10:26:43 +0900 | [diff] [blame] | 1461 | |
| 1462 | if (!ksmbd_compare_user(sess->user, user)) { |
| 1463 | ksmbd_free_user(user); |
| 1464 | return -EPERM; |
| 1465 | } |
| 1466 | ksmbd_free_user(user); |
| 1467 | } else { |
| 1468 | sess->user = user; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1469 | } |
| 1470 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1471 | if (user_guest(sess->user)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1472 | rsp->SessionFlags = SMB2_SESSION_FLAG_IS_GUEST_LE; |
| 1473 | } else { |
| 1474 | struct authenticate_message *authblob; |
| 1475 | |
| 1476 | authblob = user_authblob(conn, req); |
| 1477 | sz = le16_to_cpu(req->SecurityBufferLength); |
Namjae Jeon | ce53d36 | 2021-12-15 14:57:27 +0900 | [diff] [blame] | 1478 | rc = ksmbd_decode_ntlmssp_auth_blob(authblob, sz, conn, sess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1479 | if (rc) { |
| 1480 | set_user_flag(sess->user, KSMBD_USER_FLAG_BAD_PASSWORD); |
| 1481 | ksmbd_debug(SMB, "authentication failed\n"); |
Namjae Jeon | 58090b1 | 2021-07-16 14:52:09 +0900 | [diff] [blame] | 1482 | return -EPERM; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1483 | } |
Namjae Jeon | ac090d9 | 2022-01-17 22:16:01 +0900 | [diff] [blame] | 1484 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1485 | |
Namjae Jeon | ac090d9 | 2022-01-17 22:16:01 +0900 | [diff] [blame] | 1486 | /* |
| 1487 | * If session state is SMB2_SESSION_VALID, We can assume |
| 1488 | * that it is reauthentication. And the user/password |
| 1489 | * has been verified, so return it here. |
| 1490 | */ |
| 1491 | if (sess->state == SMB2_SESSION_VALID) { |
| 1492 | if (conn->binding) |
| 1493 | goto binding_session; |
| 1494 | return 0; |
| 1495 | } |
| 1496 | |
| 1497 | if ((rsp->SessionFlags != SMB2_SESSION_FLAG_IS_GUEST_LE && |
| 1498 | (conn->sign || server_conf.enforced_signing)) || |
| 1499 | (req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED)) |
| 1500 | sess->sign = true; |
| 1501 | |
| 1502 | if (smb3_encryption_negotiated(conn) && |
| 1503 | !(req->Flags & SMB2_SESSION_REQ_FLAG_BINDING)) { |
Namjae Jeon | af7c39d | 2022-07-25 13:36:52 +0900 | [diff] [blame] | 1504 | rc = conn->ops->generate_encryptionkey(conn, sess); |
Namjae Jeon | ac090d9 | 2022-01-17 22:16:01 +0900 | [diff] [blame] | 1505 | if (rc) { |
| 1506 | ksmbd_debug(SMB, |
| 1507 | "SMB3 encryption key generation failed\n"); |
| 1508 | return -EINVAL; |
| 1509 | } |
| 1510 | sess->enc = true; |
Namjae Jeon | 37ba7b0 | 2022-10-29 00:01:38 +0900 | [diff] [blame] | 1511 | if (server_conf.flags & KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION) |
| 1512 | rsp->SessionFlags = SMB2_SESSION_FLAG_ENCRYPT_DATA_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1513 | /* |
Namjae Jeon | ac090d9 | 2022-01-17 22:16:01 +0900 | [diff] [blame] | 1514 | * signing is disable if encryption is enable |
| 1515 | * on this session |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1516 | */ |
Namjae Jeon | ac090d9 | 2022-01-17 22:16:01 +0900 | [diff] [blame] | 1517 | sess->sign = false; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1518 | } |
| 1519 | |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1520 | binding_session: |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1521 | if (conn->dialect >= SMB30_PROT_ID) { |
Namjae Jeon | 8e06b31 | 2022-07-22 10:15:57 +0900 | [diff] [blame] | 1522 | read_lock(&sess->chann_lock); |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1523 | chann = lookup_chann_list(sess, conn); |
Namjae Jeon | 8e06b31 | 2022-07-22 10:15:57 +0900 | [diff] [blame] | 1524 | read_unlock(&sess->chann_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1525 | if (!chann) { |
| 1526 | chann = kmalloc(sizeof(struct channel), GFP_KERNEL); |
| 1527 | if (!chann) |
| 1528 | return -ENOMEM; |
| 1529 | |
| 1530 | chann->conn = conn; |
| 1531 | INIT_LIST_HEAD(&chann->chann_list); |
Namjae Jeon | 8e06b31 | 2022-07-22 10:15:57 +0900 | [diff] [blame] | 1532 | write_lock(&sess->chann_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1533 | list_add(&chann->chann_list, &sess->ksmbd_chann_list); |
Namjae Jeon | 8e06b31 | 2022-07-22 10:15:57 +0900 | [diff] [blame] | 1534 | write_unlock(&sess->chann_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1535 | } |
| 1536 | } |
| 1537 | |
| 1538 | if (conn->ops->generate_signingkey) { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1539 | rc = conn->ops->generate_signingkey(sess, conn); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1540 | if (rc) { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1541 | ksmbd_debug(SMB, "SMB3 signing key generation failed\n"); |
Namjae Jeon | 58090b1 | 2021-07-16 14:52:09 +0900 | [diff] [blame] | 1542 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1543 | } |
| 1544 | } |
| 1545 | |
Namjae Jeon | 51a1387 | 2021-09-29 13:09:24 +0900 | [diff] [blame] | 1546 | if (!ksmbd_conn_lookup_dialect(conn)) { |
| 1547 | pr_err("fail to verify the dialect\n"); |
| 1548 | return -ENOENT; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1549 | } |
| 1550 | return 0; |
| 1551 | } |
| 1552 | |
| 1553 | #ifdef CONFIG_SMB_SERVER_KERBEROS5 |
| 1554 | static int krb5_authenticate(struct ksmbd_work *work) |
| 1555 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1556 | struct smb2_sess_setup_req *req = smb2_get_msg(work->request_buf); |
| 1557 | struct smb2_sess_setup_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1558 | struct ksmbd_conn *conn = work->conn; |
| 1559 | struct ksmbd_session *sess = work->sess; |
| 1560 | char *in_blob, *out_blob; |
| 1561 | struct channel *chann = NULL; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1562 | u64 prev_sess_id; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1563 | int in_len, out_len; |
| 1564 | int retval; |
| 1565 | |
| 1566 | in_blob = (char *)&req->hdr.ProtocolId + |
| 1567 | le16_to_cpu(req->SecurityBufferOffset); |
| 1568 | in_len = le16_to_cpu(req->SecurityBufferLength); |
| 1569 | out_blob = (char *)&rsp->hdr.ProtocolId + |
| 1570 | le16_to_cpu(rsp->SecurityBufferOffset); |
| 1571 | out_len = work->response_sz - |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1572 | (le16_to_cpu(rsp->SecurityBufferOffset) + 4); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1573 | |
| 1574 | /* Check previous session */ |
| 1575 | prev_sess_id = le64_to_cpu(req->PreviousSessionId); |
| 1576 | if (prev_sess_id && prev_sess_id != sess->id) |
Namjae Jeon | e4d3e6b | 2022-07-22 10:15:10 +0900 | [diff] [blame] | 1577 | destroy_previous_session(conn, sess->user, prev_sess_id); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1578 | |
| 1579 | if (sess->state == SMB2_SESSION_VALID) |
| 1580 | ksmbd_free_user(sess->user); |
| 1581 | |
| 1582 | retval = ksmbd_krb5_authenticate(sess, in_blob, in_len, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1583 | out_blob, &out_len); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1584 | if (retval) { |
| 1585 | ksmbd_debug(SMB, "krb5 authentication failed\n"); |
Namjae Jeon | 58090b1 | 2021-07-16 14:52:09 +0900 | [diff] [blame] | 1586 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1587 | } |
| 1588 | rsp->SecurityBufferLength = cpu_to_le16(out_len); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1589 | inc_rfc1001_len(work->response_buf, out_len - 1); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1590 | |
| 1591 | if ((conn->sign || server_conf.enforced_signing) || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1592 | (req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1593 | sess->sign = true; |
| 1594 | |
Marcos Del Sol Vives | 83912d6 | 2021-12-16 11:37:22 +0100 | [diff] [blame] | 1595 | if (smb3_encryption_negotiated(conn)) { |
Namjae Jeon | af7c39d | 2022-07-25 13:36:52 +0900 | [diff] [blame] | 1596 | retval = conn->ops->generate_encryptionkey(conn, sess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1597 | if (retval) { |
| 1598 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1599 | "SMB3 encryption key generation failed\n"); |
Namjae Jeon | 58090b1 | 2021-07-16 14:52:09 +0900 | [diff] [blame] | 1600 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1601 | } |
| 1602 | sess->enc = true; |
Namjae Jeon | 37ba7b0 | 2022-10-29 00:01:38 +0900 | [diff] [blame] | 1603 | if (server_conf.flags & KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION) |
| 1604 | rsp->SessionFlags = SMB2_SESSION_FLAG_ENCRYPT_DATA_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1605 | sess->sign = false; |
| 1606 | } |
| 1607 | |
| 1608 | if (conn->dialect >= SMB30_PROT_ID) { |
Namjae Jeon | 8e06b31 | 2022-07-22 10:15:57 +0900 | [diff] [blame] | 1609 | read_lock(&sess->chann_lock); |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1610 | chann = lookup_chann_list(sess, conn); |
Namjae Jeon | 8e06b31 | 2022-07-22 10:15:57 +0900 | [diff] [blame] | 1611 | read_unlock(&sess->chann_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1612 | if (!chann) { |
| 1613 | chann = kmalloc(sizeof(struct channel), GFP_KERNEL); |
| 1614 | if (!chann) |
| 1615 | return -ENOMEM; |
| 1616 | |
| 1617 | chann->conn = conn; |
| 1618 | INIT_LIST_HEAD(&chann->chann_list); |
Namjae Jeon | 8e06b31 | 2022-07-22 10:15:57 +0900 | [diff] [blame] | 1619 | write_lock(&sess->chann_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1620 | list_add(&chann->chann_list, &sess->ksmbd_chann_list); |
Namjae Jeon | 8e06b31 | 2022-07-22 10:15:57 +0900 | [diff] [blame] | 1621 | write_unlock(&sess->chann_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1622 | } |
| 1623 | } |
| 1624 | |
| 1625 | if (conn->ops->generate_signingkey) { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1626 | retval = conn->ops->generate_signingkey(sess, conn); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1627 | if (retval) { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1628 | ksmbd_debug(SMB, "SMB3 signing key generation failed\n"); |
Namjae Jeon | 58090b1 | 2021-07-16 14:52:09 +0900 | [diff] [blame] | 1629 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1630 | } |
| 1631 | } |
| 1632 | |
Namjae Jeon | 51a1387 | 2021-09-29 13:09:24 +0900 | [diff] [blame] | 1633 | if (!ksmbd_conn_lookup_dialect(conn)) { |
| 1634 | pr_err("fail to verify the dialect\n"); |
| 1635 | return -ENOENT; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1636 | } |
| 1637 | return 0; |
| 1638 | } |
| 1639 | #else |
| 1640 | static int krb5_authenticate(struct ksmbd_work *work) |
| 1641 | { |
| 1642 | return -EOPNOTSUPP; |
| 1643 | } |
| 1644 | #endif |
| 1645 | |
| 1646 | int smb2_sess_setup(struct ksmbd_work *work) |
| 1647 | { |
| 1648 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1649 | struct smb2_sess_setup_req *req = smb2_get_msg(work->request_buf); |
| 1650 | struct smb2_sess_setup_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1651 | struct ksmbd_session *sess; |
| 1652 | struct negotiate_message *negblob; |
Marios Makassikis | 0d994cd | 2021-10-19 17:39:38 +0200 | [diff] [blame] | 1653 | unsigned int negblob_len, negblob_off; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1654 | int rc = 0; |
| 1655 | |
| 1656 | ksmbd_debug(SMB, "Received request for session setup\n"); |
| 1657 | |
| 1658 | rsp->StructureSize = cpu_to_le16(9); |
| 1659 | rsp->SessionFlags = 0; |
| 1660 | rsp->SecurityBufferOffset = cpu_to_le16(72); |
| 1661 | rsp->SecurityBufferLength = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1662 | inc_rfc1001_len(work->response_buf, 9); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1663 | |
| 1664 | if (!req->hdr.SessionId) { |
| 1665 | sess = ksmbd_smb2_session_create(); |
| 1666 | if (!sess) { |
| 1667 | rc = -ENOMEM; |
| 1668 | goto out_err; |
| 1669 | } |
| 1670 | rsp->hdr.SessionId = cpu_to_le64(sess->id); |
Namjae Jeon | e4d3e6b | 2022-07-22 10:15:10 +0900 | [diff] [blame] | 1671 | rc = ksmbd_session_register(conn, sess); |
| 1672 | if (rc) |
| 1673 | goto out_err; |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1674 | } else if (conn->dialect >= SMB30_PROT_ID && |
| 1675 | (server_conf.flags & KSMBD_GLOBAL_FLAG_SMB3_MULTICHANNEL) && |
| 1676 | req->Flags & SMB2_SESSION_REQ_FLAG_BINDING) { |
| 1677 | u64 sess_id = le64_to_cpu(req->hdr.SessionId); |
| 1678 | |
| 1679 | sess = ksmbd_session_lookup_slowpath(sess_id); |
| 1680 | if (!sess) { |
| 1681 | rc = -ENOENT; |
| 1682 | goto out_err; |
| 1683 | } |
| 1684 | |
Namjae Jeon | af7c39d | 2022-07-25 13:36:52 +0900 | [diff] [blame] | 1685 | if (conn->dialect != sess->dialect) { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1686 | rc = -EINVAL; |
| 1687 | goto out_err; |
| 1688 | } |
| 1689 | |
| 1690 | if (!(req->hdr.Flags & SMB2_FLAGS_SIGNED)) { |
| 1691 | rc = -EINVAL; |
| 1692 | goto out_err; |
| 1693 | } |
| 1694 | |
Namjae Jeon | af7c39d | 2022-07-25 13:36:52 +0900 | [diff] [blame] | 1695 | if (strncmp(conn->ClientGUID, sess->ClientGUID, |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1696 | SMB2_CLIENT_GUID_SIZE)) { |
| 1697 | rc = -ENOENT; |
| 1698 | goto out_err; |
| 1699 | } |
| 1700 | |
| 1701 | if (sess->state == SMB2_SESSION_IN_PROGRESS) { |
| 1702 | rc = -EACCES; |
| 1703 | goto out_err; |
| 1704 | } |
| 1705 | |
| 1706 | if (sess->state == SMB2_SESSION_EXPIRED) { |
| 1707 | rc = -EFAULT; |
| 1708 | goto out_err; |
| 1709 | } |
| 1710 | |
| 1711 | if (ksmbd_session_lookup(conn, sess_id)) { |
| 1712 | rc = -EACCES; |
| 1713 | goto out_err; |
| 1714 | } |
| 1715 | |
| 1716 | conn->binding = true; |
| 1717 | } else if ((conn->dialect < SMB30_PROT_ID || |
| 1718 | server_conf.flags & KSMBD_GLOBAL_FLAG_SMB3_MULTICHANNEL) && |
| 1719 | (req->Flags & SMB2_SESSION_REQ_FLAG_BINDING)) { |
Colin Ian King | 4951a84 | 2021-07-06 13:05:01 +0100 | [diff] [blame] | 1720 | sess = NULL; |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1721 | rc = -EACCES; |
| 1722 | goto out_err; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1723 | } else { |
| 1724 | sess = ksmbd_session_lookup(conn, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1725 | le64_to_cpu(req->hdr.SessionId)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1726 | if (!sess) { |
| 1727 | rc = -ENOENT; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1728 | goto out_err; |
| 1729 | } |
| 1730 | } |
| 1731 | work->sess = sess; |
| 1732 | |
| 1733 | if (sess->state == SMB2_SESSION_EXPIRED) |
| 1734 | sess->state = SMB2_SESSION_IN_PROGRESS; |
| 1735 | |
Marios Makassikis | 0d994cd | 2021-10-19 17:39:38 +0200 | [diff] [blame] | 1736 | negblob_off = le16_to_cpu(req->SecurityBufferOffset); |
| 1737 | negblob_len = le16_to_cpu(req->SecurityBufferLength); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1738 | if (negblob_off < offsetof(struct smb2_sess_setup_req, Buffer) || |
Christophe JAILLET | f8fbfd8 | 2021-11-07 16:22:57 +0100 | [diff] [blame] | 1739 | negblob_len < offsetof(struct negotiate_message, NegotiateFlags)) { |
| 1740 | rc = -EINVAL; |
| 1741 | goto out_err; |
| 1742 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1743 | |
Marios Makassikis | 0d994cd | 2021-10-19 17:39:38 +0200 | [diff] [blame] | 1744 | negblob = (struct negotiate_message *)((char *)&req->hdr.ProtocolId + |
| 1745 | negblob_off); |
| 1746 | |
| 1747 | if (decode_negotiation_token(conn, negblob, negblob_len) == 0) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1748 | if (conn->mechToken) |
| 1749 | negblob = (struct negotiate_message *)conn->mechToken; |
| 1750 | } |
| 1751 | |
| 1752 | if (server_conf.auth_mechs & conn->auth_mechs) { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1753 | rc = generate_preauth_hash(work); |
| 1754 | if (rc) |
| 1755 | goto out_err; |
| 1756 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1757 | if (conn->preferred_auth_mech & |
| 1758 | (KSMBD_AUTH_KRB5 | KSMBD_AUTH_MSKRB5)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1759 | rc = krb5_authenticate(work); |
| 1760 | if (rc) { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1761 | rc = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1762 | goto out_err; |
| 1763 | } |
| 1764 | |
| 1765 | ksmbd_conn_set_good(work); |
| 1766 | sess->state = SMB2_SESSION_VALID; |
Muhammad Usama Anjum | 822bc8e | 2021-04-02 09:25:35 +0900 | [diff] [blame] | 1767 | kfree(sess->Preauth_HashValue); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1768 | sess->Preauth_HashValue = NULL; |
| 1769 | } else if (conn->preferred_auth_mech == KSMBD_AUTH_NTLMSSP) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1770 | if (negblob->MessageType == NtLmNegotiate) { |
Marios Makassikis | 0d994cd | 2021-10-19 17:39:38 +0200 | [diff] [blame] | 1771 | rc = ntlm_negotiate(work, negblob, negblob_len); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1772 | if (rc) |
| 1773 | goto out_err; |
| 1774 | rsp->hdr.Status = |
| 1775 | STATUS_MORE_PROCESSING_REQUIRED; |
| 1776 | /* |
| 1777 | * Note: here total size -1 is done as an |
| 1778 | * adjustment for 0 size blob |
| 1779 | */ |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1780 | inc_rfc1001_len(work->response_buf, |
| 1781 | le16_to_cpu(rsp->SecurityBufferLength) - 1); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1782 | |
| 1783 | } else if (negblob->MessageType == NtLmAuthenticate) { |
| 1784 | rc = ntlm_authenticate(work); |
| 1785 | if (rc) |
| 1786 | goto out_err; |
| 1787 | |
| 1788 | ksmbd_conn_set_good(work); |
| 1789 | sess->state = SMB2_SESSION_VALID; |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1790 | if (conn->binding) { |
| 1791 | struct preauth_session *preauth_sess; |
| 1792 | |
| 1793 | preauth_sess = |
| 1794 | ksmbd_preauth_session_lookup(conn, sess->id); |
| 1795 | if (preauth_sess) { |
| 1796 | list_del(&preauth_sess->preauth_entry); |
| 1797 | kfree(preauth_sess); |
| 1798 | } |
| 1799 | } |
Muhammad Usama Anjum | 822bc8e | 2021-04-02 09:25:35 +0900 | [diff] [blame] | 1800 | kfree(sess->Preauth_HashValue); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1801 | sess->Preauth_HashValue = NULL; |
| 1802 | } |
| 1803 | } else { |
| 1804 | /* TODO: need one more negotiation */ |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 1805 | pr_err("Not support the preferred authentication\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1806 | rc = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1807 | } |
| 1808 | } else { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 1809 | pr_err("Not support authentication\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1810 | rc = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1811 | } |
| 1812 | |
| 1813 | out_err: |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1814 | if (rc == -EINVAL) |
| 1815 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 1816 | else if (rc == -ENOENT) |
| 1817 | rsp->hdr.Status = STATUS_USER_SESSION_DELETED; |
| 1818 | else if (rc == -EACCES) |
| 1819 | rsp->hdr.Status = STATUS_REQUEST_NOT_ACCEPTED; |
| 1820 | else if (rc == -EFAULT) |
| 1821 | rsp->hdr.Status = STATUS_NETWORK_SESSION_EXPIRED; |
Namjae Jeon | 58090b1 | 2021-07-16 14:52:09 +0900 | [diff] [blame] | 1822 | else if (rc == -ENOMEM) |
| 1823 | rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES; |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1824 | else if (rc) |
| 1825 | rsp->hdr.Status = STATUS_LOGON_FAILURE; |
| 1826 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1827 | if (conn->use_spnego && conn->mechToken) { |
| 1828 | kfree(conn->mechToken); |
| 1829 | conn->mechToken = NULL; |
| 1830 | } |
| 1831 | |
Namjae Jeon | 621be84 | 2021-10-13 17:28:31 +0900 | [diff] [blame] | 1832 | if (rc < 0) { |
| 1833 | /* |
| 1834 | * SecurityBufferOffset should be set to zero |
| 1835 | * in session setup error response. |
| 1836 | */ |
| 1837 | rsp->SecurityBufferOffset = 0; |
| 1838 | |
| 1839 | if (sess) { |
| 1840 | bool try_delay = false; |
| 1841 | |
| 1842 | /* |
| 1843 | * To avoid dictionary attacks (repeated session setups rapidly sent) to |
| 1844 | * connect to server, ksmbd make a delay of a 5 seconds on session setup |
| 1845 | * failure to make it harder to send enough random connection requests |
| 1846 | * to break into a server. |
| 1847 | */ |
| 1848 | if (sess->user && sess->user->flags & KSMBD_USER_FLAG_DELAY_SESSION) |
| 1849 | try_delay = true; |
| 1850 | |
Namjae Jeon | e4d3e6b | 2022-07-22 10:15:10 +0900 | [diff] [blame] | 1851 | xa_erase(&conn->sessions, sess->id); |
Namjae Jeon | 621be84 | 2021-10-13 17:28:31 +0900 | [diff] [blame] | 1852 | ksmbd_session_destroy(sess); |
| 1853 | work->sess = NULL; |
| 1854 | if (try_delay) |
| 1855 | ssleep(5); |
| 1856 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1857 | } |
| 1858 | |
| 1859 | return rc; |
| 1860 | } |
| 1861 | |
| 1862 | /** |
| 1863 | * smb2_tree_connect() - handler for smb2 tree connect command |
| 1864 | * @work: smb work containing smb request buffer |
| 1865 | * |
| 1866 | * Return: 0 on success, otherwise error |
| 1867 | */ |
| 1868 | int smb2_tree_connect(struct ksmbd_work *work) |
| 1869 | { |
| 1870 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1871 | struct smb2_tree_connect_req *req = smb2_get_msg(work->request_buf); |
| 1872 | struct smb2_tree_connect_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1873 | struct ksmbd_session *sess = work->sess; |
| 1874 | char *treename = NULL, *name = NULL; |
| 1875 | struct ksmbd_tree_conn_status status; |
| 1876 | struct ksmbd_share_config *share; |
| 1877 | int rc = -EINVAL; |
| 1878 | |
| 1879 | treename = smb_strndup_from_utf16(req->Buffer, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1880 | le16_to_cpu(req->PathLength), true, |
| 1881 | conn->local_nls); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1882 | if (IS_ERR(treename)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 1883 | pr_err("treename is NULL\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1884 | status.ret = KSMBD_TREE_CONN_STATUS_ERROR; |
| 1885 | goto out_err1; |
| 1886 | } |
| 1887 | |
Atte Heikkilä | 16b5f54 | 2022-09-15 22:49:11 +0900 | [diff] [blame] | 1888 | name = ksmbd_extract_sharename(conn->um, treename); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1889 | if (IS_ERR(name)) { |
| 1890 | status.ret = KSMBD_TREE_CONN_STATUS_ERROR; |
| 1891 | goto out_err1; |
| 1892 | } |
| 1893 | |
| 1894 | ksmbd_debug(SMB, "tree connect request for tree %s treename %s\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1895 | name, treename); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1896 | |
Namjae Jeon | af7c39d | 2022-07-25 13:36:52 +0900 | [diff] [blame] | 1897 | status = ksmbd_tree_conn_connect(conn, sess, name); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1898 | if (status.ret == KSMBD_TREE_CONN_STATUS_OK) |
| 1899 | rsp->hdr.Id.SyncId.TreeId = cpu_to_le32(status.tree_conn->id); |
| 1900 | else |
| 1901 | goto out_err1; |
| 1902 | |
| 1903 | share = status.tree_conn->share_conf; |
| 1904 | if (test_share_config_flag(share, KSMBD_SHARE_FLAG_PIPE)) { |
| 1905 | ksmbd_debug(SMB, "IPC share path request\n"); |
| 1906 | rsp->ShareType = SMB2_SHARE_TYPE_PIPE; |
| 1907 | rsp->MaximalAccess = FILE_READ_DATA_LE | FILE_READ_EA_LE | |
| 1908 | FILE_EXECUTE_LE | FILE_READ_ATTRIBUTES_LE | |
| 1909 | FILE_DELETE_LE | FILE_READ_CONTROL_LE | |
| 1910 | FILE_WRITE_DAC_LE | FILE_WRITE_OWNER_LE | |
| 1911 | FILE_SYNCHRONIZE_LE; |
| 1912 | } else { |
| 1913 | rsp->ShareType = SMB2_SHARE_TYPE_DISK; |
| 1914 | rsp->MaximalAccess = FILE_READ_DATA_LE | FILE_READ_EA_LE | |
| 1915 | FILE_EXECUTE_LE | FILE_READ_ATTRIBUTES_LE; |
| 1916 | if (test_tree_conn_flag(status.tree_conn, |
| 1917 | KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
| 1918 | rsp->MaximalAccess |= FILE_WRITE_DATA_LE | |
| 1919 | FILE_APPEND_DATA_LE | FILE_WRITE_EA_LE | |
Wan Jiabing | 3aefd54 | 2021-06-07 12:54:32 +0800 | [diff] [blame] | 1920 | FILE_DELETE_LE | FILE_WRITE_ATTRIBUTES_LE | |
| 1921 | FILE_DELETE_CHILD_LE | FILE_READ_CONTROL_LE | |
| 1922 | FILE_WRITE_DAC_LE | FILE_WRITE_OWNER_LE | |
| 1923 | FILE_SYNCHRONIZE_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1924 | } |
| 1925 | } |
| 1926 | |
| 1927 | status.tree_conn->maximal_access = le32_to_cpu(rsp->MaximalAccess); |
| 1928 | if (conn->posix_ext_supported) |
| 1929 | status.tree_conn->posix_extensions = true; |
| 1930 | |
| 1931 | out_err1: |
| 1932 | rsp->StructureSize = cpu_to_le16(16); |
| 1933 | rsp->Capabilities = 0; |
| 1934 | rsp->Reserved = 0; |
| 1935 | /* default manual caching */ |
| 1936 | rsp->ShareFlags = SMB2_SHAREFLAG_MANUAL_CACHING; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1937 | inc_rfc1001_len(work->response_buf, 16); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1938 | |
| 1939 | if (!IS_ERR(treename)) |
| 1940 | kfree(treename); |
| 1941 | if (!IS_ERR(name)) |
| 1942 | kfree(name); |
| 1943 | |
| 1944 | switch (status.ret) { |
| 1945 | case KSMBD_TREE_CONN_STATUS_OK: |
| 1946 | rsp->hdr.Status = STATUS_SUCCESS; |
| 1947 | rc = 0; |
| 1948 | break; |
Atte Heikkilä | 4963d74 | 2022-08-09 01:02:14 +0300 | [diff] [blame] | 1949 | case -ESTALE: |
Namjae Jeon | fe54833 | 2022-08-08 21:56:48 +0900 | [diff] [blame] | 1950 | case -ENOENT: |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1951 | case KSMBD_TREE_CONN_STATUS_NO_SHARE: |
Namjae Jeon | fe54833 | 2022-08-08 21:56:48 +0900 | [diff] [blame] | 1952 | rsp->hdr.Status = STATUS_BAD_NETWORK_NAME; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1953 | break; |
| 1954 | case -ENOMEM: |
| 1955 | case KSMBD_TREE_CONN_STATUS_NOMEM: |
| 1956 | rsp->hdr.Status = STATUS_NO_MEMORY; |
| 1957 | break; |
| 1958 | case KSMBD_TREE_CONN_STATUS_ERROR: |
| 1959 | case KSMBD_TREE_CONN_STATUS_TOO_MANY_CONNS: |
| 1960 | case KSMBD_TREE_CONN_STATUS_TOO_MANY_SESSIONS: |
| 1961 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 1962 | break; |
| 1963 | case -EINVAL: |
| 1964 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 1965 | break; |
| 1966 | default: |
| 1967 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 1968 | } |
| 1969 | |
| 1970 | return rc; |
| 1971 | } |
| 1972 | |
| 1973 | /** |
| 1974 | * smb2_create_open_flags() - convert smb open flags to unix open flags |
| 1975 | * @file_present: is file already present |
| 1976 | * @access: file access flags |
| 1977 | * @disposition: file disposition flags |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 1978 | * @may_flags: set with MAY_ flags |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1979 | * |
| 1980 | * Return: file open flags |
| 1981 | */ |
| 1982 | static int smb2_create_open_flags(bool file_present, __le32 access, |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 1983 | __le32 disposition, |
| 1984 | int *may_flags) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1985 | { |
| 1986 | int oflags = O_NONBLOCK | O_LARGEFILE; |
| 1987 | |
| 1988 | if (access & FILE_READ_DESIRED_ACCESS_LE && |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 1989 | access & FILE_WRITE_DESIRE_ACCESS_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1990 | oflags |= O_RDWR; |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 1991 | *may_flags = MAY_OPEN | MAY_READ | MAY_WRITE; |
| 1992 | } else if (access & FILE_WRITE_DESIRE_ACCESS_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1993 | oflags |= O_WRONLY; |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 1994 | *may_flags = MAY_OPEN | MAY_WRITE; |
| 1995 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1996 | oflags |= O_RDONLY; |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 1997 | *may_flags = MAY_OPEN | MAY_READ; |
| 1998 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1999 | |
| 2000 | if (access == FILE_READ_ATTRIBUTES_LE) |
| 2001 | oflags |= O_PATH; |
| 2002 | |
| 2003 | if (file_present) { |
| 2004 | switch (disposition & FILE_CREATE_MASK_LE) { |
| 2005 | case FILE_OPEN_LE: |
| 2006 | case FILE_CREATE_LE: |
| 2007 | break; |
| 2008 | case FILE_SUPERSEDE_LE: |
| 2009 | case FILE_OVERWRITE_LE: |
| 2010 | case FILE_OVERWRITE_IF_LE: |
| 2011 | oflags |= O_TRUNC; |
| 2012 | break; |
| 2013 | default: |
| 2014 | break; |
| 2015 | } |
| 2016 | } else { |
| 2017 | switch (disposition & FILE_CREATE_MASK_LE) { |
| 2018 | case FILE_SUPERSEDE_LE: |
| 2019 | case FILE_CREATE_LE: |
| 2020 | case FILE_OPEN_IF_LE: |
| 2021 | case FILE_OVERWRITE_IF_LE: |
| 2022 | oflags |= O_CREAT; |
| 2023 | break; |
| 2024 | case FILE_OPEN_LE: |
| 2025 | case FILE_OVERWRITE_LE: |
| 2026 | oflags &= ~O_CREAT; |
| 2027 | break; |
| 2028 | default: |
| 2029 | break; |
| 2030 | } |
| 2031 | } |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 2032 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2033 | return oflags; |
| 2034 | } |
| 2035 | |
| 2036 | /** |
| 2037 | * smb2_tree_disconnect() - handler for smb tree connect request |
| 2038 | * @work: smb work containing request buffer |
| 2039 | * |
| 2040 | * Return: 0 |
| 2041 | */ |
| 2042 | int smb2_tree_disconnect(struct ksmbd_work *work) |
| 2043 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 2044 | struct smb2_tree_disconnect_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2045 | struct ksmbd_session *sess = work->sess; |
| 2046 | struct ksmbd_tree_connect *tcon = work->tcon; |
| 2047 | |
| 2048 | rsp->StructureSize = cpu_to_le16(4); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 2049 | inc_rfc1001_len(work->response_buf, 4); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2050 | |
| 2051 | ksmbd_debug(SMB, "request\n"); |
| 2052 | |
| 2053 | if (!tcon) { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 2054 | struct smb2_tree_disconnect_req *req = |
| 2055 | smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2056 | |
| 2057 | ksmbd_debug(SMB, "Invalid tid %d\n", req->hdr.Id.SyncId.TreeId); |
| 2058 | rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED; |
| 2059 | smb2_set_err_rsp(work); |
| 2060 | return 0; |
| 2061 | } |
| 2062 | |
| 2063 | ksmbd_close_tree_conn_fds(work); |
| 2064 | ksmbd_tree_conn_disconnect(sess, tcon); |
Namjae Jeon | cf6531d | 2022-07-28 21:57:08 +0900 | [diff] [blame] | 2065 | work->tcon = NULL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2066 | return 0; |
| 2067 | } |
| 2068 | |
| 2069 | /** |
| 2070 | * smb2_session_logoff() - handler for session log off request |
| 2071 | * @work: smb work containing request buffer |
| 2072 | * |
| 2073 | * Return: 0 |
| 2074 | */ |
| 2075 | int smb2_session_logoff(struct ksmbd_work *work) |
| 2076 | { |
| 2077 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 2078 | struct smb2_logoff_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2079 | struct ksmbd_session *sess = work->sess; |
| 2080 | |
| 2081 | rsp->StructureSize = cpu_to_le16(4); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 2082 | inc_rfc1001_len(work->response_buf, 4); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2083 | |
| 2084 | ksmbd_debug(SMB, "request\n"); |
| 2085 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2086 | /* setting CifsExiting here may race with start_tcp_sess */ |
| 2087 | ksmbd_conn_set_need_reconnect(work); |
| 2088 | ksmbd_close_session_fds(work); |
| 2089 | ksmbd_conn_wait_idle(conn); |
| 2090 | |
| 2091 | if (ksmbd_tree_conn_session_logoff(sess)) { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 2092 | struct smb2_logoff_req *req = smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2093 | |
| 2094 | ksmbd_debug(SMB, "Invalid tid %d\n", req->hdr.Id.SyncId.TreeId); |
| 2095 | rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED; |
| 2096 | smb2_set_err_rsp(work); |
| 2097 | return 0; |
| 2098 | } |
| 2099 | |
| 2100 | ksmbd_destroy_file_table(&sess->file_table); |
| 2101 | sess->state = SMB2_SESSION_EXPIRED; |
| 2102 | |
| 2103 | ksmbd_free_user(sess->user); |
| 2104 | sess->user = NULL; |
| 2105 | |
| 2106 | /* let start_tcp_sess free connection info now */ |
| 2107 | ksmbd_conn_set_need_negotiate(work); |
| 2108 | return 0; |
| 2109 | } |
| 2110 | |
| 2111 | /** |
| 2112 | * create_smb2_pipe() - create IPC pipe |
| 2113 | * @work: smb work containing request buffer |
| 2114 | * |
| 2115 | * Return: 0 on success, otherwise error |
| 2116 | */ |
| 2117 | static noinline int create_smb2_pipe(struct ksmbd_work *work) |
| 2118 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 2119 | struct smb2_create_rsp *rsp = smb2_get_msg(work->response_buf); |
| 2120 | struct smb2_create_req *req = smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2121 | int id; |
| 2122 | int err; |
| 2123 | char *name; |
| 2124 | |
| 2125 | name = smb_strndup_from_utf16(req->Buffer, le16_to_cpu(req->NameLength), |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2126 | 1, work->conn->local_nls); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2127 | if (IS_ERR(name)) { |
| 2128 | rsp->hdr.Status = STATUS_NO_MEMORY; |
| 2129 | err = PTR_ERR(name); |
| 2130 | goto out; |
| 2131 | } |
| 2132 | |
| 2133 | id = ksmbd_session_rpc_open(work->sess, name); |
Marios Makassikis | 79caa96 | 2021-05-06 11:38:35 +0900 | [diff] [blame] | 2134 | if (id < 0) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2135 | pr_err("Unable to open RPC pipe: %d\n", id); |
Marios Makassikis | 79caa96 | 2021-05-06 11:38:35 +0900 | [diff] [blame] | 2136 | err = id; |
| 2137 | goto out; |
| 2138 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2139 | |
Marios Makassikis | 79caa96 | 2021-05-06 11:38:35 +0900 | [diff] [blame] | 2140 | rsp->hdr.Status = STATUS_SUCCESS; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2141 | rsp->StructureSize = cpu_to_le16(89); |
| 2142 | rsp->OplockLevel = SMB2_OPLOCK_LEVEL_NONE; |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 2143 | rsp->Flags = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2144 | rsp->CreateAction = cpu_to_le32(FILE_OPENED); |
| 2145 | |
| 2146 | rsp->CreationTime = cpu_to_le64(0); |
| 2147 | rsp->LastAccessTime = cpu_to_le64(0); |
| 2148 | rsp->ChangeTime = cpu_to_le64(0); |
| 2149 | rsp->AllocationSize = cpu_to_le64(0); |
| 2150 | rsp->EndofFile = cpu_to_le64(0); |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 2151 | rsp->FileAttributes = FILE_ATTRIBUTE_NORMAL_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2152 | rsp->Reserved2 = 0; |
Paulo Alcantara | 2d004c6 | 2022-03-21 13:08:26 -0300 | [diff] [blame] | 2153 | rsp->VolatileFileId = id; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2154 | rsp->PersistentFileId = 0; |
| 2155 | rsp->CreateContextsOffset = 0; |
| 2156 | rsp->CreateContextsLength = 0; |
| 2157 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 2158 | inc_rfc1001_len(work->response_buf, 88); /* StructureSize - 1*/ |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2159 | kfree(name); |
| 2160 | return 0; |
| 2161 | |
| 2162 | out: |
Marios Makassikis | 79caa96 | 2021-05-06 11:38:35 +0900 | [diff] [blame] | 2163 | switch (err) { |
| 2164 | case -EINVAL: |
| 2165 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 2166 | break; |
| 2167 | case -ENOSPC: |
| 2168 | case -ENOMEM: |
| 2169 | rsp->hdr.Status = STATUS_NO_MEMORY; |
| 2170 | break; |
| 2171 | } |
| 2172 | |
| 2173 | if (!IS_ERR(name)) |
| 2174 | kfree(name); |
| 2175 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2176 | smb2_set_err_rsp(work); |
| 2177 | return err; |
| 2178 | } |
| 2179 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2180 | /** |
| 2181 | * smb2_set_ea() - handler for setting extended attributes using set |
| 2182 | * info command |
| 2183 | * @eabuf: set info command buffer |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 2184 | * @buf_len: set info command buffer length |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2185 | * @path: dentry path for get ea |
| 2186 | * |
| 2187 | * Return: 0 on success, otherwise error |
| 2188 | */ |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 2189 | static int smb2_set_ea(struct smb2_ea_info *eabuf, unsigned int buf_len, |
Al Viro | c22180a | 2022-08-04 12:51:14 -0400 | [diff] [blame] | 2190 | const struct path *path) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2191 | { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2192 | struct user_namespace *user_ns = mnt_user_ns(path->mnt); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2193 | char *attr_name = NULL, *value; |
| 2194 | int rc = 0; |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 2195 | unsigned int next = 0; |
| 2196 | |
| 2197 | if (buf_len < sizeof(struct smb2_ea_info) + eabuf->EaNameLength + |
| 2198 | le16_to_cpu(eabuf->EaValueLength)) |
| 2199 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2200 | |
| 2201 | attr_name = kmalloc(XATTR_NAME_MAX + 1, GFP_KERNEL); |
| 2202 | if (!attr_name) |
| 2203 | return -ENOMEM; |
| 2204 | |
| 2205 | do { |
| 2206 | if (!eabuf->EaNameLength) |
| 2207 | goto next; |
| 2208 | |
| 2209 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2210 | "name : <%s>, name_len : %u, value_len : %u, next : %u\n", |
| 2211 | eabuf->name, eabuf->EaNameLength, |
| 2212 | le16_to_cpu(eabuf->EaValueLength), |
| 2213 | le32_to_cpu(eabuf->NextEntryOffset)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2214 | |
| 2215 | if (eabuf->EaNameLength > |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2216 | (XATTR_NAME_MAX - XATTR_USER_PREFIX_LEN)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2217 | rc = -EINVAL; |
| 2218 | break; |
| 2219 | } |
| 2220 | |
| 2221 | memcpy(attr_name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN); |
| 2222 | memcpy(&attr_name[XATTR_USER_PREFIX_LEN], eabuf->name, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2223 | eabuf->EaNameLength); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2224 | attr_name[XATTR_USER_PREFIX_LEN + eabuf->EaNameLength] = '\0'; |
| 2225 | value = (char *)&eabuf->name + eabuf->EaNameLength + 1; |
| 2226 | |
| 2227 | if (!eabuf->EaValueLength) { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2228 | rc = ksmbd_vfs_casexattr_len(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2229 | path->dentry, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2230 | attr_name, |
| 2231 | XATTR_USER_PREFIX_LEN + |
| 2232 | eabuf->EaNameLength); |
| 2233 | |
| 2234 | /* delete the EA only when it exits */ |
| 2235 | if (rc > 0) { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2236 | rc = ksmbd_vfs_remove_xattr(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2237 | path->dentry, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2238 | attr_name); |
| 2239 | |
| 2240 | if (rc < 0) { |
| 2241 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2242 | "remove xattr failed(%d)\n", |
| 2243 | rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2244 | break; |
| 2245 | } |
| 2246 | } |
| 2247 | |
| 2248 | /* if the EA doesn't exist, just do nothing. */ |
| 2249 | rc = 0; |
| 2250 | } else { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2251 | rc = ksmbd_vfs_setxattr(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2252 | path->dentry, attr_name, value, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2253 | le16_to_cpu(eabuf->EaValueLength), 0); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2254 | if (rc < 0) { |
| 2255 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2256 | "ksmbd_vfs_setxattr is failed(%d)\n", |
| 2257 | rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2258 | break; |
| 2259 | } |
| 2260 | } |
| 2261 | |
| 2262 | next: |
| 2263 | next = le32_to_cpu(eabuf->NextEntryOffset); |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 2264 | if (next == 0 || buf_len < next) |
| 2265 | break; |
| 2266 | buf_len -= next; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2267 | eabuf = (struct smb2_ea_info *)((char *)eabuf + next); |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 2268 | if (next < (u32)eabuf->EaNameLength + le16_to_cpu(eabuf->EaValueLength)) |
| 2269 | break; |
| 2270 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2271 | } while (next != 0); |
| 2272 | |
| 2273 | kfree(attr_name); |
| 2274 | return rc; |
| 2275 | } |
| 2276 | |
Al Viro | c22180a | 2022-08-04 12:51:14 -0400 | [diff] [blame] | 2277 | static noinline int smb2_set_stream_name_xattr(const struct path *path, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2278 | struct ksmbd_file *fp, |
| 2279 | char *stream_name, int s_type) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2280 | { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2281 | struct user_namespace *user_ns = mnt_user_ns(path->mnt); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2282 | size_t xattr_stream_size; |
| 2283 | char *xattr_stream_name; |
| 2284 | int rc; |
| 2285 | |
| 2286 | rc = ksmbd_vfs_xattr_stream_name(stream_name, |
| 2287 | &xattr_stream_name, |
| 2288 | &xattr_stream_size, |
| 2289 | s_type); |
| 2290 | if (rc) |
| 2291 | return rc; |
| 2292 | |
| 2293 | fp->stream.name = xattr_stream_name; |
| 2294 | fp->stream.size = xattr_stream_size; |
| 2295 | |
| 2296 | /* Check if there is stream prefix in xattr space */ |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2297 | rc = ksmbd_vfs_casexattr_len(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2298 | path->dentry, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2299 | xattr_stream_name, |
| 2300 | xattr_stream_size); |
| 2301 | if (rc >= 0) |
| 2302 | return 0; |
| 2303 | |
| 2304 | if (fp->cdoption == FILE_OPEN_LE) { |
| 2305 | ksmbd_debug(SMB, "XATTR stream name lookup failed: %d\n", rc); |
| 2306 | return -EBADF; |
| 2307 | } |
| 2308 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2309 | rc = ksmbd_vfs_setxattr(user_ns, path->dentry, |
| 2310 | xattr_stream_name, NULL, 0, 0); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2311 | if (rc < 0) |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2312 | pr_err("Failed to store XATTR stream name :%d\n", rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2313 | return 0; |
| 2314 | } |
| 2315 | |
Al Viro | c22180a | 2022-08-04 12:51:14 -0400 | [diff] [blame] | 2316 | static int smb2_remove_smb_xattrs(const struct path *path) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2317 | { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2318 | struct user_namespace *user_ns = mnt_user_ns(path->mnt); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2319 | char *name, *xattr_list = NULL; |
| 2320 | ssize_t xattr_list_len; |
| 2321 | int err = 0; |
| 2322 | |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 2323 | xattr_list_len = ksmbd_vfs_listxattr(path->dentry, &xattr_list); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2324 | if (xattr_list_len < 0) { |
| 2325 | goto out; |
| 2326 | } else if (!xattr_list_len) { |
| 2327 | ksmbd_debug(SMB, "empty xattr in the file\n"); |
| 2328 | goto out; |
| 2329 | } |
| 2330 | |
| 2331 | for (name = xattr_list; name - xattr_list < xattr_list_len; |
| 2332 | name += strlen(name) + 1) { |
| 2333 | ksmbd_debug(SMB, "%s, len %zd\n", name, strlen(name)); |
| 2334 | |
Namjae Jeon | 17661ec | 2022-08-14 22:40:25 +0900 | [diff] [blame] | 2335 | if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) && |
| 2336 | !strncmp(&name[XATTR_USER_PREFIX_LEN], STREAM_PREFIX, |
| 2337 | STREAM_PREFIX_LEN)) { |
| 2338 | err = ksmbd_vfs_remove_xattr(user_ns, path->dentry, |
| 2339 | name); |
| 2340 | if (err) |
| 2341 | ksmbd_debug(SMB, "remove xattr failed : %s\n", |
| 2342 | name); |
| 2343 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2344 | } |
| 2345 | out: |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 2346 | kvfree(xattr_list); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2347 | return err; |
| 2348 | } |
| 2349 | |
Al Viro | c22180a | 2022-08-04 12:51:14 -0400 | [diff] [blame] | 2350 | static int smb2_create_truncate(const struct path *path) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2351 | { |
| 2352 | int rc = vfs_truncate(path, 0); |
| 2353 | |
| 2354 | if (rc) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2355 | pr_err("vfs_truncate failed, rc %d\n", rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2356 | return rc; |
| 2357 | } |
| 2358 | |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 2359 | rc = smb2_remove_smb_xattrs(path); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2360 | if (rc == -EOPNOTSUPP) |
| 2361 | rc = 0; |
| 2362 | if (rc) |
| 2363 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2364 | "ksmbd_truncate_stream_name_xattr failed, rc %d\n", |
| 2365 | rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2366 | return rc; |
| 2367 | } |
| 2368 | |
Al Viro | c22180a | 2022-08-04 12:51:14 -0400 | [diff] [blame] | 2369 | static void smb2_new_xattrs(struct ksmbd_tree_connect *tcon, const struct path *path, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2370 | struct ksmbd_file *fp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2371 | { |
| 2372 | struct xattr_dos_attrib da = {0}; |
| 2373 | int rc; |
| 2374 | |
| 2375 | if (!test_share_config_flag(tcon->share_conf, |
| 2376 | KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) |
| 2377 | return; |
| 2378 | |
| 2379 | da.version = 4; |
| 2380 | da.attr = le32_to_cpu(fp->f_ci->m_fattr); |
| 2381 | da.itime = da.create_time = fp->create_time; |
| 2382 | da.flags = XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME | |
| 2383 | XATTR_DOSINFO_ITIME; |
| 2384 | |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2385 | rc = ksmbd_vfs_set_dos_attrib_xattr(mnt_user_ns(path->mnt), |
| 2386 | path->dentry, &da); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2387 | if (rc) |
| 2388 | ksmbd_debug(SMB, "failed to store file attribute into xattr\n"); |
| 2389 | } |
| 2390 | |
| 2391 | static void smb2_update_xattrs(struct ksmbd_tree_connect *tcon, |
Al Viro | c22180a | 2022-08-04 12:51:14 -0400 | [diff] [blame] | 2392 | const struct path *path, struct ksmbd_file *fp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2393 | { |
| 2394 | struct xattr_dos_attrib da; |
| 2395 | int rc; |
| 2396 | |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 2397 | fp->f_ci->m_fattr &= ~(FILE_ATTRIBUTE_HIDDEN_LE | FILE_ATTRIBUTE_SYSTEM_LE); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2398 | |
| 2399 | /* get FileAttributes from XATTR_NAME_DOS_ATTRIBUTE */ |
| 2400 | if (!test_share_config_flag(tcon->share_conf, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2401 | KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2402 | return; |
| 2403 | |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2404 | rc = ksmbd_vfs_get_dos_attrib_xattr(mnt_user_ns(path->mnt), |
| 2405 | path->dentry, &da); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2406 | if (rc > 0) { |
| 2407 | fp->f_ci->m_fattr = cpu_to_le32(da.attr); |
| 2408 | fp->create_time = da.create_time; |
| 2409 | fp->itime = da.itime; |
| 2410 | } |
| 2411 | } |
| 2412 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2413 | static int smb2_creat(struct ksmbd_work *work, struct path *path, char *name, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2414 | int open_flags, umode_t posix_mode, bool is_dir) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2415 | { |
| 2416 | struct ksmbd_tree_connect *tcon = work->tcon; |
| 2417 | struct ksmbd_share_config *share = tcon->share_conf; |
| 2418 | umode_t mode; |
| 2419 | int rc; |
| 2420 | |
| 2421 | if (!(open_flags & O_CREAT)) |
| 2422 | return -EBADF; |
| 2423 | |
| 2424 | ksmbd_debug(SMB, "file does not exist, so creating\n"); |
| 2425 | if (is_dir == true) { |
| 2426 | ksmbd_debug(SMB, "creating directory\n"); |
| 2427 | |
| 2428 | mode = share_config_directory_mode(share, posix_mode); |
| 2429 | rc = ksmbd_vfs_mkdir(work, name, mode); |
| 2430 | if (rc) |
| 2431 | return rc; |
| 2432 | } else { |
| 2433 | ksmbd_debug(SMB, "creating regular file\n"); |
| 2434 | |
| 2435 | mode = share_config_create_mode(share, posix_mode); |
| 2436 | rc = ksmbd_vfs_create(work, name, mode); |
| 2437 | if (rc) |
| 2438 | return rc; |
| 2439 | } |
| 2440 | |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 2441 | rc = ksmbd_vfs_kern_path(work, name, 0, path, 0); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2442 | if (rc) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2443 | pr_err("cannot get linux path (%s), err = %d\n", |
| 2444 | name, rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2445 | return rc; |
| 2446 | } |
| 2447 | return 0; |
| 2448 | } |
| 2449 | |
| 2450 | static int smb2_create_sd_buffer(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2451 | struct smb2_create_req *req, |
Al Viro | c22180a | 2022-08-04 12:51:14 -0400 | [diff] [blame] | 2452 | const struct path *path) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2453 | { |
| 2454 | struct create_context *context; |
Hyunchul Lee | 21dd1fd | 2021-07-09 17:06:34 +0900 | [diff] [blame] | 2455 | struct create_sd_buf_req *sd_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2456 | |
| 2457 | if (!req->CreateContextsOffset) |
Hyunchul Lee | 21dd1fd | 2021-07-09 17:06:34 +0900 | [diff] [blame] | 2458 | return -ENOENT; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2459 | |
| 2460 | /* Parse SD BUFFER create contexts */ |
| 2461 | context = smb2_find_context_vals(req, SMB2_CREATE_SD_BUFFER); |
Hyunchul Lee | 21dd1fd | 2021-07-09 17:06:34 +0900 | [diff] [blame] | 2462 | if (!context) |
| 2463 | return -ENOENT; |
| 2464 | else if (IS_ERR(context)) |
| 2465 | return PTR_ERR(context); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2466 | |
Hyunchul Lee | 21dd1fd | 2021-07-09 17:06:34 +0900 | [diff] [blame] | 2467 | ksmbd_debug(SMB, |
| 2468 | "Set ACLs using SMB2_CREATE_SD_BUFFER context\n"); |
| 2469 | sd_buf = (struct create_sd_buf_req *)context; |
Hyunchul Lee | 8f77150 | 2021-09-24 22:22:22 +0900 | [diff] [blame] | 2470 | if (le16_to_cpu(context->DataOffset) + |
| 2471 | le32_to_cpu(context->DataLength) < |
| 2472 | sizeof(struct create_sd_buf_req)) |
| 2473 | return -EINVAL; |
Hyunchul Lee | 21dd1fd | 2021-07-09 17:06:34 +0900 | [diff] [blame] | 2474 | return set_info_sec(work->conn, work->tcon, path, &sd_buf->ntsd, |
| 2475 | le32_to_cpu(sd_buf->ccontext.DataLength), true); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2476 | } |
| 2477 | |
Christian Brauner | 43205ca | 2021-08-23 17:13:50 +0200 | [diff] [blame] | 2478 | static void ksmbd_acls_fattr(struct smb_fattr *fattr, |
| 2479 | struct user_namespace *mnt_userns, |
| 2480 | struct inode *inode) |
Namjae Jeon | 3d47e54 | 2021-04-20 14:25:35 +0900 | [diff] [blame] | 2481 | { |
Christian Brauner | 276a3f7 | 2022-09-09 12:51:19 +0200 | [diff] [blame] | 2482 | vfsuid_t vfsuid = i_uid_into_vfsuid(mnt_userns, inode); |
| 2483 | vfsgid_t vfsgid = i_gid_into_vfsgid(mnt_userns, inode); |
| 2484 | |
| 2485 | fattr->cf_uid = vfsuid_into_kuid(vfsuid); |
| 2486 | fattr->cf_gid = vfsgid_into_kgid(vfsgid); |
Namjae Jeon | 3d47e54 | 2021-04-20 14:25:35 +0900 | [diff] [blame] | 2487 | fattr->cf_mode = inode->i_mode; |
Namjae Jeon | 777cad1 | 2021-08-13 08:15:33 +0900 | [diff] [blame] | 2488 | fattr->cf_acls = NULL; |
Namjae Jeon | 3d47e54 | 2021-04-20 14:25:35 +0900 | [diff] [blame] | 2489 | fattr->cf_dacls = NULL; |
| 2490 | |
Namjae Jeon | 777cad1 | 2021-08-13 08:15:33 +0900 | [diff] [blame] | 2491 | if (IS_ENABLED(CONFIG_FS_POSIX_ACL)) { |
Christian Brauner | cac2f8b | 2022-09-22 17:17:00 +0200 | [diff] [blame] | 2492 | fattr->cf_acls = get_inode_acl(inode, ACL_TYPE_ACCESS); |
Namjae Jeon | 777cad1 | 2021-08-13 08:15:33 +0900 | [diff] [blame] | 2493 | if (S_ISDIR(inode->i_mode)) |
Christian Brauner | cac2f8b | 2022-09-22 17:17:00 +0200 | [diff] [blame] | 2494 | fattr->cf_dacls = get_inode_acl(inode, ACL_TYPE_DEFAULT); |
Namjae Jeon | 777cad1 | 2021-08-13 08:15:33 +0900 | [diff] [blame] | 2495 | } |
Namjae Jeon | 3d47e54 | 2021-04-20 14:25:35 +0900 | [diff] [blame] | 2496 | } |
| 2497 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2498 | /** |
| 2499 | * smb2_open() - handler for smb file open request |
| 2500 | * @work: smb work containing request buffer |
| 2501 | * |
| 2502 | * Return: 0 on success, otherwise error |
| 2503 | */ |
| 2504 | int smb2_open(struct ksmbd_work *work) |
| 2505 | { |
| 2506 | struct ksmbd_conn *conn = work->conn; |
| 2507 | struct ksmbd_session *sess = work->sess; |
| 2508 | struct ksmbd_tree_connect *tcon = work->tcon; |
| 2509 | struct smb2_create_req *req; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 2510 | struct smb2_create_rsp *rsp; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2511 | struct path path; |
| 2512 | struct ksmbd_share_config *share = tcon->share_conf; |
| 2513 | struct ksmbd_file *fp = NULL; |
| 2514 | struct file *filp = NULL; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2515 | struct user_namespace *user_ns = NULL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2516 | struct kstat stat; |
| 2517 | struct create_context *context; |
| 2518 | struct lease_ctx_info *lc = NULL; |
| 2519 | struct create_ea_buf_req *ea_buf = NULL; |
| 2520 | struct oplock_info *opinfo; |
| 2521 | __le32 *next_ptr = NULL; |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 2522 | int req_op_level = 0, open_flags = 0, may_flags = 0, file_info = 0; |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 2523 | int rc = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2524 | int contxt_cnt = 0, query_disk_id = 0; |
| 2525 | int maximal_access_ctxt = 0, posix_ctxt = 0; |
| 2526 | int s_type = 0; |
| 2527 | int next_off = 0; |
| 2528 | char *name = NULL; |
| 2529 | char *stream_name = NULL; |
| 2530 | bool file_present = false, created = false, already_permitted = false; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2531 | int share_ret, need_truncate = 0; |
| 2532 | u64 time; |
| 2533 | umode_t posix_mode = 0; |
| 2534 | __le32 daccess, maximal_access = 0; |
| 2535 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2536 | WORK_BUFFERS(work, req, rsp); |
| 2537 | |
| 2538 | if (req->hdr.NextCommand && !work->next_smb2_rcv_hdr_off && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2539 | (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2540 | ksmbd_debug(SMB, "invalid flag in chained command\n"); |
| 2541 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 2542 | smb2_set_err_rsp(work); |
| 2543 | return -EINVAL; |
| 2544 | } |
| 2545 | |
| 2546 | if (test_share_config_flag(share, KSMBD_SHARE_FLAG_PIPE)) { |
| 2547 | ksmbd_debug(SMB, "IPC pipe create request\n"); |
| 2548 | return create_smb2_pipe(work); |
| 2549 | } |
| 2550 | |
| 2551 | if (req->NameLength) { |
| 2552 | if ((req->CreateOptions & FILE_DIRECTORY_FILE_LE) && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2553 | *(char *)req->Buffer == '\\') { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2554 | pr_err("not allow directory name included leading slash\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2555 | rc = -EINVAL; |
| 2556 | goto err_out1; |
| 2557 | } |
| 2558 | |
Marios Makassikis | 80917f1 | 2021-12-01 21:41:19 +0100 | [diff] [blame] | 2559 | name = smb2_get_name(req->Buffer, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2560 | le16_to_cpu(req->NameLength), |
| 2561 | work->conn->local_nls); |
| 2562 | if (IS_ERR(name)) { |
| 2563 | rc = PTR_ERR(name); |
| 2564 | if (rc != -ENOMEM) |
| 2565 | rc = -ENOENT; |
Dan Carpenter | 8b99f35 | 2021-08-02 08:14:03 +0900 | [diff] [blame] | 2566 | name = NULL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2567 | goto err_out1; |
| 2568 | } |
| 2569 | |
| 2570 | ksmbd_debug(SMB, "converted name = %s\n", name); |
| 2571 | if (strchr(name, ':')) { |
| 2572 | if (!test_share_config_flag(work->tcon->share_conf, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2573 | KSMBD_SHARE_FLAG_STREAMS)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2574 | rc = -EBADF; |
| 2575 | goto err_out1; |
| 2576 | } |
| 2577 | rc = parse_stream_name(name, &stream_name, &s_type); |
| 2578 | if (rc < 0) |
| 2579 | goto err_out1; |
| 2580 | } |
| 2581 | |
| 2582 | rc = ksmbd_validate_filename(name); |
| 2583 | if (rc < 0) |
| 2584 | goto err_out1; |
| 2585 | |
| 2586 | if (ksmbd_share_veto_filename(share, name)) { |
| 2587 | rc = -ENOENT; |
| 2588 | ksmbd_debug(SMB, "Reject open(), vetoed file: %s\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2589 | name); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2590 | goto err_out1; |
| 2591 | } |
| 2592 | } else { |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 2593 | name = kstrdup("", GFP_KERNEL); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2594 | if (!name) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2595 | rc = -ENOMEM; |
| 2596 | goto err_out1; |
| 2597 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2598 | } |
| 2599 | |
| 2600 | req_op_level = req->RequestedOplockLevel; |
Namjae Jeon | 73f9dad | 2021-04-16 14:12:06 +0900 | [diff] [blame] | 2601 | if (req_op_level == SMB2_OPLOCK_LEVEL_LEASE) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2602 | lc = parse_lease_state(req); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2603 | |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 2604 | if (le32_to_cpu(req->ImpersonationLevel) > le32_to_cpu(IL_DELEGATE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2605 | pr_err("Invalid impersonationlevel : 0x%x\n", |
| 2606 | le32_to_cpu(req->ImpersonationLevel)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2607 | rc = -EIO; |
| 2608 | rsp->hdr.Status = STATUS_BAD_IMPERSONATION_LEVEL; |
| 2609 | goto err_out1; |
| 2610 | } |
| 2611 | |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 2612 | if (req->CreateOptions && !(req->CreateOptions & CREATE_OPTIONS_MASK_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2613 | pr_err("Invalid create options : 0x%x\n", |
| 2614 | le32_to_cpu(req->CreateOptions)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2615 | rc = -EINVAL; |
| 2616 | goto err_out1; |
| 2617 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2618 | if (req->CreateOptions & FILE_SEQUENTIAL_ONLY_LE && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2619 | req->CreateOptions & FILE_RANDOM_ACCESS_LE) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2620 | req->CreateOptions = ~(FILE_SEQUENTIAL_ONLY_LE); |
| 2621 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2622 | if (req->CreateOptions & |
| 2623 | (FILE_OPEN_BY_FILE_ID_LE | CREATE_TREE_CONNECTION | |
| 2624 | FILE_RESERVE_OPFILTER_LE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2625 | rc = -EOPNOTSUPP; |
| 2626 | goto err_out1; |
| 2627 | } |
| 2628 | |
| 2629 | if (req->CreateOptions & FILE_DIRECTORY_FILE_LE) { |
| 2630 | if (req->CreateOptions & FILE_NON_DIRECTORY_FILE_LE) { |
| 2631 | rc = -EINVAL; |
| 2632 | goto err_out1; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2633 | } else if (req->CreateOptions & FILE_NO_COMPRESSION_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2634 | req->CreateOptions = ~(FILE_NO_COMPRESSION_LE); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2635 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2636 | } |
| 2637 | } |
| 2638 | |
| 2639 | if (le32_to_cpu(req->CreateDisposition) > |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2640 | le32_to_cpu(FILE_OVERWRITE_IF_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2641 | pr_err("Invalid create disposition : 0x%x\n", |
| 2642 | le32_to_cpu(req->CreateDisposition)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2643 | rc = -EINVAL; |
| 2644 | goto err_out1; |
| 2645 | } |
| 2646 | |
| 2647 | if (!(req->DesiredAccess & DESIRED_ACCESS_MASK)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2648 | pr_err("Invalid desired access : 0x%x\n", |
| 2649 | le32_to_cpu(req->DesiredAccess)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2650 | rc = -EACCES; |
| 2651 | goto err_out1; |
| 2652 | } |
| 2653 | |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 2654 | if (req->FileAttributes && !(req->FileAttributes & FILE_ATTRIBUTE_MASK_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2655 | pr_err("Invalid file attribute : 0x%x\n", |
| 2656 | le32_to_cpu(req->FileAttributes)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2657 | rc = -EINVAL; |
| 2658 | goto err_out1; |
| 2659 | } |
| 2660 | |
| 2661 | if (req->CreateContextsOffset) { |
| 2662 | /* Parse non-durable handle create contexts */ |
| 2663 | context = smb2_find_context_vals(req, SMB2_CREATE_EA_BUFFER); |
Namjae Jeon | f19b396 | 2021-07-13 09:59:34 +0900 | [diff] [blame] | 2664 | if (IS_ERR(context)) { |
| 2665 | rc = PTR_ERR(context); |
| 2666 | goto err_out1; |
| 2667 | } else if (context) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2668 | ea_buf = (struct create_ea_buf_req *)context; |
Hyunchul Lee | 8f77150 | 2021-09-24 22:22:22 +0900 | [diff] [blame] | 2669 | if (le16_to_cpu(context->DataOffset) + |
| 2670 | le32_to_cpu(context->DataLength) < |
| 2671 | sizeof(struct create_ea_buf_req)) { |
| 2672 | rc = -EINVAL; |
| 2673 | goto err_out1; |
| 2674 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2675 | if (req->CreateOptions & FILE_NO_EA_KNOWLEDGE_LE) { |
| 2676 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 2677 | rc = -EACCES; |
| 2678 | goto err_out1; |
| 2679 | } |
| 2680 | } |
| 2681 | |
| 2682 | context = smb2_find_context_vals(req, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2683 | SMB2_CREATE_QUERY_MAXIMAL_ACCESS_REQUEST); |
Namjae Jeon | f19b396 | 2021-07-13 09:59:34 +0900 | [diff] [blame] | 2684 | if (IS_ERR(context)) { |
| 2685 | rc = PTR_ERR(context); |
| 2686 | goto err_out1; |
| 2687 | } else if (context) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2688 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2689 | "get query maximal access context\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2690 | maximal_access_ctxt = 1; |
| 2691 | } |
| 2692 | |
| 2693 | context = smb2_find_context_vals(req, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2694 | SMB2_CREATE_TIMEWARP_REQUEST); |
Namjae Jeon | f19b396 | 2021-07-13 09:59:34 +0900 | [diff] [blame] | 2695 | if (IS_ERR(context)) { |
| 2696 | rc = PTR_ERR(context); |
| 2697 | goto err_out1; |
| 2698 | } else if (context) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2699 | ksmbd_debug(SMB, "get timewarp context\n"); |
| 2700 | rc = -EBADF; |
| 2701 | goto err_out1; |
| 2702 | } |
| 2703 | |
| 2704 | if (tcon->posix_extensions) { |
| 2705 | context = smb2_find_context_vals(req, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2706 | SMB2_CREATE_TAG_POSIX); |
Namjae Jeon | f19b396 | 2021-07-13 09:59:34 +0900 | [diff] [blame] | 2707 | if (IS_ERR(context)) { |
| 2708 | rc = PTR_ERR(context); |
| 2709 | goto err_out1; |
| 2710 | } else if (context) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2711 | struct create_posix *posix = |
| 2712 | (struct create_posix *)context; |
Hyunchul Lee | 8f77150 | 2021-09-24 22:22:22 +0900 | [diff] [blame] | 2713 | if (le16_to_cpu(context->DataOffset) + |
| 2714 | le32_to_cpu(context->DataLength) < |
Namjae Jeon | 9ca8581 | 2022-01-22 10:47:22 +0900 | [diff] [blame] | 2715 | sizeof(struct create_posix) - 4) { |
Hyunchul Lee | 8f77150 | 2021-09-24 22:22:22 +0900 | [diff] [blame] | 2716 | rc = -EINVAL; |
| 2717 | goto err_out1; |
| 2718 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2719 | ksmbd_debug(SMB, "get posix context\n"); |
| 2720 | |
| 2721 | posix_mode = le32_to_cpu(posix->Mode); |
| 2722 | posix_ctxt = 1; |
| 2723 | } |
| 2724 | } |
| 2725 | } |
| 2726 | |
| 2727 | if (ksmbd_override_fsids(work)) { |
| 2728 | rc = -ENOMEM; |
| 2729 | goto err_out1; |
| 2730 | } |
| 2731 | |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 2732 | rc = ksmbd_vfs_kern_path(work, name, LOOKUP_NO_SYMLINKS, &path, 1); |
Namjae Jeon | 4ea4779 | 2021-09-21 14:19:33 +0900 | [diff] [blame] | 2733 | if (!rc) { |
| 2734 | if (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2735 | /* |
| 2736 | * If file exists with under flags, return access |
| 2737 | * denied error. |
| 2738 | */ |
| 2739 | if (req->CreateDisposition == FILE_OVERWRITE_IF_LE || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2740 | req->CreateDisposition == FILE_OPEN_IF_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2741 | rc = -EACCES; |
| 2742 | path_put(&path); |
| 2743 | goto err_out; |
| 2744 | } |
| 2745 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2746 | if (!test_tree_conn_flag(tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2747 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2748 | "User does not have write permission\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2749 | rc = -EACCES; |
| 2750 | path_put(&path); |
| 2751 | goto err_out; |
| 2752 | } |
Namjae Jeon | 4ea4779 | 2021-09-21 14:19:33 +0900 | [diff] [blame] | 2753 | } else if (d_is_symlink(path.dentry)) { |
| 2754 | rc = -EACCES; |
| 2755 | path_put(&path); |
| 2756 | goto err_out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2757 | } |
| 2758 | } |
| 2759 | |
| 2760 | if (rc) { |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 2761 | if (rc != -ENOENT) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2762 | goto err_out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2763 | ksmbd_debug(SMB, "can not get linux path for %s, rc = %d\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2764 | name, rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2765 | rc = 0; |
| 2766 | } else { |
| 2767 | file_present = true; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2768 | user_ns = mnt_user_ns(path.mnt); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2769 | } |
| 2770 | if (stream_name) { |
| 2771 | if (req->CreateOptions & FILE_DIRECTORY_FILE_LE) { |
| 2772 | if (s_type == DATA_STREAM) { |
| 2773 | rc = -EIO; |
| 2774 | rsp->hdr.Status = STATUS_NOT_A_DIRECTORY; |
| 2775 | } |
| 2776 | } else { |
Namjae Jeon | 823d0d3 | 2022-08-30 23:01:18 +0900 | [diff] [blame] | 2777 | if (file_present && S_ISDIR(d_inode(path.dentry)->i_mode) && |
| 2778 | s_type == DATA_STREAM) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2779 | rc = -EIO; |
| 2780 | rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY; |
| 2781 | } |
| 2782 | } |
| 2783 | |
| 2784 | if (req->CreateOptions & FILE_DIRECTORY_FILE_LE && |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 2785 | req->FileAttributes & FILE_ATTRIBUTE_NORMAL_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2786 | rsp->hdr.Status = STATUS_NOT_A_DIRECTORY; |
| 2787 | rc = -EIO; |
| 2788 | } |
| 2789 | |
| 2790 | if (rc < 0) |
| 2791 | goto err_out; |
| 2792 | } |
| 2793 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2794 | if (file_present && req->CreateOptions & FILE_NON_DIRECTORY_FILE_LE && |
Namjae Jeon | 823d0d3 | 2022-08-30 23:01:18 +0900 | [diff] [blame] | 2795 | S_ISDIR(d_inode(path.dentry)->i_mode) && |
| 2796 | !(req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2797 | ksmbd_debug(SMB, "open() argument is a directory: %s, %x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2798 | name, req->CreateOptions); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2799 | rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY; |
| 2800 | rc = -EIO; |
| 2801 | goto err_out; |
| 2802 | } |
| 2803 | |
| 2804 | if (file_present && (req->CreateOptions & FILE_DIRECTORY_FILE_LE) && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2805 | !(req->CreateDisposition == FILE_CREATE_LE) && |
Namjae Jeon | 823d0d3 | 2022-08-30 23:01:18 +0900 | [diff] [blame] | 2806 | !S_ISDIR(d_inode(path.dentry)->i_mode)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2807 | rsp->hdr.Status = STATUS_NOT_A_DIRECTORY; |
| 2808 | rc = -EIO; |
| 2809 | goto err_out; |
| 2810 | } |
| 2811 | |
| 2812 | if (!stream_name && file_present && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2813 | req->CreateDisposition == FILE_CREATE_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2814 | rc = -EEXIST; |
| 2815 | goto err_out; |
| 2816 | } |
| 2817 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2818 | daccess = smb_map_generic_desired_access(req->DesiredAccess); |
| 2819 | |
| 2820 | if (file_present && !(req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) { |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 2821 | rc = smb_check_perm_dacl(conn, &path, &daccess, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2822 | sess->user->uid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2823 | if (rc) |
| 2824 | goto err_out; |
| 2825 | } |
| 2826 | |
| 2827 | if (daccess & FILE_MAXIMAL_ACCESS_LE) { |
| 2828 | if (!file_present) { |
| 2829 | daccess = cpu_to_le32(GENERIC_ALL_FLAGS); |
| 2830 | } else { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2831 | rc = ksmbd_vfs_query_maximal_access(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2832 | path.dentry, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2833 | &daccess); |
| 2834 | if (rc) |
| 2835 | goto err_out; |
| 2836 | already_permitted = true; |
| 2837 | } |
| 2838 | maximal_access = daccess; |
| 2839 | } |
| 2840 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2841 | open_flags = smb2_create_open_flags(file_present, daccess, |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 2842 | req->CreateDisposition, |
| 2843 | &may_flags); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2844 | |
| 2845 | if (!test_tree_conn_flag(tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
| 2846 | if (open_flags & O_CREAT) { |
| 2847 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2848 | "User does not have write permission\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2849 | rc = -EACCES; |
| 2850 | goto err_out; |
| 2851 | } |
| 2852 | } |
| 2853 | |
| 2854 | /*create file if not present */ |
| 2855 | if (!file_present) { |
| 2856 | rc = smb2_creat(work, &path, name, open_flags, posix_mode, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2857 | req->CreateOptions & FILE_DIRECTORY_FILE_LE); |
Marios Makassikis | d337a44 | 2021-07-27 09:24:51 +0900 | [diff] [blame] | 2858 | if (rc) { |
| 2859 | if (rc == -ENOENT) { |
| 2860 | rc = -EIO; |
| 2861 | rsp->hdr.Status = STATUS_OBJECT_PATH_NOT_FOUND; |
| 2862 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2863 | goto err_out; |
Marios Makassikis | d337a44 | 2021-07-27 09:24:51 +0900 | [diff] [blame] | 2864 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2865 | |
| 2866 | created = true; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2867 | user_ns = mnt_user_ns(path.mnt); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2868 | if (ea_buf) { |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 2869 | if (le32_to_cpu(ea_buf->ccontext.DataLength) < |
| 2870 | sizeof(struct smb2_ea_info)) { |
| 2871 | rc = -EINVAL; |
| 2872 | goto err_out; |
| 2873 | } |
| 2874 | |
| 2875 | rc = smb2_set_ea(&ea_buf->ea, |
| 2876 | le32_to_cpu(ea_buf->ccontext.DataLength), |
| 2877 | &path); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2878 | if (rc == -EOPNOTSUPP) |
| 2879 | rc = 0; |
| 2880 | else if (rc) |
| 2881 | goto err_out; |
| 2882 | } |
| 2883 | } else if (!already_permitted) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2884 | /* FILE_READ_ATTRIBUTE is allowed without inode_permission, |
| 2885 | * because execute(search) permission on a parent directory, |
| 2886 | * is already granted. |
| 2887 | */ |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2888 | if (daccess & ~(FILE_READ_ATTRIBUTES_LE | FILE_READ_CONTROL_LE)) { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2889 | rc = inode_permission(user_ns, |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 2890 | d_inode(path.dentry), |
| 2891 | may_flags); |
Namjae Jeon | ff1d572 | 2021-04-13 13:18:10 +0900 | [diff] [blame] | 2892 | if (rc) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2893 | goto err_out; |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 2894 | |
| 2895 | if ((daccess & FILE_DELETE_LE) || |
| 2896 | (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2897 | rc = ksmbd_vfs_may_delete(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2898 | path.dentry); |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 2899 | if (rc) |
| 2900 | goto err_out; |
| 2901 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2902 | } |
| 2903 | } |
| 2904 | |
| 2905 | rc = ksmbd_query_inode_status(d_inode(path.dentry->d_parent)); |
| 2906 | if (rc == KSMBD_INODE_STATUS_PENDING_DELETE) { |
| 2907 | rc = -EBUSY; |
| 2908 | goto err_out; |
| 2909 | } |
| 2910 | |
| 2911 | rc = 0; |
| 2912 | filp = dentry_open(&path, open_flags, current_cred()); |
| 2913 | if (IS_ERR(filp)) { |
| 2914 | rc = PTR_ERR(filp); |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2915 | pr_err("dentry open for dir failed, rc %d\n", rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2916 | goto err_out; |
| 2917 | } |
| 2918 | |
| 2919 | if (file_present) { |
| 2920 | if (!(open_flags & O_TRUNC)) |
| 2921 | file_info = FILE_OPENED; |
| 2922 | else |
| 2923 | file_info = FILE_OVERWRITTEN; |
| 2924 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2925 | if ((req->CreateDisposition & FILE_CREATE_MASK_LE) == |
| 2926 | FILE_SUPERSEDE_LE) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2927 | file_info = FILE_SUPERSEDED; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2928 | } else if (open_flags & O_CREAT) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2929 | file_info = FILE_CREATED; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2930 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2931 | |
| 2932 | ksmbd_vfs_set_fadvise(filp, req->CreateOptions); |
| 2933 | |
| 2934 | /* Obtain Volatile-ID */ |
| 2935 | fp = ksmbd_open_fd(work, filp); |
| 2936 | if (IS_ERR(fp)) { |
| 2937 | fput(filp); |
| 2938 | rc = PTR_ERR(fp); |
| 2939 | fp = NULL; |
| 2940 | goto err_out; |
| 2941 | } |
| 2942 | |
| 2943 | /* Get Persistent-ID */ |
| 2944 | ksmbd_open_durable_fd(fp); |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 2945 | if (!has_file_id(fp->persistent_id)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2946 | rc = -ENOMEM; |
| 2947 | goto err_out; |
| 2948 | } |
| 2949 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2950 | fp->cdoption = req->CreateDisposition; |
| 2951 | fp->daccess = daccess; |
| 2952 | fp->saccess = req->ShareAccess; |
| 2953 | fp->coption = req->CreateOptions; |
| 2954 | |
| 2955 | /* Set default windows and posix acls if creating new file */ |
| 2956 | if (created) { |
| 2957 | int posix_acl_rc; |
Namjae Jeon | fba08fa | 2021-04-15 10:29:39 +0900 | [diff] [blame] | 2958 | struct inode *inode = d_inode(path.dentry); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2959 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2960 | posix_acl_rc = ksmbd_vfs_inherit_posix_acl(user_ns, |
Christian Brauner | 138060b | 2022-09-23 10:29:39 +0200 | [diff] [blame] | 2961 | path.dentry, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2962 | d_inode(path.dentry->d_parent)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2963 | if (posix_acl_rc) |
| 2964 | ksmbd_debug(SMB, "inherit posix acl failed : %d\n", posix_acl_rc); |
| 2965 | |
| 2966 | if (test_share_config_flag(work->tcon->share_conf, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2967 | KSMBD_SHARE_FLAG_ACL_XATTR)) { |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 2968 | rc = smb_inherit_dacl(conn, &path, sess->user->uid, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2969 | sess->user->gid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2970 | } |
| 2971 | |
| 2972 | if (rc) { |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 2973 | rc = smb2_create_sd_buffer(work, req, &path); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2974 | if (rc) { |
| 2975 | if (posix_acl_rc) |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2976 | ksmbd_vfs_set_init_posix_acl(user_ns, |
Christian Brauner | 138060b | 2022-09-23 10:29:39 +0200 | [diff] [blame] | 2977 | path.dentry); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2978 | |
| 2979 | if (test_share_config_flag(work->tcon->share_conf, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2980 | KSMBD_SHARE_FLAG_ACL_XATTR)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2981 | struct smb_fattr fattr; |
| 2982 | struct smb_ntsd *pntsd; |
Namjae Jeon | 3d47e54 | 2021-04-20 14:25:35 +0900 | [diff] [blame] | 2983 | int pntsd_size, ace_num = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2984 | |
Christian Brauner | 43205ca | 2021-08-23 17:13:50 +0200 | [diff] [blame] | 2985 | ksmbd_acls_fattr(&fattr, user_ns, inode); |
Marios Makassikis | e6b1059 | 2021-04-15 10:24:56 +0900 | [diff] [blame] | 2986 | if (fattr.cf_acls) |
| 2987 | ace_num = fattr.cf_acls->a_count; |
Namjae Jeon | 3d47e54 | 2021-04-20 14:25:35 +0900 | [diff] [blame] | 2988 | if (fattr.cf_dacls) |
| 2989 | ace_num += fattr.cf_dacls->a_count; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2990 | |
| 2991 | pntsd = kmalloc(sizeof(struct smb_ntsd) + |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2992 | sizeof(struct smb_sid) * 3 + |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2993 | sizeof(struct smb_acl) + |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2994 | sizeof(struct smb_ace) * ace_num * 2, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2995 | GFP_KERNEL); |
| 2996 | if (!pntsd) |
| 2997 | goto err_out; |
| 2998 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2999 | rc = build_sec_desc(user_ns, |
Namjae Jeon | 8f05411 | 2022-08-02 07:28:51 +0900 | [diff] [blame] | 3000 | pntsd, NULL, 0, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3001 | OWNER_SECINFO | |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 3002 | GROUP_SECINFO | |
| 3003 | DACL_SECINFO, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3004 | &pntsd_size, &fattr); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3005 | posix_acl_release(fattr.cf_acls); |
| 3006 | posix_acl_release(fattr.cf_dacls); |
Namjae Jeon | f2e78af | 2021-12-01 10:12:39 +0900 | [diff] [blame] | 3007 | if (rc) { |
| 3008 | kfree(pntsd); |
| 3009 | goto err_out; |
| 3010 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3011 | |
| 3012 | rc = ksmbd_vfs_set_sd_xattr(conn, |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 3013 | user_ns, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3014 | path.dentry, |
| 3015 | pntsd, |
| 3016 | pntsd_size); |
Namjae Jeon | 3d47e54 | 2021-04-20 14:25:35 +0900 | [diff] [blame] | 3017 | kfree(pntsd); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3018 | if (rc) |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 3019 | pr_err("failed to store ntacl in xattr : %d\n", |
| 3020 | rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3021 | } |
| 3022 | } |
| 3023 | } |
| 3024 | rc = 0; |
| 3025 | } |
| 3026 | |
| 3027 | if (stream_name) { |
| 3028 | rc = smb2_set_stream_name_xattr(&path, |
| 3029 | fp, |
| 3030 | stream_name, |
| 3031 | s_type); |
| 3032 | if (rc) |
| 3033 | goto err_out; |
| 3034 | file_info = FILE_CREATED; |
| 3035 | } |
| 3036 | |
| 3037 | fp->attrib_only = !(req->DesiredAccess & ~(FILE_READ_ATTRIBUTES_LE | |
| 3038 | FILE_WRITE_ATTRIBUTES_LE | FILE_SYNCHRONIZE_LE)); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3039 | if (!S_ISDIR(file_inode(filp)->i_mode) && open_flags & O_TRUNC && |
| 3040 | !fp->attrib_only && !stream_name) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3041 | smb_break_all_oplock(work, fp); |
| 3042 | need_truncate = 1; |
| 3043 | } |
| 3044 | |
| 3045 | /* fp should be searchable through ksmbd_inode.m_fp_list |
| 3046 | * after daccess, saccess, attrib_only, and stream are |
| 3047 | * initialized. |
| 3048 | */ |
| 3049 | write_lock(&fp->f_ci->m_lock); |
| 3050 | list_add(&fp->node, &fp->f_ci->m_fp_list); |
| 3051 | write_unlock(&fp->f_ci->m_lock); |
| 3052 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3053 | /* Check delete pending among previous fp before oplock break */ |
| 3054 | if (ksmbd_inode_pending_delete(fp)) { |
| 3055 | rc = -EBUSY; |
| 3056 | goto err_out; |
| 3057 | } |
| 3058 | |
| 3059 | share_ret = ksmbd_smb_check_shared_mode(fp->filp, fp); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3060 | if (!test_share_config_flag(work->tcon->share_conf, KSMBD_SHARE_FLAG_OPLOCKS) || |
| 3061 | (req_op_level == SMB2_OPLOCK_LEVEL_LEASE && |
| 3062 | !(conn->vals->capabilities & SMB2_GLOBAL_CAP_LEASING))) { |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 3063 | if (share_ret < 0 && !S_ISDIR(file_inode(fp->filp)->i_mode)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3064 | rc = share_ret; |
| 3065 | goto err_out; |
| 3066 | } |
| 3067 | } else { |
| 3068 | if (req_op_level == SMB2_OPLOCK_LEVEL_LEASE) { |
| 3069 | req_op_level = smb2_map_lease_to_oplock(lc->req_state); |
| 3070 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3071 | "lease req for(%s) req oplock state 0x%x, lease state 0x%x\n", |
| 3072 | name, req_op_level, lc->req_state); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3073 | rc = find_same_lease_key(sess, fp->f_ci, lc); |
| 3074 | if (rc) |
| 3075 | goto err_out; |
| 3076 | } else if (open_flags == O_RDONLY && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3077 | (req_op_level == SMB2_OPLOCK_LEVEL_BATCH || |
| 3078 | req_op_level == SMB2_OPLOCK_LEVEL_EXCLUSIVE)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3079 | req_op_level = SMB2_OPLOCK_LEVEL_II; |
| 3080 | |
| 3081 | rc = smb_grant_oplock(work, req_op_level, |
| 3082 | fp->persistent_id, fp, |
| 3083 | le32_to_cpu(req->hdr.Id.SyncId.TreeId), |
| 3084 | lc, share_ret); |
| 3085 | if (rc < 0) |
| 3086 | goto err_out; |
| 3087 | } |
| 3088 | |
| 3089 | if (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE) |
| 3090 | ksmbd_fd_set_delete_on_close(fp, file_info); |
| 3091 | |
| 3092 | if (need_truncate) { |
| 3093 | rc = smb2_create_truncate(&path); |
| 3094 | if (rc) |
| 3095 | goto err_out; |
| 3096 | } |
| 3097 | |
| 3098 | if (req->CreateContextsOffset) { |
| 3099 | struct create_alloc_size_req *az_req; |
| 3100 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3101 | az_req = (struct create_alloc_size_req *)smb2_find_context_vals(req, |
| 3102 | SMB2_CREATE_ALLOCATION_SIZE); |
Namjae Jeon | f19b396 | 2021-07-13 09:59:34 +0900 | [diff] [blame] | 3103 | if (IS_ERR(az_req)) { |
| 3104 | rc = PTR_ERR(az_req); |
| 3105 | goto err_out; |
| 3106 | } else if (az_req) { |
Hyunchul Lee | 8f77150 | 2021-09-24 22:22:22 +0900 | [diff] [blame] | 3107 | loff_t alloc_size; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3108 | int err; |
| 3109 | |
Hyunchul Lee | 8f77150 | 2021-09-24 22:22:22 +0900 | [diff] [blame] | 3110 | if (le16_to_cpu(az_req->ccontext.DataOffset) + |
| 3111 | le32_to_cpu(az_req->ccontext.DataLength) < |
| 3112 | sizeof(struct create_alloc_size_req)) { |
| 3113 | rc = -EINVAL; |
| 3114 | goto err_out; |
| 3115 | } |
| 3116 | alloc_size = le64_to_cpu(az_req->AllocationSize); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3117 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3118 | "request smb2 create allocate size : %llu\n", |
| 3119 | alloc_size); |
Namjae Jeon | e8c0619 | 2021-06-22 11:06:11 +0900 | [diff] [blame] | 3120 | smb_break_all_levII_oplock(work, fp, 1); |
| 3121 | err = vfs_fallocate(fp->filp, FALLOC_FL_KEEP_SIZE, 0, |
| 3122 | alloc_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3123 | if (err < 0) |
| 3124 | ksmbd_debug(SMB, |
Namjae Jeon | e8c0619 | 2021-06-22 11:06:11 +0900 | [diff] [blame] | 3125 | "vfs_fallocate is failed : %d\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3126 | err); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3127 | } |
| 3128 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3129 | context = smb2_find_context_vals(req, SMB2_CREATE_QUERY_ON_DISK_ID); |
Namjae Jeon | f19b396 | 2021-07-13 09:59:34 +0900 | [diff] [blame] | 3130 | if (IS_ERR(context)) { |
| 3131 | rc = PTR_ERR(context); |
| 3132 | goto err_out; |
| 3133 | } else if (context) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3134 | ksmbd_debug(SMB, "get query on disk id context\n"); |
| 3135 | query_disk_id = 1; |
| 3136 | } |
| 3137 | } |
| 3138 | |
Hyunchul Lee | c90b31e | 2022-08-12 11:11:32 +0900 | [diff] [blame] | 3139 | rc = ksmbd_vfs_getattr(&path, &stat); |
| 3140 | if (rc) |
| 3141 | goto err_out; |
| 3142 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3143 | if (stat.result_mask & STATX_BTIME) |
| 3144 | fp->create_time = ksmbd_UnixTimeToNT(stat.btime); |
| 3145 | else |
| 3146 | fp->create_time = ksmbd_UnixTimeToNT(stat.ctime); |
| 3147 | if (req->FileAttributes || fp->f_ci->m_fattr == 0) |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3148 | fp->f_ci->m_fattr = |
| 3149 | cpu_to_le32(smb2_get_dos_mode(&stat, le32_to_cpu(req->FileAttributes))); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3150 | |
| 3151 | if (!created) |
| 3152 | smb2_update_xattrs(tcon, &path, fp); |
| 3153 | else |
| 3154 | smb2_new_xattrs(tcon, &path, fp); |
| 3155 | |
| 3156 | memcpy(fp->client_guid, conn->ClientGUID, SMB2_CLIENT_GUID_SIZE); |
| 3157 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3158 | rsp->StructureSize = cpu_to_le16(89); |
| 3159 | rcu_read_lock(); |
| 3160 | opinfo = rcu_dereference(fp->f_opinfo); |
| 3161 | rsp->OplockLevel = opinfo != NULL ? opinfo->level : 0; |
| 3162 | rcu_read_unlock(); |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 3163 | rsp->Flags = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3164 | rsp->CreateAction = cpu_to_le32(file_info); |
| 3165 | rsp->CreationTime = cpu_to_le64(fp->create_time); |
| 3166 | time = ksmbd_UnixTimeToNT(stat.atime); |
| 3167 | rsp->LastAccessTime = cpu_to_le64(time); |
| 3168 | time = ksmbd_UnixTimeToNT(stat.mtime); |
| 3169 | rsp->LastWriteTime = cpu_to_le64(time); |
| 3170 | time = ksmbd_UnixTimeToNT(stat.ctime); |
| 3171 | rsp->ChangeTime = cpu_to_le64(time); |
| 3172 | rsp->AllocationSize = S_ISDIR(stat.mode) ? 0 : |
| 3173 | cpu_to_le64(stat.blocks << 9); |
| 3174 | rsp->EndofFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size); |
| 3175 | rsp->FileAttributes = fp->f_ci->m_fattr; |
| 3176 | |
| 3177 | rsp->Reserved2 = 0; |
| 3178 | |
Paulo Alcantara | 2d004c6 | 2022-03-21 13:08:26 -0300 | [diff] [blame] | 3179 | rsp->PersistentFileId = fp->persistent_id; |
| 3180 | rsp->VolatileFileId = fp->volatile_id; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3181 | |
| 3182 | rsp->CreateContextsOffset = 0; |
| 3183 | rsp->CreateContextsLength = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 3184 | inc_rfc1001_len(work->response_buf, 88); /* StructureSize - 1*/ |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3185 | |
| 3186 | /* If lease is request send lease context response */ |
| 3187 | if (opinfo && opinfo->is_lease) { |
| 3188 | struct create_context *lease_ccontext; |
| 3189 | |
| 3190 | ksmbd_debug(SMB, "lease granted on(%s) lease state 0x%x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3191 | name, opinfo->o_lease->state); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3192 | rsp->OplockLevel = SMB2_OPLOCK_LEVEL_LEASE; |
| 3193 | |
| 3194 | lease_ccontext = (struct create_context *)rsp->Buffer; |
| 3195 | contxt_cnt++; |
| 3196 | create_lease_buf(rsp->Buffer, opinfo->o_lease); |
| 3197 | le32_add_cpu(&rsp->CreateContextsLength, |
| 3198 | conn->vals->create_lease_size); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 3199 | inc_rfc1001_len(work->response_buf, |
| 3200 | conn->vals->create_lease_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3201 | next_ptr = &lease_ccontext->Next; |
| 3202 | next_off = conn->vals->create_lease_size; |
| 3203 | } |
| 3204 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3205 | if (maximal_access_ctxt) { |
| 3206 | struct create_context *mxac_ccontext; |
| 3207 | |
| 3208 | if (maximal_access == 0) |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 3209 | ksmbd_vfs_query_maximal_access(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 3210 | path.dentry, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3211 | &maximal_access); |
| 3212 | mxac_ccontext = (struct create_context *)(rsp->Buffer + |
| 3213 | le32_to_cpu(rsp->CreateContextsLength)); |
| 3214 | contxt_cnt++; |
| 3215 | create_mxac_rsp_buf(rsp->Buffer + |
| 3216 | le32_to_cpu(rsp->CreateContextsLength), |
| 3217 | le32_to_cpu(maximal_access)); |
| 3218 | le32_add_cpu(&rsp->CreateContextsLength, |
| 3219 | conn->vals->create_mxac_size); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 3220 | inc_rfc1001_len(work->response_buf, |
| 3221 | conn->vals->create_mxac_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3222 | if (next_ptr) |
| 3223 | *next_ptr = cpu_to_le32(next_off); |
| 3224 | next_ptr = &mxac_ccontext->Next; |
| 3225 | next_off = conn->vals->create_mxac_size; |
| 3226 | } |
| 3227 | |
| 3228 | if (query_disk_id) { |
| 3229 | struct create_context *disk_id_ccontext; |
| 3230 | |
| 3231 | disk_id_ccontext = (struct create_context *)(rsp->Buffer + |
| 3232 | le32_to_cpu(rsp->CreateContextsLength)); |
| 3233 | contxt_cnt++; |
| 3234 | create_disk_id_rsp_buf(rsp->Buffer + |
| 3235 | le32_to_cpu(rsp->CreateContextsLength), |
| 3236 | stat.ino, tcon->id); |
| 3237 | le32_add_cpu(&rsp->CreateContextsLength, |
| 3238 | conn->vals->create_disk_id_size); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 3239 | inc_rfc1001_len(work->response_buf, |
| 3240 | conn->vals->create_disk_id_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3241 | if (next_ptr) |
| 3242 | *next_ptr = cpu_to_le32(next_off); |
| 3243 | next_ptr = &disk_id_ccontext->Next; |
| 3244 | next_off = conn->vals->create_disk_id_size; |
| 3245 | } |
| 3246 | |
| 3247 | if (posix_ctxt) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3248 | contxt_cnt++; |
| 3249 | create_posix_rsp_buf(rsp->Buffer + |
| 3250 | le32_to_cpu(rsp->CreateContextsLength), |
| 3251 | fp); |
| 3252 | le32_add_cpu(&rsp->CreateContextsLength, |
| 3253 | conn->vals->create_posix_size); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 3254 | inc_rfc1001_len(work->response_buf, |
| 3255 | conn->vals->create_posix_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3256 | if (next_ptr) |
| 3257 | *next_ptr = cpu_to_le32(next_off); |
| 3258 | } |
| 3259 | |
| 3260 | if (contxt_cnt > 0) { |
| 3261 | rsp->CreateContextsOffset = |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 3262 | cpu_to_le32(offsetof(struct smb2_create_rsp, Buffer)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3263 | } |
| 3264 | |
| 3265 | err_out: |
| 3266 | if (file_present || created) |
| 3267 | path_put(&path); |
| 3268 | ksmbd_revert_fsids(work); |
| 3269 | err_out1: |
| 3270 | if (rc) { |
| 3271 | if (rc == -EINVAL) |
| 3272 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 3273 | else if (rc == -EOPNOTSUPP) |
| 3274 | rsp->hdr.Status = STATUS_NOT_SUPPORTED; |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 3275 | else if (rc == -EACCES || rc == -ESTALE || rc == -EXDEV) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3276 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 3277 | else if (rc == -ENOENT) |
| 3278 | rsp->hdr.Status = STATUS_OBJECT_NAME_INVALID; |
| 3279 | else if (rc == -EPERM) |
| 3280 | rsp->hdr.Status = STATUS_SHARING_VIOLATION; |
| 3281 | else if (rc == -EBUSY) |
| 3282 | rsp->hdr.Status = STATUS_DELETE_PENDING; |
| 3283 | else if (rc == -EBADF) |
| 3284 | rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND; |
| 3285 | else if (rc == -ENOEXEC) |
| 3286 | rsp->hdr.Status = STATUS_DUPLICATE_OBJECTID; |
| 3287 | else if (rc == -ENXIO) |
| 3288 | rsp->hdr.Status = STATUS_NO_SUCH_DEVICE; |
| 3289 | else if (rc == -EEXIST) |
| 3290 | rsp->hdr.Status = STATUS_OBJECT_NAME_COLLISION; |
| 3291 | else if (rc == -EMFILE) |
| 3292 | rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES; |
| 3293 | if (!rsp->hdr.Status) |
| 3294 | rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR; |
| 3295 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3296 | if (fp) |
| 3297 | ksmbd_fd_put(work, fp); |
| 3298 | smb2_set_err_rsp(work); |
| 3299 | ksmbd_debug(SMB, "Error response: %x\n", rsp->hdr.Status); |
| 3300 | } |
| 3301 | |
Namjae Jeon | 50f500b | 2022-02-24 11:03:41 +0900 | [diff] [blame] | 3302 | kfree(name); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3303 | kfree(lc); |
| 3304 | |
| 3305 | return 0; |
| 3306 | } |
| 3307 | |
| 3308 | static int readdir_info_level_struct_sz(int info_level) |
| 3309 | { |
| 3310 | switch (info_level) { |
| 3311 | case FILE_FULL_DIRECTORY_INFORMATION: |
| 3312 | return sizeof(struct file_full_directory_info); |
| 3313 | case FILE_BOTH_DIRECTORY_INFORMATION: |
| 3314 | return sizeof(struct file_both_directory_info); |
| 3315 | case FILE_DIRECTORY_INFORMATION: |
| 3316 | return sizeof(struct file_directory_info); |
| 3317 | case FILE_NAMES_INFORMATION: |
| 3318 | return sizeof(struct file_names_info); |
| 3319 | case FILEID_FULL_DIRECTORY_INFORMATION: |
| 3320 | return sizeof(struct file_id_full_dir_info); |
| 3321 | case FILEID_BOTH_DIRECTORY_INFORMATION: |
| 3322 | return sizeof(struct file_id_both_directory_info); |
| 3323 | case SMB_FIND_FILE_POSIX_INFO: |
| 3324 | return sizeof(struct smb2_posix_info); |
| 3325 | default: |
| 3326 | return -EOPNOTSUPP; |
| 3327 | } |
| 3328 | } |
| 3329 | |
| 3330 | static int dentry_name(struct ksmbd_dir_info *d_info, int info_level) |
| 3331 | { |
| 3332 | switch (info_level) { |
| 3333 | case FILE_FULL_DIRECTORY_INFORMATION: |
| 3334 | { |
| 3335 | struct file_full_directory_info *ffdinfo; |
| 3336 | |
| 3337 | ffdinfo = (struct file_full_directory_info *)d_info->rptr; |
| 3338 | d_info->rptr += le32_to_cpu(ffdinfo->NextEntryOffset); |
| 3339 | d_info->name = ffdinfo->FileName; |
| 3340 | d_info->name_len = le32_to_cpu(ffdinfo->FileNameLength); |
| 3341 | return 0; |
| 3342 | } |
| 3343 | case FILE_BOTH_DIRECTORY_INFORMATION: |
| 3344 | { |
| 3345 | struct file_both_directory_info *fbdinfo; |
| 3346 | |
| 3347 | fbdinfo = (struct file_both_directory_info *)d_info->rptr; |
| 3348 | d_info->rptr += le32_to_cpu(fbdinfo->NextEntryOffset); |
| 3349 | d_info->name = fbdinfo->FileName; |
| 3350 | d_info->name_len = le32_to_cpu(fbdinfo->FileNameLength); |
| 3351 | return 0; |
| 3352 | } |
| 3353 | case FILE_DIRECTORY_INFORMATION: |
| 3354 | { |
| 3355 | struct file_directory_info *fdinfo; |
| 3356 | |
| 3357 | fdinfo = (struct file_directory_info *)d_info->rptr; |
| 3358 | d_info->rptr += le32_to_cpu(fdinfo->NextEntryOffset); |
| 3359 | d_info->name = fdinfo->FileName; |
| 3360 | d_info->name_len = le32_to_cpu(fdinfo->FileNameLength); |
| 3361 | return 0; |
| 3362 | } |
| 3363 | case FILE_NAMES_INFORMATION: |
| 3364 | { |
| 3365 | struct file_names_info *fninfo; |
| 3366 | |
| 3367 | fninfo = (struct file_names_info *)d_info->rptr; |
| 3368 | d_info->rptr += le32_to_cpu(fninfo->NextEntryOffset); |
| 3369 | d_info->name = fninfo->FileName; |
| 3370 | d_info->name_len = le32_to_cpu(fninfo->FileNameLength); |
| 3371 | return 0; |
| 3372 | } |
| 3373 | case FILEID_FULL_DIRECTORY_INFORMATION: |
| 3374 | { |
| 3375 | struct file_id_full_dir_info *dinfo; |
| 3376 | |
| 3377 | dinfo = (struct file_id_full_dir_info *)d_info->rptr; |
| 3378 | d_info->rptr += le32_to_cpu(dinfo->NextEntryOffset); |
| 3379 | d_info->name = dinfo->FileName; |
| 3380 | d_info->name_len = le32_to_cpu(dinfo->FileNameLength); |
| 3381 | return 0; |
| 3382 | } |
| 3383 | case FILEID_BOTH_DIRECTORY_INFORMATION: |
| 3384 | { |
| 3385 | struct file_id_both_directory_info *fibdinfo; |
| 3386 | |
| 3387 | fibdinfo = (struct file_id_both_directory_info *)d_info->rptr; |
| 3388 | d_info->rptr += le32_to_cpu(fibdinfo->NextEntryOffset); |
| 3389 | d_info->name = fibdinfo->FileName; |
| 3390 | d_info->name_len = le32_to_cpu(fibdinfo->FileNameLength); |
| 3391 | return 0; |
| 3392 | } |
| 3393 | case SMB_FIND_FILE_POSIX_INFO: |
| 3394 | { |
| 3395 | struct smb2_posix_info *posix_info; |
| 3396 | |
| 3397 | posix_info = (struct smb2_posix_info *)d_info->rptr; |
| 3398 | d_info->rptr += le32_to_cpu(posix_info->NextEntryOffset); |
| 3399 | d_info->name = posix_info->name; |
| 3400 | d_info->name_len = le32_to_cpu(posix_info->name_len); |
| 3401 | return 0; |
| 3402 | } |
| 3403 | default: |
| 3404 | return -EINVAL; |
| 3405 | } |
| 3406 | } |
| 3407 | |
| 3408 | /** |
| 3409 | * smb2_populate_readdir_entry() - encode directory entry in smb2 response |
| 3410 | * buffer |
| 3411 | * @conn: connection instance |
| 3412 | * @info_level: smb information level |
| 3413 | * @d_info: structure included variables for query dir |
| 3414 | * @ksmbd_kstat: ksmbd wrapper of dirent stat information |
| 3415 | * |
| 3416 | * if directory has many entries, find first can't read it fully. |
| 3417 | * find next might be called multiple times to read remaining dir entries |
| 3418 | * |
| 3419 | * Return: 0 on success, otherwise error |
| 3420 | */ |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3421 | static int smb2_populate_readdir_entry(struct ksmbd_conn *conn, int info_level, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3422 | struct ksmbd_dir_info *d_info, |
| 3423 | struct ksmbd_kstat *ksmbd_kstat) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3424 | { |
| 3425 | int next_entry_offset = 0; |
| 3426 | char *conv_name; |
| 3427 | int conv_len; |
| 3428 | void *kstat; |
Namjae Jeon | dac0ec6 | 2021-07-07 14:57:24 +0900 | [diff] [blame] | 3429 | int struct_sz, rc = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3430 | |
| 3431 | conv_name = ksmbd_convert_dir_info_name(d_info, |
| 3432 | conn->local_nls, |
| 3433 | &conv_len); |
| 3434 | if (!conv_name) |
| 3435 | return -ENOMEM; |
| 3436 | |
| 3437 | /* Somehow the name has only terminating NULL bytes */ |
| 3438 | if (conv_len < 0) { |
Namjae Jeon | dac0ec6 | 2021-07-07 14:57:24 +0900 | [diff] [blame] | 3439 | rc = -EINVAL; |
| 3440 | goto free_conv_name; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3441 | } |
| 3442 | |
Gustavo A. R. Silva | d272e01 | 2022-11-15 09:35:10 -0600 | [diff] [blame] | 3443 | struct_sz = readdir_info_level_struct_sz(info_level) + conv_len; |
Namjae Jeon | 04e2609 | 2022-01-30 18:31:01 +0900 | [diff] [blame] | 3444 | next_entry_offset = ALIGN(struct_sz, KSMBD_DIR_INFO_ALIGNMENT); |
| 3445 | d_info->last_entry_off_align = next_entry_offset - struct_sz; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3446 | |
| 3447 | if (next_entry_offset > d_info->out_buf_len) { |
| 3448 | d_info->out_buf_len = 0; |
Namjae Jeon | dac0ec6 | 2021-07-07 14:57:24 +0900 | [diff] [blame] | 3449 | rc = -ENOSPC; |
| 3450 | goto free_conv_name; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3451 | } |
| 3452 | |
| 3453 | kstat = d_info->wptr; |
| 3454 | if (info_level != FILE_NAMES_INFORMATION) |
| 3455 | kstat = ksmbd_vfs_init_kstat(&d_info->wptr, ksmbd_kstat); |
| 3456 | |
| 3457 | switch (info_level) { |
| 3458 | case FILE_FULL_DIRECTORY_INFORMATION: |
| 3459 | { |
| 3460 | struct file_full_directory_info *ffdinfo; |
| 3461 | |
| 3462 | ffdinfo = (struct file_full_directory_info *)kstat; |
| 3463 | ffdinfo->FileNameLength = cpu_to_le32(conv_len); |
| 3464 | ffdinfo->EaSize = |
| 3465 | smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode); |
| 3466 | if (ffdinfo->EaSize) |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 3467 | ffdinfo->ExtFileAttributes = FILE_ATTRIBUTE_REPARSE_POINT_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3468 | if (d_info->hide_dot_file && d_info->name[0] == '.') |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 3469 | ffdinfo->ExtFileAttributes |= FILE_ATTRIBUTE_HIDDEN_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3470 | memcpy(ffdinfo->FileName, conv_name, conv_len); |
| 3471 | ffdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3472 | break; |
| 3473 | } |
| 3474 | case FILE_BOTH_DIRECTORY_INFORMATION: |
| 3475 | { |
| 3476 | struct file_both_directory_info *fbdinfo; |
| 3477 | |
| 3478 | fbdinfo = (struct file_both_directory_info *)kstat; |
| 3479 | fbdinfo->FileNameLength = cpu_to_le32(conv_len); |
| 3480 | fbdinfo->EaSize = |
| 3481 | smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode); |
| 3482 | if (fbdinfo->EaSize) |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 3483 | fbdinfo->ExtFileAttributes = FILE_ATTRIBUTE_REPARSE_POINT_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3484 | fbdinfo->ShortNameLength = 0; |
| 3485 | fbdinfo->Reserved = 0; |
| 3486 | if (d_info->hide_dot_file && d_info->name[0] == '.') |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 3487 | fbdinfo->ExtFileAttributes |= FILE_ATTRIBUTE_HIDDEN_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3488 | memcpy(fbdinfo->FileName, conv_name, conv_len); |
| 3489 | fbdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3490 | break; |
| 3491 | } |
| 3492 | case FILE_DIRECTORY_INFORMATION: |
| 3493 | { |
| 3494 | struct file_directory_info *fdinfo; |
| 3495 | |
| 3496 | fdinfo = (struct file_directory_info *)kstat; |
| 3497 | fdinfo->FileNameLength = cpu_to_le32(conv_len); |
| 3498 | if (d_info->hide_dot_file && d_info->name[0] == '.') |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 3499 | fdinfo->ExtFileAttributes |= FILE_ATTRIBUTE_HIDDEN_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3500 | memcpy(fdinfo->FileName, conv_name, conv_len); |
| 3501 | fdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3502 | break; |
| 3503 | } |
| 3504 | case FILE_NAMES_INFORMATION: |
| 3505 | { |
| 3506 | struct file_names_info *fninfo; |
| 3507 | |
| 3508 | fninfo = (struct file_names_info *)kstat; |
| 3509 | fninfo->FileNameLength = cpu_to_le32(conv_len); |
| 3510 | memcpy(fninfo->FileName, conv_name, conv_len); |
| 3511 | fninfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3512 | break; |
| 3513 | } |
| 3514 | case FILEID_FULL_DIRECTORY_INFORMATION: |
| 3515 | { |
| 3516 | struct file_id_full_dir_info *dinfo; |
| 3517 | |
| 3518 | dinfo = (struct file_id_full_dir_info *)kstat; |
| 3519 | dinfo->FileNameLength = cpu_to_le32(conv_len); |
| 3520 | dinfo->EaSize = |
| 3521 | smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode); |
| 3522 | if (dinfo->EaSize) |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 3523 | dinfo->ExtFileAttributes = FILE_ATTRIBUTE_REPARSE_POINT_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3524 | dinfo->Reserved = 0; |
| 3525 | dinfo->UniqueId = cpu_to_le64(ksmbd_kstat->kstat->ino); |
| 3526 | if (d_info->hide_dot_file && d_info->name[0] == '.') |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 3527 | dinfo->ExtFileAttributes |= FILE_ATTRIBUTE_HIDDEN_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3528 | memcpy(dinfo->FileName, conv_name, conv_len); |
| 3529 | dinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3530 | break; |
| 3531 | } |
| 3532 | case FILEID_BOTH_DIRECTORY_INFORMATION: |
| 3533 | { |
| 3534 | struct file_id_both_directory_info *fibdinfo; |
| 3535 | |
| 3536 | fibdinfo = (struct file_id_both_directory_info *)kstat; |
| 3537 | fibdinfo->FileNameLength = cpu_to_le32(conv_len); |
| 3538 | fibdinfo->EaSize = |
| 3539 | smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode); |
| 3540 | if (fibdinfo->EaSize) |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 3541 | fibdinfo->ExtFileAttributes = FILE_ATTRIBUTE_REPARSE_POINT_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3542 | fibdinfo->UniqueId = cpu_to_le64(ksmbd_kstat->kstat->ino); |
| 3543 | fibdinfo->ShortNameLength = 0; |
| 3544 | fibdinfo->Reserved = 0; |
| 3545 | fibdinfo->Reserved2 = cpu_to_le16(0); |
| 3546 | if (d_info->hide_dot_file && d_info->name[0] == '.') |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 3547 | fibdinfo->ExtFileAttributes |= FILE_ATTRIBUTE_HIDDEN_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3548 | memcpy(fibdinfo->FileName, conv_name, conv_len); |
| 3549 | fibdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3550 | break; |
| 3551 | } |
| 3552 | case SMB_FIND_FILE_POSIX_INFO: |
| 3553 | { |
| 3554 | struct smb2_posix_info *posix_info; |
| 3555 | u64 time; |
| 3556 | |
| 3557 | posix_info = (struct smb2_posix_info *)kstat; |
| 3558 | posix_info->Ignored = 0; |
| 3559 | posix_info->CreationTime = cpu_to_le64(ksmbd_kstat->create_time); |
| 3560 | time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->ctime); |
| 3561 | posix_info->ChangeTime = cpu_to_le64(time); |
| 3562 | time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->atime); |
| 3563 | posix_info->LastAccessTime = cpu_to_le64(time); |
| 3564 | time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->mtime); |
| 3565 | posix_info->LastWriteTime = cpu_to_le64(time); |
| 3566 | posix_info->EndOfFile = cpu_to_le64(ksmbd_kstat->kstat->size); |
| 3567 | posix_info->AllocationSize = cpu_to_le64(ksmbd_kstat->kstat->blocks << 9); |
| 3568 | posix_info->DeviceId = cpu_to_le32(ksmbd_kstat->kstat->rdev); |
| 3569 | posix_info->HardLinks = cpu_to_le32(ksmbd_kstat->kstat->nlink); |
Namjae Jeon | f6c2b20 | 2022-09-22 23:16:12 +0900 | [diff] [blame] | 3570 | posix_info->Mode = cpu_to_le32(ksmbd_kstat->kstat->mode & 0777); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3571 | posix_info->Inode = cpu_to_le64(ksmbd_kstat->kstat->ino); |
| 3572 | posix_info->DosAttributes = |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 3573 | S_ISDIR(ksmbd_kstat->kstat->mode) ? |
| 3574 | FILE_ATTRIBUTE_DIRECTORY_LE : FILE_ATTRIBUTE_ARCHIVE_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3575 | if (d_info->hide_dot_file && d_info->name[0] == '.') |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 3576 | posix_info->DosAttributes |= FILE_ATTRIBUTE_HIDDEN_LE; |
Namjae Jeon | 5609bdd | 2022-09-22 23:08:49 +0900 | [diff] [blame] | 3577 | /* |
| 3578 | * SidBuffer(32) contain two sids(Domain sid(16), UNIX group sid(16)). |
| 3579 | * UNIX sid(16) = revision(1) + num_subauth(1) + authority(6) + |
| 3580 | * sub_auth(4 * 1(num_subauth)) + RID(4). |
| 3581 | */ |
Christian Brauner | 475d6f9 | 2021-08-23 17:13:48 +0200 | [diff] [blame] | 3582 | id_to_sid(from_kuid_munged(&init_user_ns, ksmbd_kstat->kstat->uid), |
Namjae Jeon | 5609bdd | 2022-09-22 23:08:49 +0900 | [diff] [blame] | 3583 | SIDUNIX_USER, (struct smb_sid *)&posix_info->SidBuffer[0]); |
Christian Brauner | 475d6f9 | 2021-08-23 17:13:48 +0200 | [diff] [blame] | 3584 | id_to_sid(from_kgid_munged(&init_user_ns, ksmbd_kstat->kstat->gid), |
Namjae Jeon | 5609bdd | 2022-09-22 23:08:49 +0900 | [diff] [blame] | 3585 | SIDUNIX_GROUP, (struct smb_sid *)&posix_info->SidBuffer[16]); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3586 | memcpy(posix_info->name, conv_name, conv_len); |
| 3587 | posix_info->name_len = cpu_to_le32(conv_len); |
| 3588 | posix_info->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3589 | break; |
| 3590 | } |
| 3591 | |
| 3592 | } /* switch (info_level) */ |
| 3593 | |
| 3594 | d_info->last_entry_offset = d_info->data_count; |
| 3595 | d_info->data_count += next_entry_offset; |
Marios Makassikis | e7735c8 | 2021-05-06 11:40:02 +0900 | [diff] [blame] | 3596 | d_info->out_buf_len -= next_entry_offset; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3597 | d_info->wptr += next_entry_offset; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3598 | |
| 3599 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3600 | "info_level : %d, buf_len :%d, next_offset : %d, data_count : %d\n", |
| 3601 | info_level, d_info->out_buf_len, |
| 3602 | next_entry_offset, d_info->data_count); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3603 | |
Namjae Jeon | dac0ec6 | 2021-07-07 14:57:24 +0900 | [diff] [blame] | 3604 | free_conv_name: |
| 3605 | kfree(conv_name); |
| 3606 | return rc; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3607 | } |
| 3608 | |
| 3609 | struct smb2_query_dir_private { |
| 3610 | struct ksmbd_work *work; |
| 3611 | char *search_pattern; |
| 3612 | struct ksmbd_file *dir_fp; |
| 3613 | |
| 3614 | struct ksmbd_dir_info *d_info; |
| 3615 | int info_level; |
| 3616 | }; |
| 3617 | |
| 3618 | static void lock_dir(struct ksmbd_file *dir_fp) |
| 3619 | { |
| 3620 | struct dentry *dir = dir_fp->filp->f_path.dentry; |
| 3621 | |
| 3622 | inode_lock_nested(d_inode(dir), I_MUTEX_PARENT); |
| 3623 | } |
| 3624 | |
| 3625 | static void unlock_dir(struct ksmbd_file *dir_fp) |
| 3626 | { |
| 3627 | struct dentry *dir = dir_fp->filp->f_path.dentry; |
| 3628 | |
| 3629 | inode_unlock(d_inode(dir)); |
| 3630 | } |
| 3631 | |
| 3632 | static int process_query_dir_entries(struct smb2_query_dir_private *priv) |
| 3633 | { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 3634 | struct user_namespace *user_ns = file_mnt_user_ns(priv->dir_fp->filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3635 | struct kstat kstat; |
| 3636 | struct ksmbd_kstat ksmbd_kstat; |
| 3637 | int rc; |
| 3638 | int i; |
| 3639 | |
| 3640 | for (i = 0; i < priv->d_info->num_entry; i++) { |
| 3641 | struct dentry *dent; |
| 3642 | |
| 3643 | if (dentry_name(priv->d_info, priv->info_level)) |
| 3644 | return -EINVAL; |
| 3645 | |
| 3646 | lock_dir(priv->dir_fp); |
Christian Brauner | da1e7ad | 2021-08-23 17:13:47 +0200 | [diff] [blame] | 3647 | dent = lookup_one(user_ns, priv->d_info->name, |
| 3648 | priv->dir_fp->filp->f_path.dentry, |
| 3649 | priv->d_info->name_len); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3650 | unlock_dir(priv->dir_fp); |
| 3651 | |
| 3652 | if (IS_ERR(dent)) { |
| 3653 | ksmbd_debug(SMB, "Cannot lookup `%s' [%ld]\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3654 | priv->d_info->name, |
| 3655 | PTR_ERR(dent)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3656 | continue; |
| 3657 | } |
| 3658 | if (unlikely(d_is_negative(dent))) { |
| 3659 | dput(dent); |
| 3660 | ksmbd_debug(SMB, "Negative dentry `%s'\n", |
| 3661 | priv->d_info->name); |
| 3662 | continue; |
| 3663 | } |
| 3664 | |
| 3665 | ksmbd_kstat.kstat = &kstat; |
| 3666 | if (priv->info_level != FILE_NAMES_INFORMATION) |
| 3667 | ksmbd_vfs_fill_dentry_attrs(priv->work, |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 3668 | user_ns, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3669 | dent, |
| 3670 | &ksmbd_kstat); |
| 3671 | |
| 3672 | rc = smb2_populate_readdir_entry(priv->work->conn, |
| 3673 | priv->info_level, |
| 3674 | priv->d_info, |
| 3675 | &ksmbd_kstat); |
| 3676 | dput(dent); |
| 3677 | if (rc) |
| 3678 | return rc; |
| 3679 | } |
| 3680 | return 0; |
| 3681 | } |
| 3682 | |
| 3683 | static int reserve_populate_dentry(struct ksmbd_dir_info *d_info, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3684 | int info_level) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3685 | { |
| 3686 | int struct_sz; |
| 3687 | int conv_len; |
| 3688 | int next_entry_offset; |
| 3689 | |
| 3690 | struct_sz = readdir_info_level_struct_sz(info_level); |
| 3691 | if (struct_sz == -EOPNOTSUPP) |
| 3692 | return -EOPNOTSUPP; |
| 3693 | |
| 3694 | conv_len = (d_info->name_len + 1) * 2; |
Gustavo A. R. Silva | d272e01 | 2022-11-15 09:35:10 -0600 | [diff] [blame] | 3695 | next_entry_offset = ALIGN(struct_sz + conv_len, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3696 | KSMBD_DIR_INFO_ALIGNMENT); |
| 3697 | |
| 3698 | if (next_entry_offset > d_info->out_buf_len) { |
| 3699 | d_info->out_buf_len = 0; |
| 3700 | return -ENOSPC; |
| 3701 | } |
| 3702 | |
| 3703 | switch (info_level) { |
| 3704 | case FILE_FULL_DIRECTORY_INFORMATION: |
| 3705 | { |
| 3706 | struct file_full_directory_info *ffdinfo; |
| 3707 | |
| 3708 | ffdinfo = (struct file_full_directory_info *)d_info->wptr; |
| 3709 | memcpy(ffdinfo->FileName, d_info->name, d_info->name_len); |
| 3710 | ffdinfo->FileName[d_info->name_len] = 0x00; |
| 3711 | ffdinfo->FileNameLength = cpu_to_le32(d_info->name_len); |
| 3712 | ffdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3713 | break; |
| 3714 | } |
| 3715 | case FILE_BOTH_DIRECTORY_INFORMATION: |
| 3716 | { |
| 3717 | struct file_both_directory_info *fbdinfo; |
| 3718 | |
| 3719 | fbdinfo = (struct file_both_directory_info *)d_info->wptr; |
| 3720 | memcpy(fbdinfo->FileName, d_info->name, d_info->name_len); |
| 3721 | fbdinfo->FileName[d_info->name_len] = 0x00; |
| 3722 | fbdinfo->FileNameLength = cpu_to_le32(d_info->name_len); |
| 3723 | fbdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3724 | break; |
| 3725 | } |
| 3726 | case FILE_DIRECTORY_INFORMATION: |
| 3727 | { |
| 3728 | struct file_directory_info *fdinfo; |
| 3729 | |
| 3730 | fdinfo = (struct file_directory_info *)d_info->wptr; |
| 3731 | memcpy(fdinfo->FileName, d_info->name, d_info->name_len); |
| 3732 | fdinfo->FileName[d_info->name_len] = 0x00; |
| 3733 | fdinfo->FileNameLength = cpu_to_le32(d_info->name_len); |
| 3734 | fdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3735 | break; |
| 3736 | } |
| 3737 | case FILE_NAMES_INFORMATION: |
| 3738 | { |
| 3739 | struct file_names_info *fninfo; |
| 3740 | |
| 3741 | fninfo = (struct file_names_info *)d_info->wptr; |
| 3742 | memcpy(fninfo->FileName, d_info->name, d_info->name_len); |
| 3743 | fninfo->FileName[d_info->name_len] = 0x00; |
| 3744 | fninfo->FileNameLength = cpu_to_le32(d_info->name_len); |
| 3745 | fninfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3746 | break; |
| 3747 | } |
| 3748 | case FILEID_FULL_DIRECTORY_INFORMATION: |
| 3749 | { |
| 3750 | struct file_id_full_dir_info *dinfo; |
| 3751 | |
| 3752 | dinfo = (struct file_id_full_dir_info *)d_info->wptr; |
| 3753 | memcpy(dinfo->FileName, d_info->name, d_info->name_len); |
| 3754 | dinfo->FileName[d_info->name_len] = 0x00; |
| 3755 | dinfo->FileNameLength = cpu_to_le32(d_info->name_len); |
| 3756 | dinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3757 | break; |
| 3758 | } |
| 3759 | case FILEID_BOTH_DIRECTORY_INFORMATION: |
| 3760 | { |
| 3761 | struct file_id_both_directory_info *fibdinfo; |
| 3762 | |
| 3763 | fibdinfo = (struct file_id_both_directory_info *)d_info->wptr; |
| 3764 | memcpy(fibdinfo->FileName, d_info->name, d_info->name_len); |
| 3765 | fibdinfo->FileName[d_info->name_len] = 0x00; |
| 3766 | fibdinfo->FileNameLength = cpu_to_le32(d_info->name_len); |
| 3767 | fibdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3768 | break; |
| 3769 | } |
| 3770 | case SMB_FIND_FILE_POSIX_INFO: |
| 3771 | { |
| 3772 | struct smb2_posix_info *posix_info; |
| 3773 | |
| 3774 | posix_info = (struct smb2_posix_info *)d_info->wptr; |
| 3775 | memcpy(posix_info->name, d_info->name, d_info->name_len); |
| 3776 | posix_info->name[d_info->name_len] = 0x00; |
| 3777 | posix_info->name_len = cpu_to_le32(d_info->name_len); |
| 3778 | posix_info->NextEntryOffset = |
| 3779 | cpu_to_le32(next_entry_offset); |
| 3780 | break; |
| 3781 | } |
| 3782 | } /* switch (info_level) */ |
| 3783 | |
| 3784 | d_info->num_entry++; |
| 3785 | d_info->out_buf_len -= next_entry_offset; |
| 3786 | d_info->wptr += next_entry_offset; |
| 3787 | return 0; |
| 3788 | } |
| 3789 | |
Al Viro | 25885a3 | 2022-08-16 11:57:56 -0400 | [diff] [blame] | 3790 | static bool __query_dir(struct dir_context *ctx, const char *name, int namlen, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3791 | loff_t offset, u64 ino, unsigned int d_type) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3792 | { |
| 3793 | struct ksmbd_readdir_data *buf; |
| 3794 | struct smb2_query_dir_private *priv; |
| 3795 | struct ksmbd_dir_info *d_info; |
| 3796 | int rc; |
| 3797 | |
| 3798 | buf = container_of(ctx, struct ksmbd_readdir_data, ctx); |
| 3799 | priv = buf->private; |
| 3800 | d_info = priv->d_info; |
| 3801 | |
| 3802 | /* dot and dotdot entries are already reserved */ |
| 3803 | if (!strcmp(".", name) || !strcmp("..", name)) |
Al Viro | 25885a3 | 2022-08-16 11:57:56 -0400 | [diff] [blame] | 3804 | return true; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3805 | if (ksmbd_share_veto_filename(priv->work->tcon->share_conf, name)) |
Al Viro | 25885a3 | 2022-08-16 11:57:56 -0400 | [diff] [blame] | 3806 | return true; |
Namjae Jeon | b24c933 | 2021-03-21 17:32:19 +0900 | [diff] [blame] | 3807 | if (!match_pattern(name, namlen, priv->search_pattern)) |
Al Viro | 25885a3 | 2022-08-16 11:57:56 -0400 | [diff] [blame] | 3808 | return true; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3809 | |
| 3810 | d_info->name = name; |
| 3811 | d_info->name_len = namlen; |
| 3812 | rc = reserve_populate_dentry(d_info, priv->info_level); |
| 3813 | if (rc) |
Al Viro | 25885a3 | 2022-08-16 11:57:56 -0400 | [diff] [blame] | 3814 | return false; |
| 3815 | if (d_info->flags & SMB2_RETURN_SINGLE_ENTRY) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3816 | d_info->out_buf_len = 0; |
Al Viro | 25885a3 | 2022-08-16 11:57:56 -0400 | [diff] [blame] | 3817 | return true; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3818 | } |
| 3819 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3820 | static int verify_info_level(int info_level) |
| 3821 | { |
| 3822 | switch (info_level) { |
| 3823 | case FILE_FULL_DIRECTORY_INFORMATION: |
| 3824 | case FILE_BOTH_DIRECTORY_INFORMATION: |
| 3825 | case FILE_DIRECTORY_INFORMATION: |
| 3826 | case FILE_NAMES_INFORMATION: |
| 3827 | case FILEID_FULL_DIRECTORY_INFORMATION: |
| 3828 | case FILEID_BOTH_DIRECTORY_INFORMATION: |
| 3829 | case SMB_FIND_FILE_POSIX_INFO: |
| 3830 | break; |
| 3831 | default: |
| 3832 | return -EOPNOTSUPP; |
| 3833 | } |
| 3834 | |
| 3835 | return 0; |
| 3836 | } |
| 3837 | |
Namjae Jeon | 8f05411 | 2022-08-02 07:28:51 +0900 | [diff] [blame] | 3838 | static int smb2_resp_buf_len(struct ksmbd_work *work, unsigned short hdr2_len) |
| 3839 | { |
| 3840 | int free_len; |
| 3841 | |
| 3842 | free_len = (int)(work->response_sz - |
| 3843 | (get_rfc1002_len(work->response_buf) + 4)) - hdr2_len; |
| 3844 | return free_len; |
| 3845 | } |
| 3846 | |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 3847 | static int smb2_calc_max_out_buf_len(struct ksmbd_work *work, |
| 3848 | unsigned short hdr2_len, |
| 3849 | unsigned int out_buf_len) |
| 3850 | { |
| 3851 | int free_len; |
| 3852 | |
| 3853 | if (out_buf_len > work->conn->vals->max_trans_size) |
| 3854 | return -EINVAL; |
| 3855 | |
Namjae Jeon | 8f05411 | 2022-08-02 07:28:51 +0900 | [diff] [blame] | 3856 | free_len = smb2_resp_buf_len(work, hdr2_len); |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 3857 | if (free_len < 0) |
| 3858 | return -EINVAL; |
| 3859 | |
| 3860 | return min_t(int, out_buf_len, free_len); |
| 3861 | } |
| 3862 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3863 | int smb2_query_dir(struct ksmbd_work *work) |
| 3864 | { |
| 3865 | struct ksmbd_conn *conn = work->conn; |
| 3866 | struct smb2_query_directory_req *req; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 3867 | struct smb2_query_directory_rsp *rsp; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3868 | struct ksmbd_share_config *share = work->tcon->share_conf; |
| 3869 | struct ksmbd_file *dir_fp = NULL; |
| 3870 | struct ksmbd_dir_info d_info; |
| 3871 | int rc = 0; |
| 3872 | char *srch_ptr = NULL; |
| 3873 | unsigned char srch_flag; |
| 3874 | int buffer_sz; |
| 3875 | struct smb2_query_dir_private query_dir_private = {NULL, }; |
| 3876 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3877 | WORK_BUFFERS(work, req, rsp); |
| 3878 | |
| 3879 | if (ksmbd_override_fsids(work)) { |
| 3880 | rsp->hdr.Status = STATUS_NO_MEMORY; |
| 3881 | smb2_set_err_rsp(work); |
| 3882 | return -ENOMEM; |
| 3883 | } |
| 3884 | |
| 3885 | rc = verify_info_level(req->FileInformationClass); |
| 3886 | if (rc) { |
| 3887 | rc = -EFAULT; |
| 3888 | goto err_out2; |
| 3889 | } |
| 3890 | |
Paulo Alcantara | 2d004c6 | 2022-03-21 13:08:26 -0300 | [diff] [blame] | 3891 | dir_fp = ksmbd_lookup_fd_slow(work, req->VolatileFileId, req->PersistentFileId); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3892 | if (!dir_fp) { |
| 3893 | rc = -EBADF; |
| 3894 | goto err_out2; |
| 3895 | } |
| 3896 | |
| 3897 | if (!(dir_fp->daccess & FILE_LIST_DIRECTORY_LE) || |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 3898 | inode_permission(file_mnt_user_ns(dir_fp->filp), |
| 3899 | file_inode(dir_fp->filp), |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3900 | MAY_READ | MAY_EXEC)) { |
Al Viro | 369c163 | 2022-01-30 21:11:02 -0500 | [diff] [blame] | 3901 | pr_err("no right to enumerate directory (%pD)\n", dir_fp->filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3902 | rc = -EACCES; |
| 3903 | goto err_out2; |
| 3904 | } |
| 3905 | |
| 3906 | if (!S_ISDIR(file_inode(dir_fp->filp)->i_mode)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 3907 | pr_err("can't do query dir for a file\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3908 | rc = -EINVAL; |
| 3909 | goto err_out2; |
| 3910 | } |
| 3911 | |
| 3912 | srch_flag = req->Flags; |
| 3913 | srch_ptr = smb_strndup_from_utf16(req->Buffer, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3914 | le16_to_cpu(req->FileNameLength), 1, |
| 3915 | conn->local_nls); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3916 | if (IS_ERR(srch_ptr)) { |
| 3917 | ksmbd_debug(SMB, "Search Pattern not found\n"); |
| 3918 | rc = -EINVAL; |
| 3919 | goto err_out2; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3920 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3921 | ksmbd_debug(SMB, "Search pattern is %s\n", srch_ptr); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3922 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3923 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3924 | if (srch_flag & SMB2_REOPEN || srch_flag & SMB2_RESTART_SCANS) { |
| 3925 | ksmbd_debug(SMB, "Restart directory scan\n"); |
| 3926 | generic_file_llseek(dir_fp->filp, 0, SEEK_SET); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3927 | } |
| 3928 | |
| 3929 | memset(&d_info, 0, sizeof(struct ksmbd_dir_info)); |
| 3930 | d_info.wptr = (char *)rsp->Buffer; |
| 3931 | d_info.rptr = (char *)rsp->Buffer; |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 3932 | d_info.out_buf_len = |
| 3933 | smb2_calc_max_out_buf_len(work, 8, |
| 3934 | le32_to_cpu(req->OutputBufferLength)); |
| 3935 | if (d_info.out_buf_len < 0) { |
| 3936 | rc = -EINVAL; |
| 3937 | goto err_out; |
| 3938 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3939 | d_info.flags = srch_flag; |
| 3940 | |
| 3941 | /* |
| 3942 | * reserve dot and dotdot entries in head of buffer |
| 3943 | * in first response |
| 3944 | */ |
| 3945 | rc = ksmbd_populate_dot_dotdot_entries(work, req->FileInformationClass, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3946 | dir_fp, &d_info, srch_ptr, |
| 3947 | smb2_populate_readdir_entry); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3948 | if (rc == -ENOSPC) |
| 3949 | rc = 0; |
| 3950 | else if (rc) |
| 3951 | goto err_out; |
| 3952 | |
| 3953 | if (test_share_config_flag(share, KSMBD_SHARE_FLAG_HIDE_DOT_FILES)) |
| 3954 | d_info.hide_dot_file = true; |
| 3955 | |
| 3956 | buffer_sz = d_info.out_buf_len; |
| 3957 | d_info.rptr = d_info.wptr; |
| 3958 | query_dir_private.work = work; |
| 3959 | query_dir_private.search_pattern = srch_ptr; |
| 3960 | query_dir_private.dir_fp = dir_fp; |
| 3961 | query_dir_private.d_info = &d_info; |
| 3962 | query_dir_private.info_level = req->FileInformationClass; |
| 3963 | dir_fp->readdir_data.private = &query_dir_private; |
| 3964 | set_ctx_actor(&dir_fp->readdir_data.ctx, __query_dir); |
| 3965 | |
Namjae Jeon | e8c0619 | 2021-06-22 11:06:11 +0900 | [diff] [blame] | 3966 | rc = iterate_dir(dir_fp->filp, &dir_fp->readdir_data.ctx); |
Namjae Jeon | 65ca7a3 | 2022-05-16 16:22:09 +0900 | [diff] [blame] | 3967 | /* |
| 3968 | * req->OutputBufferLength is too small to contain even one entry. |
| 3969 | * In this case, it immediately returns OutputBufferLength 0 to client. |
| 3970 | */ |
| 3971 | if (!d_info.out_buf_len && !d_info.num_entry) |
| 3972 | goto no_buf_len; |
Namjae Jeon | 88541cb | 2022-09-09 17:43:53 +0900 | [diff] [blame] | 3973 | if (rc > 0 || rc == -ENOSPC) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3974 | rc = 0; |
Namjae Jeon | 88541cb | 2022-09-09 17:43:53 +0900 | [diff] [blame] | 3975 | else if (rc) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3976 | goto err_out; |
| 3977 | |
| 3978 | d_info.wptr = d_info.rptr; |
| 3979 | d_info.out_buf_len = buffer_sz; |
| 3980 | rc = process_query_dir_entries(&query_dir_private); |
| 3981 | if (rc) |
| 3982 | goto err_out; |
| 3983 | |
| 3984 | if (!d_info.data_count && d_info.out_buf_len >= 0) { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3985 | if (srch_flag & SMB2_RETURN_SINGLE_ENTRY && !is_asterisk(srch_ptr)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3986 | rsp->hdr.Status = STATUS_NO_SUCH_FILE; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3987 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3988 | dir_fp->dot_dotdot[0] = dir_fp->dot_dotdot[1] = 0; |
| 3989 | rsp->hdr.Status = STATUS_NO_MORE_FILES; |
| 3990 | } |
| 3991 | rsp->StructureSize = cpu_to_le16(9); |
| 3992 | rsp->OutputBufferOffset = cpu_to_le16(0); |
| 3993 | rsp->OutputBufferLength = cpu_to_le32(0); |
| 3994 | rsp->Buffer[0] = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 3995 | inc_rfc1001_len(work->response_buf, 9); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3996 | } else { |
Namjae Jeon | 65ca7a3 | 2022-05-16 16:22:09 +0900 | [diff] [blame] | 3997 | no_buf_len: |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3998 | ((struct file_directory_info *) |
| 3999 | ((char *)rsp->Buffer + d_info.last_entry_offset)) |
| 4000 | ->NextEntryOffset = 0; |
Namjae Jeon | 65ca7a3 | 2022-05-16 16:22:09 +0900 | [diff] [blame] | 4001 | if (d_info.data_count >= d_info.last_entry_off_align) |
| 4002 | d_info.data_count -= d_info.last_entry_off_align; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4003 | |
| 4004 | rsp->StructureSize = cpu_to_le16(9); |
| 4005 | rsp->OutputBufferOffset = cpu_to_le16(72); |
| 4006 | rsp->OutputBufferLength = cpu_to_le32(d_info.data_count); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4007 | inc_rfc1001_len(work->response_buf, 8 + d_info.data_count); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4008 | } |
| 4009 | |
| 4010 | kfree(srch_ptr); |
| 4011 | ksmbd_fd_put(work, dir_fp); |
| 4012 | ksmbd_revert_fsids(work); |
| 4013 | return 0; |
| 4014 | |
| 4015 | err_out: |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4016 | pr_err("error while processing smb2 query dir rc = %d\n", rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4017 | kfree(srch_ptr); |
| 4018 | |
| 4019 | err_out2: |
| 4020 | if (rc == -EINVAL) |
| 4021 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 4022 | else if (rc == -EACCES) |
| 4023 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 4024 | else if (rc == -ENOENT) |
| 4025 | rsp->hdr.Status = STATUS_NO_SUCH_FILE; |
| 4026 | else if (rc == -EBADF) |
| 4027 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 4028 | else if (rc == -ENOMEM) |
| 4029 | rsp->hdr.Status = STATUS_NO_MEMORY; |
| 4030 | else if (rc == -EFAULT) |
| 4031 | rsp->hdr.Status = STATUS_INVALID_INFO_CLASS; |
Namjae Jeon | 88541cb | 2022-09-09 17:43:53 +0900 | [diff] [blame] | 4032 | else if (rc == -EIO) |
| 4033 | rsp->hdr.Status = STATUS_FILE_CORRUPT_ERROR; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4034 | if (!rsp->hdr.Status) |
| 4035 | rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR; |
| 4036 | |
| 4037 | smb2_set_err_rsp(work); |
| 4038 | ksmbd_fd_put(work, dir_fp); |
| 4039 | ksmbd_revert_fsids(work); |
| 4040 | return 0; |
| 4041 | } |
| 4042 | |
| 4043 | /** |
| 4044 | * buffer_check_err() - helper function to check buffer errors |
| 4045 | * @reqOutputBufferLength: max buffer length expected in command response |
| 4046 | * @rsp: query info response buffer contains output buffer length |
Yang Li | e230d01 | 2021-12-21 17:07:11 +0800 | [diff] [blame] | 4047 | * @rsp_org: base response buffer pointer in case of chained response |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4048 | * @infoclass_size: query info class response buffer size |
| 4049 | * |
| 4050 | * Return: 0 on success, otherwise error |
| 4051 | */ |
| 4052 | static int buffer_check_err(int reqOutputBufferLength, |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4053 | struct smb2_query_info_rsp *rsp, |
| 4054 | void *rsp_org, int infoclass_size) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4055 | { |
| 4056 | if (reqOutputBufferLength < le32_to_cpu(rsp->OutputBufferLength)) { |
| 4057 | if (reqOutputBufferLength < infoclass_size) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4058 | pr_err("Invalid Buffer Size Requested\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4059 | rsp->hdr.Status = STATUS_INFO_LENGTH_MISMATCH; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4060 | *(__be32 *)rsp_org = cpu_to_be32(sizeof(struct smb2_hdr)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4061 | return -EINVAL; |
| 4062 | } |
| 4063 | |
| 4064 | ksmbd_debug(SMB, "Buffer Overflow\n"); |
| 4065 | rsp->hdr.Status = STATUS_BUFFER_OVERFLOW; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4066 | *(__be32 *)rsp_org = cpu_to_be32(sizeof(struct smb2_hdr) + |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4067 | reqOutputBufferLength); |
| 4068 | rsp->OutputBufferLength = cpu_to_le32(reqOutputBufferLength); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4069 | } |
| 4070 | return 0; |
| 4071 | } |
| 4072 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4073 | static void get_standard_info_pipe(struct smb2_query_info_rsp *rsp, |
| 4074 | void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4075 | { |
| 4076 | struct smb2_file_standard_info *sinfo; |
| 4077 | |
| 4078 | sinfo = (struct smb2_file_standard_info *)rsp->Buffer; |
| 4079 | |
| 4080 | sinfo->AllocationSize = cpu_to_le64(4096); |
| 4081 | sinfo->EndOfFile = cpu_to_le64(0); |
| 4082 | sinfo->NumberOfLinks = cpu_to_le32(1); |
| 4083 | sinfo->DeletePending = 1; |
| 4084 | sinfo->Directory = 0; |
| 4085 | rsp->OutputBufferLength = |
| 4086 | cpu_to_le32(sizeof(struct smb2_file_standard_info)); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4087 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_standard_info)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4088 | } |
| 4089 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4090 | static void get_internal_info_pipe(struct smb2_query_info_rsp *rsp, u64 num, |
| 4091 | void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4092 | { |
| 4093 | struct smb2_file_internal_info *file_info; |
| 4094 | |
| 4095 | file_info = (struct smb2_file_internal_info *)rsp->Buffer; |
| 4096 | |
| 4097 | /* any unique number */ |
| 4098 | file_info->IndexNumber = cpu_to_le64(num | (1ULL << 63)); |
| 4099 | rsp->OutputBufferLength = |
| 4100 | cpu_to_le32(sizeof(struct smb2_file_internal_info)); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4101 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_internal_info)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4102 | } |
| 4103 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4104 | static int smb2_get_info_file_pipe(struct ksmbd_session *sess, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4105 | struct smb2_query_info_req *req, |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4106 | struct smb2_query_info_rsp *rsp, |
| 4107 | void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4108 | { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4109 | u64 id; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4110 | int rc; |
| 4111 | |
| 4112 | /* |
| 4113 | * Windows can sometime send query file info request on |
| 4114 | * pipe without opening it, checking error condition here |
| 4115 | */ |
Paulo Alcantara | 2d004c6 | 2022-03-21 13:08:26 -0300 | [diff] [blame] | 4116 | id = req->VolatileFileId; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4117 | if (!ksmbd_session_rpc_method(sess, id)) |
| 4118 | return -ENOENT; |
| 4119 | |
| 4120 | ksmbd_debug(SMB, "FileInfoClass %u, FileId 0x%llx\n", |
Paulo Alcantara | 2d004c6 | 2022-03-21 13:08:26 -0300 | [diff] [blame] | 4121 | req->FileInfoClass, req->VolatileFileId); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4122 | |
| 4123 | switch (req->FileInfoClass) { |
| 4124 | case FILE_STANDARD_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4125 | get_standard_info_pipe(rsp, rsp_org); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4126 | rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength), |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4127 | rsp, rsp_org, |
| 4128 | FILE_STANDARD_INFORMATION_SIZE); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4129 | break; |
| 4130 | case FILE_INTERNAL_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4131 | get_internal_info_pipe(rsp, id, rsp_org); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4132 | rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength), |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4133 | rsp, rsp_org, |
| 4134 | FILE_INTERNAL_INFORMATION_SIZE); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4135 | break; |
| 4136 | default: |
| 4137 | ksmbd_debug(SMB, "smb2_info_file_pipe for %u not supported\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4138 | req->FileInfoClass); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4139 | rc = -EOPNOTSUPP; |
| 4140 | } |
| 4141 | return rc; |
| 4142 | } |
| 4143 | |
| 4144 | /** |
| 4145 | * smb2_get_ea() - handler for smb2 get extended attribute command |
| 4146 | * @work: smb work containing query info command buffer |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 4147 | * @fp: ksmbd_file pointer |
| 4148 | * @req: get extended attribute request |
| 4149 | * @rsp: response buffer pointer |
| 4150 | * @rsp_org: base response buffer pointer in case of chained response |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4151 | * |
| 4152 | * Return: 0 on success, otherwise error |
| 4153 | */ |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4154 | static int smb2_get_ea(struct ksmbd_work *work, struct ksmbd_file *fp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4155 | struct smb2_query_info_req *req, |
| 4156 | struct smb2_query_info_rsp *rsp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4157 | { |
| 4158 | struct smb2_ea_info *eainfo, *prev_eainfo; |
| 4159 | char *name, *ptr, *xattr_list = NULL, *buf; |
| 4160 | int rc, name_len, value_len, xattr_list_len, idx; |
| 4161 | ssize_t buf_free_len, alignment_bytes, next_offset, rsp_data_cnt = 0; |
| 4162 | struct smb2_ea_info_req *ea_req = NULL; |
Al Viro | c22180a | 2022-08-04 12:51:14 -0400 | [diff] [blame] | 4163 | const struct path *path; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 4164 | struct user_namespace *user_ns = file_mnt_user_ns(fp->filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4165 | |
| 4166 | if (!(fp->daccess & FILE_READ_EA_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4167 | pr_err("Not permitted to read ext attr : 0x%x\n", |
| 4168 | fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4169 | return -EACCES; |
| 4170 | } |
| 4171 | |
| 4172 | path = &fp->filp->f_path; |
| 4173 | /* single EA entry is requested with given user.* name */ |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4174 | if (req->InputBufferLength) { |
Namjae Jeon | 6d56262 | 2021-09-18 18:45:12 +0900 | [diff] [blame] | 4175 | if (le32_to_cpu(req->InputBufferLength) < |
| 4176 | sizeof(struct smb2_ea_info_req)) |
| 4177 | return -EINVAL; |
| 4178 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4179 | ea_req = (struct smb2_ea_info_req *)req->Buffer; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4180 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4181 | /* need to send all EAs, if no specific EA is requested*/ |
| 4182 | if (le32_to_cpu(req->Flags) & SL_RETURN_SINGLE_ENTRY) |
| 4183 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4184 | "All EAs are requested but need to send single EA entry in rsp flags 0x%x\n", |
| 4185 | le32_to_cpu(req->Flags)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4186 | } |
| 4187 | |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 4188 | buf_free_len = |
| 4189 | smb2_calc_max_out_buf_len(work, 8, |
| 4190 | le32_to_cpu(req->OutputBufferLength)); |
| 4191 | if (buf_free_len < 0) |
| 4192 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4193 | |
| 4194 | rc = ksmbd_vfs_listxattr(path->dentry, &xattr_list); |
| 4195 | if (rc < 0) { |
| 4196 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 4197 | goto out; |
| 4198 | } else if (!rc) { /* there is no EA in the file */ |
| 4199 | ksmbd_debug(SMB, "no ea data in the file\n"); |
| 4200 | goto done; |
| 4201 | } |
| 4202 | xattr_list_len = rc; |
| 4203 | |
| 4204 | ptr = (char *)rsp->Buffer; |
| 4205 | eainfo = (struct smb2_ea_info *)ptr; |
| 4206 | prev_eainfo = eainfo; |
| 4207 | idx = 0; |
| 4208 | |
| 4209 | while (idx < xattr_list_len) { |
| 4210 | name = xattr_list + idx; |
| 4211 | name_len = strlen(name); |
| 4212 | |
| 4213 | ksmbd_debug(SMB, "%s, len %d\n", name, name_len); |
| 4214 | idx += name_len + 1; |
| 4215 | |
| 4216 | /* |
| 4217 | * CIFS does not support EA other than user.* namespace, |
| 4218 | * still keep the framework generic, to list other attrs |
| 4219 | * in future. |
| 4220 | */ |
| 4221 | if (strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) |
| 4222 | continue; |
| 4223 | |
| 4224 | if (!strncmp(&name[XATTR_USER_PREFIX_LEN], STREAM_PREFIX, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4225 | STREAM_PREFIX_LEN)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4226 | continue; |
| 4227 | |
| 4228 | if (req->InputBufferLength && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4229 | strncmp(&name[XATTR_USER_PREFIX_LEN], ea_req->name, |
| 4230 | ea_req->EaNameLength)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4231 | continue; |
| 4232 | |
| 4233 | if (!strncmp(&name[XATTR_USER_PREFIX_LEN], |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4234 | DOS_ATTRIBUTE_PREFIX, DOS_ATTRIBUTE_PREFIX_LEN)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4235 | continue; |
| 4236 | |
| 4237 | if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) |
| 4238 | name_len -= XATTR_USER_PREFIX_LEN; |
| 4239 | |
| 4240 | ptr = (char *)(&eainfo->name + name_len + 1); |
| 4241 | buf_free_len -= (offsetof(struct smb2_ea_info, name) + |
| 4242 | name_len + 1); |
| 4243 | /* bailout if xattr can't fit in buf_free_len */ |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 4244 | value_len = ksmbd_vfs_getxattr(user_ns, path->dentry, |
| 4245 | name, &buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4246 | if (value_len <= 0) { |
| 4247 | rc = -ENOENT; |
| 4248 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 4249 | goto out; |
| 4250 | } |
| 4251 | |
| 4252 | buf_free_len -= value_len; |
| 4253 | if (buf_free_len < 0) { |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 4254 | kfree(buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4255 | break; |
| 4256 | } |
| 4257 | |
| 4258 | memcpy(ptr, buf, value_len); |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 4259 | kfree(buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4260 | |
| 4261 | ptr += value_len; |
| 4262 | eainfo->Flags = 0; |
| 4263 | eainfo->EaNameLength = name_len; |
| 4264 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4265 | if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4266 | memcpy(eainfo->name, &name[XATTR_USER_PREFIX_LEN], |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4267 | name_len); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4268 | else |
| 4269 | memcpy(eainfo->name, name, name_len); |
| 4270 | |
| 4271 | eainfo->name[name_len] = '\0'; |
| 4272 | eainfo->EaValueLength = cpu_to_le16(value_len); |
| 4273 | next_offset = offsetof(struct smb2_ea_info, name) + |
| 4274 | name_len + 1 + value_len; |
| 4275 | |
| 4276 | /* align next xattr entry at 4 byte bundary */ |
| 4277 | alignment_bytes = ((next_offset + 3) & ~3) - next_offset; |
| 4278 | if (alignment_bytes) { |
| 4279 | memset(ptr, '\0', alignment_bytes); |
| 4280 | ptr += alignment_bytes; |
| 4281 | next_offset += alignment_bytes; |
| 4282 | buf_free_len -= alignment_bytes; |
| 4283 | } |
| 4284 | eainfo->NextEntryOffset = cpu_to_le32(next_offset); |
| 4285 | prev_eainfo = eainfo; |
| 4286 | eainfo = (struct smb2_ea_info *)ptr; |
| 4287 | rsp_data_cnt += next_offset; |
| 4288 | |
| 4289 | if (req->InputBufferLength) { |
| 4290 | ksmbd_debug(SMB, "single entry requested\n"); |
| 4291 | break; |
| 4292 | } |
| 4293 | } |
| 4294 | |
| 4295 | /* no more ea entries */ |
| 4296 | prev_eainfo->NextEntryOffset = 0; |
| 4297 | done: |
| 4298 | rc = 0; |
| 4299 | if (rsp_data_cnt == 0) |
| 4300 | rsp->hdr.Status = STATUS_NO_EAS_ON_FILE; |
| 4301 | rsp->OutputBufferLength = cpu_to_le32(rsp_data_cnt); |
| 4302 | inc_rfc1001_len(rsp_org, rsp_data_cnt); |
| 4303 | out: |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 4304 | kvfree(xattr_list); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4305 | return rc; |
| 4306 | } |
| 4307 | |
| 4308 | static void get_file_access_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4309 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4310 | { |
| 4311 | struct smb2_file_access_info *file_info; |
| 4312 | |
| 4313 | file_info = (struct smb2_file_access_info *)rsp->Buffer; |
| 4314 | file_info->AccessFlags = fp->daccess; |
| 4315 | rsp->OutputBufferLength = |
| 4316 | cpu_to_le32(sizeof(struct smb2_file_access_info)); |
| 4317 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_access_info)); |
| 4318 | } |
| 4319 | |
| 4320 | static int get_file_basic_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4321 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4322 | { |
Namjae Jeon | 88d3005 | 2021-09-29 15:37:18 +0900 | [diff] [blame] | 4323 | struct smb2_file_basic_info *basic_info; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4324 | struct kstat stat; |
| 4325 | u64 time; |
| 4326 | |
| 4327 | if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4328 | pr_err("no right to read the attributes : 0x%x\n", |
| 4329 | fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4330 | return -EACCES; |
| 4331 | } |
| 4332 | |
Namjae Jeon | 88d3005 | 2021-09-29 15:37:18 +0900 | [diff] [blame] | 4333 | basic_info = (struct smb2_file_basic_info *)rsp->Buffer; |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 4334 | generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp), |
| 4335 | &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4336 | basic_info->CreationTime = cpu_to_le64(fp->create_time); |
| 4337 | time = ksmbd_UnixTimeToNT(stat.atime); |
| 4338 | basic_info->LastAccessTime = cpu_to_le64(time); |
| 4339 | time = ksmbd_UnixTimeToNT(stat.mtime); |
| 4340 | basic_info->LastWriteTime = cpu_to_le64(time); |
| 4341 | time = ksmbd_UnixTimeToNT(stat.ctime); |
| 4342 | basic_info->ChangeTime = cpu_to_le64(time); |
| 4343 | basic_info->Attributes = fp->f_ci->m_fattr; |
| 4344 | basic_info->Pad1 = 0; |
| 4345 | rsp->OutputBufferLength = |
Namjae Jeon | 88d3005 | 2021-09-29 15:37:18 +0900 | [diff] [blame] | 4346 | cpu_to_le32(sizeof(struct smb2_file_basic_info)); |
| 4347 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_basic_info)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4348 | return 0; |
| 4349 | } |
| 4350 | |
| 4351 | static unsigned long long get_allocation_size(struct inode *inode, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4352 | struct kstat *stat) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4353 | { |
| 4354 | unsigned long long alloc_size = 0; |
| 4355 | |
| 4356 | if (!S_ISDIR(stat->mode)) { |
| 4357 | if ((inode->i_blocks << 9) <= stat->size) |
| 4358 | alloc_size = stat->size; |
| 4359 | else |
| 4360 | alloc_size = inode->i_blocks << 9; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4361 | } |
| 4362 | |
| 4363 | return alloc_size; |
| 4364 | } |
| 4365 | |
| 4366 | static void get_file_standard_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4367 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4368 | { |
| 4369 | struct smb2_file_standard_info *sinfo; |
| 4370 | unsigned int delete_pending; |
| 4371 | struct inode *inode; |
| 4372 | struct kstat stat; |
| 4373 | |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 4374 | inode = file_inode(fp->filp); |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 4375 | generic_fillattr(file_mnt_user_ns(fp->filp), inode, &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4376 | |
| 4377 | sinfo = (struct smb2_file_standard_info *)rsp->Buffer; |
| 4378 | delete_pending = ksmbd_inode_pending_delete(fp); |
| 4379 | |
| 4380 | sinfo->AllocationSize = cpu_to_le64(get_allocation_size(inode, &stat)); |
| 4381 | sinfo->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size); |
| 4382 | sinfo->NumberOfLinks = cpu_to_le32(get_nlink(&stat) - delete_pending); |
| 4383 | sinfo->DeletePending = delete_pending; |
| 4384 | sinfo->Directory = S_ISDIR(stat.mode) ? 1 : 0; |
| 4385 | rsp->OutputBufferLength = |
| 4386 | cpu_to_le32(sizeof(struct smb2_file_standard_info)); |
| 4387 | inc_rfc1001_len(rsp_org, |
| 4388 | sizeof(struct smb2_file_standard_info)); |
| 4389 | } |
| 4390 | |
| 4391 | static void get_file_alignment_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4392 | void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4393 | { |
| 4394 | struct smb2_file_alignment_info *file_info; |
| 4395 | |
| 4396 | file_info = (struct smb2_file_alignment_info *)rsp->Buffer; |
| 4397 | file_info->AlignmentRequirement = 0; |
| 4398 | rsp->OutputBufferLength = |
| 4399 | cpu_to_le32(sizeof(struct smb2_file_alignment_info)); |
| 4400 | inc_rfc1001_len(rsp_org, |
| 4401 | sizeof(struct smb2_file_alignment_info)); |
| 4402 | } |
| 4403 | |
| 4404 | static int get_file_all_info(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4405 | struct smb2_query_info_rsp *rsp, |
| 4406 | struct ksmbd_file *fp, |
| 4407 | void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4408 | { |
| 4409 | struct ksmbd_conn *conn = work->conn; |
| 4410 | struct smb2_file_all_info *file_info; |
| 4411 | unsigned int delete_pending; |
| 4412 | struct inode *inode; |
| 4413 | struct kstat stat; |
| 4414 | int conv_len; |
| 4415 | char *filename; |
| 4416 | u64 time; |
| 4417 | |
| 4418 | if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) { |
| 4419 | ksmbd_debug(SMB, "no right to read the attributes : 0x%x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4420 | fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4421 | return -EACCES; |
| 4422 | } |
| 4423 | |
Namjae Jeon | 50f500b | 2022-02-24 11:03:41 +0900 | [diff] [blame] | 4424 | filename = convert_to_nt_pathname(work->tcon->share_conf, &fp->filp->f_path); |
| 4425 | if (IS_ERR(filename)) |
| 4426 | return PTR_ERR(filename); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4427 | |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 4428 | inode = file_inode(fp->filp); |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 4429 | generic_fillattr(file_mnt_user_ns(fp->filp), inode, &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4430 | |
| 4431 | ksmbd_debug(SMB, "filename = %s\n", filename); |
| 4432 | delete_pending = ksmbd_inode_pending_delete(fp); |
| 4433 | file_info = (struct smb2_file_all_info *)rsp->Buffer; |
| 4434 | |
| 4435 | file_info->CreationTime = cpu_to_le64(fp->create_time); |
| 4436 | time = ksmbd_UnixTimeToNT(stat.atime); |
| 4437 | file_info->LastAccessTime = cpu_to_le64(time); |
| 4438 | time = ksmbd_UnixTimeToNT(stat.mtime); |
| 4439 | file_info->LastWriteTime = cpu_to_le64(time); |
| 4440 | time = ksmbd_UnixTimeToNT(stat.ctime); |
| 4441 | file_info->ChangeTime = cpu_to_le64(time); |
| 4442 | file_info->Attributes = fp->f_ci->m_fattr; |
| 4443 | file_info->Pad1 = 0; |
| 4444 | file_info->AllocationSize = |
| 4445 | cpu_to_le64(get_allocation_size(inode, &stat)); |
| 4446 | file_info->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size); |
| 4447 | file_info->NumberOfLinks = |
| 4448 | cpu_to_le32(get_nlink(&stat) - delete_pending); |
| 4449 | file_info->DeletePending = delete_pending; |
| 4450 | file_info->Directory = S_ISDIR(stat.mode) ? 1 : 0; |
| 4451 | file_info->Pad2 = 0; |
| 4452 | file_info->IndexNumber = cpu_to_le64(stat.ino); |
| 4453 | file_info->EASize = 0; |
| 4454 | file_info->AccessFlags = fp->daccess; |
| 4455 | file_info->CurrentByteOffset = cpu_to_le64(fp->filp->f_pos); |
| 4456 | file_info->Mode = fp->coption; |
| 4457 | file_info->AlignmentRequirement = 0; |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4458 | conv_len = smbConvertToUTF16((__le16 *)file_info->FileName, filename, |
| 4459 | PATH_MAX, conn->local_nls, 0); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4460 | conv_len *= 2; |
| 4461 | file_info->FileNameLength = cpu_to_le32(conv_len); |
| 4462 | rsp->OutputBufferLength = |
| 4463 | cpu_to_le32(sizeof(struct smb2_file_all_info) + conv_len - 1); |
| 4464 | kfree(filename); |
| 4465 | inc_rfc1001_len(rsp_org, le32_to_cpu(rsp->OutputBufferLength)); |
| 4466 | return 0; |
| 4467 | } |
| 4468 | |
| 4469 | static void get_file_alternate_info(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4470 | struct smb2_query_info_rsp *rsp, |
| 4471 | struct ksmbd_file *fp, |
| 4472 | void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4473 | { |
| 4474 | struct ksmbd_conn *conn = work->conn; |
| 4475 | struct smb2_file_alt_name_info *file_info; |
Namjae Jeon | 493fa2f | 2021-06-29 09:22:16 +0900 | [diff] [blame] | 4476 | struct dentry *dentry = fp->filp->f_path.dentry; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4477 | int conv_len; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4478 | |
Namjae Jeon | 493fa2f | 2021-06-29 09:22:16 +0900 | [diff] [blame] | 4479 | spin_lock(&dentry->d_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4480 | file_info = (struct smb2_file_alt_name_info *)rsp->Buffer; |
| 4481 | conv_len = ksmbd_extract_shortname(conn, |
Namjae Jeon | 493fa2f | 2021-06-29 09:22:16 +0900 | [diff] [blame] | 4482 | dentry->d_name.name, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4483 | file_info->FileName); |
Namjae Jeon | 493fa2f | 2021-06-29 09:22:16 +0900 | [diff] [blame] | 4484 | spin_unlock(&dentry->d_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4485 | file_info->FileNameLength = cpu_to_le32(conv_len); |
| 4486 | rsp->OutputBufferLength = |
| 4487 | cpu_to_le32(sizeof(struct smb2_file_alt_name_info) + conv_len); |
| 4488 | inc_rfc1001_len(rsp_org, le32_to_cpu(rsp->OutputBufferLength)); |
| 4489 | } |
| 4490 | |
| 4491 | static void get_file_stream_info(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4492 | struct smb2_query_info_rsp *rsp, |
| 4493 | struct ksmbd_file *fp, |
| 4494 | void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4495 | { |
| 4496 | struct ksmbd_conn *conn = work->conn; |
| 4497 | struct smb2_file_stream_info *file_info; |
| 4498 | char *stream_name, *xattr_list = NULL, *stream_buf; |
| 4499 | struct kstat stat; |
Al Viro | c22180a | 2022-08-04 12:51:14 -0400 | [diff] [blame] | 4500 | const struct path *path = &fp->filp->f_path; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4501 | ssize_t xattr_list_len; |
| 4502 | int nbytes = 0, streamlen, stream_name_len, next, idx = 0; |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 4503 | int buf_free_len; |
| 4504 | struct smb2_query_info_req *req = ksmbd_req_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4505 | |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 4506 | generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp), |
| 4507 | &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4508 | file_info = (struct smb2_file_stream_info *)rsp->Buffer; |
| 4509 | |
Namjae Jeon | 1ec72153 | 2021-11-21 11:32:39 +0900 | [diff] [blame] | 4510 | buf_free_len = |
| 4511 | smb2_calc_max_out_buf_len(work, 8, |
| 4512 | le32_to_cpu(req->OutputBufferLength)); |
| 4513 | if (buf_free_len < 0) |
| 4514 | goto out; |
| 4515 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4516 | xattr_list_len = ksmbd_vfs_listxattr(path->dentry, &xattr_list); |
| 4517 | if (xattr_list_len < 0) { |
| 4518 | goto out; |
| 4519 | } else if (!xattr_list_len) { |
| 4520 | ksmbd_debug(SMB, "empty xattr in the file\n"); |
| 4521 | goto out; |
| 4522 | } |
| 4523 | |
| 4524 | while (idx < xattr_list_len) { |
| 4525 | stream_name = xattr_list + idx; |
| 4526 | streamlen = strlen(stream_name); |
| 4527 | idx += streamlen + 1; |
| 4528 | |
| 4529 | ksmbd_debug(SMB, "%s, len %d\n", stream_name, streamlen); |
| 4530 | |
| 4531 | if (strncmp(&stream_name[XATTR_USER_PREFIX_LEN], |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4532 | STREAM_PREFIX, STREAM_PREFIX_LEN)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4533 | continue; |
| 4534 | |
| 4535 | stream_name_len = streamlen - (XATTR_USER_PREFIX_LEN + |
| 4536 | STREAM_PREFIX_LEN); |
| 4537 | streamlen = stream_name_len; |
| 4538 | |
| 4539 | /* plus : size */ |
| 4540 | streamlen += 1; |
| 4541 | stream_buf = kmalloc(streamlen + 1, GFP_KERNEL); |
| 4542 | if (!stream_buf) |
| 4543 | break; |
| 4544 | |
| 4545 | streamlen = snprintf(stream_buf, streamlen + 1, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4546 | ":%s", &stream_name[XATTR_NAME_STREAM_LEN]); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4547 | |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 4548 | next = sizeof(struct smb2_file_stream_info) + streamlen * 2; |
Namjae Jeon | 178ca6f | 2021-11-24 10:23:02 +0900 | [diff] [blame] | 4549 | if (next > buf_free_len) { |
| 4550 | kfree(stream_buf); |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 4551 | break; |
Namjae Jeon | 178ca6f | 2021-11-24 10:23:02 +0900 | [diff] [blame] | 4552 | } |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 4553 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4554 | file_info = (struct smb2_file_stream_info *)&rsp->Buffer[nbytes]; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4555 | streamlen = smbConvertToUTF16((__le16 *)file_info->StreamName, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4556 | stream_buf, streamlen, |
| 4557 | conn->local_nls, 0); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4558 | streamlen *= 2; |
| 4559 | kfree(stream_buf); |
| 4560 | file_info->StreamNameLength = cpu_to_le32(streamlen); |
| 4561 | file_info->StreamSize = cpu_to_le64(stream_name_len); |
| 4562 | file_info->StreamAllocationSize = cpu_to_le64(stream_name_len); |
| 4563 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4564 | nbytes += next; |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 4565 | buf_free_len -= next; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4566 | file_info->NextEntryOffset = cpu_to_le32(next); |
| 4567 | } |
| 4568 | |
Namjae Jeon | 1ec72153 | 2021-11-21 11:32:39 +0900 | [diff] [blame] | 4569 | out: |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 4570 | if (!S_ISDIR(stat.mode) && |
| 4571 | buf_free_len >= sizeof(struct smb2_file_stream_info) + 7 * 2) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4572 | file_info = (struct smb2_file_stream_info *) |
| 4573 | &rsp->Buffer[nbytes]; |
| 4574 | streamlen = smbConvertToUTF16((__le16 *)file_info->StreamName, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4575 | "::$DATA", 7, conn->local_nls, 0); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4576 | streamlen *= 2; |
| 4577 | file_info->StreamNameLength = cpu_to_le32(streamlen); |
Namjae Jeon | 1ec72153 | 2021-11-21 11:32:39 +0900 | [diff] [blame] | 4578 | file_info->StreamSize = cpu_to_le64(stat.size); |
| 4579 | file_info->StreamAllocationSize = cpu_to_le64(stat.blocks << 9); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4580 | nbytes += sizeof(struct smb2_file_stream_info) + streamlen; |
| 4581 | } |
| 4582 | |
| 4583 | /* last entry offset should be 0 */ |
| 4584 | file_info->NextEntryOffset = 0; |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 4585 | kvfree(xattr_list); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4586 | |
| 4587 | rsp->OutputBufferLength = cpu_to_le32(nbytes); |
| 4588 | inc_rfc1001_len(rsp_org, nbytes); |
| 4589 | } |
| 4590 | |
| 4591 | static void get_file_internal_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4592 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4593 | { |
| 4594 | struct smb2_file_internal_info *file_info; |
| 4595 | struct kstat stat; |
| 4596 | |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 4597 | generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp), |
| 4598 | &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4599 | file_info = (struct smb2_file_internal_info *)rsp->Buffer; |
| 4600 | file_info->IndexNumber = cpu_to_le64(stat.ino); |
| 4601 | rsp->OutputBufferLength = |
| 4602 | cpu_to_le32(sizeof(struct smb2_file_internal_info)); |
| 4603 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_internal_info)); |
| 4604 | } |
| 4605 | |
| 4606 | static int get_file_network_open_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4607 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4608 | { |
| 4609 | struct smb2_file_ntwrk_info *file_info; |
| 4610 | struct inode *inode; |
| 4611 | struct kstat stat; |
| 4612 | u64 time; |
| 4613 | |
| 4614 | if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4615 | pr_err("no right to read the attributes : 0x%x\n", |
| 4616 | fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4617 | return -EACCES; |
| 4618 | } |
| 4619 | |
| 4620 | file_info = (struct smb2_file_ntwrk_info *)rsp->Buffer; |
| 4621 | |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 4622 | inode = file_inode(fp->filp); |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 4623 | generic_fillattr(file_mnt_user_ns(fp->filp), inode, &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4624 | |
| 4625 | file_info->CreationTime = cpu_to_le64(fp->create_time); |
| 4626 | time = ksmbd_UnixTimeToNT(stat.atime); |
| 4627 | file_info->LastAccessTime = cpu_to_le64(time); |
| 4628 | time = ksmbd_UnixTimeToNT(stat.mtime); |
| 4629 | file_info->LastWriteTime = cpu_to_le64(time); |
| 4630 | time = ksmbd_UnixTimeToNT(stat.ctime); |
| 4631 | file_info->ChangeTime = cpu_to_le64(time); |
| 4632 | file_info->Attributes = fp->f_ci->m_fattr; |
| 4633 | file_info->AllocationSize = |
| 4634 | cpu_to_le64(get_allocation_size(inode, &stat)); |
| 4635 | file_info->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size); |
| 4636 | file_info->Reserved = cpu_to_le32(0); |
| 4637 | rsp->OutputBufferLength = |
| 4638 | cpu_to_le32(sizeof(struct smb2_file_ntwrk_info)); |
| 4639 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_ntwrk_info)); |
| 4640 | return 0; |
| 4641 | } |
| 4642 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4643 | static void get_file_ea_info(struct smb2_query_info_rsp *rsp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4644 | { |
| 4645 | struct smb2_file_ea_info *file_info; |
| 4646 | |
| 4647 | file_info = (struct smb2_file_ea_info *)rsp->Buffer; |
| 4648 | file_info->EASize = 0; |
| 4649 | rsp->OutputBufferLength = |
| 4650 | cpu_to_le32(sizeof(struct smb2_file_ea_info)); |
| 4651 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_ea_info)); |
| 4652 | } |
| 4653 | |
| 4654 | static void get_file_position_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4655 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4656 | { |
| 4657 | struct smb2_file_pos_info *file_info; |
| 4658 | |
| 4659 | file_info = (struct smb2_file_pos_info *)rsp->Buffer; |
| 4660 | file_info->CurrentByteOffset = cpu_to_le64(fp->filp->f_pos); |
| 4661 | rsp->OutputBufferLength = |
| 4662 | cpu_to_le32(sizeof(struct smb2_file_pos_info)); |
| 4663 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_pos_info)); |
| 4664 | } |
| 4665 | |
| 4666 | static void get_file_mode_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4667 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4668 | { |
| 4669 | struct smb2_file_mode_info *file_info; |
| 4670 | |
| 4671 | file_info = (struct smb2_file_mode_info *)rsp->Buffer; |
| 4672 | file_info->Mode = fp->coption & FILE_MODE_INFO_MASK; |
| 4673 | rsp->OutputBufferLength = |
| 4674 | cpu_to_le32(sizeof(struct smb2_file_mode_info)); |
| 4675 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_mode_info)); |
| 4676 | } |
| 4677 | |
| 4678 | static void get_file_compression_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4679 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4680 | { |
| 4681 | struct smb2_file_comp_info *file_info; |
| 4682 | struct kstat stat; |
| 4683 | |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 4684 | generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp), |
| 4685 | &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4686 | |
| 4687 | file_info = (struct smb2_file_comp_info *)rsp->Buffer; |
| 4688 | file_info->CompressedFileSize = cpu_to_le64(stat.blocks << 9); |
| 4689 | file_info->CompressionFormat = COMPRESSION_FORMAT_NONE; |
| 4690 | file_info->CompressionUnitShift = 0; |
| 4691 | file_info->ChunkShift = 0; |
| 4692 | file_info->ClusterShift = 0; |
| 4693 | memset(&file_info->Reserved[0], 0, 3); |
| 4694 | |
| 4695 | rsp->OutputBufferLength = |
| 4696 | cpu_to_le32(sizeof(struct smb2_file_comp_info)); |
| 4697 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_comp_info)); |
| 4698 | } |
| 4699 | |
| 4700 | static int get_file_attribute_tag_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4701 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4702 | { |
| 4703 | struct smb2_file_attr_tag_info *file_info; |
| 4704 | |
| 4705 | if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4706 | pr_err("no right to read the attributes : 0x%x\n", |
| 4707 | fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4708 | return -EACCES; |
| 4709 | } |
| 4710 | |
| 4711 | file_info = (struct smb2_file_attr_tag_info *)rsp->Buffer; |
| 4712 | file_info->FileAttributes = fp->f_ci->m_fattr; |
| 4713 | file_info->ReparseTag = 0; |
| 4714 | rsp->OutputBufferLength = |
| 4715 | cpu_to_le32(sizeof(struct smb2_file_attr_tag_info)); |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4716 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_attr_tag_info)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4717 | return 0; |
| 4718 | } |
| 4719 | |
| 4720 | static int find_file_posix_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4721 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4722 | { |
| 4723 | struct smb311_posix_qinfo *file_info; |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 4724 | struct inode *inode = file_inode(fp->filp); |
Namjae Jeon | d5919f2 | 2022-09-22 23:27:34 +0900 | [diff] [blame] | 4725 | struct user_namespace *user_ns = file_mnt_user_ns(fp->filp); |
| 4726 | vfsuid_t vfsuid = i_uid_into_vfsuid(user_ns, inode); |
| 4727 | vfsgid_t vfsgid = i_gid_into_vfsgid(user_ns, inode); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4728 | u64 time; |
Namjae Jeon | d5919f2 | 2022-09-22 23:27:34 +0900 | [diff] [blame] | 4729 | int out_buf_len = sizeof(struct smb311_posix_qinfo) + 32; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4730 | |
| 4731 | file_info = (struct smb311_posix_qinfo *)rsp->Buffer; |
| 4732 | file_info->CreationTime = cpu_to_le64(fp->create_time); |
| 4733 | time = ksmbd_UnixTimeToNT(inode->i_atime); |
| 4734 | file_info->LastAccessTime = cpu_to_le64(time); |
| 4735 | time = ksmbd_UnixTimeToNT(inode->i_mtime); |
| 4736 | file_info->LastWriteTime = cpu_to_le64(time); |
| 4737 | time = ksmbd_UnixTimeToNT(inode->i_ctime); |
| 4738 | file_info->ChangeTime = cpu_to_le64(time); |
| 4739 | file_info->DosAttributes = fp->f_ci->m_fattr; |
| 4740 | file_info->Inode = cpu_to_le64(inode->i_ino); |
| 4741 | file_info->EndOfFile = cpu_to_le64(inode->i_size); |
| 4742 | file_info->AllocationSize = cpu_to_le64(inode->i_blocks << 9); |
| 4743 | file_info->HardLinks = cpu_to_le32(inode->i_nlink); |
Namjae Jeon | f6c2b20 | 2022-09-22 23:16:12 +0900 | [diff] [blame] | 4744 | file_info->Mode = cpu_to_le32(inode->i_mode & 0777); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4745 | file_info->DeviceId = cpu_to_le32(inode->i_rdev); |
Namjae Jeon | d5919f2 | 2022-09-22 23:27:34 +0900 | [diff] [blame] | 4746 | |
| 4747 | /* |
| 4748 | * Sids(32) contain two sids(Domain sid(16), UNIX group sid(16)). |
| 4749 | * UNIX sid(16) = revision(1) + num_subauth(1) + authority(6) + |
| 4750 | * sub_auth(4 * 1(num_subauth)) + RID(4). |
| 4751 | */ |
| 4752 | id_to_sid(from_kuid_munged(&init_user_ns, vfsuid_into_kuid(vfsuid)), |
| 4753 | SIDUNIX_USER, (struct smb_sid *)&file_info->Sids[0]); |
| 4754 | id_to_sid(from_kgid_munged(&init_user_ns, vfsgid_into_kgid(vfsgid)), |
| 4755 | SIDUNIX_GROUP, (struct smb_sid *)&file_info->Sids[16]); |
| 4756 | |
| 4757 | rsp->OutputBufferLength = cpu_to_le32(out_buf_len); |
| 4758 | inc_rfc1001_len(rsp_org, out_buf_len); |
| 4759 | return out_buf_len; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4760 | } |
| 4761 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4762 | static int smb2_get_info_file(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4763 | struct smb2_query_info_req *req, |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4764 | struct smb2_query_info_rsp *rsp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4765 | { |
| 4766 | struct ksmbd_file *fp; |
| 4767 | int fileinfoclass = 0; |
| 4768 | int rc = 0; |
| 4769 | int file_infoclass_size; |
| 4770 | unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID; |
| 4771 | |
| 4772 | if (test_share_config_flag(work->tcon->share_conf, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4773 | KSMBD_SHARE_FLAG_PIPE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4774 | /* smb2 info file called for pipe */ |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4775 | return smb2_get_info_file_pipe(work->sess, req, rsp, |
| 4776 | work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4777 | } |
| 4778 | |
| 4779 | if (work->next_smb2_rcv_hdr_off) { |
Paulo Alcantara | 2d004c6 | 2022-03-21 13:08:26 -0300 | [diff] [blame] | 4780 | if (!has_file_id(req->VolatileFileId)) { |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 4781 | ksmbd_debug(SMB, "Compound request set FID = %llu\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4782 | work->compound_fid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4783 | id = work->compound_fid; |
| 4784 | pid = work->compound_pfid; |
| 4785 | } |
| 4786 | } |
| 4787 | |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 4788 | if (!has_file_id(id)) { |
Paulo Alcantara | 2d004c6 | 2022-03-21 13:08:26 -0300 | [diff] [blame] | 4789 | id = req->VolatileFileId; |
| 4790 | pid = req->PersistentFileId; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4791 | } |
| 4792 | |
| 4793 | fp = ksmbd_lookup_fd_slow(work, id, pid); |
| 4794 | if (!fp) |
| 4795 | return -ENOENT; |
| 4796 | |
| 4797 | fileinfoclass = req->FileInfoClass; |
| 4798 | |
| 4799 | switch (fileinfoclass) { |
| 4800 | case FILE_ACCESS_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4801 | get_file_access_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4802 | file_infoclass_size = FILE_ACCESS_INFORMATION_SIZE; |
| 4803 | break; |
| 4804 | |
| 4805 | case FILE_BASIC_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4806 | rc = get_file_basic_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4807 | file_infoclass_size = FILE_BASIC_INFORMATION_SIZE; |
| 4808 | break; |
| 4809 | |
| 4810 | case FILE_STANDARD_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4811 | get_file_standard_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4812 | file_infoclass_size = FILE_STANDARD_INFORMATION_SIZE; |
| 4813 | break; |
| 4814 | |
| 4815 | case FILE_ALIGNMENT_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4816 | get_file_alignment_info(rsp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4817 | file_infoclass_size = FILE_ALIGNMENT_INFORMATION_SIZE; |
| 4818 | break; |
| 4819 | |
| 4820 | case FILE_ALL_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4821 | rc = get_file_all_info(work, rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4822 | file_infoclass_size = FILE_ALL_INFORMATION_SIZE; |
| 4823 | break; |
| 4824 | |
| 4825 | case FILE_ALTERNATE_NAME_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4826 | get_file_alternate_info(work, rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4827 | file_infoclass_size = FILE_ALTERNATE_NAME_INFORMATION_SIZE; |
| 4828 | break; |
| 4829 | |
| 4830 | case FILE_STREAM_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4831 | get_file_stream_info(work, rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4832 | file_infoclass_size = FILE_STREAM_INFORMATION_SIZE; |
| 4833 | break; |
| 4834 | |
| 4835 | case FILE_INTERNAL_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4836 | get_file_internal_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4837 | file_infoclass_size = FILE_INTERNAL_INFORMATION_SIZE; |
| 4838 | break; |
| 4839 | |
| 4840 | case FILE_NETWORK_OPEN_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4841 | rc = get_file_network_open_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4842 | file_infoclass_size = FILE_NETWORK_OPEN_INFORMATION_SIZE; |
| 4843 | break; |
| 4844 | |
| 4845 | case FILE_EA_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4846 | get_file_ea_info(rsp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4847 | file_infoclass_size = FILE_EA_INFORMATION_SIZE; |
| 4848 | break; |
| 4849 | |
| 4850 | case FILE_FULL_EA_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4851 | rc = smb2_get_ea(work, fp, req, rsp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4852 | file_infoclass_size = FILE_FULL_EA_INFORMATION_SIZE; |
| 4853 | break; |
| 4854 | |
| 4855 | case FILE_POSITION_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4856 | get_file_position_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4857 | file_infoclass_size = FILE_POSITION_INFORMATION_SIZE; |
| 4858 | break; |
| 4859 | |
| 4860 | case FILE_MODE_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4861 | get_file_mode_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4862 | file_infoclass_size = FILE_MODE_INFORMATION_SIZE; |
| 4863 | break; |
| 4864 | |
| 4865 | case FILE_COMPRESSION_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4866 | get_file_compression_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4867 | file_infoclass_size = FILE_COMPRESSION_INFORMATION_SIZE; |
| 4868 | break; |
| 4869 | |
| 4870 | case FILE_ATTRIBUTE_TAG_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4871 | rc = get_file_attribute_tag_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4872 | file_infoclass_size = FILE_ATTRIBUTE_TAG_INFORMATION_SIZE; |
| 4873 | break; |
| 4874 | case SMB_FIND_FILE_POSIX_INFO: |
| 4875 | if (!work->tcon->posix_extensions) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4876 | pr_err("client doesn't negotiate with SMB3.1.1 POSIX Extensions\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4877 | rc = -EOPNOTSUPP; |
| 4878 | } else { |
Namjae Jeon | d5919f2 | 2022-09-22 23:27:34 +0900 | [diff] [blame] | 4879 | file_infoclass_size = find_file_posix_info(rsp, fp, |
| 4880 | work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4881 | } |
| 4882 | break; |
| 4883 | default: |
| 4884 | ksmbd_debug(SMB, "fileinfoclass %d not supported yet\n", |
| 4885 | fileinfoclass); |
| 4886 | rc = -EOPNOTSUPP; |
| 4887 | } |
| 4888 | if (!rc) |
| 4889 | rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength), |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4890 | rsp, work->response_buf, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4891 | file_infoclass_size); |
| 4892 | ksmbd_fd_put(work, fp); |
| 4893 | return rc; |
| 4894 | } |
| 4895 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4896 | static int smb2_get_info_filesystem(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4897 | struct smb2_query_info_req *req, |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4898 | struct smb2_query_info_rsp *rsp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4899 | { |
| 4900 | struct ksmbd_session *sess = work->sess; |
Namjae Jeon | af7c39d | 2022-07-25 13:36:52 +0900 | [diff] [blame] | 4901 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4902 | struct ksmbd_share_config *share = work->tcon->share_conf; |
| 4903 | int fsinfoclass = 0; |
| 4904 | struct kstatfs stfs; |
| 4905 | struct path path; |
| 4906 | int rc = 0, len; |
| 4907 | int fs_infoclass_size = 0; |
| 4908 | |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 4909 | rc = kern_path(share->path, LOOKUP_NO_SYMLINKS, &path); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4910 | if (rc) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4911 | pr_err("cannot create vfs path\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4912 | return -EIO; |
| 4913 | } |
| 4914 | |
| 4915 | rc = vfs_statfs(&path, &stfs); |
| 4916 | if (rc) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4917 | pr_err("cannot do stat of path %s\n", share->path); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4918 | path_put(&path); |
| 4919 | return -EIO; |
| 4920 | } |
| 4921 | |
| 4922 | fsinfoclass = req->FileInfoClass; |
| 4923 | |
| 4924 | switch (fsinfoclass) { |
| 4925 | case FS_DEVICE_INFORMATION: |
| 4926 | { |
| 4927 | struct filesystem_device_info *info; |
| 4928 | |
| 4929 | info = (struct filesystem_device_info *)rsp->Buffer; |
| 4930 | |
| 4931 | info->DeviceType = cpu_to_le32(stfs.f_type); |
| 4932 | info->DeviceCharacteristics = cpu_to_le32(0x00000020); |
| 4933 | rsp->OutputBufferLength = cpu_to_le32(8); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4934 | inc_rfc1001_len(work->response_buf, 8); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4935 | fs_infoclass_size = FS_DEVICE_INFORMATION_SIZE; |
| 4936 | break; |
| 4937 | } |
| 4938 | case FS_ATTRIBUTE_INFORMATION: |
| 4939 | { |
| 4940 | struct filesystem_attribute_info *info; |
| 4941 | size_t sz; |
| 4942 | |
| 4943 | info = (struct filesystem_attribute_info *)rsp->Buffer; |
| 4944 | info->Attributes = cpu_to_le32(FILE_SUPPORTS_OBJECT_IDS | |
| 4945 | FILE_PERSISTENT_ACLS | |
| 4946 | FILE_UNICODE_ON_DISK | |
| 4947 | FILE_CASE_PRESERVED_NAMES | |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 4948 | FILE_CASE_SENSITIVE_SEARCH | |
| 4949 | FILE_SUPPORTS_BLOCK_REFCOUNTING); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4950 | |
| 4951 | info->Attributes |= cpu_to_le32(server_conf.share_fake_fscaps); |
| 4952 | |
| 4953 | info->MaxPathNameComponentLength = cpu_to_le32(stfs.f_namelen); |
| 4954 | len = smbConvertToUTF16((__le16 *)info->FileSystemName, |
| 4955 | "NTFS", PATH_MAX, conn->local_nls, 0); |
| 4956 | len = len * 2; |
| 4957 | info->FileSystemNameLen = cpu_to_le32(len); |
| 4958 | sz = sizeof(struct filesystem_attribute_info) - 2 + len; |
| 4959 | rsp->OutputBufferLength = cpu_to_le32(sz); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4960 | inc_rfc1001_len(work->response_buf, sz); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4961 | fs_infoclass_size = FS_ATTRIBUTE_INFORMATION_SIZE; |
| 4962 | break; |
| 4963 | } |
| 4964 | case FS_VOLUME_INFORMATION: |
| 4965 | { |
| 4966 | struct filesystem_vol_info *info; |
| 4967 | size_t sz; |
Namjae Jeon | 5d2f0b1 | 2021-10-31 09:53:50 +0900 | [diff] [blame] | 4968 | unsigned int serial_crc = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4969 | |
| 4970 | info = (struct filesystem_vol_info *)(rsp->Buffer); |
| 4971 | info->VolumeCreationTime = 0; |
Namjae Jeon | 5d2f0b1 | 2021-10-31 09:53:50 +0900 | [diff] [blame] | 4972 | serial_crc = crc32_le(serial_crc, share->name, |
| 4973 | strlen(share->name)); |
| 4974 | serial_crc = crc32_le(serial_crc, share->path, |
| 4975 | strlen(share->path)); |
| 4976 | serial_crc = crc32_le(serial_crc, ksmbd_netbios_name(), |
| 4977 | strlen(ksmbd_netbios_name())); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4978 | /* Taking dummy value of serial number*/ |
Namjae Jeon | 5d2f0b1 | 2021-10-31 09:53:50 +0900 | [diff] [blame] | 4979 | info->SerialNumber = cpu_to_le32(serial_crc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4980 | len = smbConvertToUTF16((__le16 *)info->VolumeLabel, |
| 4981 | share->name, PATH_MAX, |
| 4982 | conn->local_nls, 0); |
| 4983 | len = len * 2; |
| 4984 | info->VolumeLabelSize = cpu_to_le32(len); |
| 4985 | info->Reserved = 0; |
| 4986 | sz = sizeof(struct filesystem_vol_info) - 2 + len; |
| 4987 | rsp->OutputBufferLength = cpu_to_le32(sz); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4988 | inc_rfc1001_len(work->response_buf, sz); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4989 | fs_infoclass_size = FS_VOLUME_INFORMATION_SIZE; |
| 4990 | break; |
| 4991 | } |
| 4992 | case FS_SIZE_INFORMATION: |
| 4993 | { |
| 4994 | struct filesystem_info *info; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4995 | |
| 4996 | info = (struct filesystem_info *)(rsp->Buffer); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4997 | info->TotalAllocationUnits = cpu_to_le64(stfs.f_blocks); |
| 4998 | info->FreeAllocationUnits = cpu_to_le64(stfs.f_bfree); |
Namjae Jeon | ee81cae | 2021-06-26 22:32:34 +0900 | [diff] [blame] | 4999 | info->SectorsPerAllocationUnit = cpu_to_le32(1); |
| 5000 | info->BytesPerSector = cpu_to_le32(stfs.f_bsize); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5001 | rsp->OutputBufferLength = cpu_to_le32(24); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5002 | inc_rfc1001_len(work->response_buf, 24); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5003 | fs_infoclass_size = FS_SIZE_INFORMATION_SIZE; |
| 5004 | break; |
| 5005 | } |
| 5006 | case FS_FULL_SIZE_INFORMATION: |
| 5007 | { |
| 5008 | struct smb2_fs_full_size_info *info; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5009 | |
| 5010 | info = (struct smb2_fs_full_size_info *)(rsp->Buffer); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5011 | info->TotalAllocationUnits = cpu_to_le64(stfs.f_blocks); |
| 5012 | info->CallerAvailableAllocationUnits = |
| 5013 | cpu_to_le64(stfs.f_bavail); |
| 5014 | info->ActualAvailableAllocationUnits = |
| 5015 | cpu_to_le64(stfs.f_bfree); |
Namjae Jeon | ee81cae | 2021-06-26 22:32:34 +0900 | [diff] [blame] | 5016 | info->SectorsPerAllocationUnit = cpu_to_le32(1); |
| 5017 | info->BytesPerSector = cpu_to_le32(stfs.f_bsize); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5018 | rsp->OutputBufferLength = cpu_to_le32(32); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5019 | inc_rfc1001_len(work->response_buf, 32); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5020 | fs_infoclass_size = FS_FULL_SIZE_INFORMATION_SIZE; |
| 5021 | break; |
| 5022 | } |
| 5023 | case FS_OBJECT_ID_INFORMATION: |
| 5024 | { |
| 5025 | struct object_id_info *info; |
| 5026 | |
| 5027 | info = (struct object_id_info *)(rsp->Buffer); |
| 5028 | |
| 5029 | if (!user_guest(sess->user)) |
| 5030 | memcpy(info->objid, user_passkey(sess->user), 16); |
| 5031 | else |
| 5032 | memset(info->objid, 0, 16); |
| 5033 | |
| 5034 | info->extended_info.magic = cpu_to_le32(EXTENDED_INFO_MAGIC); |
| 5035 | info->extended_info.version = cpu_to_le32(1); |
| 5036 | info->extended_info.release = cpu_to_le32(1); |
| 5037 | info->extended_info.rel_date = 0; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5038 | memcpy(info->extended_info.version_string, "1.1.0", strlen("1.1.0")); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5039 | rsp->OutputBufferLength = cpu_to_le32(64); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5040 | inc_rfc1001_len(work->response_buf, 64); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5041 | fs_infoclass_size = FS_OBJECT_ID_INFORMATION_SIZE; |
| 5042 | break; |
| 5043 | } |
| 5044 | case FS_SECTOR_SIZE_INFORMATION: |
| 5045 | { |
| 5046 | struct smb3_fs_ss_info *info; |
Namjae Jeon | 02655a7 | 2022-04-13 10:01:36 +0900 | [diff] [blame] | 5047 | unsigned int sector_size = |
| 5048 | min_t(unsigned int, path.mnt->mnt_sb->s_blocksize, 4096); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5049 | |
| 5050 | info = (struct smb3_fs_ss_info *)(rsp->Buffer); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5051 | |
Namjae Jeon | 02655a7 | 2022-04-13 10:01:36 +0900 | [diff] [blame] | 5052 | info->LogicalBytesPerSector = cpu_to_le32(sector_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5053 | info->PhysicalBytesPerSectorForAtomicity = |
Namjae Jeon | 02655a7 | 2022-04-13 10:01:36 +0900 | [diff] [blame] | 5054 | cpu_to_le32(sector_size); |
| 5055 | info->PhysicalBytesPerSectorForPerf = cpu_to_le32(sector_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5056 | info->FSEffPhysicalBytesPerSectorForAtomicity = |
Namjae Jeon | 02655a7 | 2022-04-13 10:01:36 +0900 | [diff] [blame] | 5057 | cpu_to_le32(sector_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5058 | info->Flags = cpu_to_le32(SSINFO_FLAGS_ALIGNED_DEVICE | |
| 5059 | SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE); |
| 5060 | info->ByteOffsetForSectorAlignment = 0; |
| 5061 | info->ByteOffsetForPartitionAlignment = 0; |
| 5062 | rsp->OutputBufferLength = cpu_to_le32(28); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5063 | inc_rfc1001_len(work->response_buf, 28); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5064 | fs_infoclass_size = FS_SECTOR_SIZE_INFORMATION_SIZE; |
| 5065 | break; |
| 5066 | } |
| 5067 | case FS_CONTROL_INFORMATION: |
| 5068 | { |
| 5069 | /* |
| 5070 | * TODO : The current implementation is based on |
| 5071 | * test result with win7(NTFS) server. It's need to |
| 5072 | * modify this to get valid Quota values |
| 5073 | * from Linux kernel |
| 5074 | */ |
| 5075 | struct smb2_fs_control_info *info; |
| 5076 | |
| 5077 | info = (struct smb2_fs_control_info *)(rsp->Buffer); |
| 5078 | info->FreeSpaceStartFiltering = 0; |
| 5079 | info->FreeSpaceThreshold = 0; |
| 5080 | info->FreeSpaceStopFiltering = 0; |
| 5081 | info->DefaultQuotaThreshold = cpu_to_le64(SMB2_NO_FID); |
| 5082 | info->DefaultQuotaLimit = cpu_to_le64(SMB2_NO_FID); |
| 5083 | info->Padding = 0; |
| 5084 | rsp->OutputBufferLength = cpu_to_le32(48); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5085 | inc_rfc1001_len(work->response_buf, 48); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5086 | fs_infoclass_size = FS_CONTROL_INFORMATION_SIZE; |
| 5087 | break; |
| 5088 | } |
| 5089 | case FS_POSIX_INFORMATION: |
| 5090 | { |
| 5091 | struct filesystem_posix_info *info; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5092 | |
| 5093 | if (!work->tcon->posix_extensions) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5094 | pr_err("client doesn't negotiate with SMB3.1.1 POSIX Extensions\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5095 | rc = -EOPNOTSUPP; |
| 5096 | } else { |
| 5097 | info = (struct filesystem_posix_info *)(rsp->Buffer); |
Namjae Jeon | ee81cae | 2021-06-26 22:32:34 +0900 | [diff] [blame] | 5098 | info->OptimalTransferSize = cpu_to_le32(stfs.f_bsize); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5099 | info->BlockSize = cpu_to_le32(stfs.f_bsize); |
| 5100 | info->TotalBlocks = cpu_to_le64(stfs.f_blocks); |
| 5101 | info->BlocksAvail = cpu_to_le64(stfs.f_bfree); |
| 5102 | info->UserBlocksAvail = cpu_to_le64(stfs.f_bavail); |
| 5103 | info->TotalFileNodes = cpu_to_le64(stfs.f_files); |
| 5104 | info->FreeFileNodes = cpu_to_le64(stfs.f_ffree); |
| 5105 | rsp->OutputBufferLength = cpu_to_le32(56); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5106 | inc_rfc1001_len(work->response_buf, 56); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5107 | fs_infoclass_size = FS_POSIX_INFORMATION_SIZE; |
| 5108 | } |
| 5109 | break; |
| 5110 | } |
| 5111 | default: |
| 5112 | path_put(&path); |
| 5113 | return -EOPNOTSUPP; |
| 5114 | } |
| 5115 | rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength), |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5116 | rsp, work->response_buf, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5117 | fs_infoclass_size); |
| 5118 | path_put(&path); |
| 5119 | return rc; |
| 5120 | } |
| 5121 | |
| 5122 | static int smb2_get_info_sec(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5123 | struct smb2_query_info_req *req, |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5124 | struct smb2_query_info_rsp *rsp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5125 | { |
| 5126 | struct ksmbd_file *fp; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 5127 | struct user_namespace *user_ns; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5128 | struct smb_ntsd *pntsd = (struct smb_ntsd *)rsp->Buffer, *ppntsd = NULL; |
| 5129 | struct smb_fattr fattr = {{0}}; |
| 5130 | struct inode *inode; |
Namjae Jeon | 8f05411 | 2022-08-02 07:28:51 +0900 | [diff] [blame] | 5131 | __u32 secdesclen = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5132 | unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID; |
| 5133 | int addition_info = le32_to_cpu(req->AdditionalInformation); |
Namjae Jeon | 8f05411 | 2022-08-02 07:28:51 +0900 | [diff] [blame] | 5134 | int rc = 0, ppntsd_size = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5135 | |
Namjae Jeon | e294f78 | 2021-06-28 15:26:37 +0900 | [diff] [blame] | 5136 | if (addition_info & ~(OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO | |
| 5137 | PROTECTED_DACL_SECINFO | |
| 5138 | UNPROTECTED_DACL_SECINFO)) { |
Namjae Jeon | 8e537d1 | 2021-11-21 07:48:45 +0900 | [diff] [blame] | 5139 | ksmbd_debug(SMB, "Unsupported addition info: 0x%x)\n", |
Namjae Jeon | e294f78 | 2021-06-28 15:26:37 +0900 | [diff] [blame] | 5140 | addition_info); |
Sebastian Gottschall | ced2b26 | 2021-04-27 15:33:54 +0900 | [diff] [blame] | 5141 | |
| 5142 | pntsd->revision = cpu_to_le16(1); |
| 5143 | pntsd->type = cpu_to_le16(SELF_RELATIVE | DACL_PROTECTED); |
| 5144 | pntsd->osidoffset = 0; |
| 5145 | pntsd->gsidoffset = 0; |
| 5146 | pntsd->sacloffset = 0; |
| 5147 | pntsd->dacloffset = 0; |
| 5148 | |
| 5149 | secdesclen = sizeof(struct smb_ntsd); |
| 5150 | rsp->OutputBufferLength = cpu_to_le32(secdesclen); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5151 | inc_rfc1001_len(work->response_buf, secdesclen); |
Sebastian Gottschall | ced2b26 | 2021-04-27 15:33:54 +0900 | [diff] [blame] | 5152 | |
| 5153 | return 0; |
| 5154 | } |
| 5155 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5156 | if (work->next_smb2_rcv_hdr_off) { |
Paulo Alcantara | 2d004c6 | 2022-03-21 13:08:26 -0300 | [diff] [blame] | 5157 | if (!has_file_id(req->VolatileFileId)) { |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 5158 | ksmbd_debug(SMB, "Compound request set FID = %llu\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5159 | work->compound_fid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5160 | id = work->compound_fid; |
| 5161 | pid = work->compound_pfid; |
| 5162 | } |
| 5163 | } |
| 5164 | |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 5165 | if (!has_file_id(id)) { |
Paulo Alcantara | 2d004c6 | 2022-03-21 13:08:26 -0300 | [diff] [blame] | 5166 | id = req->VolatileFileId; |
| 5167 | pid = req->PersistentFileId; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5168 | } |
| 5169 | |
| 5170 | fp = ksmbd_lookup_fd_slow(work, id, pid); |
| 5171 | if (!fp) |
| 5172 | return -ENOENT; |
| 5173 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 5174 | user_ns = file_mnt_user_ns(fp->filp); |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 5175 | inode = file_inode(fp->filp); |
Christian Brauner | 43205ca | 2021-08-23 17:13:50 +0200 | [diff] [blame] | 5176 | ksmbd_acls_fattr(&fattr, user_ns, inode); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5177 | |
| 5178 | if (test_share_config_flag(work->tcon->share_conf, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5179 | KSMBD_SHARE_FLAG_ACL_XATTR)) |
Namjae Jeon | 8f05411 | 2022-08-02 07:28:51 +0900 | [diff] [blame] | 5180 | ppntsd_size = ksmbd_vfs_get_sd_xattr(work->conn, user_ns, |
| 5181 | fp->filp->f_path.dentry, |
| 5182 | &ppntsd); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5183 | |
Namjae Jeon | 8f05411 | 2022-08-02 07:28:51 +0900 | [diff] [blame] | 5184 | /* Check if sd buffer size exceeds response buffer size */ |
| 5185 | if (smb2_resp_buf_len(work, 8) > ppntsd_size) |
| 5186 | rc = build_sec_desc(user_ns, pntsd, ppntsd, ppntsd_size, |
| 5187 | addition_info, &secdesclen, &fattr); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5188 | posix_acl_release(fattr.cf_acls); |
| 5189 | posix_acl_release(fattr.cf_dacls); |
| 5190 | kfree(ppntsd); |
| 5191 | ksmbd_fd_put(work, fp); |
| 5192 | if (rc) |
| 5193 | return rc; |
| 5194 | |
| 5195 | rsp->OutputBufferLength = cpu_to_le32(secdesclen); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5196 | inc_rfc1001_len(work->response_buf, secdesclen); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5197 | return 0; |
| 5198 | } |
| 5199 | |
| 5200 | /** |
| 5201 | * smb2_query_info() - handler for smb2 query info command |
| 5202 | * @work: smb work containing query info request buffer |
| 5203 | * |
| 5204 | * Return: 0 on success, otherwise error |
| 5205 | */ |
| 5206 | int smb2_query_info(struct ksmbd_work *work) |
| 5207 | { |
| 5208 | struct smb2_query_info_req *req; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5209 | struct smb2_query_info_rsp *rsp; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5210 | int rc = 0; |
| 5211 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5212 | WORK_BUFFERS(work, req, rsp); |
| 5213 | |
| 5214 | ksmbd_debug(SMB, "GOT query info request\n"); |
| 5215 | |
| 5216 | switch (req->InfoType) { |
| 5217 | case SMB2_O_INFO_FILE: |
| 5218 | ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILE\n"); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5219 | rc = smb2_get_info_file(work, req, rsp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5220 | break; |
| 5221 | case SMB2_O_INFO_FILESYSTEM: |
| 5222 | ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILESYSTEM\n"); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5223 | rc = smb2_get_info_filesystem(work, req, rsp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5224 | break; |
| 5225 | case SMB2_O_INFO_SECURITY: |
| 5226 | ksmbd_debug(SMB, "GOT SMB2_O_INFO_SECURITY\n"); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5227 | rc = smb2_get_info_sec(work, req, rsp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5228 | break; |
| 5229 | default: |
| 5230 | ksmbd_debug(SMB, "InfoType %d not supported yet\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5231 | req->InfoType); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5232 | rc = -EOPNOTSUPP; |
| 5233 | } |
| 5234 | |
| 5235 | if (rc < 0) { |
| 5236 | if (rc == -EACCES) |
| 5237 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 5238 | else if (rc == -ENOENT) |
| 5239 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 5240 | else if (rc == -EIO) |
| 5241 | rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR; |
| 5242 | else if (rc == -EOPNOTSUPP || rsp->hdr.Status == 0) |
| 5243 | rsp->hdr.Status = STATUS_INVALID_INFO_CLASS; |
| 5244 | smb2_set_err_rsp(work); |
| 5245 | |
| 5246 | ksmbd_debug(SMB, "error while processing smb2 query rc = %d\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5247 | rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5248 | return rc; |
| 5249 | } |
| 5250 | rsp->StructureSize = cpu_to_le16(9); |
| 5251 | rsp->OutputBufferOffset = cpu_to_le16(72); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5252 | inc_rfc1001_len(work->response_buf, 8); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5253 | return 0; |
| 5254 | } |
| 5255 | |
| 5256 | /** |
| 5257 | * smb2_close_pipe() - handler for closing IPC pipe |
| 5258 | * @work: smb work containing close request buffer |
| 5259 | * |
| 5260 | * Return: 0 |
| 5261 | */ |
| 5262 | static noinline int smb2_close_pipe(struct ksmbd_work *work) |
| 5263 | { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5264 | u64 id; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5265 | struct smb2_close_req *req = smb2_get_msg(work->request_buf); |
| 5266 | struct smb2_close_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5267 | |
Paulo Alcantara | 2d004c6 | 2022-03-21 13:08:26 -0300 | [diff] [blame] | 5268 | id = req->VolatileFileId; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5269 | ksmbd_session_rpc_close(work->sess, id); |
| 5270 | |
| 5271 | rsp->StructureSize = cpu_to_le16(60); |
| 5272 | rsp->Flags = 0; |
| 5273 | rsp->Reserved = 0; |
| 5274 | rsp->CreationTime = 0; |
| 5275 | rsp->LastAccessTime = 0; |
| 5276 | rsp->LastWriteTime = 0; |
| 5277 | rsp->ChangeTime = 0; |
| 5278 | rsp->AllocationSize = 0; |
| 5279 | rsp->EndOfFile = 0; |
| 5280 | rsp->Attributes = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5281 | inc_rfc1001_len(work->response_buf, 60); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5282 | return 0; |
| 5283 | } |
| 5284 | |
| 5285 | /** |
| 5286 | * smb2_close() - handler for smb2 close file command |
| 5287 | * @work: smb work containing close request buffer |
| 5288 | * |
| 5289 | * Return: 0 |
| 5290 | */ |
| 5291 | int smb2_close(struct ksmbd_work *work) |
| 5292 | { |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 5293 | u64 volatile_id = KSMBD_NO_FID; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5294 | u64 sess_id; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5295 | struct smb2_close_req *req; |
| 5296 | struct smb2_close_rsp *rsp; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5297 | struct ksmbd_conn *conn = work->conn; |
| 5298 | struct ksmbd_file *fp; |
| 5299 | struct inode *inode; |
| 5300 | u64 time; |
| 5301 | int err = 0; |
| 5302 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5303 | WORK_BUFFERS(work, req, rsp); |
| 5304 | |
| 5305 | if (test_share_config_flag(work->tcon->share_conf, |
| 5306 | KSMBD_SHARE_FLAG_PIPE)) { |
| 5307 | ksmbd_debug(SMB, "IPC pipe close request\n"); |
| 5308 | return smb2_close_pipe(work); |
| 5309 | } |
| 5310 | |
| 5311 | sess_id = le64_to_cpu(req->hdr.SessionId); |
| 5312 | if (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS) |
| 5313 | sess_id = work->compound_sid; |
| 5314 | |
| 5315 | work->compound_sid = 0; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5316 | if (check_session_id(conn, sess_id)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5317 | work->compound_sid = sess_id; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5318 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5319 | rsp->hdr.Status = STATUS_USER_SESSION_DELETED; |
| 5320 | if (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS) |
| 5321 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 5322 | err = -EBADF; |
| 5323 | goto out; |
| 5324 | } |
| 5325 | |
| 5326 | if (work->next_smb2_rcv_hdr_off && |
Paulo Alcantara | 2d004c6 | 2022-03-21 13:08:26 -0300 | [diff] [blame] | 5327 | !has_file_id(req->VolatileFileId)) { |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 5328 | if (!has_file_id(work->compound_fid)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5329 | /* file already closed, return FILE_CLOSED */ |
| 5330 | ksmbd_debug(SMB, "file already closed\n"); |
| 5331 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 5332 | err = -EBADF; |
| 5333 | goto out; |
| 5334 | } else { |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 5335 | ksmbd_debug(SMB, |
| 5336 | "Compound request set FID = %llu:%llu\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5337 | work->compound_fid, |
| 5338 | work->compound_pfid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5339 | volatile_id = work->compound_fid; |
| 5340 | |
| 5341 | /* file closed, stored id is not valid anymore */ |
| 5342 | work->compound_fid = KSMBD_NO_FID; |
| 5343 | work->compound_pfid = KSMBD_NO_FID; |
| 5344 | } |
| 5345 | } else { |
Paulo Alcantara | 2d004c6 | 2022-03-21 13:08:26 -0300 | [diff] [blame] | 5346 | volatile_id = req->VolatileFileId; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5347 | } |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 5348 | ksmbd_debug(SMB, "volatile_id = %llu\n", volatile_id); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5349 | |
| 5350 | rsp->StructureSize = cpu_to_le16(60); |
| 5351 | rsp->Reserved = 0; |
| 5352 | |
| 5353 | if (req->Flags == SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB) { |
| 5354 | fp = ksmbd_lookup_fd_fast(work, volatile_id); |
| 5355 | if (!fp) { |
| 5356 | err = -ENOENT; |
| 5357 | goto out; |
| 5358 | } |
| 5359 | |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 5360 | inode = file_inode(fp->filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5361 | rsp->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB; |
| 5362 | rsp->AllocationSize = S_ISDIR(inode->i_mode) ? 0 : |
| 5363 | cpu_to_le64(inode->i_blocks << 9); |
| 5364 | rsp->EndOfFile = cpu_to_le64(inode->i_size); |
| 5365 | rsp->Attributes = fp->f_ci->m_fattr; |
| 5366 | rsp->CreationTime = cpu_to_le64(fp->create_time); |
| 5367 | time = ksmbd_UnixTimeToNT(inode->i_atime); |
| 5368 | rsp->LastAccessTime = cpu_to_le64(time); |
| 5369 | time = ksmbd_UnixTimeToNT(inode->i_mtime); |
| 5370 | rsp->LastWriteTime = cpu_to_le64(time); |
| 5371 | time = ksmbd_UnixTimeToNT(inode->i_ctime); |
| 5372 | rsp->ChangeTime = cpu_to_le64(time); |
| 5373 | ksmbd_fd_put(work, fp); |
| 5374 | } else { |
| 5375 | rsp->Flags = 0; |
| 5376 | rsp->AllocationSize = 0; |
| 5377 | rsp->EndOfFile = 0; |
| 5378 | rsp->Attributes = 0; |
| 5379 | rsp->CreationTime = 0; |
| 5380 | rsp->LastAccessTime = 0; |
| 5381 | rsp->LastWriteTime = 0; |
| 5382 | rsp->ChangeTime = 0; |
| 5383 | } |
| 5384 | |
| 5385 | err = ksmbd_close_fd(work, volatile_id); |
| 5386 | out: |
| 5387 | if (err) { |
| 5388 | if (rsp->hdr.Status == 0) |
| 5389 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 5390 | smb2_set_err_rsp(work); |
| 5391 | } else { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5392 | inc_rfc1001_len(work->response_buf, 60); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5393 | } |
| 5394 | |
| 5395 | return 0; |
| 5396 | } |
| 5397 | |
| 5398 | /** |
| 5399 | * smb2_echo() - handler for smb2 echo(ping) command |
| 5400 | * @work: smb work containing echo request buffer |
| 5401 | * |
| 5402 | * Return: 0 |
| 5403 | */ |
| 5404 | int smb2_echo(struct ksmbd_work *work) |
| 5405 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5406 | struct smb2_echo_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5407 | |
| 5408 | rsp->StructureSize = cpu_to_le16(4); |
| 5409 | rsp->Reserved = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5410 | inc_rfc1001_len(work->response_buf, 4); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5411 | return 0; |
| 5412 | } |
| 5413 | |
Christian Brauner | da1e7ad | 2021-08-23 17:13:47 +0200 | [diff] [blame] | 5414 | static int smb2_rename(struct ksmbd_work *work, |
| 5415 | struct ksmbd_file *fp, |
| 5416 | struct user_namespace *user_ns, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5417 | struct smb2_file_rename_info *file_info, |
| 5418 | struct nls_table *local_nls) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5419 | { |
| 5420 | struct ksmbd_share_config *share = fp->tcon->share_conf; |
| 5421 | char *new_name = NULL, *abs_oldname = NULL, *old_name = NULL; |
| 5422 | char *pathname = NULL; |
| 5423 | struct path path; |
| 5424 | bool file_present = true; |
| 5425 | int rc; |
| 5426 | |
| 5427 | ksmbd_debug(SMB, "setting FILE_RENAME_INFO\n"); |
| 5428 | pathname = kmalloc(PATH_MAX, GFP_KERNEL); |
| 5429 | if (!pathname) |
| 5430 | return -ENOMEM; |
| 5431 | |
Al Viro | 2f5930c | 2022-01-30 19:43:00 -0500 | [diff] [blame] | 5432 | abs_oldname = file_path(fp->filp, pathname, PATH_MAX); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5433 | if (IS_ERR(abs_oldname)) { |
| 5434 | rc = -EINVAL; |
| 5435 | goto out; |
| 5436 | } |
| 5437 | old_name = strrchr(abs_oldname, '/'); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5438 | if (old_name && old_name[1] != '\0') { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5439 | old_name++; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5440 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5441 | ksmbd_debug(SMB, "can't get last component in path %s\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5442 | abs_oldname); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5443 | rc = -ENOENT; |
| 5444 | goto out; |
| 5445 | } |
| 5446 | |
Marios Makassikis | 80917f1 | 2021-12-01 21:41:19 +0100 | [diff] [blame] | 5447 | new_name = smb2_get_name(file_info->FileName, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5448 | le32_to_cpu(file_info->FileNameLength), |
| 5449 | local_nls); |
| 5450 | if (IS_ERR(new_name)) { |
| 5451 | rc = PTR_ERR(new_name); |
| 5452 | goto out; |
| 5453 | } |
| 5454 | |
| 5455 | if (strchr(new_name, ':')) { |
| 5456 | int s_type; |
| 5457 | char *xattr_stream_name, *stream_name = NULL; |
| 5458 | size_t xattr_stream_size; |
| 5459 | int len; |
| 5460 | |
| 5461 | rc = parse_stream_name(new_name, &stream_name, &s_type); |
| 5462 | if (rc < 0) |
| 5463 | goto out; |
| 5464 | |
| 5465 | len = strlen(new_name); |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 5466 | if (len > 0 && new_name[len - 1] != '/') { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5467 | pr_err("not allow base filename in rename\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5468 | rc = -ESHARE; |
| 5469 | goto out; |
| 5470 | } |
| 5471 | |
| 5472 | rc = ksmbd_vfs_xattr_stream_name(stream_name, |
| 5473 | &xattr_stream_name, |
| 5474 | &xattr_stream_size, |
| 5475 | s_type); |
| 5476 | if (rc) |
| 5477 | goto out; |
| 5478 | |
Christian Brauner | da1e7ad | 2021-08-23 17:13:47 +0200 | [diff] [blame] | 5479 | rc = ksmbd_vfs_setxattr(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 5480 | fp->filp->f_path.dentry, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5481 | xattr_stream_name, |
| 5482 | NULL, 0, 0); |
| 5483 | if (rc < 0) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5484 | pr_err("failed to store stream name in xattr: %d\n", |
| 5485 | rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5486 | rc = -EINVAL; |
| 5487 | goto out; |
| 5488 | } |
| 5489 | |
| 5490 | goto out; |
| 5491 | } |
| 5492 | |
| 5493 | ksmbd_debug(SMB, "new name %s\n", new_name); |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 5494 | rc = ksmbd_vfs_kern_path(work, new_name, LOOKUP_NO_SYMLINKS, &path, 1); |
| 5495 | if (rc) { |
| 5496 | if (rc != -ENOENT) |
| 5497 | goto out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5498 | file_present = false; |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 5499 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5500 | path_put(&path); |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 5501 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5502 | |
| 5503 | if (ksmbd_share_veto_filename(share, new_name)) { |
| 5504 | rc = -ENOENT; |
| 5505 | ksmbd_debug(SMB, "Can't rename vetoed file: %s\n", new_name); |
| 5506 | goto out; |
| 5507 | } |
| 5508 | |
| 5509 | if (file_info->ReplaceIfExists) { |
| 5510 | if (file_present) { |
| 5511 | rc = ksmbd_vfs_remove_file(work, new_name); |
| 5512 | if (rc) { |
| 5513 | if (rc != -ENOTEMPTY) |
| 5514 | rc = -EINVAL; |
| 5515 | ksmbd_debug(SMB, "cannot delete %s, rc %d\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5516 | new_name, rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5517 | goto out; |
| 5518 | } |
| 5519 | } |
| 5520 | } else { |
| 5521 | if (file_present && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5522 | strncmp(old_name, path.dentry->d_name.name, strlen(old_name))) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5523 | rc = -EEXIST; |
| 5524 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5525 | "cannot rename already existing file\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5526 | goto out; |
| 5527 | } |
| 5528 | } |
| 5529 | |
| 5530 | rc = ksmbd_vfs_fp_rename(work, fp, new_name); |
| 5531 | out: |
| 5532 | kfree(pathname); |
| 5533 | if (!IS_ERR(new_name)) |
Marios Makassikis | 915f570 | 2021-04-13 13:25:57 +0900 | [diff] [blame] | 5534 | kfree(new_name); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5535 | return rc; |
| 5536 | } |
| 5537 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5538 | static int smb2_create_link(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5539 | struct ksmbd_share_config *share, |
| 5540 | struct smb2_file_link_info *file_info, |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5541 | unsigned int buf_len, struct file *filp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5542 | struct nls_table *local_nls) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5543 | { |
| 5544 | char *link_name = NULL, *target_name = NULL, *pathname = NULL; |
| 5545 | struct path path; |
| 5546 | bool file_present = true; |
| 5547 | int rc; |
| 5548 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5549 | if (buf_len < (u64)sizeof(struct smb2_file_link_info) + |
| 5550 | le32_to_cpu(file_info->FileNameLength)) |
| 5551 | return -EINVAL; |
| 5552 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5553 | ksmbd_debug(SMB, "setting FILE_LINK_INFORMATION\n"); |
| 5554 | pathname = kmalloc(PATH_MAX, GFP_KERNEL); |
| 5555 | if (!pathname) |
| 5556 | return -ENOMEM; |
| 5557 | |
Marios Makassikis | 80917f1 | 2021-12-01 21:41:19 +0100 | [diff] [blame] | 5558 | link_name = smb2_get_name(file_info->FileName, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5559 | le32_to_cpu(file_info->FileNameLength), |
| 5560 | local_nls); |
| 5561 | if (IS_ERR(link_name) || S_ISDIR(file_inode(filp)->i_mode)) { |
| 5562 | rc = -EINVAL; |
| 5563 | goto out; |
| 5564 | } |
| 5565 | |
| 5566 | ksmbd_debug(SMB, "link name is %s\n", link_name); |
Al Viro | 2f5930c | 2022-01-30 19:43:00 -0500 | [diff] [blame] | 5567 | target_name = file_path(filp, pathname, PATH_MAX); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5568 | if (IS_ERR(target_name)) { |
| 5569 | rc = -EINVAL; |
| 5570 | goto out; |
| 5571 | } |
| 5572 | |
| 5573 | ksmbd_debug(SMB, "target name is %s\n", target_name); |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 5574 | rc = ksmbd_vfs_kern_path(work, link_name, LOOKUP_NO_SYMLINKS, &path, 0); |
| 5575 | if (rc) { |
| 5576 | if (rc != -ENOENT) |
| 5577 | goto out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5578 | file_present = false; |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 5579 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5580 | path_put(&path); |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 5581 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5582 | |
| 5583 | if (file_info->ReplaceIfExists) { |
| 5584 | if (file_present) { |
| 5585 | rc = ksmbd_vfs_remove_file(work, link_name); |
| 5586 | if (rc) { |
| 5587 | rc = -EINVAL; |
| 5588 | ksmbd_debug(SMB, "cannot delete %s\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5589 | link_name); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5590 | goto out; |
| 5591 | } |
| 5592 | } |
| 5593 | } else { |
| 5594 | if (file_present) { |
| 5595 | rc = -EEXIST; |
| 5596 | ksmbd_debug(SMB, "link already exists\n"); |
| 5597 | goto out; |
| 5598 | } |
| 5599 | } |
| 5600 | |
| 5601 | rc = ksmbd_vfs_link(work, target_name, link_name); |
| 5602 | if (rc) |
| 5603 | rc = -EINVAL; |
| 5604 | out: |
| 5605 | if (!IS_ERR(link_name)) |
Marios Makassikis | 915f570 | 2021-04-13 13:25:57 +0900 | [diff] [blame] | 5606 | kfree(link_name); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5607 | kfree(pathname); |
| 5608 | return rc; |
| 5609 | } |
| 5610 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5611 | static int set_file_basic_info(struct ksmbd_file *fp, |
| 5612 | struct smb2_file_basic_info *file_info, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5613 | struct ksmbd_share_config *share) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5614 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5615 | struct iattr attrs; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5616 | struct file *filp; |
| 5617 | struct inode *inode; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 5618 | struct user_namespace *user_ns; |
Namjae Jeon | 4ffd526 | 2021-09-07 08:15:21 +0900 | [diff] [blame] | 5619 | int rc = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5620 | |
Marios Makassikis | 7adfd4f | 2021-04-27 15:30:22 +0900 | [diff] [blame] | 5621 | if (!(fp->daccess & FILE_WRITE_ATTRIBUTES_LE)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5622 | return -EACCES; |
| 5623 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5624 | attrs.ia_valid = 0; |
| 5625 | filp = fp->filp; |
| 5626 | inode = file_inode(filp); |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 5627 | user_ns = file_mnt_user_ns(filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5628 | |
| 5629 | if (file_info->CreationTime) |
| 5630 | fp->create_time = le64_to_cpu(file_info->CreationTime); |
| 5631 | |
| 5632 | if (file_info->LastAccessTime) { |
| 5633 | attrs.ia_atime = ksmbd_NTtimeToUnix(file_info->LastAccessTime); |
| 5634 | attrs.ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET); |
| 5635 | } |
| 5636 | |
Namjae Jeon | 64e7875 | 2021-10-03 13:19:00 +0900 | [diff] [blame] | 5637 | attrs.ia_valid |= ATTR_CTIME; |
| 5638 | if (file_info->ChangeTime) |
Christian Brauner | db7fb6f | 2021-08-26 10:07:05 +0900 | [diff] [blame] | 5639 | attrs.ia_ctime = ksmbd_NTtimeToUnix(file_info->ChangeTime); |
Namjae Jeon | 64e7875 | 2021-10-03 13:19:00 +0900 | [diff] [blame] | 5640 | else |
| 5641 | attrs.ia_ctime = inode->i_ctime; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5642 | |
| 5643 | if (file_info->LastWriteTime) { |
| 5644 | attrs.ia_mtime = ksmbd_NTtimeToUnix(file_info->LastWriteTime); |
| 5645 | attrs.ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET); |
| 5646 | } |
| 5647 | |
| 5648 | if (file_info->Attributes) { |
| 5649 | if (!S_ISDIR(inode->i_mode) && |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 5650 | file_info->Attributes & FILE_ATTRIBUTE_DIRECTORY_LE) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5651 | pr_err("can't change a file to a directory\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5652 | return -EINVAL; |
| 5653 | } |
| 5654 | |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 5655 | if (!(S_ISDIR(inode->i_mode) && file_info->Attributes == FILE_ATTRIBUTE_NORMAL_LE)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5656 | fp->f_ci->m_fattr = file_info->Attributes | |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 5657 | (fp->f_ci->m_fattr & FILE_ATTRIBUTE_DIRECTORY_LE); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5658 | } |
| 5659 | |
| 5660 | if (test_share_config_flag(share, KSMBD_SHARE_FLAG_STORE_DOS_ATTRS) && |
| 5661 | (file_info->CreationTime || file_info->Attributes)) { |
| 5662 | struct xattr_dos_attrib da = {0}; |
| 5663 | |
| 5664 | da.version = 4; |
| 5665 | da.itime = fp->itime; |
| 5666 | da.create_time = fp->create_time; |
| 5667 | da.attr = le32_to_cpu(fp->f_ci->m_fattr); |
| 5668 | da.flags = XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME | |
| 5669 | XATTR_DOSINFO_ITIME; |
| 5670 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 5671 | rc = ksmbd_vfs_set_dos_attrib_xattr(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 5672 | filp->f_path.dentry, &da); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5673 | if (rc) |
| 5674 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5675 | "failed to restore file attribute in EA\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5676 | rc = 0; |
| 5677 | } |
| 5678 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5679 | if (attrs.ia_valid) { |
| 5680 | struct dentry *dentry = filp->f_path.dentry; |
| 5681 | struct inode *inode = d_inode(dentry); |
| 5682 | |
| 5683 | if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) |
| 5684 | return -EACCES; |
| 5685 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5686 | inode_lock(inode); |
Namjae Jeon | 64e7875 | 2021-10-03 13:19:00 +0900 | [diff] [blame] | 5687 | inode->i_ctime = attrs.ia_ctime; |
| 5688 | attrs.ia_valid &= ~ATTR_CTIME; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 5689 | rc = notify_change(user_ns, dentry, &attrs, NULL); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5690 | inode_unlock(inode); |
| 5691 | } |
Christian Brauner | eb5784f | 2021-08-23 17:13:55 +0200 | [diff] [blame] | 5692 | return rc; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5693 | } |
| 5694 | |
| 5695 | static int set_file_allocation_info(struct ksmbd_work *work, |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5696 | struct ksmbd_file *fp, |
| 5697 | struct smb2_file_alloc_info *file_alloc_info) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5698 | { |
| 5699 | /* |
| 5700 | * TODO : It's working fine only when store dos attributes |
| 5701 | * is not yes. need to implement a logic which works |
| 5702 | * properly with any smb.conf option |
| 5703 | */ |
| 5704 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5705 | loff_t alloc_blks; |
| 5706 | struct inode *inode; |
| 5707 | int rc; |
| 5708 | |
Marios Makassikis | a299669 | 2021-04-27 15:29:01 +0900 | [diff] [blame] | 5709 | if (!(fp->daccess & FILE_WRITE_DATA_LE)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5710 | return -EACCES; |
| 5711 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5712 | alloc_blks = (le64_to_cpu(file_alloc_info->AllocationSize) + 511) >> 9; |
| 5713 | inode = file_inode(fp->filp); |
| 5714 | |
| 5715 | if (alloc_blks > inode->i_blocks) { |
Namjae Jeon | e8c0619 | 2021-06-22 11:06:11 +0900 | [diff] [blame] | 5716 | smb_break_all_levII_oplock(work, fp, 1); |
| 5717 | rc = vfs_fallocate(fp->filp, FALLOC_FL_KEEP_SIZE, 0, |
| 5718 | alloc_blks * 512); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5719 | if (rc && rc != -EOPNOTSUPP) { |
Namjae Jeon | e8c0619 | 2021-06-22 11:06:11 +0900 | [diff] [blame] | 5720 | pr_err("vfs_fallocate is failed : %d\n", rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5721 | return rc; |
| 5722 | } |
| 5723 | } else if (alloc_blks < inode->i_blocks) { |
| 5724 | loff_t size; |
| 5725 | |
| 5726 | /* |
| 5727 | * Allocation size could be smaller than original one |
| 5728 | * which means allocated blocks in file should be |
| 5729 | * deallocated. use truncate to cut out it, but inode |
| 5730 | * size is also updated with truncate offset. |
| 5731 | * inode size is retained by backup inode size. |
| 5732 | */ |
| 5733 | size = i_size_read(inode); |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 5734 | rc = ksmbd_vfs_truncate(work, fp, alloc_blks * 512); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5735 | if (rc) { |
Namjae Jeon | 50f500b | 2022-02-24 11:03:41 +0900 | [diff] [blame] | 5736 | pr_err("truncate failed!, err %d\n", rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5737 | return rc; |
| 5738 | } |
| 5739 | if (size < alloc_blks * 512) |
| 5740 | i_size_write(inode, size); |
| 5741 | } |
| 5742 | return 0; |
| 5743 | } |
| 5744 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5745 | static int set_end_of_file_info(struct ksmbd_work *work, struct ksmbd_file *fp, |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5746 | struct smb2_file_eof_info *file_eof_info) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5747 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5748 | loff_t newsize; |
| 5749 | struct inode *inode; |
| 5750 | int rc; |
| 5751 | |
Marios Makassikis | a299669 | 2021-04-27 15:29:01 +0900 | [diff] [blame] | 5752 | if (!(fp->daccess & FILE_WRITE_DATA_LE)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5753 | return -EACCES; |
| 5754 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5755 | newsize = le64_to_cpu(file_eof_info->EndOfFile); |
| 5756 | inode = file_inode(fp->filp); |
| 5757 | |
| 5758 | /* |
| 5759 | * If FILE_END_OF_FILE_INFORMATION of set_info_file is called |
| 5760 | * on FAT32 shared device, truncate execution time is too long |
| 5761 | * and network error could cause from windows client. because |
| 5762 | * truncate of some filesystem like FAT32 fill zero data in |
| 5763 | * truncated range. |
| 5764 | */ |
| 5765 | if (inode->i_sb->s_magic != MSDOS_SUPER_MAGIC) { |
Namjae Jeon | 50f500b | 2022-02-24 11:03:41 +0900 | [diff] [blame] | 5766 | ksmbd_debug(SMB, "truncated to newsize %lld\n", newsize); |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 5767 | rc = ksmbd_vfs_truncate(work, fp, newsize); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5768 | if (rc) { |
Namjae Jeon | 50f500b | 2022-02-24 11:03:41 +0900 | [diff] [blame] | 5769 | ksmbd_debug(SMB, "truncate failed!, err %d\n", rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5770 | if (rc != -EAGAIN) |
| 5771 | rc = -EBADF; |
| 5772 | return rc; |
| 5773 | } |
| 5774 | } |
| 5775 | return 0; |
| 5776 | } |
| 5777 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5778 | static int set_rename_info(struct ksmbd_work *work, struct ksmbd_file *fp, |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5779 | struct smb2_file_rename_info *rename_info, |
| 5780 | unsigned int buf_len) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5781 | { |
Christian Brauner | da1e7ad | 2021-08-23 17:13:47 +0200 | [diff] [blame] | 5782 | struct user_namespace *user_ns; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5783 | struct ksmbd_file *parent_fp; |
Namjae Jeon | 12202c0 | 2021-06-29 09:23:56 +0900 | [diff] [blame] | 5784 | struct dentry *parent; |
| 5785 | struct dentry *dentry = fp->filp->f_path.dentry; |
| 5786 | int ret; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5787 | |
| 5788 | if (!(fp->daccess & FILE_DELETE_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5789 | pr_err("no right to delete : 0x%x\n", fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5790 | return -EACCES; |
| 5791 | } |
| 5792 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5793 | if (buf_len < (u64)sizeof(struct smb2_file_rename_info) + |
| 5794 | le32_to_cpu(rename_info->FileNameLength)) |
| 5795 | return -EINVAL; |
| 5796 | |
Christian Brauner | da1e7ad | 2021-08-23 17:13:47 +0200 | [diff] [blame] | 5797 | user_ns = file_mnt_user_ns(fp->filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5798 | if (ksmbd_stream_fd(fp)) |
| 5799 | goto next; |
| 5800 | |
Namjae Jeon | 12202c0 | 2021-06-29 09:23:56 +0900 | [diff] [blame] | 5801 | parent = dget_parent(dentry); |
Christian Brauner | da1e7ad | 2021-08-23 17:13:47 +0200 | [diff] [blame] | 5802 | ret = ksmbd_vfs_lock_parent(user_ns, parent, dentry); |
Namjae Jeon | 12202c0 | 2021-06-29 09:23:56 +0900 | [diff] [blame] | 5803 | if (ret) { |
| 5804 | dput(parent); |
| 5805 | return ret; |
| 5806 | } |
| 5807 | |
| 5808 | parent_fp = ksmbd_lookup_fd_inode(d_inode(parent)); |
| 5809 | inode_unlock(d_inode(parent)); |
| 5810 | dput(parent); |
| 5811 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5812 | if (parent_fp) { |
| 5813 | if (parent_fp->daccess & FILE_DELETE_LE) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5814 | pr_err("parent dir is opened with delete access\n"); |
Namjae Jeon | 8510a04 | 2022-04-05 12:04:43 +0900 | [diff] [blame] | 5815 | ksmbd_fd_put(work, parent_fp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5816 | return -ESHARE; |
| 5817 | } |
Namjae Jeon | 8510a04 | 2022-04-05 12:04:43 +0900 | [diff] [blame] | 5818 | ksmbd_fd_put(work, parent_fp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5819 | } |
| 5820 | next: |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5821 | return smb2_rename(work, fp, user_ns, rename_info, |
Namjae Jeon | af7c39d | 2022-07-25 13:36:52 +0900 | [diff] [blame] | 5822 | work->conn->local_nls); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5823 | } |
| 5824 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5825 | static int set_file_disposition_info(struct ksmbd_file *fp, |
| 5826 | struct smb2_file_disposition_info *file_info) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5827 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5828 | struct inode *inode; |
| 5829 | |
| 5830 | if (!(fp->daccess & FILE_DELETE_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5831 | pr_err("no right to delete : 0x%x\n", fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5832 | return -EACCES; |
| 5833 | } |
| 5834 | |
| 5835 | inode = file_inode(fp->filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5836 | if (file_info->DeletePending) { |
| 5837 | if (S_ISDIR(inode->i_mode) && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5838 | ksmbd_vfs_empty_dir(fp) == -ENOTEMPTY) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5839 | return -EBUSY; |
| 5840 | ksmbd_set_inode_pending_delete(fp); |
| 5841 | } else { |
| 5842 | ksmbd_clear_inode_pending_delete(fp); |
| 5843 | } |
| 5844 | return 0; |
| 5845 | } |
| 5846 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5847 | static int set_file_position_info(struct ksmbd_file *fp, |
| 5848 | struct smb2_file_pos_info *file_info) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5849 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5850 | loff_t current_byte_offset; |
Namjae Jeon | ee81cae | 2021-06-26 22:32:34 +0900 | [diff] [blame] | 5851 | unsigned long sector_size; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5852 | struct inode *inode; |
| 5853 | |
| 5854 | inode = file_inode(fp->filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5855 | current_byte_offset = le64_to_cpu(file_info->CurrentByteOffset); |
Namjae Jeon | ee81cae | 2021-06-26 22:32:34 +0900 | [diff] [blame] | 5856 | sector_size = inode->i_sb->s_blocksize; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5857 | |
| 5858 | if (current_byte_offset < 0 || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5859 | (fp->coption == FILE_NO_INTERMEDIATE_BUFFERING_LE && |
| 5860 | current_byte_offset & (sector_size - 1))) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5861 | pr_err("CurrentByteOffset is not valid : %llu\n", |
| 5862 | current_byte_offset); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5863 | return -EINVAL; |
| 5864 | } |
| 5865 | |
| 5866 | fp->filp->f_pos = current_byte_offset; |
| 5867 | return 0; |
| 5868 | } |
| 5869 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5870 | static int set_file_mode_info(struct ksmbd_file *fp, |
| 5871 | struct smb2_file_mode_info *file_info) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5872 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5873 | __le32 mode; |
| 5874 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5875 | mode = file_info->Mode; |
| 5876 | |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 5877 | if ((mode & ~FILE_MODE_INFO_MASK)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5878 | pr_err("Mode is not valid : 0x%x\n", le32_to_cpu(mode)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5879 | return -EINVAL; |
| 5880 | } |
| 5881 | |
| 5882 | /* |
| 5883 | * TODO : need to implement consideration for |
| 5884 | * FILE_SYNCHRONOUS_IO_ALERT and FILE_SYNCHRONOUS_IO_NONALERT |
| 5885 | */ |
| 5886 | ksmbd_vfs_set_fadvise(fp->filp, mode); |
| 5887 | fp->coption = mode; |
| 5888 | return 0; |
| 5889 | } |
| 5890 | |
| 5891 | /** |
| 5892 | * smb2_set_info_file() - handler for smb2 set info command |
| 5893 | * @work: smb work containing set info command buffer |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 5894 | * @fp: ksmbd_file pointer |
Yang Li | 4bfd9ee | 2021-12-21 17:07:12 +0800 | [diff] [blame] | 5895 | * @req: request buffer pointer |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 5896 | * @share: ksmbd_share_config pointer |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5897 | * |
| 5898 | * Return: 0 on success, otherwise error |
| 5899 | * TODO: need to implement an error handling for STATUS_INFO_LENGTH_MISMATCH |
| 5900 | */ |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5901 | static int smb2_set_info_file(struct ksmbd_work *work, struct ksmbd_file *fp, |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5902 | struct smb2_set_info_req *req, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5903 | struct ksmbd_share_config *share) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5904 | { |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5905 | unsigned int buf_len = le32_to_cpu(req->BufferLength); |
| 5906 | |
| 5907 | switch (req->FileInfoClass) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5908 | case FILE_BASIC_INFORMATION: |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5909 | { |
| 5910 | if (buf_len < sizeof(struct smb2_file_basic_info)) |
| 5911 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5912 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5913 | return set_file_basic_info(fp, (struct smb2_file_basic_info *)req->Buffer, share); |
| 5914 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5915 | case FILE_ALLOCATION_INFORMATION: |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5916 | { |
| 5917 | if (buf_len < sizeof(struct smb2_file_alloc_info)) |
| 5918 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5919 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5920 | return set_file_allocation_info(work, fp, |
| 5921 | (struct smb2_file_alloc_info *)req->Buffer); |
| 5922 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5923 | case FILE_END_OF_FILE_INFORMATION: |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5924 | { |
| 5925 | if (buf_len < sizeof(struct smb2_file_eof_info)) |
| 5926 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5927 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5928 | return set_end_of_file_info(work, fp, |
| 5929 | (struct smb2_file_eof_info *)req->Buffer); |
| 5930 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5931 | case FILE_RENAME_INFORMATION: |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5932 | { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5933 | if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5934 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5935 | "User does not have write permission\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5936 | return -EACCES; |
| 5937 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5938 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5939 | if (buf_len < sizeof(struct smb2_file_rename_info)) |
| 5940 | return -EINVAL; |
| 5941 | |
| 5942 | return set_rename_info(work, fp, |
| 5943 | (struct smb2_file_rename_info *)req->Buffer, |
| 5944 | buf_len); |
| 5945 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5946 | case FILE_LINK_INFORMATION: |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5947 | { |
| 5948 | if (buf_len < sizeof(struct smb2_file_link_info)) |
| 5949 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5950 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5951 | return smb2_create_link(work, work->tcon->share_conf, |
| 5952 | (struct smb2_file_link_info *)req->Buffer, |
| 5953 | buf_len, fp->filp, |
Namjae Jeon | af7c39d | 2022-07-25 13:36:52 +0900 | [diff] [blame] | 5954 | work->conn->local_nls); |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5955 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5956 | case FILE_DISPOSITION_INFORMATION: |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5957 | { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5958 | if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5959 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5960 | "User does not have write permission\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5961 | return -EACCES; |
| 5962 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5963 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5964 | if (buf_len < sizeof(struct smb2_file_disposition_info)) |
| 5965 | return -EINVAL; |
| 5966 | |
| 5967 | return set_file_disposition_info(fp, |
| 5968 | (struct smb2_file_disposition_info *)req->Buffer); |
| 5969 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5970 | case FILE_FULL_EA_INFORMATION: |
| 5971 | { |
| 5972 | if (!(fp->daccess & FILE_WRITE_EA_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5973 | pr_err("Not permitted to write ext attr: 0x%x\n", |
| 5974 | fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5975 | return -EACCES; |
| 5976 | } |
| 5977 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5978 | if (buf_len < sizeof(struct smb2_ea_info)) |
| 5979 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5980 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5981 | return smb2_set_ea((struct smb2_ea_info *)req->Buffer, |
| 5982 | buf_len, &fp->filp->f_path); |
| 5983 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5984 | case FILE_POSITION_INFORMATION: |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5985 | { |
| 5986 | if (buf_len < sizeof(struct smb2_file_pos_info)) |
| 5987 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5988 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5989 | return set_file_position_info(fp, (struct smb2_file_pos_info *)req->Buffer); |
| 5990 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5991 | case FILE_MODE_INFORMATION: |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5992 | { |
| 5993 | if (buf_len < sizeof(struct smb2_file_mode_info)) |
| 5994 | return -EINVAL; |
| 5995 | |
| 5996 | return set_file_mode_info(fp, (struct smb2_file_mode_info *)req->Buffer); |
| 5997 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5998 | } |
| 5999 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 6000 | pr_err("Unimplemented Fileinfoclass :%d\n", req->FileInfoClass); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6001 | return -EOPNOTSUPP; |
| 6002 | } |
| 6003 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6004 | static int smb2_set_info_sec(struct ksmbd_file *fp, int addition_info, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6005 | char *buffer, int buf_len) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6006 | { |
| 6007 | struct smb_ntsd *pntsd = (struct smb_ntsd *)buffer; |
| 6008 | |
| 6009 | fp->saccess |= FILE_SHARE_DELETE_LE; |
| 6010 | |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 6011 | return set_info_sec(fp->conn, fp->tcon, &fp->filp->f_path, pntsd, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6012 | buf_len, false); |
| 6013 | } |
| 6014 | |
| 6015 | /** |
| 6016 | * smb2_set_info() - handler for smb2 set info command handler |
| 6017 | * @work: smb work containing set info request buffer |
| 6018 | * |
| 6019 | * Return: 0 on success, otherwise error |
| 6020 | */ |
| 6021 | int smb2_set_info(struct ksmbd_work *work) |
| 6022 | { |
| 6023 | struct smb2_set_info_req *req; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6024 | struct smb2_set_info_rsp *rsp; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6025 | struct ksmbd_file *fp; |
| 6026 | int rc = 0; |
| 6027 | unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID; |
| 6028 | |
| 6029 | ksmbd_debug(SMB, "Received set info request\n"); |
| 6030 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6031 | if (work->next_smb2_rcv_hdr_off) { |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 6032 | req = ksmbd_req_buf_next(work); |
| 6033 | rsp = ksmbd_resp_buf_next(work); |
Paulo Alcantara | 2d004c6 | 2022-03-21 13:08:26 -0300 | [diff] [blame] | 6034 | if (!has_file_id(req->VolatileFileId)) { |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 6035 | ksmbd_debug(SMB, "Compound request set FID = %llu\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6036 | work->compound_fid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6037 | id = work->compound_fid; |
| 6038 | pid = work->compound_pfid; |
| 6039 | } |
| 6040 | } else { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6041 | req = smb2_get_msg(work->request_buf); |
| 6042 | rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6043 | } |
| 6044 | |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 6045 | if (!has_file_id(id)) { |
Paulo Alcantara | 2d004c6 | 2022-03-21 13:08:26 -0300 | [diff] [blame] | 6046 | id = req->VolatileFileId; |
| 6047 | pid = req->PersistentFileId; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6048 | } |
| 6049 | |
| 6050 | fp = ksmbd_lookup_fd_slow(work, id, pid); |
| 6051 | if (!fp) { |
| 6052 | ksmbd_debug(SMB, "Invalid id for close: %u\n", id); |
| 6053 | rc = -ENOENT; |
| 6054 | goto err_out; |
| 6055 | } |
| 6056 | |
| 6057 | switch (req->InfoType) { |
| 6058 | case SMB2_O_INFO_FILE: |
| 6059 | ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILE\n"); |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 6060 | rc = smb2_set_info_file(work, fp, req, work->tcon->share_conf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6061 | break; |
| 6062 | case SMB2_O_INFO_SECURITY: |
| 6063 | ksmbd_debug(SMB, "GOT SMB2_O_INFO_SECURITY\n"); |
Namjae Jeon | e70e392 | 2021-08-21 23:26:01 +0900 | [diff] [blame] | 6064 | if (ksmbd_override_fsids(work)) { |
| 6065 | rc = -ENOMEM; |
| 6066 | goto err_out; |
| 6067 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6068 | rc = smb2_set_info_sec(fp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6069 | le32_to_cpu(req->AdditionalInformation), |
| 6070 | req->Buffer, |
| 6071 | le32_to_cpu(req->BufferLength)); |
Namjae Jeon | e70e392 | 2021-08-21 23:26:01 +0900 | [diff] [blame] | 6072 | ksmbd_revert_fsids(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6073 | break; |
| 6074 | default: |
| 6075 | rc = -EOPNOTSUPP; |
| 6076 | } |
| 6077 | |
| 6078 | if (rc < 0) |
| 6079 | goto err_out; |
| 6080 | |
| 6081 | rsp->StructureSize = cpu_to_le16(2); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6082 | inc_rfc1001_len(work->response_buf, 2); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6083 | ksmbd_fd_put(work, fp); |
| 6084 | return 0; |
| 6085 | |
| 6086 | err_out: |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 6087 | if (rc == -EACCES || rc == -EPERM || rc == -EXDEV) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6088 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 6089 | else if (rc == -EINVAL) |
| 6090 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 6091 | else if (rc == -ESHARE) |
| 6092 | rsp->hdr.Status = STATUS_SHARING_VIOLATION; |
| 6093 | else if (rc == -ENOENT) |
| 6094 | rsp->hdr.Status = STATUS_OBJECT_NAME_INVALID; |
| 6095 | else if (rc == -EBUSY || rc == -ENOTEMPTY) |
| 6096 | rsp->hdr.Status = STATUS_DIRECTORY_NOT_EMPTY; |
| 6097 | else if (rc == -EAGAIN) |
| 6098 | rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT; |
Namjae Jeon | ff1d572 | 2021-04-13 13:18:10 +0900 | [diff] [blame] | 6099 | else if (rc == -EBADF || rc == -ESTALE) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6100 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 6101 | else if (rc == -EEXIST) |
| 6102 | rsp->hdr.Status = STATUS_OBJECT_NAME_COLLISION; |
| 6103 | else if (rsp->hdr.Status == 0 || rc == -EOPNOTSUPP) |
| 6104 | rsp->hdr.Status = STATUS_INVALID_INFO_CLASS; |
| 6105 | smb2_set_err_rsp(work); |
| 6106 | ksmbd_fd_put(work, fp); |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6107 | ksmbd_debug(SMB, "error while processing smb2 query rc = %d\n", rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6108 | return rc; |
| 6109 | } |
| 6110 | |
| 6111 | /** |
| 6112 | * smb2_read_pipe() - handler for smb2 read from IPC pipe |
| 6113 | * @work: smb work containing read IPC pipe command buffer |
| 6114 | * |
| 6115 | * Return: 0 on success, otherwise error |
| 6116 | */ |
| 6117 | static noinline int smb2_read_pipe(struct ksmbd_work *work) |
| 6118 | { |
| 6119 | int nbytes = 0, err; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6120 | u64 id; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6121 | struct ksmbd_rpc_command *rpc_resp; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6122 | struct smb2_read_req *req = smb2_get_msg(work->request_buf); |
| 6123 | struct smb2_read_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6124 | |
Paulo Alcantara | 2d004c6 | 2022-03-21 13:08:26 -0300 | [diff] [blame] | 6125 | id = req->VolatileFileId; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6126 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6127 | inc_rfc1001_len(work->response_buf, 16); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6128 | rpc_resp = ksmbd_rpc_read(work->sess, id); |
| 6129 | if (rpc_resp) { |
| 6130 | if (rpc_resp->flags != KSMBD_RPC_OK) { |
| 6131 | err = -EINVAL; |
| 6132 | goto out; |
| 6133 | } |
| 6134 | |
| 6135 | work->aux_payload_buf = |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6136 | kvmalloc(rpc_resp->payload_sz, GFP_KERNEL | __GFP_ZERO); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6137 | if (!work->aux_payload_buf) { |
| 6138 | err = -ENOMEM; |
| 6139 | goto out; |
| 6140 | } |
| 6141 | |
| 6142 | memcpy(work->aux_payload_buf, rpc_resp->payload, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6143 | rpc_resp->payload_sz); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6144 | |
| 6145 | nbytes = rpc_resp->payload_sz; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6146 | work->resp_hdr_sz = get_rfc1002_len(work->response_buf) + 4; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6147 | work->aux_payload_sz = nbytes; |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6148 | kvfree(rpc_resp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6149 | } |
| 6150 | |
| 6151 | rsp->StructureSize = cpu_to_le16(17); |
| 6152 | rsp->DataOffset = 80; |
| 6153 | rsp->Reserved = 0; |
| 6154 | rsp->DataLength = cpu_to_le32(nbytes); |
| 6155 | rsp->DataRemaining = 0; |
Ronnie Sahlberg | 699230f | 2021-09-09 12:26:12 +0900 | [diff] [blame] | 6156 | rsp->Flags = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6157 | inc_rfc1001_len(work->response_buf, nbytes); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6158 | return 0; |
| 6159 | |
| 6160 | out: |
| 6161 | rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR; |
| 6162 | smb2_set_err_rsp(work); |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6163 | kvfree(rpc_resp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6164 | return err; |
| 6165 | } |
| 6166 | |
Hyunchul Lee | 2fd5dcb | 2022-01-13 09:51:39 +0900 | [diff] [blame] | 6167 | static int smb2_set_remote_key_for_rdma(struct ksmbd_work *work, |
| 6168 | struct smb2_buffer_desc_v1 *desc, |
| 6169 | __le32 Channel, |
Hyunchul Lee | 2fd5dcb | 2022-01-13 09:51:39 +0900 | [diff] [blame] | 6170 | __le16 ChannelInfoLength) |
| 6171 | { |
Hyunchul Lee | 6d896d3 | 2022-01-20 21:10:11 +0900 | [diff] [blame] | 6172 | unsigned int i, ch_count; |
| 6173 | |
Hyunchul Lee | 2fd5dcb | 2022-01-13 09:51:39 +0900 | [diff] [blame] | 6174 | if (work->conn->dialect == SMB30_PROT_ID && |
| 6175 | Channel != SMB2_CHANNEL_RDMA_V1) |
| 6176 | return -EINVAL; |
| 6177 | |
Hyunchul Lee | 6d896d3 | 2022-01-20 21:10:11 +0900 | [diff] [blame] | 6178 | ch_count = le16_to_cpu(ChannelInfoLength) / sizeof(*desc); |
| 6179 | if (ksmbd_debug_types & KSMBD_DEBUG_RDMA) { |
| 6180 | for (i = 0; i < ch_count; i++) { |
| 6181 | pr_info("RDMA r/w request %#x: token %#x, length %#x\n", |
| 6182 | i, |
| 6183 | le32_to_cpu(desc[i].token), |
| 6184 | le32_to_cpu(desc[i].length)); |
| 6185 | } |
| 6186 | } |
Hyunchul Lee | ee1b055 | 2022-04-30 08:30:29 +0900 | [diff] [blame] | 6187 | if (!ch_count) |
Hyunchul Lee | 2fd5dcb | 2022-01-13 09:51:39 +0900 | [diff] [blame] | 6188 | return -EINVAL; |
| 6189 | |
| 6190 | work->need_invalidate_rkey = |
| 6191 | (Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE); |
Hyunchul Lee | 1807abc | 2022-04-30 08:30:25 +0900 | [diff] [blame] | 6192 | if (Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE) |
| 6193 | work->remote_key = le32_to_cpu(desc->token); |
Hyunchul Lee | 2fd5dcb | 2022-01-13 09:51:39 +0900 | [diff] [blame] | 6194 | return 0; |
| 6195 | } |
| 6196 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6197 | static ssize_t smb2_read_rdma_channel(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6198 | struct smb2_read_req *req, void *data_buf, |
| 6199 | size_t length) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6200 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6201 | int err; |
| 6202 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6203 | err = ksmbd_conn_rdma_write(work->conn, data_buf, length, |
Hyunchul Lee | 1807abc | 2022-04-30 08:30:25 +0900 | [diff] [blame] | 6204 | (struct smb2_buffer_desc_v1 *) |
| 6205 | ((char *)req + le16_to_cpu(req->ReadChannelInfoOffset)), |
| 6206 | le16_to_cpu(req->ReadChannelInfoLength)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6207 | if (err) |
| 6208 | return err; |
| 6209 | |
| 6210 | return length; |
| 6211 | } |
| 6212 | |
| 6213 | /** |
| 6214 | * smb2_read() - handler for smb2 read from file |
| 6215 | * @work: smb work containing read command buffer |
| 6216 | * |
| 6217 | * Return: 0 on success, otherwise error |
| 6218 | */ |
| 6219 | int smb2_read(struct ksmbd_work *work) |
| 6220 | { |
| 6221 | struct ksmbd_conn *conn = work->conn; |
| 6222 | struct smb2_read_req *req; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6223 | struct smb2_read_rsp *rsp; |
Hyunchul Lee | 2fd5dcb | 2022-01-13 09:51:39 +0900 | [diff] [blame] | 6224 | struct ksmbd_file *fp = NULL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6225 | loff_t offset; |
| 6226 | size_t length, mincount; |
| 6227 | ssize_t nbytes = 0, remain_bytes = 0; |
| 6228 | int err = 0; |
Namjae Jeon | 7a84399 | 2022-05-16 16:23:28 +0900 | [diff] [blame] | 6229 | bool is_rdma_channel = false; |
| 6230 | unsigned int max_read_size = conn->vals->max_read_size; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6231 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6232 | WORK_BUFFERS(work, req, rsp); |
| 6233 | |
| 6234 | if (test_share_config_flag(work->tcon->share_conf, |
| 6235 | KSMBD_SHARE_FLAG_PIPE)) { |
| 6236 | ksmbd_debug(SMB, "IPC pipe read request\n"); |
| 6237 | return smb2_read_pipe(work); |
| 6238 | } |
| 6239 | |
Hyunchul Lee | 2fd5dcb | 2022-01-13 09:51:39 +0900 | [diff] [blame] | 6240 | if (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE || |
| 6241 | req->Channel == SMB2_CHANNEL_RDMA_V1) { |
Namjae Jeon | 7a84399 | 2022-05-16 16:23:28 +0900 | [diff] [blame] | 6242 | is_rdma_channel = true; |
| 6243 | max_read_size = get_smbd_max_read_write_size(); |
| 6244 | } |
| 6245 | |
| 6246 | if (is_rdma_channel == true) { |
Hyunchul Lee | 6d896d3 | 2022-01-20 21:10:11 +0900 | [diff] [blame] | 6247 | unsigned int ch_offset = le16_to_cpu(req->ReadChannelInfoOffset); |
| 6248 | |
| 6249 | if (ch_offset < offsetof(struct smb2_read_req, Buffer)) { |
| 6250 | err = -EINVAL; |
| 6251 | goto out; |
| 6252 | } |
Hyunchul Lee | 2fd5dcb | 2022-01-13 09:51:39 +0900 | [diff] [blame] | 6253 | err = smb2_set_remote_key_for_rdma(work, |
| 6254 | (struct smb2_buffer_desc_v1 *) |
Hyunchul Lee | 6d896d3 | 2022-01-20 21:10:11 +0900 | [diff] [blame] | 6255 | ((char *)req + ch_offset), |
Hyunchul Lee | 2fd5dcb | 2022-01-13 09:51:39 +0900 | [diff] [blame] | 6256 | req->Channel, |
Hyunchul Lee | 2fd5dcb | 2022-01-13 09:51:39 +0900 | [diff] [blame] | 6257 | req->ReadChannelInfoLength); |
| 6258 | if (err) |
| 6259 | goto out; |
| 6260 | } |
| 6261 | |
Paulo Alcantara | 2d004c6 | 2022-03-21 13:08:26 -0300 | [diff] [blame] | 6262 | fp = ksmbd_lookup_fd_slow(work, req->VolatileFileId, req->PersistentFileId); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6263 | if (!fp) { |
Marios Makassikis | a4382db | 2021-05-06 11:34:52 +0900 | [diff] [blame] | 6264 | err = -ENOENT; |
| 6265 | goto out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6266 | } |
| 6267 | |
| 6268 | if (!(fp->daccess & (FILE_READ_DATA_LE | FILE_READ_ATTRIBUTES_LE))) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 6269 | pr_err("Not permitted to read : 0x%x\n", fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6270 | err = -EACCES; |
| 6271 | goto out; |
| 6272 | } |
| 6273 | |
| 6274 | offset = le64_to_cpu(req->Offset); |
| 6275 | length = le32_to_cpu(req->Length); |
| 6276 | mincount = le32_to_cpu(req->MinimumCount); |
| 6277 | |
Namjae Jeon | 7a84399 | 2022-05-16 16:23:28 +0900 | [diff] [blame] | 6278 | if (length > max_read_size) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6279 | ksmbd_debug(SMB, "limiting read size to max size(%u)\n", |
Namjae Jeon | 7a84399 | 2022-05-16 16:23:28 +0900 | [diff] [blame] | 6280 | max_read_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6281 | err = -EINVAL; |
| 6282 | goto out; |
| 6283 | } |
| 6284 | |
Al Viro | 369c163 | 2022-01-30 21:11:02 -0500 | [diff] [blame] | 6285 | ksmbd_debug(SMB, "filename %pD, offset %lld, len %zu\n", |
| 6286 | fp->filp, offset, length); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6287 | |
Namjae Jeon | c30f4eb | 2021-06-18 10:17:37 +0900 | [diff] [blame] | 6288 | work->aux_payload_buf = kvmalloc(length, GFP_KERNEL | __GFP_ZERO); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6289 | if (!work->aux_payload_buf) { |
Dan Carpenter | c1ea111 | 2021-03-22 17:50:11 +0300 | [diff] [blame] | 6290 | err = -ENOMEM; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6291 | goto out; |
| 6292 | } |
| 6293 | |
| 6294 | nbytes = ksmbd_vfs_read(work, fp, length, &offset); |
| 6295 | if (nbytes < 0) { |
| 6296 | err = nbytes; |
| 6297 | goto out; |
| 6298 | } |
| 6299 | |
| 6300 | if ((nbytes == 0 && length != 0) || nbytes < mincount) { |
Namjae Jeon | c30f4eb | 2021-06-18 10:17:37 +0900 | [diff] [blame] | 6301 | kvfree(work->aux_payload_buf); |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 6302 | work->aux_payload_buf = NULL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6303 | rsp->hdr.Status = STATUS_END_OF_FILE; |
| 6304 | smb2_set_err_rsp(work); |
| 6305 | ksmbd_fd_put(work, fp); |
| 6306 | return 0; |
| 6307 | } |
| 6308 | |
| 6309 | ksmbd_debug(SMB, "nbytes %zu, offset %lld mincount %zu\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6310 | nbytes, offset, mincount); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6311 | |
Namjae Jeon | 7a84399 | 2022-05-16 16:23:28 +0900 | [diff] [blame] | 6312 | if (is_rdma_channel == true) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6313 | /* write data to the client using rdma channel */ |
| 6314 | remain_bytes = smb2_read_rdma_channel(work, req, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6315 | work->aux_payload_buf, |
| 6316 | nbytes); |
Namjae Jeon | c30f4eb | 2021-06-18 10:17:37 +0900 | [diff] [blame] | 6317 | kvfree(work->aux_payload_buf); |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 6318 | work->aux_payload_buf = NULL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6319 | |
| 6320 | nbytes = 0; |
| 6321 | if (remain_bytes < 0) { |
| 6322 | err = (int)remain_bytes; |
| 6323 | goto out; |
| 6324 | } |
| 6325 | } |
| 6326 | |
| 6327 | rsp->StructureSize = cpu_to_le16(17); |
| 6328 | rsp->DataOffset = 80; |
| 6329 | rsp->Reserved = 0; |
| 6330 | rsp->DataLength = cpu_to_le32(nbytes); |
| 6331 | rsp->DataRemaining = cpu_to_le32(remain_bytes); |
Ronnie Sahlberg | 699230f | 2021-09-09 12:26:12 +0900 | [diff] [blame] | 6332 | rsp->Flags = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6333 | inc_rfc1001_len(work->response_buf, 16); |
| 6334 | work->resp_hdr_sz = get_rfc1002_len(work->response_buf) + 4; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6335 | work->aux_payload_sz = nbytes; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6336 | inc_rfc1001_len(work->response_buf, nbytes); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6337 | ksmbd_fd_put(work, fp); |
| 6338 | return 0; |
| 6339 | |
| 6340 | out: |
| 6341 | if (err) { |
| 6342 | if (err == -EISDIR) |
| 6343 | rsp->hdr.Status = STATUS_INVALID_DEVICE_REQUEST; |
| 6344 | else if (err == -EAGAIN) |
| 6345 | rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT; |
| 6346 | else if (err == -ENOENT) |
| 6347 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 6348 | else if (err == -EACCES) |
| 6349 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 6350 | else if (err == -ESHARE) |
| 6351 | rsp->hdr.Status = STATUS_SHARING_VIOLATION; |
| 6352 | else if (err == -EINVAL) |
| 6353 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 6354 | else |
| 6355 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 6356 | |
| 6357 | smb2_set_err_rsp(work); |
| 6358 | } |
| 6359 | ksmbd_fd_put(work, fp); |
| 6360 | return err; |
| 6361 | } |
| 6362 | |
| 6363 | /** |
| 6364 | * smb2_write_pipe() - handler for smb2 write on IPC pipe |
| 6365 | * @work: smb work containing write IPC pipe command buffer |
| 6366 | * |
| 6367 | * Return: 0 on success, otherwise error |
| 6368 | */ |
| 6369 | static noinline int smb2_write_pipe(struct ksmbd_work *work) |
| 6370 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6371 | struct smb2_write_req *req = smb2_get_msg(work->request_buf); |
| 6372 | struct smb2_write_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6373 | struct ksmbd_rpc_command *rpc_resp; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6374 | u64 id = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6375 | int err = 0, ret = 0; |
| 6376 | char *data_buf; |
| 6377 | size_t length; |
| 6378 | |
| 6379 | length = le32_to_cpu(req->Length); |
Paulo Alcantara | 2d004c6 | 2022-03-21 13:08:26 -0300 | [diff] [blame] | 6380 | id = req->VolatileFileId; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6381 | |
Marios Makassikis | 158a66b | 2022-05-04 15:40:10 +0200 | [diff] [blame] | 6382 | if ((u64)le16_to_cpu(req->DataOffset) + length > |
| 6383 | get_rfc1002_len(work->request_buf)) { |
| 6384 | pr_err("invalid write data offset %u, smb_len %u\n", |
| 6385 | le16_to_cpu(req->DataOffset), |
| 6386 | get_rfc1002_len(work->request_buf)); |
| 6387 | err = -EINVAL; |
| 6388 | goto out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6389 | } |
| 6390 | |
Marios Makassikis | 158a66b | 2022-05-04 15:40:10 +0200 | [diff] [blame] | 6391 | data_buf = (char *)(((char *)&req->hdr.ProtocolId) + |
| 6392 | le16_to_cpu(req->DataOffset)); |
| 6393 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6394 | rpc_resp = ksmbd_rpc_write(work->sess, id, data_buf, length); |
| 6395 | if (rpc_resp) { |
| 6396 | if (rpc_resp->flags == KSMBD_RPC_ENOTIMPLEMENTED) { |
| 6397 | rsp->hdr.Status = STATUS_NOT_SUPPORTED; |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6398 | kvfree(rpc_resp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6399 | smb2_set_err_rsp(work); |
| 6400 | return -EOPNOTSUPP; |
| 6401 | } |
| 6402 | if (rpc_resp->flags != KSMBD_RPC_OK) { |
| 6403 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 6404 | smb2_set_err_rsp(work); |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6405 | kvfree(rpc_resp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6406 | return ret; |
| 6407 | } |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6408 | kvfree(rpc_resp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6409 | } |
| 6410 | |
| 6411 | rsp->StructureSize = cpu_to_le16(17); |
| 6412 | rsp->DataOffset = 0; |
| 6413 | rsp->Reserved = 0; |
| 6414 | rsp->DataLength = cpu_to_le32(length); |
| 6415 | rsp->DataRemaining = 0; |
| 6416 | rsp->Reserved2 = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6417 | inc_rfc1001_len(work->response_buf, 16); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6418 | return 0; |
| 6419 | out: |
| 6420 | if (err) { |
| 6421 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 6422 | smb2_set_err_rsp(work); |
| 6423 | } |
| 6424 | |
| 6425 | return err; |
| 6426 | } |
| 6427 | |
| 6428 | static ssize_t smb2_write_rdma_channel(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6429 | struct smb2_write_req *req, |
| 6430 | struct ksmbd_file *fp, |
| 6431 | loff_t offset, size_t length, bool sync) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6432 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6433 | char *data_buf; |
| 6434 | int ret; |
| 6435 | ssize_t nbytes; |
| 6436 | |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6437 | data_buf = kvmalloc(length, GFP_KERNEL | __GFP_ZERO); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6438 | if (!data_buf) |
| 6439 | return -ENOMEM; |
| 6440 | |
| 6441 | ret = ksmbd_conn_rdma_read(work->conn, data_buf, length, |
Hyunchul Lee | 1807abc | 2022-04-30 08:30:25 +0900 | [diff] [blame] | 6442 | (struct smb2_buffer_desc_v1 *) |
| 6443 | ((char *)req + le16_to_cpu(req->WriteChannelInfoOffset)), |
| 6444 | le16_to_cpu(req->WriteChannelInfoLength)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6445 | if (ret < 0) { |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6446 | kvfree(data_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6447 | return ret; |
| 6448 | } |
| 6449 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6450 | ret = ksmbd_vfs_write(work, fp, data_buf, length, &offset, sync, &nbytes); |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6451 | kvfree(data_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6452 | if (ret < 0) |
| 6453 | return ret; |
| 6454 | |
| 6455 | return nbytes; |
| 6456 | } |
| 6457 | |
| 6458 | /** |
| 6459 | * smb2_write() - handler for smb2 write from file |
| 6460 | * @work: smb work containing write command buffer |
| 6461 | * |
| 6462 | * Return: 0 on success, otherwise error |
| 6463 | */ |
| 6464 | int smb2_write(struct ksmbd_work *work) |
| 6465 | { |
| 6466 | struct smb2_write_req *req; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6467 | struct smb2_write_rsp *rsp; |
Namjae Jeon | bcd62a3 | 2021-05-10 09:08:19 +0900 | [diff] [blame] | 6468 | struct ksmbd_file *fp = NULL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6469 | loff_t offset; |
| 6470 | size_t length; |
| 6471 | ssize_t nbytes; |
| 6472 | char *data_buf; |
Namjae Jeon | 7a84399 | 2022-05-16 16:23:28 +0900 | [diff] [blame] | 6473 | bool writethrough = false, is_rdma_channel = false; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6474 | int err = 0; |
Namjae Jeon | 7a84399 | 2022-05-16 16:23:28 +0900 | [diff] [blame] | 6475 | unsigned int max_write_size = work->conn->vals->max_write_size; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6476 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6477 | WORK_BUFFERS(work, req, rsp); |
| 6478 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6479 | if (test_share_config_flag(work->tcon->share_conf, KSMBD_SHARE_FLAG_PIPE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6480 | ksmbd_debug(SMB, "IPC pipe write request\n"); |
| 6481 | return smb2_write_pipe(work); |
| 6482 | } |
| 6483 | |
Namjae Jeon | 7a84399 | 2022-05-16 16:23:28 +0900 | [diff] [blame] | 6484 | offset = le64_to_cpu(req->Offset); |
| 6485 | length = le32_to_cpu(req->Length); |
| 6486 | |
Hyunchul Lee | 2fd5dcb | 2022-01-13 09:51:39 +0900 | [diff] [blame] | 6487 | if (req->Channel == SMB2_CHANNEL_RDMA_V1 || |
| 6488 | req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE) { |
Namjae Jeon | 7a84399 | 2022-05-16 16:23:28 +0900 | [diff] [blame] | 6489 | is_rdma_channel = true; |
| 6490 | max_write_size = get_smbd_max_read_write_size(); |
| 6491 | length = le32_to_cpu(req->RemainingBytes); |
| 6492 | } |
| 6493 | |
| 6494 | if (is_rdma_channel == true) { |
Hyunchul Lee | 6d896d3 | 2022-01-20 21:10:11 +0900 | [diff] [blame] | 6495 | unsigned int ch_offset = le16_to_cpu(req->WriteChannelInfoOffset); |
| 6496 | |
| 6497 | if (req->Length != 0 || req->DataOffset != 0 || |
| 6498 | ch_offset < offsetof(struct smb2_write_req, Buffer)) { |
| 6499 | err = -EINVAL; |
| 6500 | goto out; |
| 6501 | } |
Hyunchul Lee | 2fd5dcb | 2022-01-13 09:51:39 +0900 | [diff] [blame] | 6502 | err = smb2_set_remote_key_for_rdma(work, |
| 6503 | (struct smb2_buffer_desc_v1 *) |
Hyunchul Lee | 6d896d3 | 2022-01-20 21:10:11 +0900 | [diff] [blame] | 6504 | ((char *)req + ch_offset), |
Hyunchul Lee | 2fd5dcb | 2022-01-13 09:51:39 +0900 | [diff] [blame] | 6505 | req->Channel, |
Hyunchul Lee | 2fd5dcb | 2022-01-13 09:51:39 +0900 | [diff] [blame] | 6506 | req->WriteChannelInfoLength); |
| 6507 | if (err) |
| 6508 | goto out; |
| 6509 | } |
| 6510 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6511 | if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
| 6512 | ksmbd_debug(SMB, "User does not have write permission\n"); |
| 6513 | err = -EACCES; |
| 6514 | goto out; |
| 6515 | } |
| 6516 | |
Paulo Alcantara | 2d004c6 | 2022-03-21 13:08:26 -0300 | [diff] [blame] | 6517 | fp = ksmbd_lookup_fd_slow(work, req->VolatileFileId, req->PersistentFileId); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6518 | if (!fp) { |
Marios Makassikis | a4382db | 2021-05-06 11:34:52 +0900 | [diff] [blame] | 6519 | err = -ENOENT; |
| 6520 | goto out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6521 | } |
| 6522 | |
| 6523 | if (!(fp->daccess & (FILE_WRITE_DATA_LE | FILE_READ_ATTRIBUTES_LE))) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 6524 | pr_err("Not permitted to write : 0x%x\n", fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6525 | err = -EACCES; |
| 6526 | goto out; |
| 6527 | } |
| 6528 | |
Namjae Jeon | 7a84399 | 2022-05-16 16:23:28 +0900 | [diff] [blame] | 6529 | if (length > max_write_size) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6530 | ksmbd_debug(SMB, "limiting write size to max size(%u)\n", |
Namjae Jeon | 7a84399 | 2022-05-16 16:23:28 +0900 | [diff] [blame] | 6531 | max_write_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6532 | err = -EINVAL; |
| 6533 | goto out; |
| 6534 | } |
| 6535 | |
Hyunchul Lee | 745bbc0 | 2022-06-14 08:25:50 +0900 | [diff] [blame] | 6536 | ksmbd_debug(SMB, "flags %u\n", le32_to_cpu(req->Flags)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6537 | if (le32_to_cpu(req->Flags) & SMB2_WRITEFLAG_WRITE_THROUGH) |
| 6538 | writethrough = true; |
| 6539 | |
Namjae Jeon | 7a84399 | 2022-05-16 16:23:28 +0900 | [diff] [blame] | 6540 | if (is_rdma_channel == false) { |
Hyunchul Lee | ac60778 | 2022-07-28 23:41:51 +0900 | [diff] [blame] | 6541 | if (le16_to_cpu(req->DataOffset) < |
| 6542 | offsetof(struct smb2_write_req, Buffer)) { |
Marios Makassikis | 158a66b | 2022-05-04 15:40:10 +0200 | [diff] [blame] | 6543 | err = -EINVAL; |
| 6544 | goto out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6545 | } |
Hyunchul Lee | ac60778 | 2022-07-28 23:41:51 +0900 | [diff] [blame] | 6546 | |
Marios Makassikis | 158a66b | 2022-05-04 15:40:10 +0200 | [diff] [blame] | 6547 | data_buf = (char *)(((char *)&req->hdr.ProtocolId) + |
| 6548 | le16_to_cpu(req->DataOffset)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6549 | |
Al Viro | 369c163 | 2022-01-30 21:11:02 -0500 | [diff] [blame] | 6550 | ksmbd_debug(SMB, "filename %pD, offset %lld, len %zu\n", |
| 6551 | fp->filp, offset, length); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6552 | err = ksmbd_vfs_write(work, fp, data_buf, length, &offset, |
| 6553 | writethrough, &nbytes); |
| 6554 | if (err < 0) |
| 6555 | goto out; |
| 6556 | } else { |
| 6557 | /* read data from the client using rdma channel, and |
| 6558 | * write the data. |
| 6559 | */ |
Namjae Jeon | 7a84399 | 2022-05-16 16:23:28 +0900 | [diff] [blame] | 6560 | nbytes = smb2_write_rdma_channel(work, req, fp, offset, length, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6561 | writethrough); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6562 | if (nbytes < 0) { |
| 6563 | err = (int)nbytes; |
| 6564 | goto out; |
| 6565 | } |
| 6566 | } |
| 6567 | |
| 6568 | rsp->StructureSize = cpu_to_le16(17); |
| 6569 | rsp->DataOffset = 0; |
| 6570 | rsp->Reserved = 0; |
| 6571 | rsp->DataLength = cpu_to_le32(nbytes); |
| 6572 | rsp->DataRemaining = 0; |
| 6573 | rsp->Reserved2 = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6574 | inc_rfc1001_len(work->response_buf, 16); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6575 | ksmbd_fd_put(work, fp); |
| 6576 | return 0; |
| 6577 | |
| 6578 | out: |
| 6579 | if (err == -EAGAIN) |
| 6580 | rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT; |
| 6581 | else if (err == -ENOSPC || err == -EFBIG) |
| 6582 | rsp->hdr.Status = STATUS_DISK_FULL; |
| 6583 | else if (err == -ENOENT) |
| 6584 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 6585 | else if (err == -EACCES) |
| 6586 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 6587 | else if (err == -ESHARE) |
| 6588 | rsp->hdr.Status = STATUS_SHARING_VIOLATION; |
| 6589 | else if (err == -EINVAL) |
| 6590 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 6591 | else |
| 6592 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 6593 | |
| 6594 | smb2_set_err_rsp(work); |
| 6595 | ksmbd_fd_put(work, fp); |
| 6596 | return err; |
| 6597 | } |
| 6598 | |
| 6599 | /** |
| 6600 | * smb2_flush() - handler for smb2 flush file - fsync |
| 6601 | * @work: smb work containing flush command buffer |
| 6602 | * |
| 6603 | * Return: 0 on success, otherwise error |
| 6604 | */ |
| 6605 | int smb2_flush(struct ksmbd_work *work) |
| 6606 | { |
| 6607 | struct smb2_flush_req *req; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6608 | struct smb2_flush_rsp *rsp; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6609 | int err; |
| 6610 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6611 | WORK_BUFFERS(work, req, rsp); |
| 6612 | |
Paulo Alcantara | 2d004c6 | 2022-03-21 13:08:26 -0300 | [diff] [blame] | 6613 | ksmbd_debug(SMB, "SMB2_FLUSH called for fid %llu\n", req->VolatileFileId); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6614 | |
Paulo Alcantara | 2d004c6 | 2022-03-21 13:08:26 -0300 | [diff] [blame] | 6615 | err = ksmbd_vfs_fsync(work, req->VolatileFileId, req->PersistentFileId); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6616 | if (err) |
| 6617 | goto out; |
| 6618 | |
| 6619 | rsp->StructureSize = cpu_to_le16(4); |
| 6620 | rsp->Reserved = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6621 | inc_rfc1001_len(work->response_buf, 4); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6622 | return 0; |
| 6623 | |
| 6624 | out: |
| 6625 | if (err) { |
| 6626 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 6627 | smb2_set_err_rsp(work); |
| 6628 | } |
| 6629 | |
| 6630 | return err; |
| 6631 | } |
| 6632 | |
| 6633 | /** |
| 6634 | * smb2_cancel() - handler for smb2 cancel command |
| 6635 | * @work: smb work containing cancel command buffer |
| 6636 | * |
| 6637 | * Return: 0 on success, otherwise error |
| 6638 | */ |
| 6639 | int smb2_cancel(struct ksmbd_work *work) |
| 6640 | { |
| 6641 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6642 | struct smb2_hdr *hdr = smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6643 | struct smb2_hdr *chdr; |
Jakob Koschel | edf5f05 | 2022-03-24 08:13:01 +0100 | [diff] [blame] | 6644 | struct ksmbd_work *cancel_work = NULL, *iter; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6645 | struct list_head *command_list; |
| 6646 | |
| 6647 | ksmbd_debug(SMB, "smb2 cancel called on mid %llu, async flags 0x%x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6648 | hdr->MessageId, hdr->Flags); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6649 | |
| 6650 | if (hdr->Flags & SMB2_FLAGS_ASYNC_COMMAND) { |
| 6651 | command_list = &conn->async_requests; |
| 6652 | |
| 6653 | spin_lock(&conn->request_lock); |
Jakob Koschel | edf5f05 | 2022-03-24 08:13:01 +0100 | [diff] [blame] | 6654 | list_for_each_entry(iter, command_list, |
Namjae Jeon | 6f3d5ee | 2021-06-18 10:28:52 +0900 | [diff] [blame] | 6655 | async_request_entry) { |
Jakob Koschel | edf5f05 | 2022-03-24 08:13:01 +0100 | [diff] [blame] | 6656 | chdr = smb2_get_msg(iter->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6657 | |
Jakob Koschel | edf5f05 | 2022-03-24 08:13:01 +0100 | [diff] [blame] | 6658 | if (iter->async_id != |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6659 | le64_to_cpu(hdr->Id.AsyncId)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6660 | continue; |
| 6661 | |
| 6662 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6663 | "smb2 with AsyncId %llu cancelled command = 0x%x\n", |
| 6664 | le64_to_cpu(hdr->Id.AsyncId), |
| 6665 | le16_to_cpu(chdr->Command)); |
Jakob Koschel | edf5f05 | 2022-03-24 08:13:01 +0100 | [diff] [blame] | 6666 | cancel_work = iter; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6667 | break; |
| 6668 | } |
| 6669 | spin_unlock(&conn->request_lock); |
| 6670 | } else { |
| 6671 | command_list = &conn->requests; |
| 6672 | |
| 6673 | spin_lock(&conn->request_lock); |
Jakob Koschel | edf5f05 | 2022-03-24 08:13:01 +0100 | [diff] [blame] | 6674 | list_for_each_entry(iter, command_list, request_entry) { |
| 6675 | chdr = smb2_get_msg(iter->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6676 | |
| 6677 | if (chdr->MessageId != hdr->MessageId || |
Jakob Koschel | edf5f05 | 2022-03-24 08:13:01 +0100 | [diff] [blame] | 6678 | iter == work) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6679 | continue; |
| 6680 | |
| 6681 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6682 | "smb2 with mid %llu cancelled command = 0x%x\n", |
| 6683 | le64_to_cpu(hdr->MessageId), |
| 6684 | le16_to_cpu(chdr->Command)); |
Jakob Koschel | edf5f05 | 2022-03-24 08:13:01 +0100 | [diff] [blame] | 6685 | cancel_work = iter; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6686 | break; |
| 6687 | } |
| 6688 | spin_unlock(&conn->request_lock); |
| 6689 | } |
| 6690 | |
Jakob Koschel | edf5f05 | 2022-03-24 08:13:01 +0100 | [diff] [blame] | 6691 | if (cancel_work) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6692 | cancel_work->state = KSMBD_WORK_CANCELLED; |
| 6693 | if (cancel_work->cancel_fn) |
| 6694 | cancel_work->cancel_fn(cancel_work->cancel_argv); |
| 6695 | } |
| 6696 | |
| 6697 | /* For SMB2_CANCEL command itself send no response*/ |
| 6698 | work->send_no_response = 1; |
| 6699 | return 0; |
| 6700 | } |
| 6701 | |
| 6702 | struct file_lock *smb_flock_init(struct file *f) |
| 6703 | { |
| 6704 | struct file_lock *fl; |
| 6705 | |
| 6706 | fl = locks_alloc_lock(); |
| 6707 | if (!fl) |
| 6708 | goto out; |
| 6709 | |
| 6710 | locks_init_lock(fl); |
| 6711 | |
| 6712 | fl->fl_owner = f; |
| 6713 | fl->fl_pid = current->tgid; |
| 6714 | fl->fl_file = f; |
| 6715 | fl->fl_flags = FL_POSIX; |
| 6716 | fl->fl_ops = NULL; |
| 6717 | fl->fl_lmops = NULL; |
| 6718 | |
| 6719 | out: |
| 6720 | return fl; |
| 6721 | } |
| 6722 | |
| 6723 | static int smb2_set_flock_flags(struct file_lock *flock, int flags) |
| 6724 | { |
| 6725 | int cmd = -EINVAL; |
| 6726 | |
| 6727 | /* Checking for wrong flag combination during lock request*/ |
| 6728 | switch (flags) { |
| 6729 | case SMB2_LOCKFLAG_SHARED: |
| 6730 | ksmbd_debug(SMB, "received shared request\n"); |
| 6731 | cmd = F_SETLKW; |
| 6732 | flock->fl_type = F_RDLCK; |
| 6733 | flock->fl_flags |= FL_SLEEP; |
| 6734 | break; |
| 6735 | case SMB2_LOCKFLAG_EXCLUSIVE: |
| 6736 | ksmbd_debug(SMB, "received exclusive request\n"); |
| 6737 | cmd = F_SETLKW; |
| 6738 | flock->fl_type = F_WRLCK; |
| 6739 | flock->fl_flags |= FL_SLEEP; |
| 6740 | break; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6741 | case SMB2_LOCKFLAG_SHARED | SMB2_LOCKFLAG_FAIL_IMMEDIATELY: |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6742 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6743 | "received shared & fail immediately request\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6744 | cmd = F_SETLK; |
| 6745 | flock->fl_type = F_RDLCK; |
| 6746 | break; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6747 | case SMB2_LOCKFLAG_EXCLUSIVE | SMB2_LOCKFLAG_FAIL_IMMEDIATELY: |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6748 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6749 | "received exclusive & fail immediately request\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6750 | cmd = F_SETLK; |
| 6751 | flock->fl_type = F_WRLCK; |
| 6752 | break; |
| 6753 | case SMB2_LOCKFLAG_UNLOCK: |
| 6754 | ksmbd_debug(SMB, "received unlock request\n"); |
| 6755 | flock->fl_type = F_UNLCK; |
Jeff Layton | 7ecbe92 | 2022-11-11 08:11:53 -0500 | [diff] [blame] | 6756 | cmd = F_SETLK; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6757 | break; |
| 6758 | } |
| 6759 | |
| 6760 | return cmd; |
| 6761 | } |
| 6762 | |
| 6763 | static struct ksmbd_lock *smb2_lock_init(struct file_lock *flock, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6764 | unsigned int cmd, int flags, |
| 6765 | struct list_head *lock_list) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6766 | { |
| 6767 | struct ksmbd_lock *lock; |
| 6768 | |
| 6769 | lock = kzalloc(sizeof(struct ksmbd_lock), GFP_KERNEL); |
| 6770 | if (!lock) |
| 6771 | return NULL; |
| 6772 | |
| 6773 | lock->cmd = cmd; |
| 6774 | lock->fl = flock; |
| 6775 | lock->start = flock->fl_start; |
| 6776 | lock->end = flock->fl_end; |
| 6777 | lock->flags = flags; |
| 6778 | if (lock->start == lock->end) |
| 6779 | lock->zero_len = 1; |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6780 | INIT_LIST_HEAD(&lock->clist); |
| 6781 | INIT_LIST_HEAD(&lock->flist); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6782 | INIT_LIST_HEAD(&lock->llist); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6783 | list_add_tail(&lock->llist, lock_list); |
| 6784 | |
| 6785 | return lock; |
| 6786 | } |
| 6787 | |
| 6788 | static void smb2_remove_blocked_lock(void **argv) |
| 6789 | { |
| 6790 | struct file_lock *flock = (struct file_lock *)argv[0]; |
| 6791 | |
| 6792 | ksmbd_vfs_posix_lock_unblock(flock); |
| 6793 | wake_up(&flock->fl_wait); |
| 6794 | } |
| 6795 | |
| 6796 | static inline bool lock_defer_pending(struct file_lock *fl) |
| 6797 | { |
| 6798 | /* check pending lock waiters */ |
| 6799 | return waitqueue_active(&fl->fl_wait); |
| 6800 | } |
| 6801 | |
| 6802 | /** |
| 6803 | * smb2_lock() - handler for smb2 file lock command |
| 6804 | * @work: smb work containing lock command buffer |
| 6805 | * |
| 6806 | * Return: 0 on success, otherwise error |
| 6807 | */ |
| 6808 | int smb2_lock(struct ksmbd_work *work) |
| 6809 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6810 | struct smb2_lock_req *req = smb2_get_msg(work->request_buf); |
| 6811 | struct smb2_lock_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6812 | struct smb2_lock_element *lock_ele; |
| 6813 | struct ksmbd_file *fp = NULL; |
| 6814 | struct file_lock *flock = NULL; |
| 6815 | struct file *filp = NULL; |
| 6816 | int lock_count; |
| 6817 | int flags = 0; |
| 6818 | int cmd = 0; |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6819 | int err = -EIO, i, rc = 0; |
Namjae Jeon | 50bf80a | 2021-05-14 12:20:07 +0900 | [diff] [blame] | 6820 | u64 lock_start, lock_length; |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6821 | struct ksmbd_lock *smb_lock = NULL, *cmp_lock, *tmp, *tmp2; |
| 6822 | struct ksmbd_conn *conn; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6823 | int nolock = 0; |
| 6824 | LIST_HEAD(lock_list); |
| 6825 | LIST_HEAD(rollback_list); |
| 6826 | int prior_lock = 0; |
| 6827 | |
| 6828 | ksmbd_debug(SMB, "Received lock request\n"); |
Paulo Alcantara | 2d004c6 | 2022-03-21 13:08:26 -0300 | [diff] [blame] | 6829 | fp = ksmbd_lookup_fd_slow(work, req->VolatileFileId, req->PersistentFileId); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6830 | if (!fp) { |
Paulo Alcantara | 2d004c6 | 2022-03-21 13:08:26 -0300 | [diff] [blame] | 6831 | ksmbd_debug(SMB, "Invalid file id for lock : %llu\n", req->VolatileFileId); |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6832 | err = -ENOENT; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6833 | goto out2; |
| 6834 | } |
| 6835 | |
| 6836 | filp = fp->filp; |
| 6837 | lock_count = le16_to_cpu(req->LockCount); |
| 6838 | lock_ele = req->locks; |
| 6839 | |
| 6840 | ksmbd_debug(SMB, "lock count is %d\n", lock_count); |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6841 | if (!lock_count) { |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6842 | err = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6843 | goto out2; |
| 6844 | } |
| 6845 | |
| 6846 | for (i = 0; i < lock_count; i++) { |
| 6847 | flags = le32_to_cpu(lock_ele[i].Flags); |
| 6848 | |
| 6849 | flock = smb_flock_init(filp); |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6850 | if (!flock) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6851 | goto out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6852 | |
| 6853 | cmd = smb2_set_flock_flags(flock, flags); |
| 6854 | |
Namjae Jeon | 50bf80a | 2021-05-14 12:20:07 +0900 | [diff] [blame] | 6855 | lock_start = le64_to_cpu(lock_ele[i].Offset); |
| 6856 | lock_length = le64_to_cpu(lock_ele[i].Length); |
| 6857 | if (lock_start > U64_MAX - lock_length) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 6858 | pr_err("Invalid lock range requested\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6859 | rsp->hdr.Status = STATUS_INVALID_LOCK_RANGE; |
Marios Makassikis | 01f6c61 | 2022-11-29 12:19:33 +0100 | [diff] [blame] | 6860 | locks_free_lock(flock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6861 | goto out; |
| 6862 | } |
| 6863 | |
Namjae Jeon | 50bf80a | 2021-05-14 12:20:07 +0900 | [diff] [blame] | 6864 | if (lock_start > OFFSET_MAX) |
| 6865 | flock->fl_start = OFFSET_MAX; |
| 6866 | else |
| 6867 | flock->fl_start = lock_start; |
| 6868 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6869 | lock_length = le64_to_cpu(lock_ele[i].Length); |
Namjae Jeon | 50bf80a | 2021-05-14 12:20:07 +0900 | [diff] [blame] | 6870 | if (lock_length > OFFSET_MAX - flock->fl_start) |
| 6871 | lock_length = OFFSET_MAX - flock->fl_start; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6872 | |
| 6873 | flock->fl_end = flock->fl_start + lock_length; |
| 6874 | |
| 6875 | if (flock->fl_end < flock->fl_start) { |
| 6876 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6877 | "the end offset(%llx) is smaller than the start offset(%llx)\n", |
| 6878 | flock->fl_end, flock->fl_start); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6879 | rsp->hdr.Status = STATUS_INVALID_LOCK_RANGE; |
Marios Makassikis | 01f6c61 | 2022-11-29 12:19:33 +0100 | [diff] [blame] | 6880 | locks_free_lock(flock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6881 | goto out; |
| 6882 | } |
| 6883 | |
| 6884 | /* Check conflict locks in one request */ |
| 6885 | list_for_each_entry(cmp_lock, &lock_list, llist) { |
| 6886 | if (cmp_lock->fl->fl_start <= flock->fl_start && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6887 | cmp_lock->fl->fl_end >= flock->fl_end) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6888 | if (cmp_lock->fl->fl_type != F_UNLCK && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6889 | flock->fl_type != F_UNLCK) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 6890 | pr_err("conflict two locks in one request\n"); |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6891 | err = -EINVAL; |
Marios Makassikis | 01f6c61 | 2022-11-29 12:19:33 +0100 | [diff] [blame] | 6892 | locks_free_lock(flock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6893 | goto out; |
| 6894 | } |
| 6895 | } |
| 6896 | } |
| 6897 | |
| 6898 | smb_lock = smb2_lock_init(flock, cmd, flags, &lock_list); |
| 6899 | if (!smb_lock) { |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6900 | err = -EINVAL; |
Marios Makassikis | 01f6c61 | 2022-11-29 12:19:33 +0100 | [diff] [blame] | 6901 | locks_free_lock(flock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6902 | goto out; |
| 6903 | } |
| 6904 | } |
| 6905 | |
| 6906 | list_for_each_entry_safe(smb_lock, tmp, &lock_list, llist) { |
| 6907 | if (smb_lock->cmd < 0) { |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6908 | err = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6909 | goto out; |
| 6910 | } |
| 6911 | |
| 6912 | if (!(smb_lock->flags & SMB2_LOCKFLAG_MASK)) { |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6913 | err = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6914 | goto out; |
| 6915 | } |
| 6916 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6917 | if ((prior_lock & (SMB2_LOCKFLAG_EXCLUSIVE | SMB2_LOCKFLAG_SHARED) && |
| 6918 | smb_lock->flags & SMB2_LOCKFLAG_UNLOCK) || |
| 6919 | (prior_lock == SMB2_LOCKFLAG_UNLOCK && |
| 6920 | !(smb_lock->flags & SMB2_LOCKFLAG_UNLOCK))) { |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6921 | err = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6922 | goto out; |
| 6923 | } |
| 6924 | |
| 6925 | prior_lock = smb_lock->flags; |
| 6926 | |
| 6927 | if (!(smb_lock->flags & SMB2_LOCKFLAG_UNLOCK) && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6928 | !(smb_lock->flags & SMB2_LOCKFLAG_FAIL_IMMEDIATELY)) |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6929 | goto no_check_cl; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6930 | |
| 6931 | nolock = 1; |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6932 | /* check locks in connection list */ |
| 6933 | read_lock(&conn_list_lock); |
| 6934 | list_for_each_entry(conn, &conn_list, conns_list) { |
| 6935 | spin_lock(&conn->llist_lock); |
| 6936 | list_for_each_entry_safe(cmp_lock, tmp2, &conn->lock_list, clist) { |
| 6937 | if (file_inode(cmp_lock->fl->fl_file) != |
| 6938 | file_inode(smb_lock->fl->fl_file)) |
| 6939 | continue; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6940 | |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6941 | if (smb_lock->fl->fl_type == F_UNLCK) { |
| 6942 | if (cmp_lock->fl->fl_file == smb_lock->fl->fl_file && |
| 6943 | cmp_lock->start == smb_lock->start && |
| 6944 | cmp_lock->end == smb_lock->end && |
| 6945 | !lock_defer_pending(cmp_lock->fl)) { |
| 6946 | nolock = 0; |
| 6947 | list_del(&cmp_lock->flist); |
| 6948 | list_del(&cmp_lock->clist); |
| 6949 | spin_unlock(&conn->llist_lock); |
| 6950 | read_unlock(&conn_list_lock); |
| 6951 | |
| 6952 | locks_free_lock(cmp_lock->fl); |
| 6953 | kfree(cmp_lock); |
| 6954 | goto out_check_cl; |
| 6955 | } |
| 6956 | continue; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6957 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6958 | |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6959 | if (cmp_lock->fl->fl_file == smb_lock->fl->fl_file) { |
| 6960 | if (smb_lock->flags & SMB2_LOCKFLAG_SHARED) |
| 6961 | continue; |
| 6962 | } else { |
| 6963 | if (cmp_lock->flags & SMB2_LOCKFLAG_SHARED) |
| 6964 | continue; |
| 6965 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6966 | |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6967 | /* check zero byte lock range */ |
| 6968 | if (cmp_lock->zero_len && !smb_lock->zero_len && |
| 6969 | cmp_lock->start > smb_lock->start && |
| 6970 | cmp_lock->start < smb_lock->end) { |
| 6971 | spin_unlock(&conn->llist_lock); |
| 6972 | read_unlock(&conn_list_lock); |
| 6973 | pr_err("previous lock conflict with zero byte lock range\n"); |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6974 | goto out; |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6975 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6976 | |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6977 | if (smb_lock->zero_len && !cmp_lock->zero_len && |
| 6978 | smb_lock->start > cmp_lock->start && |
| 6979 | smb_lock->start < cmp_lock->end) { |
| 6980 | spin_unlock(&conn->llist_lock); |
| 6981 | read_unlock(&conn_list_lock); |
| 6982 | pr_err("current lock conflict with zero byte lock range\n"); |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6983 | goto out; |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6984 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6985 | |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6986 | if (((cmp_lock->start <= smb_lock->start && |
| 6987 | cmp_lock->end > smb_lock->start) || |
| 6988 | (cmp_lock->start < smb_lock->end && |
| 6989 | cmp_lock->end >= smb_lock->end)) && |
| 6990 | !cmp_lock->zero_len && !smb_lock->zero_len) { |
| 6991 | spin_unlock(&conn->llist_lock); |
| 6992 | read_unlock(&conn_list_lock); |
| 6993 | pr_err("Not allow lock operation on exclusive lock range\n"); |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6994 | goto out; |
| 6995 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6996 | } |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6997 | spin_unlock(&conn->llist_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6998 | } |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6999 | read_unlock(&conn_list_lock); |
| 7000 | out_check_cl: |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7001 | if (smb_lock->fl->fl_type == F_UNLCK && nolock) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 7002 | pr_err("Try to unlock nolocked range\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7003 | rsp->hdr.Status = STATUS_RANGE_NOT_LOCKED; |
| 7004 | goto out; |
| 7005 | } |
| 7006 | |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 7007 | no_check_cl: |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7008 | if (smb_lock->zero_len) { |
| 7009 | err = 0; |
| 7010 | goto skip; |
| 7011 | } |
| 7012 | |
| 7013 | flock = smb_lock->fl; |
| 7014 | list_del(&smb_lock->llist); |
| 7015 | retry: |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 7016 | rc = vfs_lock_file(filp, smb_lock->cmd, flock, NULL); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7017 | skip: |
| 7018 | if (flags & SMB2_LOCKFLAG_UNLOCK) { |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 7019 | if (!rc) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7020 | ksmbd_debug(SMB, "File unlocked\n"); |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 7021 | } else if (rc == -ENOENT) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7022 | rsp->hdr.Status = STATUS_NOT_LOCKED; |
| 7023 | goto out; |
| 7024 | } |
| 7025 | locks_free_lock(flock); |
| 7026 | kfree(smb_lock); |
| 7027 | } else { |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 7028 | if (rc == FILE_LOCK_DEFERRED) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7029 | void **argv; |
| 7030 | |
| 7031 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7032 | "would have to wait for getting lock\n"); |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 7033 | spin_lock(&work->conn->llist_lock); |
| 7034 | list_add_tail(&smb_lock->clist, |
| 7035 | &work->conn->lock_list); |
| 7036 | spin_unlock(&work->conn->llist_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7037 | list_add(&smb_lock->llist, &rollback_list); |
| 7038 | |
| 7039 | argv = kmalloc(sizeof(void *), GFP_KERNEL); |
| 7040 | if (!argv) { |
| 7041 | err = -ENOMEM; |
| 7042 | goto out; |
| 7043 | } |
| 7044 | argv[0] = flock; |
| 7045 | |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 7046 | rc = setup_async_work(work, |
| 7047 | smb2_remove_blocked_lock, |
| 7048 | argv); |
| 7049 | if (rc) { |
| 7050 | err = -ENOMEM; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7051 | goto out; |
| 7052 | } |
| 7053 | spin_lock(&fp->f_lock); |
| 7054 | list_add(&work->fp_entry, &fp->blocked_works); |
| 7055 | spin_unlock(&fp->f_lock); |
| 7056 | |
| 7057 | smb2_send_interim_resp(work, STATUS_PENDING); |
| 7058 | |
Hyunchul Lee | 45a64e8 | 2021-07-10 09:34:20 +0900 | [diff] [blame] | 7059 | ksmbd_vfs_posix_lock_wait(flock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7060 | |
Hyunchul Lee | d4075ab | 2021-06-25 07:02:10 +0900 | [diff] [blame] | 7061 | if (work->state != KSMBD_WORK_ACTIVE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7062 | list_del(&smb_lock->llist); |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 7063 | spin_lock(&work->conn->llist_lock); |
| 7064 | list_del(&smb_lock->clist); |
| 7065 | spin_unlock(&work->conn->llist_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7066 | locks_free_lock(flock); |
| 7067 | |
Hyunchul Lee | d4075ab | 2021-06-25 07:02:10 +0900 | [diff] [blame] | 7068 | if (work->state == KSMBD_WORK_CANCELLED) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7069 | spin_lock(&fp->f_lock); |
| 7070 | list_del(&work->fp_entry); |
| 7071 | spin_unlock(&fp->f_lock); |
| 7072 | rsp->hdr.Status = |
| 7073 | STATUS_CANCELLED; |
| 7074 | kfree(smb_lock); |
| 7075 | smb2_send_interim_resp(work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7076 | STATUS_CANCELLED); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7077 | work->send_no_response = 1; |
| 7078 | goto out; |
| 7079 | } |
| 7080 | init_smb2_rsp_hdr(work); |
| 7081 | smb2_set_err_rsp(work); |
| 7082 | rsp->hdr.Status = |
| 7083 | STATUS_RANGE_NOT_LOCKED; |
| 7084 | kfree(smb_lock); |
| 7085 | goto out2; |
| 7086 | } |
| 7087 | |
| 7088 | list_del(&smb_lock->llist); |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 7089 | spin_lock(&work->conn->llist_lock); |
| 7090 | list_del(&smb_lock->clist); |
| 7091 | spin_unlock(&work->conn->llist_lock); |
| 7092 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7093 | spin_lock(&fp->f_lock); |
| 7094 | list_del(&work->fp_entry); |
| 7095 | spin_unlock(&fp->f_lock); |
| 7096 | goto retry; |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 7097 | } else if (!rc) { |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 7098 | spin_lock(&work->conn->llist_lock); |
| 7099 | list_add_tail(&smb_lock->clist, |
| 7100 | &work->conn->lock_list); |
| 7101 | list_add_tail(&smb_lock->flist, |
| 7102 | &fp->lock_list); |
| 7103 | spin_unlock(&work->conn->llist_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7104 | list_add(&smb_lock->llist, &rollback_list); |
| 7105 | ksmbd_debug(SMB, "successful in taking lock\n"); |
| 7106 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7107 | goto out; |
| 7108 | } |
| 7109 | } |
| 7110 | } |
| 7111 | |
| 7112 | if (atomic_read(&fp->f_ci->op_count) > 1) |
| 7113 | smb_break_all_oplock(work, fp); |
| 7114 | |
| 7115 | rsp->StructureSize = cpu_to_le16(4); |
| 7116 | ksmbd_debug(SMB, "successful in taking lock\n"); |
| 7117 | rsp->hdr.Status = STATUS_SUCCESS; |
| 7118 | rsp->Reserved = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 7119 | inc_rfc1001_len(work->response_buf, 4); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7120 | ksmbd_fd_put(work, fp); |
Namjae Jeon | 96ad4ec | 2021-07-13 17:17:28 +0900 | [diff] [blame] | 7121 | return 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7122 | |
| 7123 | out: |
| 7124 | list_for_each_entry_safe(smb_lock, tmp, &lock_list, llist) { |
| 7125 | locks_free_lock(smb_lock->fl); |
| 7126 | list_del(&smb_lock->llist); |
| 7127 | kfree(smb_lock); |
| 7128 | } |
| 7129 | |
| 7130 | list_for_each_entry_safe(smb_lock, tmp, &rollback_list, llist) { |
| 7131 | struct file_lock *rlock = NULL; |
| 7132 | |
| 7133 | rlock = smb_flock_init(filp); |
| 7134 | rlock->fl_type = F_UNLCK; |
| 7135 | rlock->fl_start = smb_lock->start; |
| 7136 | rlock->fl_end = smb_lock->end; |
| 7137 | |
Jeff Layton | 7ecbe92 | 2022-11-11 08:11:53 -0500 | [diff] [blame] | 7138 | rc = vfs_lock_file(filp, F_SETLK, rlock, NULL); |
Namjae Jeon | 96ad4ec | 2021-07-13 17:17:28 +0900 | [diff] [blame] | 7139 | if (rc) |
| 7140 | pr_err("rollback unlock fail : %d\n", rc); |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 7141 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7142 | list_del(&smb_lock->llist); |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 7143 | spin_lock(&work->conn->llist_lock); |
| 7144 | if (!list_empty(&smb_lock->flist)) |
| 7145 | list_del(&smb_lock->flist); |
| 7146 | list_del(&smb_lock->clist); |
| 7147 | spin_unlock(&work->conn->llist_lock); |
| 7148 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7149 | locks_free_lock(smb_lock->fl); |
| 7150 | locks_free_lock(rlock); |
| 7151 | kfree(smb_lock); |
| 7152 | } |
| 7153 | out2: |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 7154 | ksmbd_debug(SMB, "failed in taking lock(flags : %x), err : %d\n", flags, err); |
| 7155 | |
| 7156 | if (!rsp->hdr.Status) { |
| 7157 | if (err == -EINVAL) |
| 7158 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 7159 | else if (err == -ENOMEM) |
| 7160 | rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES; |
| 7161 | else if (err == -ENOENT) |
| 7162 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 7163 | else |
| 7164 | rsp->hdr.Status = STATUS_LOCK_NOT_GRANTED; |
| 7165 | } |
| 7166 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7167 | smb2_set_err_rsp(work); |
| 7168 | ksmbd_fd_put(work, fp); |
Namjae Jeon | 96ad4ec | 2021-07-13 17:17:28 +0900 | [diff] [blame] | 7169 | return err; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7170 | } |
| 7171 | |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7172 | static int fsctl_copychunk(struct ksmbd_work *work, |
| 7173 | struct copychunk_ioctl_req *ci_req, |
| 7174 | unsigned int cnt_code, |
| 7175 | unsigned int input_count, |
| 7176 | unsigned long long volatile_id, |
| 7177 | unsigned long long persistent_id, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7178 | struct smb2_ioctl_rsp *rsp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7179 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7180 | struct copychunk_ioctl_rsp *ci_rsp; |
| 7181 | struct ksmbd_file *src_fp = NULL, *dst_fp = NULL; |
| 7182 | struct srv_copychunk *chunks; |
| 7183 | unsigned int i, chunk_count, chunk_count_written = 0; |
| 7184 | unsigned int chunk_size_written = 0; |
| 7185 | loff_t total_size_written = 0; |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7186 | int ret = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7187 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7188 | ci_rsp = (struct copychunk_ioctl_rsp *)&rsp->Buffer[0]; |
| 7189 | |
Paulo Alcantara | 2d004c6 | 2022-03-21 13:08:26 -0300 | [diff] [blame] | 7190 | rsp->VolatileFileId = volatile_id; |
| 7191 | rsp->PersistentFileId = persistent_id; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7192 | ci_rsp->ChunksWritten = |
| 7193 | cpu_to_le32(ksmbd_server_side_copy_max_chunk_count()); |
| 7194 | ci_rsp->ChunkBytesWritten = |
| 7195 | cpu_to_le32(ksmbd_server_side_copy_max_chunk_size()); |
| 7196 | ci_rsp->TotalBytesWritten = |
| 7197 | cpu_to_le32(ksmbd_server_side_copy_max_total_size()); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7198 | |
| 7199 | chunks = (struct srv_copychunk *)&ci_req->Chunks[0]; |
| 7200 | chunk_count = le32_to_cpu(ci_req->ChunkCount); |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7201 | if (chunk_count == 0) |
| 7202 | goto out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7203 | total_size_written = 0; |
| 7204 | |
| 7205 | /* verify the SRV_COPYCHUNK_COPY packet */ |
| 7206 | if (chunk_count > ksmbd_server_side_copy_max_chunk_count() || |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7207 | input_count < offsetof(struct copychunk_ioctl_req, Chunks) + |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7208 | chunk_count * sizeof(struct srv_copychunk)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7209 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 7210 | return -EINVAL; |
| 7211 | } |
| 7212 | |
| 7213 | for (i = 0; i < chunk_count; i++) { |
| 7214 | if (le32_to_cpu(chunks[i].Length) == 0 || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7215 | le32_to_cpu(chunks[i].Length) > ksmbd_server_side_copy_max_chunk_size()) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7216 | break; |
| 7217 | total_size_written += le32_to_cpu(chunks[i].Length); |
| 7218 | } |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7219 | |
| 7220 | if (i < chunk_count || |
| 7221 | total_size_written > ksmbd_server_side_copy_max_total_size()) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7222 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 7223 | return -EINVAL; |
| 7224 | } |
| 7225 | |
| 7226 | src_fp = ksmbd_lookup_foreign_fd(work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7227 | le64_to_cpu(ci_req->ResumeKey[0])); |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7228 | dst_fp = ksmbd_lookup_fd_slow(work, volatile_id, persistent_id); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7229 | ret = -EINVAL; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7230 | if (!src_fp || |
| 7231 | src_fp->persistent_id != le64_to_cpu(ci_req->ResumeKey[1])) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7232 | rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND; |
| 7233 | goto out; |
| 7234 | } |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7235 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7236 | if (!dst_fp) { |
| 7237 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 7238 | goto out; |
| 7239 | } |
| 7240 | |
| 7241 | /* |
| 7242 | * FILE_READ_DATA should only be included in |
| 7243 | * the FSCTL_COPYCHUNK case |
| 7244 | */ |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7245 | if (cnt_code == FSCTL_COPYCHUNK && |
| 7246 | !(dst_fp->daccess & (FILE_READ_DATA_LE | FILE_GENERIC_READ_LE))) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7247 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 7248 | goto out; |
| 7249 | } |
| 7250 | |
| 7251 | ret = ksmbd_vfs_copy_file_ranges(work, src_fp, dst_fp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7252 | chunks, chunk_count, |
| 7253 | &chunk_count_written, |
| 7254 | &chunk_size_written, |
| 7255 | &total_size_written); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7256 | if (ret < 0) { |
| 7257 | if (ret == -EACCES) |
| 7258 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 7259 | if (ret == -EAGAIN) |
| 7260 | rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT; |
| 7261 | else if (ret == -EBADF) |
| 7262 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 7263 | else if (ret == -EFBIG || ret == -ENOSPC) |
| 7264 | rsp->hdr.Status = STATUS_DISK_FULL; |
| 7265 | else if (ret == -EINVAL) |
| 7266 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 7267 | else if (ret == -EISDIR) |
| 7268 | rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY; |
| 7269 | else if (ret == -E2BIG) |
| 7270 | rsp->hdr.Status = STATUS_INVALID_VIEW_SIZE; |
| 7271 | else |
| 7272 | rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR; |
| 7273 | } |
| 7274 | |
| 7275 | ci_rsp->ChunksWritten = cpu_to_le32(chunk_count_written); |
| 7276 | ci_rsp->ChunkBytesWritten = cpu_to_le32(chunk_size_written); |
| 7277 | ci_rsp->TotalBytesWritten = cpu_to_le32(total_size_written); |
| 7278 | out: |
| 7279 | ksmbd_fd_put(work, src_fp); |
| 7280 | ksmbd_fd_put(work, dst_fp); |
| 7281 | return ret; |
| 7282 | } |
| 7283 | |
| 7284 | static __be32 idev_ipv4_address(struct in_device *idev) |
| 7285 | { |
| 7286 | __be32 addr = 0; |
| 7287 | |
| 7288 | struct in_ifaddr *ifa; |
| 7289 | |
| 7290 | rcu_read_lock(); |
| 7291 | in_dev_for_each_ifa_rcu(ifa, idev) { |
| 7292 | if (ifa->ifa_flags & IFA_F_SECONDARY) |
| 7293 | continue; |
| 7294 | |
| 7295 | addr = ifa->ifa_address; |
| 7296 | break; |
| 7297 | } |
| 7298 | rcu_read_unlock(); |
| 7299 | return addr; |
| 7300 | } |
| 7301 | |
| 7302 | static int fsctl_query_iface_info_ioctl(struct ksmbd_conn *conn, |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7303 | struct smb2_ioctl_rsp *rsp, |
| 7304 | unsigned int out_buf_len) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7305 | { |
| 7306 | struct network_interface_info_ioctl_rsp *nii_rsp = NULL; |
| 7307 | int nbytes = 0; |
| 7308 | struct net_device *netdev; |
| 7309 | struct sockaddr_storage_rsp *sockaddr_storage; |
| 7310 | unsigned int flags; |
| 7311 | unsigned long long speed; |
| 7312 | |
| 7313 | rtnl_lock(); |
| 7314 | for_each_netdev(&init_net, netdev) { |
Namjae Jeon | 71cd9cb | 2021-12-16 10:31:44 +0900 | [diff] [blame] | 7315 | bool ipv4_set = false; |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7316 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7317 | if (netdev->type == ARPHRD_LOOPBACK) |
| 7318 | continue; |
| 7319 | |
| 7320 | flags = dev_get_flags(netdev); |
| 7321 | if (!(flags & IFF_RUNNING)) |
| 7322 | continue; |
Namjae Jeon | 71cd9cb | 2021-12-16 10:31:44 +0900 | [diff] [blame] | 7323 | ipv6_retry: |
| 7324 | if (out_buf_len < |
| 7325 | nbytes + sizeof(struct network_interface_info_ioctl_rsp)) { |
| 7326 | rtnl_unlock(); |
| 7327 | return -ENOSPC; |
| 7328 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7329 | |
| 7330 | nii_rsp = (struct network_interface_info_ioctl_rsp *) |
| 7331 | &rsp->Buffer[nbytes]; |
| 7332 | nii_rsp->IfIndex = cpu_to_le32(netdev->ifindex); |
| 7333 | |
Hyunchul Lee | 03d8d4f | 2021-07-13 16:09:34 +0900 | [diff] [blame] | 7334 | nii_rsp->Capability = 0; |
Namjae Jeon | a58b45a | 2021-12-16 10:26:43 +0900 | [diff] [blame] | 7335 | if (netdev->real_num_tx_queues > 1) |
| 7336 | nii_rsp->Capability |= cpu_to_le32(RSS_CAPABLE); |
Hyunchul Lee | 03d8d4f | 2021-07-13 16:09:34 +0900 | [diff] [blame] | 7337 | if (ksmbd_rdma_capable_netdev(netdev)) |
| 7338 | nii_rsp->Capability |= cpu_to_le32(RDMA_CAPABLE); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7339 | |
| 7340 | nii_rsp->Next = cpu_to_le32(152); |
| 7341 | nii_rsp->Reserved = 0; |
| 7342 | |
| 7343 | if (netdev->ethtool_ops->get_link_ksettings) { |
| 7344 | struct ethtool_link_ksettings cmd; |
| 7345 | |
| 7346 | netdev->ethtool_ops->get_link_ksettings(netdev, &cmd); |
| 7347 | speed = cmd.base.speed; |
| 7348 | } else { |
Per Forlin | d475866 | 2021-08-30 13:23:04 +0900 | [diff] [blame] | 7349 | ksmbd_debug(SMB, "%s %s\n", netdev->name, |
| 7350 | "speed is unknown, defaulting to 1Gb/sec"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7351 | speed = SPEED_1000; |
| 7352 | } |
| 7353 | |
| 7354 | speed *= 1000000; |
| 7355 | nii_rsp->LinkSpeed = cpu_to_le64(speed); |
| 7356 | |
| 7357 | sockaddr_storage = (struct sockaddr_storage_rsp *) |
| 7358 | nii_rsp->SockAddr_Storage; |
| 7359 | memset(sockaddr_storage, 0, 128); |
| 7360 | |
Namjae Jeon | 71cd9cb | 2021-12-16 10:31:44 +0900 | [diff] [blame] | 7361 | if (!ipv4_set) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7362 | struct in_device *idev; |
| 7363 | |
| 7364 | sockaddr_storage->Family = cpu_to_le16(INTERNETWORK); |
| 7365 | sockaddr_storage->addr4.Port = 0; |
| 7366 | |
| 7367 | idev = __in_dev_get_rtnl(netdev); |
| 7368 | if (!idev) |
| 7369 | continue; |
| 7370 | sockaddr_storage->addr4.IPv4address = |
| 7371 | idev_ipv4_address(idev); |
Namjae Jeon | 71cd9cb | 2021-12-16 10:31:44 +0900 | [diff] [blame] | 7372 | nbytes += sizeof(struct network_interface_info_ioctl_rsp); |
| 7373 | ipv4_set = true; |
| 7374 | goto ipv6_retry; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7375 | } else { |
| 7376 | struct inet6_dev *idev6; |
| 7377 | struct inet6_ifaddr *ifa; |
| 7378 | __u8 *ipv6_addr = sockaddr_storage->addr6.IPv6address; |
| 7379 | |
| 7380 | sockaddr_storage->Family = cpu_to_le16(INTERNETWORKV6); |
| 7381 | sockaddr_storage->addr6.Port = 0; |
| 7382 | sockaddr_storage->addr6.FlowInfo = 0; |
| 7383 | |
| 7384 | idev6 = __in6_dev_get(netdev); |
| 7385 | if (!idev6) |
| 7386 | continue; |
| 7387 | |
| 7388 | list_for_each_entry(ifa, &idev6->addr_list, if_list) { |
| 7389 | if (ifa->flags & (IFA_F_TENTATIVE | |
| 7390 | IFA_F_DEPRECATED)) |
| 7391 | continue; |
| 7392 | memcpy(ipv6_addr, ifa->addr.s6_addr, 16); |
| 7393 | break; |
| 7394 | } |
| 7395 | sockaddr_storage->addr6.ScopeId = 0; |
Namjae Jeon | 71cd9cb | 2021-12-16 10:31:44 +0900 | [diff] [blame] | 7396 | nbytes += sizeof(struct network_interface_info_ioctl_rsp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7397 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7398 | } |
| 7399 | rtnl_unlock(); |
| 7400 | |
| 7401 | /* zero if this is last one */ |
| 7402 | if (nii_rsp) |
| 7403 | nii_rsp->Next = 0; |
| 7404 | |
Paulo Alcantara | 2d004c6 | 2022-03-21 13:08:26 -0300 | [diff] [blame] | 7405 | rsp->PersistentFileId = SMB2_NO_FID; |
| 7406 | rsp->VolatileFileId = SMB2_NO_FID; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7407 | return nbytes; |
| 7408 | } |
| 7409 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7410 | static int fsctl_validate_negotiate_info(struct ksmbd_conn *conn, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7411 | struct validate_negotiate_info_req *neg_req, |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7412 | struct validate_negotiate_info_rsp *neg_rsp, |
| 7413 | unsigned int in_buf_len) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7414 | { |
| 7415 | int ret = 0; |
| 7416 | int dialect; |
| 7417 | |
Marios Makassikis | 78f1688 | 2021-10-28 21:01:27 +0200 | [diff] [blame] | 7418 | if (in_buf_len < offsetof(struct validate_negotiate_info_req, Dialects) + |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7419 | le16_to_cpu(neg_req->DialectCount) * sizeof(__le16)) |
| 7420 | return -EINVAL; |
| 7421 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7422 | dialect = ksmbd_lookup_dialect_by_id(neg_req->Dialects, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7423 | neg_req->DialectCount); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7424 | if (dialect == BAD_PROT_ID || dialect != conn->dialect) { |
| 7425 | ret = -EINVAL; |
| 7426 | goto err_out; |
| 7427 | } |
| 7428 | |
| 7429 | if (strncmp(neg_req->Guid, conn->ClientGUID, SMB2_CLIENT_GUID_SIZE)) { |
| 7430 | ret = -EINVAL; |
| 7431 | goto err_out; |
| 7432 | } |
| 7433 | |
| 7434 | if (le16_to_cpu(neg_req->SecurityMode) != conn->cli_sec_mode) { |
| 7435 | ret = -EINVAL; |
| 7436 | goto err_out; |
| 7437 | } |
| 7438 | |
| 7439 | if (le32_to_cpu(neg_req->Capabilities) != conn->cli_cap) { |
| 7440 | ret = -EINVAL; |
| 7441 | goto err_out; |
| 7442 | } |
| 7443 | |
| 7444 | neg_rsp->Capabilities = cpu_to_le32(conn->vals->capabilities); |
| 7445 | memset(neg_rsp->Guid, 0, SMB2_CLIENT_GUID_SIZE); |
| 7446 | neg_rsp->SecurityMode = cpu_to_le16(conn->srv_sec_mode); |
| 7447 | neg_rsp->Dialect = cpu_to_le16(conn->dialect); |
| 7448 | err_out: |
| 7449 | return ret; |
| 7450 | } |
| 7451 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7452 | static int fsctl_query_allocated_ranges(struct ksmbd_work *work, u64 id, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7453 | struct file_allocated_range_buffer *qar_req, |
| 7454 | struct file_allocated_range_buffer *qar_rsp, |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7455 | unsigned int in_count, unsigned int *out_count) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7456 | { |
| 7457 | struct ksmbd_file *fp; |
| 7458 | loff_t start, length; |
| 7459 | int ret = 0; |
| 7460 | |
| 7461 | *out_count = 0; |
| 7462 | if (in_count == 0) |
| 7463 | return -EINVAL; |
| 7464 | |
| 7465 | fp = ksmbd_lookup_fd_fast(work, id); |
| 7466 | if (!fp) |
| 7467 | return -ENOENT; |
| 7468 | |
| 7469 | start = le64_to_cpu(qar_req->file_offset); |
| 7470 | length = le64_to_cpu(qar_req->length); |
| 7471 | |
| 7472 | ret = ksmbd_vfs_fqar_lseek(fp, start, length, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7473 | qar_rsp, in_count, out_count); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7474 | if (ret && ret != -E2BIG) |
| 7475 | *out_count = 0; |
| 7476 | |
| 7477 | ksmbd_fd_put(work, fp); |
| 7478 | return ret; |
| 7479 | } |
| 7480 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7481 | static int fsctl_pipe_transceive(struct ksmbd_work *work, u64 id, |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7482 | unsigned int out_buf_len, |
| 7483 | struct smb2_ioctl_req *req, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7484 | struct smb2_ioctl_rsp *rsp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7485 | { |
| 7486 | struct ksmbd_rpc_command *rpc_resp; |
| 7487 | char *data_buf = (char *)&req->Buffer[0]; |
| 7488 | int nbytes = 0; |
| 7489 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7490 | rpc_resp = ksmbd_rpc_ioctl(work->sess, id, data_buf, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7491 | le32_to_cpu(req->InputCount)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7492 | if (rpc_resp) { |
| 7493 | if (rpc_resp->flags == KSMBD_RPC_SOME_NOT_MAPPED) { |
| 7494 | /* |
| 7495 | * set STATUS_SOME_NOT_MAPPED response |
| 7496 | * for unknown domain sid. |
| 7497 | */ |
| 7498 | rsp->hdr.Status = STATUS_SOME_NOT_MAPPED; |
| 7499 | } else if (rpc_resp->flags == KSMBD_RPC_ENOTIMPLEMENTED) { |
| 7500 | rsp->hdr.Status = STATUS_NOT_SUPPORTED; |
| 7501 | goto out; |
| 7502 | } else if (rpc_resp->flags != KSMBD_RPC_OK) { |
| 7503 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 7504 | goto out; |
| 7505 | } |
| 7506 | |
| 7507 | nbytes = rpc_resp->payload_sz; |
| 7508 | if (rpc_resp->payload_sz > out_buf_len) { |
| 7509 | rsp->hdr.Status = STATUS_BUFFER_OVERFLOW; |
| 7510 | nbytes = out_buf_len; |
| 7511 | } |
| 7512 | |
| 7513 | if (!rpc_resp->payload_sz) { |
| 7514 | rsp->hdr.Status = |
| 7515 | STATUS_UNEXPECTED_IO_ERROR; |
| 7516 | goto out; |
| 7517 | } |
| 7518 | |
| 7519 | memcpy((char *)rsp->Buffer, rpc_resp->payload, nbytes); |
| 7520 | } |
| 7521 | out: |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 7522 | kvfree(rpc_resp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7523 | return nbytes; |
| 7524 | } |
| 7525 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7526 | static inline int fsctl_set_sparse(struct ksmbd_work *work, u64 id, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7527 | struct file_sparse *sparse) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7528 | { |
| 7529 | struct ksmbd_file *fp; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 7530 | struct user_namespace *user_ns; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7531 | int ret = 0; |
| 7532 | __le32 old_fattr; |
| 7533 | |
| 7534 | fp = ksmbd_lookup_fd_fast(work, id); |
| 7535 | if (!fp) |
| 7536 | return -ENOENT; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 7537 | user_ns = file_mnt_user_ns(fp->filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7538 | |
| 7539 | old_fattr = fp->f_ci->m_fattr; |
| 7540 | if (sparse->SetSparse) |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 7541 | fp->f_ci->m_fattr |= FILE_ATTRIBUTE_SPARSE_FILE_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7542 | else |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 7543 | fp->f_ci->m_fattr &= ~FILE_ATTRIBUTE_SPARSE_FILE_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7544 | |
| 7545 | if (fp->f_ci->m_fattr != old_fattr && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7546 | test_share_config_flag(work->tcon->share_conf, |
| 7547 | KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7548 | struct xattr_dos_attrib da; |
| 7549 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 7550 | ret = ksmbd_vfs_get_dos_attrib_xattr(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 7551 | fp->filp->f_path.dentry, &da); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7552 | if (ret <= 0) |
| 7553 | goto out; |
| 7554 | |
| 7555 | da.attr = le32_to_cpu(fp->f_ci->m_fattr); |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 7556 | ret = ksmbd_vfs_set_dos_attrib_xattr(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 7557 | fp->filp->f_path.dentry, &da); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7558 | if (ret) |
| 7559 | fp->f_ci->m_fattr = old_fattr; |
| 7560 | } |
| 7561 | |
| 7562 | out: |
| 7563 | ksmbd_fd_put(work, fp); |
| 7564 | return ret; |
| 7565 | } |
| 7566 | |
| 7567 | static int fsctl_request_resume_key(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7568 | struct smb2_ioctl_req *req, |
| 7569 | struct resume_key_ioctl_rsp *key_rsp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7570 | { |
| 7571 | struct ksmbd_file *fp; |
| 7572 | |
Paulo Alcantara | 2d004c6 | 2022-03-21 13:08:26 -0300 | [diff] [blame] | 7573 | fp = ksmbd_lookup_fd_slow(work, req->VolatileFileId, req->PersistentFileId); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7574 | if (!fp) |
| 7575 | return -ENOENT; |
| 7576 | |
| 7577 | memset(key_rsp, 0, sizeof(*key_rsp)); |
| 7578 | key_rsp->ResumeKey[0] = req->VolatileFileId; |
| 7579 | key_rsp->ResumeKey[1] = req->PersistentFileId; |
| 7580 | ksmbd_fd_put(work, fp); |
| 7581 | |
| 7582 | return 0; |
| 7583 | } |
| 7584 | |
| 7585 | /** |
| 7586 | * smb2_ioctl() - handler for smb2 ioctl command |
| 7587 | * @work: smb work containing ioctl command buffer |
| 7588 | * |
| 7589 | * Return: 0 on success, otherwise error |
| 7590 | */ |
| 7591 | int smb2_ioctl(struct ksmbd_work *work) |
| 7592 | { |
| 7593 | struct smb2_ioctl_req *req; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 7594 | struct smb2_ioctl_rsp *rsp; |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7595 | unsigned int cnt_code, nbytes = 0, out_buf_len, in_buf_len; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7596 | u64 id = KSMBD_NO_FID; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7597 | struct ksmbd_conn *conn = work->conn; |
| 7598 | int ret = 0; |
| 7599 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7600 | if (work->next_smb2_rcv_hdr_off) { |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 7601 | req = ksmbd_req_buf_next(work); |
| 7602 | rsp = ksmbd_resp_buf_next(work); |
Paulo Alcantara | 2d004c6 | 2022-03-21 13:08:26 -0300 | [diff] [blame] | 7603 | if (!has_file_id(req->VolatileFileId)) { |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 7604 | ksmbd_debug(SMB, "Compound request set FID = %llu\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7605 | work->compound_fid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7606 | id = work->compound_fid; |
| 7607 | } |
| 7608 | } else { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 7609 | req = smb2_get_msg(work->request_buf); |
| 7610 | rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7611 | } |
| 7612 | |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 7613 | if (!has_file_id(id)) |
Paulo Alcantara | 2d004c6 | 2022-03-21 13:08:26 -0300 | [diff] [blame] | 7614 | id = req->VolatileFileId; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7615 | |
| 7616 | if (req->Flags != cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL)) { |
| 7617 | rsp->hdr.Status = STATUS_NOT_SUPPORTED; |
| 7618 | goto out; |
| 7619 | } |
| 7620 | |
Steve French | 15e7b6d | 2022-03-26 12:47:57 -0500 | [diff] [blame] | 7621 | cnt_code = le32_to_cpu(req->CtlCode); |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 7622 | ret = smb2_calc_max_out_buf_len(work, 48, |
| 7623 | le32_to_cpu(req->MaxOutputResponse)); |
| 7624 | if (ret < 0) { |
| 7625 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 7626 | goto out; |
| 7627 | } |
| 7628 | out_buf_len = (unsigned int)ret; |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7629 | in_buf_len = le32_to_cpu(req->InputCount); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7630 | |
| 7631 | switch (cnt_code) { |
| 7632 | case FSCTL_DFS_GET_REFERRALS: |
| 7633 | case FSCTL_DFS_GET_REFERRALS_EX: |
| 7634 | /* Not support DFS yet */ |
| 7635 | rsp->hdr.Status = STATUS_FS_DRIVER_REQUIRED; |
| 7636 | goto out; |
| 7637 | case FSCTL_CREATE_OR_GET_OBJECT_ID: |
| 7638 | { |
| 7639 | struct file_object_buf_type1_ioctl_rsp *obj_buf; |
| 7640 | |
| 7641 | nbytes = sizeof(struct file_object_buf_type1_ioctl_rsp); |
| 7642 | obj_buf = (struct file_object_buf_type1_ioctl_rsp *) |
| 7643 | &rsp->Buffer[0]; |
| 7644 | |
| 7645 | /* |
| 7646 | * TODO: This is dummy implementation to pass smbtorture |
| 7647 | * Need to check correct response later |
| 7648 | */ |
| 7649 | memset(obj_buf->ObjectId, 0x0, 16); |
| 7650 | memset(obj_buf->BirthVolumeId, 0x0, 16); |
| 7651 | memset(obj_buf->BirthObjectId, 0x0, 16); |
| 7652 | memset(obj_buf->DomainId, 0x0, 16); |
| 7653 | |
| 7654 | break; |
| 7655 | } |
| 7656 | case FSCTL_PIPE_TRANSCEIVE: |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7657 | out_buf_len = min_t(u32, KSMBD_IPC_MAX_PAYLOAD, out_buf_len); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7658 | nbytes = fsctl_pipe_transceive(work, id, out_buf_len, req, rsp); |
| 7659 | break; |
| 7660 | case FSCTL_VALIDATE_NEGOTIATE_INFO: |
| 7661 | if (conn->dialect < SMB30_PROT_ID) { |
| 7662 | ret = -EOPNOTSUPP; |
| 7663 | goto out; |
| 7664 | } |
| 7665 | |
Zhang Xiaoxu | b1763d2 | 2022-09-26 11:36:30 +0800 | [diff] [blame] | 7666 | if (in_buf_len < offsetof(struct validate_negotiate_info_req, |
| 7667 | Dialects)) { |
| 7668 | ret = -EINVAL; |
| 7669 | goto out; |
| 7670 | } |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7671 | |
Zhang Xiaoxu | b1763d2 | 2022-09-26 11:36:30 +0800 | [diff] [blame] | 7672 | if (out_buf_len < sizeof(struct validate_negotiate_info_rsp)) { |
| 7673 | ret = -EINVAL; |
| 7674 | goto out; |
| 7675 | } |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7676 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7677 | ret = fsctl_validate_negotiate_info(conn, |
| 7678 | (struct validate_negotiate_info_req *)&req->Buffer[0], |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7679 | (struct validate_negotiate_info_rsp *)&rsp->Buffer[0], |
| 7680 | in_buf_len); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7681 | if (ret < 0) |
| 7682 | goto out; |
| 7683 | |
| 7684 | nbytes = sizeof(struct validate_negotiate_info_rsp); |
Paulo Alcantara | 2d004c6 | 2022-03-21 13:08:26 -0300 | [diff] [blame] | 7685 | rsp->PersistentFileId = SMB2_NO_FID; |
| 7686 | rsp->VolatileFileId = SMB2_NO_FID; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7687 | break; |
| 7688 | case FSCTL_QUERY_NETWORK_INTERFACE_INFO: |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7689 | ret = fsctl_query_iface_info_ioctl(conn, rsp, out_buf_len); |
| 7690 | if (ret < 0) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7691 | goto out; |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7692 | nbytes = ret; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7693 | break; |
| 7694 | case FSCTL_REQUEST_RESUME_KEY: |
| 7695 | if (out_buf_len < sizeof(struct resume_key_ioctl_rsp)) { |
| 7696 | ret = -EINVAL; |
| 7697 | goto out; |
| 7698 | } |
| 7699 | |
| 7700 | ret = fsctl_request_resume_key(work, req, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7701 | (struct resume_key_ioctl_rsp *)&rsp->Buffer[0]); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7702 | if (ret < 0) |
| 7703 | goto out; |
| 7704 | rsp->PersistentFileId = req->PersistentFileId; |
| 7705 | rsp->VolatileFileId = req->VolatileFileId; |
| 7706 | nbytes = sizeof(struct resume_key_ioctl_rsp); |
| 7707 | break; |
| 7708 | case FSCTL_COPYCHUNK: |
| 7709 | case FSCTL_COPYCHUNK_WRITE: |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7710 | if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7711 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7712 | "User does not have write permission\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7713 | ret = -EACCES; |
| 7714 | goto out; |
| 7715 | } |
| 7716 | |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7717 | if (in_buf_len < sizeof(struct copychunk_ioctl_req)) { |
| 7718 | ret = -EINVAL; |
| 7719 | goto out; |
| 7720 | } |
| 7721 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7722 | if (out_buf_len < sizeof(struct copychunk_ioctl_rsp)) { |
| 7723 | ret = -EINVAL; |
| 7724 | goto out; |
| 7725 | } |
| 7726 | |
| 7727 | nbytes = sizeof(struct copychunk_ioctl_rsp); |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7728 | rsp->VolatileFileId = req->VolatileFileId; |
| 7729 | rsp->PersistentFileId = req->PersistentFileId; |
| 7730 | fsctl_copychunk(work, |
| 7731 | (struct copychunk_ioctl_req *)&req->Buffer[0], |
Steve French | 15e7b6d | 2022-03-26 12:47:57 -0500 | [diff] [blame] | 7732 | le32_to_cpu(req->CtlCode), |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7733 | le32_to_cpu(req->InputCount), |
Paulo Alcantara | 2d004c6 | 2022-03-21 13:08:26 -0300 | [diff] [blame] | 7734 | req->VolatileFileId, |
| 7735 | req->PersistentFileId, |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7736 | rsp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7737 | break; |
| 7738 | case FSCTL_SET_SPARSE: |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7739 | if (in_buf_len < sizeof(struct file_sparse)) { |
| 7740 | ret = -EINVAL; |
| 7741 | goto out; |
| 7742 | } |
| 7743 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7744 | ret = fsctl_set_sparse(work, id, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7745 | (struct file_sparse *)&req->Buffer[0]); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7746 | if (ret < 0) |
| 7747 | goto out; |
| 7748 | break; |
| 7749 | case FSCTL_SET_ZERO_DATA: |
| 7750 | { |
| 7751 | struct file_zero_data_information *zero_data; |
| 7752 | struct ksmbd_file *fp; |
Namjae Jeon | b5e5f9d | 2022-06-19 22:37:17 +0900 | [diff] [blame] | 7753 | loff_t off, len, bfz; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7754 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7755 | if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7756 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7757 | "User does not have write permission\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7758 | ret = -EACCES; |
| 7759 | goto out; |
| 7760 | } |
| 7761 | |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7762 | if (in_buf_len < sizeof(struct file_zero_data_information)) { |
| 7763 | ret = -EINVAL; |
| 7764 | goto out; |
| 7765 | } |
| 7766 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7767 | zero_data = |
| 7768 | (struct file_zero_data_information *)&req->Buffer[0]; |
| 7769 | |
Namjae Jeon | b5e5f9d | 2022-06-19 22:37:17 +0900 | [diff] [blame] | 7770 | off = le64_to_cpu(zero_data->FileOffset); |
| 7771 | bfz = le64_to_cpu(zero_data->BeyondFinalZero); |
| 7772 | if (off > bfz) { |
| 7773 | ret = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7774 | goto out; |
| 7775 | } |
| 7776 | |
Namjae Jeon | b5e5f9d | 2022-06-19 22:37:17 +0900 | [diff] [blame] | 7777 | len = bfz - off; |
| 7778 | if (len) { |
| 7779 | fp = ksmbd_lookup_fd_fast(work, id); |
| 7780 | if (!fp) { |
| 7781 | ret = -ENOENT; |
| 7782 | goto out; |
| 7783 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7784 | |
Namjae Jeon | b5e5f9d | 2022-06-19 22:37:17 +0900 | [diff] [blame] | 7785 | ret = ksmbd_vfs_zero_data(work, fp, off, len); |
| 7786 | ksmbd_fd_put(work, fp); |
| 7787 | if (ret < 0) |
| 7788 | goto out; |
| 7789 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7790 | break; |
| 7791 | } |
| 7792 | case FSCTL_QUERY_ALLOCATED_RANGES: |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7793 | if (in_buf_len < sizeof(struct file_allocated_range_buffer)) { |
| 7794 | ret = -EINVAL; |
| 7795 | goto out; |
| 7796 | } |
| 7797 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7798 | ret = fsctl_query_allocated_ranges(work, id, |
| 7799 | (struct file_allocated_range_buffer *)&req->Buffer[0], |
| 7800 | (struct file_allocated_range_buffer *)&rsp->Buffer[0], |
| 7801 | out_buf_len / |
| 7802 | sizeof(struct file_allocated_range_buffer), &nbytes); |
| 7803 | if (ret == -E2BIG) { |
| 7804 | rsp->hdr.Status = STATUS_BUFFER_OVERFLOW; |
| 7805 | } else if (ret < 0) { |
| 7806 | nbytes = 0; |
| 7807 | goto out; |
| 7808 | } |
| 7809 | |
| 7810 | nbytes *= sizeof(struct file_allocated_range_buffer); |
| 7811 | break; |
| 7812 | case FSCTL_GET_REPARSE_POINT: |
| 7813 | { |
| 7814 | struct reparse_data_buffer *reparse_ptr; |
| 7815 | struct ksmbd_file *fp; |
| 7816 | |
| 7817 | reparse_ptr = (struct reparse_data_buffer *)&rsp->Buffer[0]; |
| 7818 | fp = ksmbd_lookup_fd_fast(work, id); |
| 7819 | if (!fp) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 7820 | pr_err("not found fp!!\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7821 | ret = -ENOENT; |
| 7822 | goto out; |
| 7823 | } |
| 7824 | |
| 7825 | reparse_ptr->ReparseTag = |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 7826 | smb2_get_reparse_tag_special_file(file_inode(fp->filp)->i_mode); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7827 | reparse_ptr->ReparseDataLength = 0; |
| 7828 | ksmbd_fd_put(work, fp); |
| 7829 | nbytes = sizeof(struct reparse_data_buffer); |
| 7830 | break; |
| 7831 | } |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 7832 | case FSCTL_DUPLICATE_EXTENTS_TO_FILE: |
| 7833 | { |
| 7834 | struct ksmbd_file *fp_in, *fp_out = NULL; |
| 7835 | struct duplicate_extents_to_file *dup_ext; |
| 7836 | loff_t src_off, dst_off, length, cloned; |
| 7837 | |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7838 | if (in_buf_len < sizeof(struct duplicate_extents_to_file)) { |
| 7839 | ret = -EINVAL; |
| 7840 | goto out; |
| 7841 | } |
| 7842 | |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 7843 | dup_ext = (struct duplicate_extents_to_file *)&req->Buffer[0]; |
| 7844 | |
| 7845 | fp_in = ksmbd_lookup_fd_slow(work, dup_ext->VolatileFileHandle, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7846 | dup_ext->PersistentFileHandle); |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 7847 | if (!fp_in) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 7848 | pr_err("not found file handle in duplicate extent to file\n"); |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 7849 | ret = -ENOENT; |
| 7850 | goto out; |
| 7851 | } |
| 7852 | |
| 7853 | fp_out = ksmbd_lookup_fd_fast(work, id); |
| 7854 | if (!fp_out) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 7855 | pr_err("not found fp\n"); |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 7856 | ret = -ENOENT; |
| 7857 | goto dup_ext_out; |
| 7858 | } |
| 7859 | |
| 7860 | src_off = le64_to_cpu(dup_ext->SourceFileOffset); |
| 7861 | dst_off = le64_to_cpu(dup_ext->TargetFileOffset); |
| 7862 | length = le64_to_cpu(dup_ext->ByteCount); |
Amir Goldstein | 868f9f2 | 2022-06-30 22:58:49 +0300 | [diff] [blame] | 7863 | /* |
| 7864 | * XXX: It is not clear if FSCTL_DUPLICATE_EXTENTS_TO_FILE |
| 7865 | * should fall back to vfs_copy_file_range(). This could be |
| 7866 | * beneficial when re-exporting nfs/smb mount, but note that |
| 7867 | * this can result in partial copy that returns an error status. |
| 7868 | * If/when FSCTL_DUPLICATE_EXTENTS_TO_FILE_EX is implemented, |
| 7869 | * fall back to vfs_copy_file_range(), should be avoided when |
| 7870 | * the flag DUPLICATE_EXTENTS_DATA_EX_SOURCE_ATOMIC is set. |
| 7871 | */ |
| 7872 | cloned = vfs_clone_file_range(fp_in->filp, src_off, |
| 7873 | fp_out->filp, dst_off, length, 0); |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 7874 | if (cloned == -EXDEV || cloned == -EOPNOTSUPP) { |
| 7875 | ret = -EOPNOTSUPP; |
| 7876 | goto dup_ext_out; |
| 7877 | } else if (cloned != length) { |
Namjae Jeon | f852477 | 2021-06-18 10:28:00 +0900 | [diff] [blame] | 7878 | cloned = vfs_copy_file_range(fp_in->filp, src_off, |
Amir Goldstein | 868f9f2 | 2022-06-30 22:58:49 +0300 | [diff] [blame] | 7879 | fp_out->filp, dst_off, |
| 7880 | length, 0); |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 7881 | if (cloned != length) { |
| 7882 | if (cloned < 0) |
| 7883 | ret = cloned; |
| 7884 | else |
| 7885 | ret = -EINVAL; |
| 7886 | } |
| 7887 | } |
| 7888 | |
| 7889 | dup_ext_out: |
| 7890 | ksmbd_fd_put(work, fp_in); |
| 7891 | ksmbd_fd_put(work, fp_out); |
| 7892 | if (ret < 0) |
| 7893 | goto out; |
| 7894 | break; |
| 7895 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7896 | default: |
| 7897 | ksmbd_debug(SMB, "not implemented yet ioctl command 0x%x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7898 | cnt_code); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7899 | ret = -EOPNOTSUPP; |
| 7900 | goto out; |
| 7901 | } |
| 7902 | |
Steve French | 15e7b6d | 2022-03-26 12:47:57 -0500 | [diff] [blame] | 7903 | rsp->CtlCode = cpu_to_le32(cnt_code); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7904 | rsp->InputCount = cpu_to_le32(0); |
| 7905 | rsp->InputOffset = cpu_to_le32(112); |
| 7906 | rsp->OutputOffset = cpu_to_le32(112); |
| 7907 | rsp->OutputCount = cpu_to_le32(nbytes); |
| 7908 | rsp->StructureSize = cpu_to_le16(49); |
| 7909 | rsp->Reserved = cpu_to_le16(0); |
| 7910 | rsp->Flags = cpu_to_le32(0); |
| 7911 | rsp->Reserved2 = cpu_to_le32(0); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 7912 | inc_rfc1001_len(work->response_buf, 48 + nbytes); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7913 | |
| 7914 | return 0; |
| 7915 | |
| 7916 | out: |
| 7917 | if (ret == -EACCES) |
| 7918 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 7919 | else if (ret == -ENOENT) |
| 7920 | rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND; |
| 7921 | else if (ret == -EOPNOTSUPP) |
| 7922 | rsp->hdr.Status = STATUS_NOT_SUPPORTED; |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7923 | else if (ret == -ENOSPC) |
| 7924 | rsp->hdr.Status = STATUS_BUFFER_TOO_SMALL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7925 | else if (ret < 0 || rsp->hdr.Status == 0) |
| 7926 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 7927 | smb2_set_err_rsp(work); |
| 7928 | return 0; |
| 7929 | } |
| 7930 | |
| 7931 | /** |
| 7932 | * smb20_oplock_break_ack() - handler for smb2.0 oplock break command |
| 7933 | * @work: smb work containing oplock break command buffer |
| 7934 | * |
| 7935 | * Return: 0 |
| 7936 | */ |
| 7937 | static void smb20_oplock_break_ack(struct ksmbd_work *work) |
| 7938 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 7939 | struct smb2_oplock_break *req = smb2_get_msg(work->request_buf); |
| 7940 | struct smb2_oplock_break *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7941 | struct ksmbd_file *fp; |
| 7942 | struct oplock_info *opinfo = NULL; |
| 7943 | __le32 err = 0; |
| 7944 | int ret = 0; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7945 | u64 volatile_id, persistent_id; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7946 | char req_oplevel = 0, rsp_oplevel = 0; |
| 7947 | unsigned int oplock_change_type; |
| 7948 | |
Steve French | c7803b0 | 2022-03-28 17:45:55 -0500 | [diff] [blame] | 7949 | volatile_id = req->VolatileFid; |
| 7950 | persistent_id = req->PersistentFid; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7951 | req_oplevel = req->OplockLevel; |
| 7952 | ksmbd_debug(OPLOCK, "v_id %llu, p_id %llu request oplock level %d\n", |
| 7953 | volatile_id, persistent_id, req_oplevel); |
| 7954 | |
| 7955 | fp = ksmbd_lookup_fd_slow(work, volatile_id, persistent_id); |
| 7956 | if (!fp) { |
| 7957 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 7958 | smb2_set_err_rsp(work); |
| 7959 | return; |
| 7960 | } |
| 7961 | |
| 7962 | opinfo = opinfo_get(fp); |
| 7963 | if (!opinfo) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 7964 | pr_err("unexpected null oplock_info\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7965 | rsp->hdr.Status = STATUS_INVALID_OPLOCK_PROTOCOL; |
| 7966 | smb2_set_err_rsp(work); |
| 7967 | ksmbd_fd_put(work, fp); |
| 7968 | return; |
| 7969 | } |
| 7970 | |
| 7971 | if (opinfo->level == SMB2_OPLOCK_LEVEL_NONE) { |
| 7972 | rsp->hdr.Status = STATUS_INVALID_OPLOCK_PROTOCOL; |
| 7973 | goto err_out; |
| 7974 | } |
| 7975 | |
| 7976 | if (opinfo->op_state == OPLOCK_STATE_NONE) { |
| 7977 | ksmbd_debug(SMB, "unexpected oplock state 0x%x\n", opinfo->op_state); |
| 7978 | rsp->hdr.Status = STATUS_UNSUCCESSFUL; |
| 7979 | goto err_out; |
| 7980 | } |
| 7981 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7982 | if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE || |
| 7983 | opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) && |
| 7984 | (req_oplevel != SMB2_OPLOCK_LEVEL_II && |
| 7985 | req_oplevel != SMB2_OPLOCK_LEVEL_NONE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7986 | err = STATUS_INVALID_OPLOCK_PROTOCOL; |
| 7987 | oplock_change_type = OPLOCK_WRITE_TO_NONE; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7988 | } else if (opinfo->level == SMB2_OPLOCK_LEVEL_II && |
| 7989 | req_oplevel != SMB2_OPLOCK_LEVEL_NONE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7990 | err = STATUS_INVALID_OPLOCK_PROTOCOL; |
| 7991 | oplock_change_type = OPLOCK_READ_TO_NONE; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7992 | } else if (req_oplevel == SMB2_OPLOCK_LEVEL_II || |
| 7993 | req_oplevel == SMB2_OPLOCK_LEVEL_NONE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7994 | err = STATUS_INVALID_DEVICE_STATE; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7995 | if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE || |
| 7996 | opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) && |
| 7997 | req_oplevel == SMB2_OPLOCK_LEVEL_II) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7998 | oplock_change_type = OPLOCK_WRITE_TO_READ; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7999 | } else if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE || |
| 8000 | opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) && |
| 8001 | req_oplevel == SMB2_OPLOCK_LEVEL_NONE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8002 | oplock_change_type = OPLOCK_WRITE_TO_NONE; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 8003 | } else if (opinfo->level == SMB2_OPLOCK_LEVEL_II && |
| 8004 | req_oplevel == SMB2_OPLOCK_LEVEL_NONE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8005 | oplock_change_type = OPLOCK_READ_TO_NONE; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 8006 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8007 | oplock_change_type = 0; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 8008 | } |
| 8009 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8010 | oplock_change_type = 0; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 8011 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8012 | |
| 8013 | switch (oplock_change_type) { |
| 8014 | case OPLOCK_WRITE_TO_READ: |
| 8015 | ret = opinfo_write_to_read(opinfo); |
| 8016 | rsp_oplevel = SMB2_OPLOCK_LEVEL_II; |
| 8017 | break; |
| 8018 | case OPLOCK_WRITE_TO_NONE: |
| 8019 | ret = opinfo_write_to_none(opinfo); |
| 8020 | rsp_oplevel = SMB2_OPLOCK_LEVEL_NONE; |
| 8021 | break; |
| 8022 | case OPLOCK_READ_TO_NONE: |
| 8023 | ret = opinfo_read_to_none(opinfo); |
| 8024 | rsp_oplevel = SMB2_OPLOCK_LEVEL_NONE; |
| 8025 | break; |
| 8026 | default: |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 8027 | pr_err("unknown oplock change 0x%x -> 0x%x\n", |
| 8028 | opinfo->level, rsp_oplevel); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8029 | } |
| 8030 | |
| 8031 | if (ret < 0) { |
| 8032 | rsp->hdr.Status = err; |
| 8033 | goto err_out; |
| 8034 | } |
| 8035 | |
| 8036 | opinfo_put(opinfo); |
| 8037 | ksmbd_fd_put(work, fp); |
| 8038 | opinfo->op_state = OPLOCK_STATE_NONE; |
| 8039 | wake_up_interruptible_all(&opinfo->oplock_q); |
| 8040 | |
| 8041 | rsp->StructureSize = cpu_to_le16(24); |
| 8042 | rsp->OplockLevel = rsp_oplevel; |
| 8043 | rsp->Reserved = 0; |
| 8044 | rsp->Reserved2 = 0; |
Steve French | c7803b0 | 2022-03-28 17:45:55 -0500 | [diff] [blame] | 8045 | rsp->VolatileFid = volatile_id; |
| 8046 | rsp->PersistentFid = persistent_id; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8047 | inc_rfc1001_len(work->response_buf, 24); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8048 | return; |
| 8049 | |
| 8050 | err_out: |
| 8051 | opinfo->op_state = OPLOCK_STATE_NONE; |
| 8052 | wake_up_interruptible_all(&opinfo->oplock_q); |
| 8053 | |
| 8054 | opinfo_put(opinfo); |
| 8055 | ksmbd_fd_put(work, fp); |
| 8056 | smb2_set_err_rsp(work); |
| 8057 | } |
| 8058 | |
| 8059 | static int check_lease_state(struct lease *lease, __le32 req_state) |
| 8060 | { |
| 8061 | if ((lease->new_state == |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 8062 | (SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_HANDLE_CACHING_LE)) && |
| 8063 | !(req_state & SMB2_LEASE_WRITE_CACHING_LE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8064 | lease->new_state = req_state; |
| 8065 | return 0; |
| 8066 | } |
| 8067 | |
| 8068 | if (lease->new_state == req_state) |
| 8069 | return 0; |
| 8070 | |
| 8071 | return 1; |
| 8072 | } |
| 8073 | |
| 8074 | /** |
| 8075 | * smb21_lease_break_ack() - handler for smb2.1 lease break command |
| 8076 | * @work: smb work containing lease break command buffer |
| 8077 | * |
| 8078 | * Return: 0 |
| 8079 | */ |
| 8080 | static void smb21_lease_break_ack(struct ksmbd_work *work) |
| 8081 | { |
| 8082 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8083 | struct smb2_lease_ack *req = smb2_get_msg(work->request_buf); |
| 8084 | struct smb2_lease_ack *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8085 | struct oplock_info *opinfo; |
| 8086 | __le32 err = 0; |
| 8087 | int ret = 0; |
| 8088 | unsigned int lease_change_type; |
| 8089 | __le32 lease_state; |
| 8090 | struct lease *lease; |
| 8091 | |
| 8092 | ksmbd_debug(OPLOCK, "smb21 lease break, lease state(0x%x)\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 8093 | le32_to_cpu(req->LeaseState)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8094 | opinfo = lookup_lease_in_table(conn, req->LeaseKey); |
| 8095 | if (!opinfo) { |
| 8096 | ksmbd_debug(OPLOCK, "file not opened\n"); |
| 8097 | smb2_set_err_rsp(work); |
| 8098 | rsp->hdr.Status = STATUS_UNSUCCESSFUL; |
| 8099 | return; |
| 8100 | } |
| 8101 | lease = opinfo->o_lease; |
| 8102 | |
| 8103 | if (opinfo->op_state == OPLOCK_STATE_NONE) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 8104 | pr_err("unexpected lease break state 0x%x\n", |
| 8105 | opinfo->op_state); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8106 | rsp->hdr.Status = STATUS_UNSUCCESSFUL; |
| 8107 | goto err_out; |
| 8108 | } |
| 8109 | |
| 8110 | if (check_lease_state(lease, req->LeaseState)) { |
| 8111 | rsp->hdr.Status = STATUS_REQUEST_NOT_ACCEPTED; |
| 8112 | ksmbd_debug(OPLOCK, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 8113 | "req lease state: 0x%x, expected state: 0x%x\n", |
| 8114 | req->LeaseState, lease->new_state); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8115 | goto err_out; |
| 8116 | } |
| 8117 | |
| 8118 | if (!atomic_read(&opinfo->breaking_cnt)) { |
| 8119 | rsp->hdr.Status = STATUS_UNSUCCESSFUL; |
| 8120 | goto err_out; |
| 8121 | } |
| 8122 | |
| 8123 | /* check for bad lease state */ |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 8124 | if (req->LeaseState & |
| 8125 | (~(SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_HANDLE_CACHING_LE))) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8126 | err = STATUS_INVALID_OPLOCK_PROTOCOL; |
| 8127 | if (lease->state & SMB2_LEASE_WRITE_CACHING_LE) |
| 8128 | lease_change_type = OPLOCK_WRITE_TO_NONE; |
| 8129 | else |
| 8130 | lease_change_type = OPLOCK_READ_TO_NONE; |
| 8131 | ksmbd_debug(OPLOCK, "handle bad lease state 0x%x -> 0x%x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 8132 | le32_to_cpu(lease->state), |
| 8133 | le32_to_cpu(req->LeaseState)); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 8134 | } else if (lease->state == SMB2_LEASE_READ_CACHING_LE && |
| 8135 | req->LeaseState != SMB2_LEASE_NONE_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8136 | err = STATUS_INVALID_OPLOCK_PROTOCOL; |
| 8137 | lease_change_type = OPLOCK_READ_TO_NONE; |
| 8138 | ksmbd_debug(OPLOCK, "handle bad lease state 0x%x -> 0x%x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 8139 | le32_to_cpu(lease->state), |
| 8140 | le32_to_cpu(req->LeaseState)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8141 | } else { |
| 8142 | /* valid lease state changes */ |
| 8143 | err = STATUS_INVALID_DEVICE_STATE; |
| 8144 | if (req->LeaseState == SMB2_LEASE_NONE_LE) { |
| 8145 | if (lease->state & SMB2_LEASE_WRITE_CACHING_LE) |
| 8146 | lease_change_type = OPLOCK_WRITE_TO_NONE; |
| 8147 | else |
| 8148 | lease_change_type = OPLOCK_READ_TO_NONE; |
| 8149 | } else if (req->LeaseState & SMB2_LEASE_READ_CACHING_LE) { |
| 8150 | if (lease->state & SMB2_LEASE_WRITE_CACHING_LE) |
| 8151 | lease_change_type = OPLOCK_WRITE_TO_READ; |
| 8152 | else |
| 8153 | lease_change_type = OPLOCK_READ_HANDLE_TO_READ; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 8154 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8155 | lease_change_type = 0; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 8156 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8157 | } |
| 8158 | |
| 8159 | switch (lease_change_type) { |
| 8160 | case OPLOCK_WRITE_TO_READ: |
| 8161 | ret = opinfo_write_to_read(opinfo); |
| 8162 | break; |
| 8163 | case OPLOCK_READ_HANDLE_TO_READ: |
| 8164 | ret = opinfo_read_handle_to_read(opinfo); |
| 8165 | break; |
| 8166 | case OPLOCK_WRITE_TO_NONE: |
| 8167 | ret = opinfo_write_to_none(opinfo); |
| 8168 | break; |
| 8169 | case OPLOCK_READ_TO_NONE: |
| 8170 | ret = opinfo_read_to_none(opinfo); |
| 8171 | break; |
| 8172 | default: |
| 8173 | ksmbd_debug(OPLOCK, "unknown lease change 0x%x -> 0x%x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 8174 | le32_to_cpu(lease->state), |
| 8175 | le32_to_cpu(req->LeaseState)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8176 | } |
| 8177 | |
| 8178 | lease_state = lease->state; |
| 8179 | opinfo->op_state = OPLOCK_STATE_NONE; |
| 8180 | wake_up_interruptible_all(&opinfo->oplock_q); |
| 8181 | atomic_dec(&opinfo->breaking_cnt); |
| 8182 | wake_up_interruptible_all(&opinfo->oplock_brk); |
| 8183 | opinfo_put(opinfo); |
| 8184 | |
| 8185 | if (ret < 0) { |
| 8186 | rsp->hdr.Status = err; |
| 8187 | goto err_out; |
| 8188 | } |
| 8189 | |
| 8190 | rsp->StructureSize = cpu_to_le16(36); |
| 8191 | rsp->Reserved = 0; |
| 8192 | rsp->Flags = 0; |
| 8193 | memcpy(rsp->LeaseKey, req->LeaseKey, 16); |
| 8194 | rsp->LeaseState = lease_state; |
| 8195 | rsp->LeaseDuration = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8196 | inc_rfc1001_len(work->response_buf, 36); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8197 | return; |
| 8198 | |
| 8199 | err_out: |
| 8200 | opinfo->op_state = OPLOCK_STATE_NONE; |
| 8201 | wake_up_interruptible_all(&opinfo->oplock_q); |
| 8202 | atomic_dec(&opinfo->breaking_cnt); |
| 8203 | wake_up_interruptible_all(&opinfo->oplock_brk); |
| 8204 | |
| 8205 | opinfo_put(opinfo); |
| 8206 | smb2_set_err_rsp(work); |
| 8207 | } |
| 8208 | |
| 8209 | /** |
| 8210 | * smb2_oplock_break() - dispatcher for smb2.0 and 2.1 oplock/lease break |
| 8211 | * @work: smb work containing oplock/lease break command buffer |
| 8212 | * |
| 8213 | * Return: 0 |
| 8214 | */ |
| 8215 | int smb2_oplock_break(struct ksmbd_work *work) |
| 8216 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8217 | struct smb2_oplock_break *req = smb2_get_msg(work->request_buf); |
| 8218 | struct smb2_oplock_break *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8219 | |
| 8220 | switch (le16_to_cpu(req->StructureSize)) { |
| 8221 | case OP_BREAK_STRUCT_SIZE_20: |
| 8222 | smb20_oplock_break_ack(work); |
| 8223 | break; |
| 8224 | case OP_BREAK_STRUCT_SIZE_21: |
| 8225 | smb21_lease_break_ack(work); |
| 8226 | break; |
| 8227 | default: |
| 8228 | ksmbd_debug(OPLOCK, "invalid break cmd %d\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 8229 | le16_to_cpu(req->StructureSize)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8230 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 8231 | smb2_set_err_rsp(work); |
| 8232 | } |
| 8233 | |
| 8234 | return 0; |
| 8235 | } |
| 8236 | |
| 8237 | /** |
| 8238 | * smb2_notify() - handler for smb2 notify request |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 8239 | * @work: smb work containing notify command buffer |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8240 | * |
| 8241 | * Return: 0 |
| 8242 | */ |
| 8243 | int smb2_notify(struct ksmbd_work *work) |
| 8244 | { |
Ronnie Sahlberg | 699230f | 2021-09-09 12:26:12 +0900 | [diff] [blame] | 8245 | struct smb2_change_notify_req *req; |
| 8246 | struct smb2_change_notify_rsp *rsp; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8247 | |
| 8248 | WORK_BUFFERS(work, req, rsp); |
| 8249 | |
| 8250 | if (work->next_smb2_rcv_hdr_off && req->hdr.NextCommand) { |
| 8251 | rsp->hdr.Status = STATUS_INTERNAL_ERROR; |
| 8252 | smb2_set_err_rsp(work); |
| 8253 | return 0; |
| 8254 | } |
| 8255 | |
| 8256 | smb2_set_err_rsp(work); |
| 8257 | rsp->hdr.Status = STATUS_NOT_IMPLEMENTED; |
| 8258 | return 0; |
| 8259 | } |
| 8260 | |
| 8261 | /** |
| 8262 | * smb2_is_sign_req() - handler for checking packet signing status |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 8263 | * @work: smb work containing notify command buffer |
| 8264 | * @command: SMB2 command id |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8265 | * |
| 8266 | * Return: true if packed is signed, false otherwise |
| 8267 | */ |
| 8268 | bool smb2_is_sign_req(struct ksmbd_work *work, unsigned int command) |
| 8269 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8270 | struct smb2_hdr *rcv_hdr2 = smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8271 | |
| 8272 | if ((rcv_hdr2->Flags & SMB2_FLAGS_SIGNED) && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 8273 | command != SMB2_NEGOTIATE_HE && |
| 8274 | command != SMB2_SESSION_SETUP_HE && |
| 8275 | command != SMB2_OPLOCK_BREAK_HE) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8276 | return true; |
| 8277 | |
kernel test robot | 5616015 | 2021-05-12 09:24:37 +0900 | [diff] [blame] | 8278 | return false; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8279 | } |
| 8280 | |
| 8281 | /** |
| 8282 | * smb2_check_sign_req() - handler for req packet sign processing |
| 8283 | * @work: smb work containing notify command buffer |
| 8284 | * |
| 8285 | * Return: 1 on success, 0 otherwise |
| 8286 | */ |
| 8287 | int smb2_check_sign_req(struct ksmbd_work *work) |
| 8288 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8289 | struct smb2_hdr *hdr; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8290 | char signature_req[SMB2_SIGNATURE_SIZE]; |
| 8291 | char signature[SMB2_HMACSHA256_SIZE]; |
| 8292 | struct kvec iov[1]; |
| 8293 | size_t len; |
| 8294 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8295 | hdr = smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8296 | if (work->next_smb2_rcv_hdr_off) |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 8297 | hdr = ksmbd_req_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8298 | |
| 8299 | if (!hdr->NextCommand && !work->next_smb2_rcv_hdr_off) |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8300 | len = get_rfc1002_len(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8301 | else if (hdr->NextCommand) |
| 8302 | len = le32_to_cpu(hdr->NextCommand); |
| 8303 | else |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8304 | len = get_rfc1002_len(work->request_buf) - |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8305 | work->next_smb2_rcv_hdr_off; |
| 8306 | |
| 8307 | memcpy(signature_req, hdr->Signature, SMB2_SIGNATURE_SIZE); |
| 8308 | memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE); |
| 8309 | |
| 8310 | iov[0].iov_base = (char *)&hdr->ProtocolId; |
| 8311 | iov[0].iov_len = len; |
| 8312 | |
| 8313 | if (ksmbd_sign_smb2_pdu(work->conn, work->sess->sess_key, iov, 1, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 8314 | signature)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8315 | return 0; |
| 8316 | |
| 8317 | if (memcmp(signature, signature_req, SMB2_SIGNATURE_SIZE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 8318 | pr_err("bad smb2 signature\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8319 | return 0; |
| 8320 | } |
| 8321 | |
| 8322 | return 1; |
| 8323 | } |
| 8324 | |
| 8325 | /** |
| 8326 | * smb2_set_sign_rsp() - handler for rsp packet sign processing |
| 8327 | * @work: smb work containing notify command buffer |
| 8328 | * |
| 8329 | */ |
| 8330 | void smb2_set_sign_rsp(struct ksmbd_work *work) |
| 8331 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8332 | struct smb2_hdr *hdr; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8333 | struct smb2_hdr *req_hdr; |
| 8334 | char signature[SMB2_HMACSHA256_SIZE]; |
| 8335 | struct kvec iov[2]; |
| 8336 | size_t len; |
| 8337 | int n_vec = 1; |
| 8338 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8339 | hdr = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8340 | if (work->next_smb2_rsp_hdr_off) |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 8341 | hdr = ksmbd_resp_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8342 | |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 8343 | req_hdr = ksmbd_req_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8344 | |
| 8345 | if (!work->next_smb2_rsp_hdr_off) { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8346 | len = get_rfc1002_len(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8347 | if (req_hdr->NextCommand) |
| 8348 | len = ALIGN(len, 8); |
| 8349 | } else { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8350 | len = get_rfc1002_len(work->response_buf) - |
| 8351 | work->next_smb2_rsp_hdr_off; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8352 | len = ALIGN(len, 8); |
| 8353 | } |
| 8354 | |
| 8355 | if (req_hdr->NextCommand) |
| 8356 | hdr->NextCommand = cpu_to_le32(len); |
| 8357 | |
| 8358 | hdr->Flags |= SMB2_FLAGS_SIGNED; |
| 8359 | memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE); |
| 8360 | |
| 8361 | iov[0].iov_base = (char *)&hdr->ProtocolId; |
| 8362 | iov[0].iov_len = len; |
| 8363 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8364 | if (work->aux_payload_sz) { |
| 8365 | iov[0].iov_len -= work->aux_payload_sz; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8366 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8367 | iov[1].iov_base = work->aux_payload_buf; |
| 8368 | iov[1].iov_len = work->aux_payload_sz; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8369 | n_vec++; |
| 8370 | } |
| 8371 | |
| 8372 | if (!ksmbd_sign_smb2_pdu(work->conn, work->sess->sess_key, iov, n_vec, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 8373 | signature)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8374 | memcpy(hdr->Signature, signature, SMB2_SIGNATURE_SIZE); |
| 8375 | } |
| 8376 | |
| 8377 | /** |
| 8378 | * smb3_check_sign_req() - handler for req packet sign processing |
| 8379 | * @work: smb work containing notify command buffer |
| 8380 | * |
| 8381 | * Return: 1 on success, 0 otherwise |
| 8382 | */ |
| 8383 | int smb3_check_sign_req(struct ksmbd_work *work) |
| 8384 | { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 8385 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8386 | char *signing_key; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8387 | struct smb2_hdr *hdr; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8388 | struct channel *chann; |
| 8389 | char signature_req[SMB2_SIGNATURE_SIZE]; |
| 8390 | char signature[SMB2_CMACAES_SIZE]; |
| 8391 | struct kvec iov[1]; |
| 8392 | size_t len; |
| 8393 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8394 | hdr = smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8395 | if (work->next_smb2_rcv_hdr_off) |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 8396 | hdr = ksmbd_req_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8397 | |
| 8398 | if (!hdr->NextCommand && !work->next_smb2_rcv_hdr_off) |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8399 | len = get_rfc1002_len(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8400 | else if (hdr->NextCommand) |
| 8401 | len = le32_to_cpu(hdr->NextCommand); |
| 8402 | else |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8403 | len = get_rfc1002_len(work->request_buf) - |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8404 | work->next_smb2_rcv_hdr_off; |
| 8405 | |
| 8406 | if (le16_to_cpu(hdr->Command) == SMB2_SESSION_SETUP_HE) { |
| 8407 | signing_key = work->sess->smb3signingkey; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8408 | } else { |
Namjae Jeon | 8e06b31 | 2022-07-22 10:15:57 +0900 | [diff] [blame] | 8409 | read_lock(&work->sess->chann_lock); |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 8410 | chann = lookup_chann_list(work->sess, conn); |
Namjae Jeon | 8e06b31 | 2022-07-22 10:15:57 +0900 | [diff] [blame] | 8411 | if (!chann) { |
| 8412 | read_unlock(&work->sess->chann_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8413 | return 0; |
Namjae Jeon | 8e06b31 | 2022-07-22 10:15:57 +0900 | [diff] [blame] | 8414 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8415 | signing_key = chann->smb3signingkey; |
Namjae Jeon | 8e06b31 | 2022-07-22 10:15:57 +0900 | [diff] [blame] | 8416 | read_unlock(&work->sess->chann_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8417 | } |
| 8418 | |
| 8419 | if (!signing_key) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 8420 | pr_err("SMB3 signing key is not generated\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8421 | return 0; |
| 8422 | } |
| 8423 | |
| 8424 | memcpy(signature_req, hdr->Signature, SMB2_SIGNATURE_SIZE); |
| 8425 | memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE); |
| 8426 | iov[0].iov_base = (char *)&hdr->ProtocolId; |
| 8427 | iov[0].iov_len = len; |
| 8428 | |
| 8429 | if (ksmbd_sign_smb3_pdu(conn, signing_key, iov, 1, signature)) |
| 8430 | return 0; |
| 8431 | |
| 8432 | if (memcmp(signature, signature_req, SMB2_SIGNATURE_SIZE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 8433 | pr_err("bad smb2 signature\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8434 | return 0; |
| 8435 | } |
| 8436 | |
| 8437 | return 1; |
| 8438 | } |
| 8439 | |
| 8440 | /** |
| 8441 | * smb3_set_sign_rsp() - handler for rsp packet sign processing |
| 8442 | * @work: smb work containing notify command buffer |
| 8443 | * |
| 8444 | */ |
| 8445 | void smb3_set_sign_rsp(struct ksmbd_work *work) |
| 8446 | { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 8447 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8448 | struct smb2_hdr *req_hdr, *hdr; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8449 | struct channel *chann; |
| 8450 | char signature[SMB2_CMACAES_SIZE]; |
| 8451 | struct kvec iov[2]; |
| 8452 | int n_vec = 1; |
| 8453 | size_t len; |
| 8454 | char *signing_key; |
| 8455 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8456 | hdr = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8457 | if (work->next_smb2_rsp_hdr_off) |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 8458 | hdr = ksmbd_resp_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8459 | |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 8460 | req_hdr = ksmbd_req_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8461 | |
| 8462 | if (!work->next_smb2_rsp_hdr_off) { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8463 | len = get_rfc1002_len(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8464 | if (req_hdr->NextCommand) |
| 8465 | len = ALIGN(len, 8); |
| 8466 | } else { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8467 | len = get_rfc1002_len(work->response_buf) - |
| 8468 | work->next_smb2_rsp_hdr_off; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8469 | len = ALIGN(len, 8); |
| 8470 | } |
| 8471 | |
Namjae Jeon | 08bdbc6 | 2021-07-27 09:30:29 +0900 | [diff] [blame] | 8472 | if (conn->binding == false && |
| 8473 | le16_to_cpu(hdr->Command) == SMB2_SESSION_SETUP_HE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8474 | signing_key = work->sess->smb3signingkey; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8475 | } else { |
Namjae Jeon | 8e06b31 | 2022-07-22 10:15:57 +0900 | [diff] [blame] | 8476 | read_lock(&work->sess->chann_lock); |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 8477 | chann = lookup_chann_list(work->sess, work->conn); |
Namjae Jeon | 8e06b31 | 2022-07-22 10:15:57 +0900 | [diff] [blame] | 8478 | if (!chann) { |
| 8479 | read_unlock(&work->sess->chann_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8480 | return; |
Namjae Jeon | 8e06b31 | 2022-07-22 10:15:57 +0900 | [diff] [blame] | 8481 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8482 | signing_key = chann->smb3signingkey; |
Namjae Jeon | 8e06b31 | 2022-07-22 10:15:57 +0900 | [diff] [blame] | 8483 | read_unlock(&work->sess->chann_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8484 | } |
| 8485 | |
| 8486 | if (!signing_key) |
| 8487 | return; |
| 8488 | |
| 8489 | if (req_hdr->NextCommand) |
| 8490 | hdr->NextCommand = cpu_to_le32(len); |
| 8491 | |
| 8492 | hdr->Flags |= SMB2_FLAGS_SIGNED; |
| 8493 | memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE); |
| 8494 | iov[0].iov_base = (char *)&hdr->ProtocolId; |
| 8495 | iov[0].iov_len = len; |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8496 | if (work->aux_payload_sz) { |
| 8497 | iov[0].iov_len -= work->aux_payload_sz; |
| 8498 | iov[1].iov_base = work->aux_payload_buf; |
| 8499 | iov[1].iov_len = work->aux_payload_sz; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8500 | n_vec++; |
| 8501 | } |
| 8502 | |
| 8503 | if (!ksmbd_sign_smb3_pdu(conn, signing_key, iov, n_vec, signature)) |
| 8504 | memcpy(hdr->Signature, signature, SMB2_SIGNATURE_SIZE); |
| 8505 | } |
| 8506 | |
| 8507 | /** |
| 8508 | * smb3_preauth_hash_rsp() - handler for computing preauth hash on response |
| 8509 | * @work: smb work containing response buffer |
| 8510 | * |
| 8511 | */ |
| 8512 | void smb3_preauth_hash_rsp(struct ksmbd_work *work) |
| 8513 | { |
| 8514 | struct ksmbd_conn *conn = work->conn; |
| 8515 | struct ksmbd_session *sess = work->sess; |
| 8516 | struct smb2_hdr *req, *rsp; |
| 8517 | |
| 8518 | if (conn->dialect != SMB311_PROT_ID) |
| 8519 | return; |
| 8520 | |
| 8521 | WORK_BUFFERS(work, req, rsp); |
| 8522 | |
Namjae Jeon | 442ff9e | 2021-09-29 15:44:32 +0900 | [diff] [blame] | 8523 | if (le16_to_cpu(req->Command) == SMB2_NEGOTIATE_HE && |
| 8524 | conn->preauth_info) |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8525 | ksmbd_gen_preauth_integrity_hash(conn, work->response_buf, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 8526 | conn->preauth_info->Preauth_HashValue); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8527 | |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 8528 | if (le16_to_cpu(rsp->Command) == SMB2_SESSION_SETUP_HE && sess) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8529 | __u8 *hash_value; |
| 8530 | |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 8531 | if (conn->binding) { |
| 8532 | struct preauth_session *preauth_sess; |
| 8533 | |
| 8534 | preauth_sess = ksmbd_preauth_session_lookup(conn, sess->id); |
| 8535 | if (!preauth_sess) |
| 8536 | return; |
| 8537 | hash_value = preauth_sess->Preauth_HashValue; |
| 8538 | } else { |
| 8539 | hash_value = sess->Preauth_HashValue; |
| 8540 | if (!hash_value) |
| 8541 | return; |
| 8542 | } |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8543 | ksmbd_gen_preauth_integrity_hash(conn, work->response_buf, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 8544 | hash_value); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8545 | } |
| 8546 | } |
| 8547 | |
Namjae Jeon | 2dd9129f | 2021-11-03 08:25:54 +0900 | [diff] [blame] | 8548 | static void fill_transform_hdr(void *tr_buf, char *old_buf, __le16 cipher_type) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8549 | { |
Namjae Jeon | 2dd9129f | 2021-11-03 08:25:54 +0900 | [diff] [blame] | 8550 | struct smb2_transform_hdr *tr_hdr = tr_buf + 4; |
| 8551 | struct smb2_hdr *hdr = smb2_get_msg(old_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8552 | unsigned int orig_len = get_rfc1002_len(old_buf); |
| 8553 | |
Christophe JAILLET | 56b401f | 2022-03-23 07:32:05 +0100 | [diff] [blame] | 8554 | /* tr_buf must be cleared by the caller */ |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8555 | tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM; |
| 8556 | tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len); |
Ronnie Sahlberg | 4355a8f | 2021-11-03 08:43:42 +0900 | [diff] [blame] | 8557 | tr_hdr->Flags = cpu_to_le16(TRANSFORM_FLAG_ENCRYPTED); |
Namjae Jeon | 5a0ca77 | 2021-05-06 11:43:37 +0900 | [diff] [blame] | 8558 | if (cipher_type == SMB2_ENCRYPTION_AES128_GCM || |
| 8559 | cipher_type == SMB2_ENCRYPTION_AES256_GCM) |
| 8560 | get_random_bytes(&tr_hdr->Nonce, SMB3_AES_GCM_NONCE); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8561 | else |
Namjae Jeon | 5a0ca77 | 2021-05-06 11:43:37 +0900 | [diff] [blame] | 8562 | get_random_bytes(&tr_hdr->Nonce, SMB3_AES_CCM_NONCE); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8563 | memcpy(&tr_hdr->SessionId, &hdr->SessionId, 8); |
Namjae Jeon | 2dd9129f | 2021-11-03 08:25:54 +0900 | [diff] [blame] | 8564 | inc_rfc1001_len(tr_buf, sizeof(struct smb2_transform_hdr)); |
| 8565 | inc_rfc1001_len(tr_buf, orig_len); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8566 | } |
| 8567 | |
| 8568 | int smb3_encrypt_resp(struct ksmbd_work *work) |
| 8569 | { |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8570 | char *buf = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8571 | struct kvec iov[3]; |
| 8572 | int rc = -ENOMEM; |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8573 | int buf_size = 0, rq_nvec = 2 + (work->aux_payload_sz ? 1 : 0); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8574 | |
| 8575 | if (ARRAY_SIZE(iov) < rq_nvec) |
| 8576 | return -ENOMEM; |
| 8577 | |
Namjae Jeon | 2dd9129f | 2021-11-03 08:25:54 +0900 | [diff] [blame] | 8578 | work->tr_buf = kzalloc(sizeof(struct smb2_transform_hdr) + 4, GFP_KERNEL); |
| 8579 | if (!work->tr_buf) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8580 | return rc; |
| 8581 | |
| 8582 | /* fill transform header */ |
Namjae Jeon | 2dd9129f | 2021-11-03 08:25:54 +0900 | [diff] [blame] | 8583 | fill_transform_hdr(work->tr_buf, buf, work->conn->cipher_type); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8584 | |
Namjae Jeon | 2dd9129f | 2021-11-03 08:25:54 +0900 | [diff] [blame] | 8585 | iov[0].iov_base = work->tr_buf; |
| 8586 | iov[0].iov_len = sizeof(struct smb2_transform_hdr) + 4; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8587 | buf_size += iov[0].iov_len - 4; |
| 8588 | |
| 8589 | iov[1].iov_base = buf + 4; |
| 8590 | iov[1].iov_len = get_rfc1002_len(buf); |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8591 | if (work->aux_payload_sz) { |
| 8592 | iov[1].iov_len = work->resp_hdr_sz - 4; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8593 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8594 | iov[2].iov_base = work->aux_payload_buf; |
| 8595 | iov[2].iov_len = work->aux_payload_sz; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8596 | buf_size += iov[2].iov_len; |
| 8597 | } |
| 8598 | buf_size += iov[1].iov_len; |
| 8599 | work->resp_hdr_sz = iov[1].iov_len; |
| 8600 | |
Namjae Jeon | af705ef | 2022-09-22 23:36:34 +0900 | [diff] [blame] | 8601 | rc = ksmbd_crypt_message(work, iov, rq_nvec, 1); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8602 | if (rc) |
| 8603 | return rc; |
| 8604 | |
| 8605 | memmove(buf, iov[1].iov_base, iov[1].iov_len); |
Namjae Jeon | 2dd9129f | 2021-11-03 08:25:54 +0900 | [diff] [blame] | 8606 | *(__be32 *)work->tr_buf = cpu_to_be32(buf_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8607 | |
| 8608 | return rc; |
| 8609 | } |
| 8610 | |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 8611 | bool smb3_is_transform_hdr(void *buf) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8612 | { |
Namjae Jeon | 2dd9129f | 2021-11-03 08:25:54 +0900 | [diff] [blame] | 8613 | struct smb2_transform_hdr *trhdr = smb2_get_msg(buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8614 | |
| 8615 | return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM; |
| 8616 | } |
| 8617 | |
| 8618 | int smb3_decrypt_req(struct ksmbd_work *work) |
| 8619 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8620 | struct ksmbd_session *sess; |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8621 | char *buf = work->request_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8622 | unsigned int pdu_length = get_rfc1002_len(buf); |
| 8623 | struct kvec iov[2]; |
Namjae Jeon | 2dd9129f | 2021-11-03 08:25:54 +0900 | [diff] [blame] | 8624 | int buf_data_size = pdu_length - sizeof(struct smb2_transform_hdr); |
| 8625 | struct smb2_transform_hdr *tr_hdr = smb2_get_msg(buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8626 | int rc = 0; |
| 8627 | |
Namjae Jeon | c7705ee | 2021-10-04 20:44:52 +0900 | [diff] [blame] | 8628 | if (buf_data_size < sizeof(struct smb2_hdr)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 8629 | pr_err("Transform message is too small (%u)\n", |
| 8630 | pdu_length); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8631 | return -ECONNABORTED; |
| 8632 | } |
| 8633 | |
Namjae Jeon | c7705ee | 2021-10-04 20:44:52 +0900 | [diff] [blame] | 8634 | if (buf_data_size < le32_to_cpu(tr_hdr->OriginalMessageSize)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 8635 | pr_err("Transform message is broken\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8636 | return -ECONNABORTED; |
| 8637 | } |
| 8638 | |
Namjae Jeon | af705ef | 2022-09-22 23:36:34 +0900 | [diff] [blame] | 8639 | sess = ksmbd_session_lookup_all(work->conn, le64_to_cpu(tr_hdr->SessionId)); |
Namjae Jeon | 4227f81 | 2021-09-29 19:52:51 +0900 | [diff] [blame] | 8640 | if (!sess) { |
| 8641 | pr_err("invalid session id(%llx) in transform header\n", |
| 8642 | le64_to_cpu(tr_hdr->SessionId)); |
| 8643 | return -ECONNABORTED; |
| 8644 | } |
| 8645 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8646 | iov[0].iov_base = buf; |
Namjae Jeon | 2dd9129f | 2021-11-03 08:25:54 +0900 | [diff] [blame] | 8647 | iov[0].iov_len = sizeof(struct smb2_transform_hdr) + 4; |
| 8648 | iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr) + 4; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8649 | iov[1].iov_len = buf_data_size; |
Namjae Jeon | af705ef | 2022-09-22 23:36:34 +0900 | [diff] [blame] | 8650 | rc = ksmbd_crypt_message(work, iov, 2, 0); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8651 | if (rc) |
| 8652 | return rc; |
| 8653 | |
| 8654 | memmove(buf + 4, iov[1].iov_base, buf_data_size); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8655 | *(__be32 *)buf = cpu_to_be32(buf_data_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8656 | |
| 8657 | return rc; |
| 8658 | } |
| 8659 | |
| 8660 | bool smb3_11_final_sess_setup_resp(struct ksmbd_work *work) |
| 8661 | { |
| 8662 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8663 | struct smb2_hdr *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8664 | |
| 8665 | if (conn->dialect < SMB30_PROT_ID) |
| 8666 | return false; |
| 8667 | |
| 8668 | if (work->next_smb2_rcv_hdr_off) |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 8669 | rsp = ksmbd_resp_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8670 | |
| 8671 | if (le16_to_cpu(rsp->Command) == SMB2_SESSION_SETUP_HE && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 8672 | rsp->Status == STATUS_SUCCESS) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8673 | return true; |
| 8674 | return false; |
| 8675 | } |