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 | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 14 | |
| 15 | #include "glob.h" |
| 16 | #include "smb2pdu.h" |
| 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 | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 47 | *req = work->request_buf; |
| 48 | *rsp = 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 | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 96 | struct smb2_hdr *req_hdr = work->request_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 97 | int tree_id; |
| 98 | |
| 99 | work->tcon = NULL; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 100 | if (work->conn->ops->get_cmd_val(work) == SMB2_TREE_CONNECT_HE || |
| 101 | work->conn->ops->get_cmd_val(work) == SMB2_CANCEL_HE || |
| 102 | work->conn->ops->get_cmd_val(work) == SMB2_LOGOFF_HE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 103 | ksmbd_debug(SMB, "skip to check tree connect request\n"); |
| 104 | return 0; |
| 105 | } |
| 106 | |
Namjae Jeon | 02b68b2 | 2021-04-01 17:45:33 +0900 | [diff] [blame] | 107 | if (xa_empty(&work->sess->tree_conns)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 108 | ksmbd_debug(SMB, "NO tree connected\n"); |
Namjae Jeon | c6ce2b5 | 2021-08-12 10:18:18 +0900 | [diff] [blame] | 109 | return -ENOENT; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | tree_id = le32_to_cpu(req_hdr->Id.SyncId.TreeId); |
| 113 | work->tcon = ksmbd_tree_conn_lookup(work->sess, tree_id); |
| 114 | if (!work->tcon) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 115 | pr_err("Invalid tid %d\n", tree_id); |
Namjae Jeon | c6ce2b5 | 2021-08-12 10:18:18 +0900 | [diff] [blame] | 116 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | return 1; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * smb2_set_err_rsp() - set error response code on smb response |
| 124 | * @work: smb work containing response buffer |
| 125 | */ |
| 126 | void smb2_set_err_rsp(struct ksmbd_work *work) |
| 127 | { |
| 128 | struct smb2_err_rsp *err_rsp; |
| 129 | |
| 130 | if (work->next_smb2_rcv_hdr_off) |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 131 | err_rsp = ksmbd_resp_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 132 | else |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 133 | err_rsp = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 134 | |
| 135 | if (err_rsp->hdr.Status != STATUS_STOPPED_ON_SYMLINK) { |
| 136 | err_rsp->StructureSize = SMB2_ERROR_STRUCTURE_SIZE2_LE; |
| 137 | err_rsp->ErrorContextCount = 0; |
| 138 | err_rsp->Reserved = 0; |
| 139 | err_rsp->ByteCount = 0; |
| 140 | err_rsp->ErrorData[0] = 0; |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 141 | inc_rfc1001_len(work->response_buf, SMB2_ERROR_STRUCTURE_SIZE2); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * is_smb2_neg_cmd() - is it smb2 negotiation command |
| 147 | * @work: smb work containing smb header |
| 148 | * |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 149 | * Return: true if smb2 negotiation command, otherwise false |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 150 | */ |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 151 | bool is_smb2_neg_cmd(struct ksmbd_work *work) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 152 | { |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 153 | struct smb2_hdr *hdr = work->request_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 154 | |
| 155 | /* is it SMB2 header ? */ |
| 156 | if (hdr->ProtocolId != SMB2_PROTO_NUMBER) |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 157 | return false; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 158 | |
| 159 | /* make sure it is request not response message */ |
| 160 | if (hdr->Flags & SMB2_FLAGS_SERVER_TO_REDIR) |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 161 | return false; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 162 | |
| 163 | if (hdr->Command != SMB2_NEGOTIATE) |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 164 | return false; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 165 | |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 166 | return true; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /** |
| 170 | * is_smb2_rsp() - is it smb2 response |
| 171 | * @work: smb work containing smb response buffer |
| 172 | * |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 173 | * Return: true if smb2 response, otherwise false |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 174 | */ |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 175 | bool is_smb2_rsp(struct ksmbd_work *work) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 176 | { |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 177 | struct smb2_hdr *hdr = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 178 | |
| 179 | /* is it SMB2 header ? */ |
| 180 | if (hdr->ProtocolId != SMB2_PROTO_NUMBER) |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 181 | return false; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 182 | |
| 183 | /* make sure it is response not request message */ |
| 184 | if (!(hdr->Flags & SMB2_FLAGS_SERVER_TO_REDIR)) |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 185 | return false; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 186 | |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 187 | return true; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /** |
| 191 | * get_smb2_cmd_val() - get smb command code from smb header |
| 192 | * @work: smb work containing smb request buffer |
| 193 | * |
| 194 | * Return: smb2 request command value |
| 195 | */ |
Namjae Jeon | fc2d1b5 | 2021-05-26 18:01:08 +0900 | [diff] [blame] | 196 | u16 get_smb2_cmd_val(struct ksmbd_work *work) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 197 | { |
| 198 | struct smb2_hdr *rcv_hdr; |
| 199 | |
| 200 | if (work->next_smb2_rcv_hdr_off) |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 201 | rcv_hdr = ksmbd_req_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 202 | else |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 203 | rcv_hdr = work->request_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 204 | return le16_to_cpu(rcv_hdr->Command); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * set_smb2_rsp_status() - set error response code on smb2 header |
| 209 | * @work: smb work containing response buffer |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 210 | * @err: error response code |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 211 | */ |
| 212 | void set_smb2_rsp_status(struct ksmbd_work *work, __le32 err) |
| 213 | { |
| 214 | struct smb2_hdr *rsp_hdr; |
| 215 | |
| 216 | if (work->next_smb2_rcv_hdr_off) |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 217 | rsp_hdr = ksmbd_resp_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 218 | else |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 219 | rsp_hdr = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 220 | rsp_hdr->Status = err; |
| 221 | smb2_set_err_rsp(work); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * init_smb2_neg_rsp() - initialize smb2 response for negotiate command |
| 226 | * @work: smb work containing smb request buffer |
| 227 | * |
| 228 | * smb2 negotiate response is sent in reply of smb1 negotiate command for |
| 229 | * dialect auto-negotiation. |
| 230 | */ |
| 231 | int init_smb2_neg_rsp(struct ksmbd_work *work) |
| 232 | { |
| 233 | struct smb2_hdr *rsp_hdr; |
| 234 | struct smb2_negotiate_rsp *rsp; |
| 235 | struct ksmbd_conn *conn = work->conn; |
| 236 | |
| 237 | if (conn->need_neg == false) |
| 238 | return -EINVAL; |
| 239 | if (!(conn->dialect >= SMB20_PROT_ID && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 240 | conn->dialect <= SMB311_PROT_ID)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 241 | return -EINVAL; |
| 242 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 243 | rsp_hdr = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 244 | |
| 245 | memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2); |
| 246 | |
| 247 | rsp_hdr->smb2_buf_length = |
Hyunchul Lee | d8fb299 | 2021-06-25 11:53:26 +0900 | [diff] [blame] | 248 | cpu_to_be32(smb2_hdr_size_no_buflen(conn->vals)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 249 | |
| 250 | rsp_hdr->ProtocolId = SMB2_PROTO_NUMBER; |
| 251 | rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE; |
| 252 | rsp_hdr->CreditRequest = cpu_to_le16(2); |
| 253 | rsp_hdr->Command = SMB2_NEGOTIATE; |
| 254 | rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR); |
| 255 | rsp_hdr->NextCommand = 0; |
| 256 | rsp_hdr->MessageId = 0; |
| 257 | rsp_hdr->Id.SyncId.ProcessId = 0; |
| 258 | rsp_hdr->Id.SyncId.TreeId = 0; |
| 259 | rsp_hdr->SessionId = 0; |
| 260 | memset(rsp_hdr->Signature, 0, 16); |
| 261 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 262 | rsp = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 263 | |
| 264 | WARN_ON(ksmbd_conn_good(work)); |
| 265 | |
| 266 | rsp->StructureSize = cpu_to_le16(65); |
| 267 | ksmbd_debug(SMB, "conn->dialect 0x%x\n", conn->dialect); |
| 268 | rsp->DialectRevision = cpu_to_le16(conn->dialect); |
| 269 | /* Not setting conn guid rsp->ServerGUID, as it |
| 270 | * not used by client for identifying connection |
| 271 | */ |
| 272 | rsp->Capabilities = cpu_to_le32(conn->vals->capabilities); |
| 273 | /* Default Max Message Size till SMB2.0, 64K*/ |
| 274 | rsp->MaxTransactSize = cpu_to_le32(conn->vals->max_trans_size); |
| 275 | rsp->MaxReadSize = cpu_to_le32(conn->vals->max_read_size); |
| 276 | rsp->MaxWriteSize = cpu_to_le32(conn->vals->max_write_size); |
| 277 | |
| 278 | rsp->SystemTime = cpu_to_le64(ksmbd_systime()); |
| 279 | rsp->ServerStartTime = 0; |
| 280 | |
| 281 | rsp->SecurityBufferOffset = cpu_to_le16(128); |
| 282 | rsp->SecurityBufferLength = cpu_to_le16(AUTH_GSS_LENGTH); |
| 283 | ksmbd_copy_gss_neg_header(((char *)(&rsp->hdr) + |
| 284 | sizeof(rsp->hdr.smb2_buf_length)) + |
| 285 | le16_to_cpu(rsp->SecurityBufferOffset)); |
| 286 | inc_rfc1001_len(rsp, sizeof(struct smb2_negotiate_rsp) - |
| 287 | sizeof(struct smb2_hdr) - sizeof(rsp->Buffer) + |
| 288 | AUTH_GSS_LENGTH); |
| 289 | rsp->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED_LE; |
| 290 | if (server_conf.signing == KSMBD_CONFIG_OPT_MANDATORY) |
| 291 | rsp->SecurityMode |= SMB2_NEGOTIATE_SIGNING_REQUIRED_LE; |
| 292 | conn->use_spnego = true; |
| 293 | |
| 294 | ksmbd_conn_set_need_negotiate(work); |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | static int smb2_consume_credit_charge(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 299 | unsigned short credit_charge) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 300 | { |
| 301 | struct ksmbd_conn *conn = work->conn; |
| 302 | unsigned int rsp_credits = 1; |
| 303 | |
| 304 | if (!conn->total_credits) |
| 305 | return 0; |
| 306 | |
| 307 | if (credit_charge > 0) |
| 308 | rsp_credits = credit_charge; |
| 309 | |
| 310 | conn->total_credits -= rsp_credits; |
| 311 | return rsp_credits; |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * smb2_set_rsp_credits() - set number of credits in response buffer |
| 316 | * @work: smb work containing smb response buffer |
| 317 | */ |
| 318 | int smb2_set_rsp_credits(struct ksmbd_work *work) |
| 319 | { |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 320 | struct smb2_hdr *req_hdr = ksmbd_req_buf_next(work); |
| 321 | struct smb2_hdr *hdr = ksmbd_resp_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 322 | struct ksmbd_conn *conn = work->conn; |
| 323 | unsigned short credits_requested = le16_to_cpu(req_hdr->CreditRequest); |
| 324 | unsigned short credit_charge = 1, credits_granted = 0; |
| 325 | unsigned short aux_max, aux_credits, min_credits; |
| 326 | int rsp_credit_charge; |
| 327 | |
| 328 | if (hdr->Command == SMB2_CANCEL) |
| 329 | goto out; |
| 330 | |
| 331 | /* get default minimum credits by shifting maximum credits by 4 */ |
| 332 | min_credits = conn->max_credits >> 4; |
| 333 | |
| 334 | if (conn->total_credits >= conn->max_credits) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 335 | pr_err("Total credits overflow: %d\n", conn->total_credits); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 336 | conn->total_credits = min_credits; |
| 337 | } |
| 338 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 339 | rsp_credit_charge = |
| 340 | smb2_consume_credit_charge(work, le16_to_cpu(req_hdr->CreditCharge)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 341 | if (rsp_credit_charge < 0) |
| 342 | return -EINVAL; |
| 343 | |
| 344 | hdr->CreditCharge = cpu_to_le16(rsp_credit_charge); |
| 345 | |
| 346 | if (credits_requested > 0) { |
| 347 | aux_credits = credits_requested - 1; |
| 348 | aux_max = 32; |
| 349 | if (hdr->Command == SMB2_NEGOTIATE) |
| 350 | aux_max = 0; |
| 351 | aux_credits = (aux_credits < aux_max) ? aux_credits : aux_max; |
| 352 | credits_granted = aux_credits + credit_charge; |
| 353 | |
| 354 | /* if credits granted per client is getting bigger than default |
| 355 | * minimum credits then we should wrap it up within the limits. |
| 356 | */ |
| 357 | if ((conn->total_credits + credits_granted) > min_credits) |
| 358 | credits_granted = min_credits - conn->total_credits; |
| 359 | /* |
| 360 | * TODO: Need to adjuct CreditRequest value according to |
| 361 | * current cpu load |
| 362 | */ |
| 363 | } else if (conn->total_credits == 0) { |
| 364 | credits_granted = 1; |
| 365 | } |
| 366 | |
| 367 | conn->total_credits += credits_granted; |
| 368 | work->credits_granted += credits_granted; |
| 369 | |
| 370 | if (!req_hdr->NextCommand) { |
| 371 | /* Update CreditRequest in last request */ |
| 372 | hdr->CreditRequest = cpu_to_le16(work->credits_granted); |
| 373 | } |
| 374 | out: |
| 375 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 376 | "credits: requested[%d] granted[%d] total_granted[%d]\n", |
| 377 | credits_requested, credits_granted, |
| 378 | conn->total_credits); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 379 | return 0; |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * init_chained_smb2_rsp() - initialize smb2 chained response |
| 384 | * @work: smb work containing smb response buffer |
| 385 | */ |
| 386 | static void init_chained_smb2_rsp(struct ksmbd_work *work) |
| 387 | { |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 388 | struct smb2_hdr *req = ksmbd_req_buf_next(work); |
| 389 | struct smb2_hdr *rsp = ksmbd_resp_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 390 | struct smb2_hdr *rsp_hdr; |
| 391 | struct smb2_hdr *rcv_hdr; |
| 392 | int next_hdr_offset = 0; |
| 393 | int len, new_len; |
| 394 | |
| 395 | /* Len of this response = updated RFC len - offset of previous cmd |
| 396 | * in the compound rsp |
| 397 | */ |
| 398 | |
| 399 | /* Storing the current local FID which may be needed by subsequent |
| 400 | * command in the compound request |
| 401 | */ |
| 402 | if (req->Command == SMB2_CREATE && rsp->Status == STATUS_SUCCESS) { |
| 403 | work->compound_fid = |
| 404 | le64_to_cpu(((struct smb2_create_rsp *)rsp)-> |
| 405 | VolatileFileId); |
| 406 | work->compound_pfid = |
| 407 | le64_to_cpu(((struct smb2_create_rsp *)rsp)-> |
| 408 | PersistentFileId); |
| 409 | work->compound_sid = le64_to_cpu(rsp->SessionId); |
| 410 | } |
| 411 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 412 | 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] | 413 | next_hdr_offset = le32_to_cpu(req->NextCommand); |
| 414 | |
| 415 | new_len = ALIGN(len, 8); |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 416 | inc_rfc1001_len(work->response_buf, ((sizeof(struct smb2_hdr) - 4) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 417 | + new_len - len)); |
| 418 | rsp->NextCommand = cpu_to_le32(new_len); |
| 419 | |
| 420 | work->next_smb2_rcv_hdr_off += next_hdr_offset; |
| 421 | work->next_smb2_rsp_hdr_off += new_len; |
| 422 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 423 | "Compound req new_len = %d rcv off = %d rsp off = %d\n", |
| 424 | new_len, work->next_smb2_rcv_hdr_off, |
| 425 | work->next_smb2_rsp_hdr_off); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 426 | |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 427 | rsp_hdr = ksmbd_resp_buf_next(work); |
| 428 | rcv_hdr = ksmbd_req_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 429 | |
| 430 | if (!(rcv_hdr->Flags & SMB2_FLAGS_RELATED_OPERATIONS)) { |
| 431 | ksmbd_debug(SMB, "related flag should be set\n"); |
| 432 | work->compound_fid = KSMBD_NO_FID; |
| 433 | work->compound_pfid = KSMBD_NO_FID; |
| 434 | } |
| 435 | memset((char *)rsp_hdr + 4, 0, sizeof(struct smb2_hdr) + 2); |
Namjae Jeon | 18a015b | 2021-09-22 21:00:57 +0900 | [diff] [blame] | 436 | rsp_hdr->ProtocolId = SMB2_PROTO_NUMBER; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 437 | rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE; |
| 438 | rsp_hdr->Command = rcv_hdr->Command; |
| 439 | |
| 440 | /* |
| 441 | * Message is response. We don't grant oplock yet. |
| 442 | */ |
| 443 | rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR | |
| 444 | SMB2_FLAGS_RELATED_OPERATIONS); |
| 445 | rsp_hdr->NextCommand = 0; |
| 446 | rsp_hdr->MessageId = rcv_hdr->MessageId; |
| 447 | rsp_hdr->Id.SyncId.ProcessId = rcv_hdr->Id.SyncId.ProcessId; |
| 448 | rsp_hdr->Id.SyncId.TreeId = rcv_hdr->Id.SyncId.TreeId; |
| 449 | rsp_hdr->SessionId = rcv_hdr->SessionId; |
| 450 | memcpy(rsp_hdr->Signature, rcv_hdr->Signature, 16); |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * is_chained_smb2_message() - check for chained command |
| 455 | * @work: smb work containing smb request buffer |
| 456 | * |
| 457 | * Return: true if chained request, otherwise false |
| 458 | */ |
| 459 | bool is_chained_smb2_message(struct ksmbd_work *work) |
| 460 | { |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 461 | struct smb2_hdr *hdr = work->request_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 462 | unsigned int len; |
| 463 | |
| 464 | if (hdr->ProtocolId != SMB2_PROTO_NUMBER) |
| 465 | return false; |
| 466 | |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 467 | hdr = ksmbd_req_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 468 | if (le32_to_cpu(hdr->NextCommand) > 0) { |
| 469 | ksmbd_debug(SMB, "got SMB2 chained command\n"); |
| 470 | init_chained_smb2_rsp(work); |
| 471 | return true; |
| 472 | } else if (work->next_smb2_rcv_hdr_off) { |
| 473 | /* |
| 474 | * This is last request in chained command, |
| 475 | * align response to 8 byte |
| 476 | */ |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 477 | len = ALIGN(get_rfc1002_len(work->response_buf), 8); |
| 478 | len = len - get_rfc1002_len(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 479 | if (len) { |
| 480 | ksmbd_debug(SMB, "padding len %u\n", len); |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 481 | inc_rfc1001_len(work->response_buf, len); |
| 482 | if (work->aux_payload_sz) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 483 | work->aux_payload_sz += len; |
| 484 | } |
| 485 | } |
| 486 | return false; |
| 487 | } |
| 488 | |
| 489 | /** |
| 490 | * init_smb2_rsp_hdr() - initialize smb2 response |
| 491 | * @work: smb work containing smb request buffer |
| 492 | * |
| 493 | * Return: 0 |
| 494 | */ |
| 495 | int init_smb2_rsp_hdr(struct ksmbd_work *work) |
| 496 | { |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 497 | struct smb2_hdr *rsp_hdr = work->response_buf; |
| 498 | struct smb2_hdr *rcv_hdr = work->request_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 499 | struct ksmbd_conn *conn = work->conn; |
| 500 | |
| 501 | memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2); |
Hyunchul Lee | d8fb299 | 2021-06-25 11:53:26 +0900 | [diff] [blame] | 502 | rsp_hdr->smb2_buf_length = |
| 503 | cpu_to_be32(smb2_hdr_size_no_buflen(conn->vals)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 504 | rsp_hdr->ProtocolId = rcv_hdr->ProtocolId; |
| 505 | rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE; |
| 506 | rsp_hdr->Command = rcv_hdr->Command; |
| 507 | |
| 508 | /* |
| 509 | * Message is response. We don't grant oplock yet. |
| 510 | */ |
| 511 | rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR); |
| 512 | rsp_hdr->NextCommand = 0; |
| 513 | rsp_hdr->MessageId = rcv_hdr->MessageId; |
| 514 | rsp_hdr->Id.SyncId.ProcessId = rcv_hdr->Id.SyncId.ProcessId; |
| 515 | rsp_hdr->Id.SyncId.TreeId = rcv_hdr->Id.SyncId.TreeId; |
| 516 | rsp_hdr->SessionId = rcv_hdr->SessionId; |
| 517 | memcpy(rsp_hdr->Signature, rcv_hdr->Signature, 16); |
| 518 | |
| 519 | work->syncronous = true; |
| 520 | if (work->async_id) { |
Namjae Jeon | d40012a | 2021-04-13 13:06:30 +0900 | [diff] [blame] | 521 | ksmbd_release_id(&conn->async_ida, work->async_id); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 522 | work->async_id = 0; |
| 523 | } |
| 524 | |
| 525 | return 0; |
| 526 | } |
| 527 | |
| 528 | /** |
| 529 | * smb2_allocate_rsp_buf() - allocate smb2 response buffer |
| 530 | * @work: smb work containing smb request buffer |
| 531 | * |
| 532 | * Return: 0 on success, otherwise -ENOMEM |
| 533 | */ |
| 534 | int smb2_allocate_rsp_buf(struct ksmbd_work *work) |
| 535 | { |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 536 | struct smb2_hdr *hdr = work->request_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 537 | size_t small_sz = MAX_CIFS_SMALL_BUFFER_SIZE; |
| 538 | size_t large_sz = work->conn->vals->max_trans_size + MAX_SMB2_HDR_SIZE; |
| 539 | size_t sz = small_sz; |
| 540 | int cmd = le16_to_cpu(hdr->Command); |
| 541 | |
Namjae Jeon | c30f4eb | 2021-06-18 10:17:37 +0900 | [diff] [blame] | 542 | if (cmd == SMB2_IOCTL_HE || cmd == SMB2_QUERY_DIRECTORY_HE) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 543 | sz = large_sz; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 544 | |
| 545 | if (cmd == SMB2_QUERY_INFO_HE) { |
| 546 | struct smb2_query_info_req *req; |
| 547 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 548 | req = work->request_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 549 | if (req->InfoType == SMB2_O_INFO_FILE && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 550 | (req->FileInfoClass == FILE_FULL_EA_INFORMATION || |
Namjae Jeon | c30f4eb | 2021-06-18 10:17:37 +0900 | [diff] [blame] | 551 | req->FileInfoClass == FILE_ALL_INFORMATION)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 552 | sz = large_sz; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | /* allocate large response buf for chained commands */ |
| 556 | if (le32_to_cpu(hdr->NextCommand) > 0) |
| 557 | sz = large_sz; |
| 558 | |
Namjae Jeon | c30f4eb | 2021-06-18 10:17:37 +0900 | [diff] [blame] | 559 | work->response_buf = kvmalloc(sz, GFP_KERNEL | __GFP_ZERO); |
Namjae Jeon | 63c454f | 2021-04-20 14:24:28 +0900 | [diff] [blame] | 560 | if (!work->response_buf) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 561 | return -ENOMEM; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 562 | |
| 563 | work->response_sz = sz; |
| 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | /** |
| 568 | * smb2_check_user_session() - check for valid session for a user |
| 569 | * @work: smb work containing smb request buffer |
| 570 | * |
| 571 | * Return: 0 on success, otherwise error |
| 572 | */ |
| 573 | int smb2_check_user_session(struct ksmbd_work *work) |
| 574 | { |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 575 | struct smb2_hdr *req_hdr = work->request_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 576 | struct ksmbd_conn *conn = work->conn; |
| 577 | unsigned int cmd = conn->ops->get_cmd_val(work); |
| 578 | unsigned long long sess_id; |
| 579 | |
| 580 | work->sess = NULL; |
| 581 | /* |
| 582 | * SMB2_ECHO, SMB2_NEGOTIATE, SMB2_SESSION_SETUP command do not |
| 583 | * require a session id, so no need to validate user session's for |
| 584 | * these commands. |
| 585 | */ |
| 586 | if (cmd == SMB2_ECHO_HE || cmd == SMB2_NEGOTIATE_HE || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 587 | cmd == SMB2_SESSION_SETUP_HE) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 588 | return 0; |
| 589 | |
| 590 | if (!ksmbd_conn_good(work)) |
| 591 | return -EINVAL; |
| 592 | |
| 593 | sess_id = le64_to_cpu(req_hdr->SessionId); |
| 594 | /* Check for validity of user session */ |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 595 | work->sess = ksmbd_session_lookup_all(conn, sess_id); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 596 | if (work->sess) |
| 597 | return 1; |
| 598 | ksmbd_debug(SMB, "Invalid user session, Uid %llu\n", sess_id); |
| 599 | return -EINVAL; |
| 600 | } |
| 601 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 602 | static void destroy_previous_session(struct ksmbd_user *user, u64 id) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 603 | { |
| 604 | struct ksmbd_session *prev_sess = ksmbd_session_lookup_slowpath(id); |
| 605 | struct ksmbd_user *prev_user; |
| 606 | |
| 607 | if (!prev_sess) |
| 608 | return; |
| 609 | |
| 610 | prev_user = prev_sess->user; |
| 611 | |
Marios Makassikis | 1fca803 | 2021-05-06 11:41:54 +0900 | [diff] [blame] | 612 | if (!prev_user || |
| 613 | strcmp(user->name, prev_user->name) || |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 614 | user->passkey_sz != prev_user->passkey_sz || |
| 615 | memcmp(user->passkey, prev_user->passkey, user->passkey_sz)) { |
| 616 | put_session(prev_sess); |
| 617 | return; |
| 618 | } |
| 619 | |
| 620 | put_session(prev_sess); |
| 621 | ksmbd_session_destroy(prev_sess); |
| 622 | } |
| 623 | |
| 624 | /** |
| 625 | * smb2_get_name() - get filename string from on the wire smb format |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 626 | * @share: ksmbd_share_config pointer |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 627 | * @src: source buffer |
| 628 | * @maxlen: maxlen of source string |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 629 | * @nls_table: nls_table pointer |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 630 | * |
| 631 | * Return: matching converted filename on success, otherwise error ptr |
| 632 | */ |
| 633 | static char * |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 634 | smb2_get_name(struct ksmbd_share_config *share, const char *src, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 635 | const int maxlen, struct nls_table *local_nls) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 636 | { |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame^] | 637 | char *name; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 638 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 639 | name = smb_strndup_from_utf16(src, maxlen, 1, local_nls); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 640 | if (IS_ERR(name)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 641 | pr_err("failed to get name %ld\n", PTR_ERR(name)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 642 | return name; |
| 643 | } |
| 644 | |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame^] | 645 | ksmbd_conv_path_to_unix(name); |
| 646 | ksmbd_strip_last_slash(name); |
| 647 | return name; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 648 | } |
| 649 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 650 | int setup_async_work(struct ksmbd_work *work, void (*fn)(void **), void **arg) |
| 651 | { |
| 652 | struct smb2_hdr *rsp_hdr; |
| 653 | struct ksmbd_conn *conn = work->conn; |
| 654 | int id; |
| 655 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 656 | rsp_hdr = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 657 | rsp_hdr->Flags |= SMB2_FLAGS_ASYNC_COMMAND; |
| 658 | |
Namjae Jeon | d40012a | 2021-04-13 13:06:30 +0900 | [diff] [blame] | 659 | id = ksmbd_acquire_async_msg_id(&conn->async_ida); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 660 | if (id < 0) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 661 | pr_err("Failed to alloc async message id\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 662 | return id; |
| 663 | } |
| 664 | work->syncronous = false; |
| 665 | work->async_id = id; |
| 666 | rsp_hdr->Id.AsyncId = cpu_to_le64(id); |
| 667 | |
| 668 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 669 | "Send interim Response to inform async request id : %d\n", |
| 670 | work->async_id); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 671 | |
| 672 | work->cancel_fn = fn; |
| 673 | work->cancel_argv = arg; |
| 674 | |
Namjae Jeon | 6c4e675 | 2021-06-07 09:08:45 +0900 | [diff] [blame] | 675 | if (list_empty(&work->async_request_entry)) { |
| 676 | spin_lock(&conn->request_lock); |
| 677 | list_add_tail(&work->async_request_entry, &conn->async_requests); |
| 678 | spin_unlock(&conn->request_lock); |
| 679 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 680 | |
| 681 | return 0; |
| 682 | } |
| 683 | |
| 684 | void smb2_send_interim_resp(struct ksmbd_work *work, __le32 status) |
| 685 | { |
| 686 | struct smb2_hdr *rsp_hdr; |
| 687 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 688 | rsp_hdr = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 689 | smb2_set_err_rsp(work); |
| 690 | rsp_hdr->Status = status; |
| 691 | |
| 692 | work->multiRsp = 1; |
| 693 | ksmbd_conn_write(work); |
| 694 | rsp_hdr->Status = 0; |
| 695 | work->multiRsp = 0; |
| 696 | } |
| 697 | |
| 698 | static __le32 smb2_get_reparse_tag_special_file(umode_t mode) |
| 699 | { |
| 700 | if (S_ISDIR(mode) || S_ISREG(mode)) |
| 701 | return 0; |
| 702 | |
| 703 | if (S_ISLNK(mode)) |
| 704 | return IO_REPARSE_TAG_LX_SYMLINK_LE; |
| 705 | else if (S_ISFIFO(mode)) |
| 706 | return IO_REPARSE_TAG_LX_FIFO_LE; |
| 707 | else if (S_ISSOCK(mode)) |
| 708 | return IO_REPARSE_TAG_AF_UNIX_LE; |
| 709 | else if (S_ISCHR(mode)) |
| 710 | return IO_REPARSE_TAG_LX_CHR_LE; |
| 711 | else if (S_ISBLK(mode)) |
| 712 | return IO_REPARSE_TAG_LX_BLK_LE; |
| 713 | |
| 714 | return 0; |
| 715 | } |
| 716 | |
| 717 | /** |
| 718 | * smb2_get_dos_mode() - get file mode in dos format from unix mode |
| 719 | * @stat: kstat containing file mode |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 720 | * @attribute: attribute flags |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 721 | * |
| 722 | * Return: converted dos mode |
| 723 | */ |
| 724 | static int smb2_get_dos_mode(struct kstat *stat, int attribute) |
| 725 | { |
| 726 | int attr = 0; |
| 727 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 728 | if (S_ISDIR(stat->mode)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 729 | attr = ATTR_DIRECTORY | |
| 730 | (attribute & (ATTR_HIDDEN | ATTR_SYSTEM)); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 731 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 732 | attr = (attribute & 0x00005137) | ATTR_ARCHIVE; |
| 733 | attr &= ~(ATTR_DIRECTORY); |
| 734 | if (S_ISREG(stat->mode) && (server_conf.share_fake_fscaps & |
| 735 | FILE_SUPPORTS_SPARSE_FILES)) |
| 736 | attr |= ATTR_SPARSE; |
| 737 | |
| 738 | if (smb2_get_reparse_tag_special_file(stat->mode)) |
| 739 | attr |= ATTR_REPARSE; |
| 740 | } |
| 741 | |
| 742 | return attr; |
| 743 | } |
| 744 | |
| 745 | static void build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 746 | __le16 hash_id) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 747 | { |
| 748 | pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES; |
| 749 | pneg_ctxt->DataLength = cpu_to_le16(38); |
| 750 | pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1); |
| 751 | pneg_ctxt->Reserved = cpu_to_le32(0); |
| 752 | pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE); |
| 753 | get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE); |
| 754 | pneg_ctxt->HashAlgorithms = hash_id; |
| 755 | } |
| 756 | |
| 757 | static void build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 758 | __le16 cipher_type) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 759 | { |
| 760 | pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES; |
| 761 | pneg_ctxt->DataLength = cpu_to_le16(4); |
| 762 | pneg_ctxt->Reserved = cpu_to_le32(0); |
| 763 | pneg_ctxt->CipherCount = cpu_to_le16(1); |
| 764 | pneg_ctxt->Ciphers[0] = cipher_type; |
| 765 | } |
| 766 | |
| 767 | static void build_compression_ctxt(struct smb2_compression_ctx *pneg_ctxt, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 768 | __le16 comp_algo) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 769 | { |
| 770 | pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES; |
| 771 | pneg_ctxt->DataLength = |
| 772 | cpu_to_le16(sizeof(struct smb2_compression_ctx) |
| 773 | - sizeof(struct smb2_neg_context)); |
| 774 | pneg_ctxt->Reserved = cpu_to_le32(0); |
| 775 | pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(1); |
| 776 | pneg_ctxt->Reserved1 = cpu_to_le32(0); |
| 777 | pneg_ctxt->CompressionAlgorithms[0] = comp_algo; |
| 778 | } |
| 779 | |
Namjae Jeon | 378087c | 2021-07-21 10:05:53 +0900 | [diff] [blame] | 780 | static void build_sign_cap_ctxt(struct smb2_signing_capabilities *pneg_ctxt, |
| 781 | __le16 sign_algo) |
| 782 | { |
| 783 | pneg_ctxt->ContextType = SMB2_SIGNING_CAPABILITIES; |
| 784 | pneg_ctxt->DataLength = |
| 785 | cpu_to_le16((sizeof(struct smb2_signing_capabilities) + 2) |
| 786 | - sizeof(struct smb2_neg_context)); |
| 787 | pneg_ctxt->Reserved = cpu_to_le32(0); |
| 788 | pneg_ctxt->SigningAlgorithmCount = cpu_to_le16(1); |
| 789 | pneg_ctxt->SigningAlgorithms[0] = sign_algo; |
| 790 | } |
| 791 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 792 | static void build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 793 | { |
| 794 | pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE; |
| 795 | pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN); |
| 796 | /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */ |
| 797 | pneg_ctxt->Name[0] = 0x93; |
| 798 | pneg_ctxt->Name[1] = 0xAD; |
| 799 | pneg_ctxt->Name[2] = 0x25; |
| 800 | pneg_ctxt->Name[3] = 0x50; |
| 801 | pneg_ctxt->Name[4] = 0x9C; |
| 802 | pneg_ctxt->Name[5] = 0xB4; |
| 803 | pneg_ctxt->Name[6] = 0x11; |
| 804 | pneg_ctxt->Name[7] = 0xE7; |
| 805 | pneg_ctxt->Name[8] = 0xB4; |
| 806 | pneg_ctxt->Name[9] = 0x23; |
| 807 | pneg_ctxt->Name[10] = 0x83; |
| 808 | pneg_ctxt->Name[11] = 0xDE; |
| 809 | pneg_ctxt->Name[12] = 0x96; |
| 810 | pneg_ctxt->Name[13] = 0x8B; |
| 811 | pneg_ctxt->Name[14] = 0xCD; |
| 812 | pneg_ctxt->Name[15] = 0x7C; |
| 813 | } |
| 814 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 815 | static void assemble_neg_contexts(struct ksmbd_conn *conn, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 816 | struct smb2_negotiate_rsp *rsp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 817 | { |
| 818 | /* +4 is to account for the RFC1001 len field */ |
| 819 | char *pneg_ctxt = (char *)rsp + |
| 820 | le32_to_cpu(rsp->NegotiateContextOffset) + 4; |
| 821 | int neg_ctxt_cnt = 1; |
| 822 | int ctxt_size; |
| 823 | |
| 824 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 825 | "assemble SMB2_PREAUTH_INTEGRITY_CAPABILITIES context\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 826 | build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 827 | conn->preauth_info->Preauth_HashId); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 828 | rsp->NegotiateContextCount = cpu_to_le16(neg_ctxt_cnt); |
| 829 | inc_rfc1001_len(rsp, AUTH_GSS_PADDING); |
| 830 | ctxt_size = sizeof(struct smb2_preauth_neg_context); |
| 831 | /* Round to 8 byte boundary */ |
| 832 | pneg_ctxt += round_up(sizeof(struct smb2_preauth_neg_context), 8); |
| 833 | |
| 834 | if (conn->cipher_type) { |
| 835 | ctxt_size = round_up(ctxt_size, 8); |
| 836 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 837 | "assemble SMB2_ENCRYPTION_CAPABILITIES context\n"); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 838 | build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 839 | conn->cipher_type); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 840 | rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt); |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 841 | ctxt_size += sizeof(struct smb2_encryption_neg_context) + 2; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 842 | /* Round to 8 byte boundary */ |
| 843 | pneg_ctxt += |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 844 | round_up(sizeof(struct smb2_encryption_neg_context) + 2, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 845 | 8); |
| 846 | } |
| 847 | |
| 848 | if (conn->compress_algorithm) { |
| 849 | ctxt_size = round_up(ctxt_size, 8); |
| 850 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 851 | "assemble SMB2_COMPRESSION_CAPABILITIES context\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 852 | /* Temporarily set to SMB3_COMPRESS_NONE */ |
| 853 | build_compression_ctxt((struct smb2_compression_ctx *)pneg_ctxt, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 854 | conn->compress_algorithm); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 855 | rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt); |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 856 | ctxt_size += sizeof(struct smb2_compression_ctx) + 2; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 857 | /* Round to 8 byte boundary */ |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 858 | pneg_ctxt += round_up(sizeof(struct smb2_compression_ctx) + 2, |
| 859 | 8); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | if (conn->posix_ext_supported) { |
| 863 | ctxt_size = round_up(ctxt_size, 8); |
| 864 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 865 | "assemble SMB2_POSIX_EXTENSIONS_AVAILABLE context\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 866 | build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt); |
| 867 | rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt); |
| 868 | ctxt_size += sizeof(struct smb2_posix_neg_context); |
Namjae Jeon | 378087c | 2021-07-21 10:05:53 +0900 | [diff] [blame] | 869 | /* Round to 8 byte boundary */ |
| 870 | pneg_ctxt += round_up(sizeof(struct smb2_posix_neg_context), 8); |
| 871 | } |
| 872 | |
| 873 | if (conn->signing_negotiated) { |
| 874 | ctxt_size = round_up(ctxt_size, 8); |
| 875 | ksmbd_debug(SMB, |
| 876 | "assemble SMB2_SIGNING_CAPABILITIES context\n"); |
| 877 | build_sign_cap_ctxt((struct smb2_signing_capabilities *)pneg_ctxt, |
| 878 | conn->signing_algorithm); |
| 879 | rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt); |
| 880 | ctxt_size += sizeof(struct smb2_signing_capabilities) + 2; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | inc_rfc1001_len(rsp, ctxt_size); |
| 884 | } |
| 885 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 886 | static __le32 decode_preauth_ctxt(struct ksmbd_conn *conn, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 887 | struct smb2_preauth_neg_context *pneg_ctxt) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 888 | { |
| 889 | __le32 err = STATUS_NO_PREAUTH_INTEGRITY_HASH_OVERLAP; |
| 890 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 891 | if (pneg_ctxt->HashAlgorithms == SMB2_PREAUTH_INTEGRITY_SHA512) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 892 | conn->preauth_info->Preauth_HashId = |
| 893 | SMB2_PREAUTH_INTEGRITY_SHA512; |
| 894 | err = STATUS_SUCCESS; |
| 895 | } |
| 896 | |
| 897 | return err; |
| 898 | } |
| 899 | |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 900 | static void decode_encrypt_ctxt(struct ksmbd_conn *conn, |
| 901 | struct smb2_encryption_neg_context *pneg_ctxt, |
| 902 | int len_of_ctxts) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 903 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 904 | int cph_cnt = le16_to_cpu(pneg_ctxt->CipherCount); |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 905 | int i, cphs_size = cph_cnt * sizeof(__le16); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 906 | |
| 907 | conn->cipher_type = 0; |
| 908 | |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 909 | if (sizeof(struct smb2_encryption_neg_context) + cphs_size > |
| 910 | len_of_ctxts) { |
| 911 | pr_err("Invalid cipher count(%d)\n", cph_cnt); |
| 912 | return; |
| 913 | } |
| 914 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 915 | if (!(server_conf.flags & KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION)) |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 916 | return; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 917 | |
| 918 | for (i = 0; i < cph_cnt; i++) { |
| 919 | if (pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES128_GCM || |
Namjae Jeon | 5a0ca77 | 2021-05-06 11:43:37 +0900 | [diff] [blame] | 920 | pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES128_CCM || |
| 921 | pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES256_CCM || |
| 922 | pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES256_GCM) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 923 | ksmbd_debug(SMB, "Cipher ID = 0x%x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 924 | pneg_ctxt->Ciphers[i]); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 925 | conn->cipher_type = pneg_ctxt->Ciphers[i]; |
| 926 | break; |
| 927 | } |
| 928 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 929 | } |
| 930 | |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 931 | static void decode_compress_ctxt(struct ksmbd_conn *conn, |
| 932 | struct smb2_compression_ctx *pneg_ctxt) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 933 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 934 | conn->compress_algorithm = SMB3_COMPRESS_NONE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 935 | } |
| 936 | |
Namjae Jeon | 378087c | 2021-07-21 10:05:53 +0900 | [diff] [blame] | 937 | static void decode_sign_cap_ctxt(struct ksmbd_conn *conn, |
| 938 | struct smb2_signing_capabilities *pneg_ctxt, |
| 939 | int len_of_ctxts) |
| 940 | { |
| 941 | int sign_algo_cnt = le16_to_cpu(pneg_ctxt->SigningAlgorithmCount); |
| 942 | int i, sign_alos_size = sign_algo_cnt * sizeof(__le16); |
| 943 | |
| 944 | conn->signing_negotiated = false; |
| 945 | |
| 946 | if (sizeof(struct smb2_signing_capabilities) + sign_alos_size > |
| 947 | len_of_ctxts) { |
| 948 | pr_err("Invalid signing algorithm count(%d)\n", sign_algo_cnt); |
| 949 | return; |
| 950 | } |
| 951 | |
| 952 | for (i = 0; i < sign_algo_cnt; i++) { |
| 953 | if (pneg_ctxt->SigningAlgorithms[i] == SIGNING_ALG_HMAC_SHA256 || |
| 954 | pneg_ctxt->SigningAlgorithms[i] == SIGNING_ALG_AES_CMAC) { |
| 955 | ksmbd_debug(SMB, "Signing Algorithm ID = 0x%x\n", |
| 956 | pneg_ctxt->SigningAlgorithms[i]); |
| 957 | conn->signing_negotiated = true; |
| 958 | conn->signing_algorithm = |
| 959 | pneg_ctxt->SigningAlgorithms[i]; |
| 960 | break; |
| 961 | } |
| 962 | } |
| 963 | } |
| 964 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 965 | static __le32 deassemble_neg_contexts(struct ksmbd_conn *conn, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 966 | struct smb2_negotiate_req *req) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 967 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 968 | /* +4 is to account for the RFC1001 len field */ |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 969 | struct smb2_neg_context *pctx = (struct smb2_neg_context *)((char *)req + 4); |
| 970 | int i = 0, len_of_ctxts; |
| 971 | int offset = le32_to_cpu(req->NegotiateContextOffset); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 972 | int neg_ctxt_cnt = le16_to_cpu(req->NegotiateContextCount); |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 973 | int len_of_smb = be32_to_cpu(req->hdr.smb2_buf_length); |
| 974 | __le32 status = STATUS_INVALID_PARAMETER; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 975 | |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 976 | ksmbd_debug(SMB, "decoding %d negotiate contexts\n", neg_ctxt_cnt); |
| 977 | if (len_of_smb <= offset) { |
| 978 | ksmbd_debug(SMB, "Invalid response: negotiate context offset\n"); |
| 979 | return status; |
| 980 | } |
| 981 | |
| 982 | len_of_ctxts = len_of_smb - offset; |
| 983 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 984 | while (i++ < neg_ctxt_cnt) { |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 985 | int clen; |
| 986 | |
| 987 | /* check that offset is not beyond end of SMB */ |
| 988 | if (len_of_ctxts == 0) |
| 989 | break; |
| 990 | |
| 991 | if (len_of_ctxts < sizeof(struct smb2_neg_context)) |
| 992 | break; |
| 993 | |
| 994 | pctx = (struct smb2_neg_context *)((char *)pctx + offset); |
| 995 | clen = le16_to_cpu(pctx->DataLength); |
| 996 | if (clen + sizeof(struct smb2_neg_context) > len_of_ctxts) |
| 997 | break; |
| 998 | |
| 999 | if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1000 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1001 | "deassemble SMB2_PREAUTH_INTEGRITY_CAPABILITIES context\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1002 | if (conn->preauth_info->Preauth_HashId) |
| 1003 | break; |
| 1004 | |
| 1005 | status = decode_preauth_ctxt(conn, |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 1006 | (struct smb2_preauth_neg_context *)pctx); |
| 1007 | if (status != STATUS_SUCCESS) |
| 1008 | break; |
| 1009 | } else if (pctx->ContextType == SMB2_ENCRYPTION_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_ENCRYPTION_CAPABILITIES context\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1012 | if (conn->cipher_type) |
| 1013 | break; |
| 1014 | |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 1015 | decode_encrypt_ctxt(conn, |
| 1016 | (struct smb2_encryption_neg_context *)pctx, |
| 1017 | len_of_ctxts); |
| 1018 | } else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1019 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1020 | "deassemble SMB2_COMPRESSION_CAPABILITIES context\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1021 | if (conn->compress_algorithm) |
| 1022 | break; |
| 1023 | |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 1024 | decode_compress_ctxt(conn, |
| 1025 | (struct smb2_compression_ctx *)pctx); |
| 1026 | } else if (pctx->ContextType == SMB2_NETNAME_NEGOTIATE_CONTEXT_ID) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1027 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1028 | "deassemble SMB2_NETNAME_NEGOTIATE_CONTEXT_ID context\n"); |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 1029 | } else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1030 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1031 | "deassemble SMB2_POSIX_EXTENSIONS_AVAILABLE context\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1032 | conn->posix_ext_supported = true; |
Namjae Jeon | 378087c | 2021-07-21 10:05:53 +0900 | [diff] [blame] | 1033 | } else if (pctx->ContextType == SMB2_SIGNING_CAPABILITIES) { |
| 1034 | ksmbd_debug(SMB, |
| 1035 | "deassemble SMB2_SIGNING_CAPABILITIES context\n"); |
| 1036 | decode_sign_cap_ctxt(conn, |
| 1037 | (struct smb2_signing_capabilities *)pctx, |
| 1038 | len_of_ctxts); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1039 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1040 | |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 1041 | /* offsets must be 8 byte aligned */ |
| 1042 | clen = (clen + 7) & ~0x7; |
| 1043 | offset = clen + sizeof(struct smb2_neg_context); |
| 1044 | len_of_ctxts -= clen + sizeof(struct smb2_neg_context); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1045 | } |
| 1046 | return status; |
| 1047 | } |
| 1048 | |
| 1049 | /** |
| 1050 | * smb2_handle_negotiate() - handler for smb2 negotiate command |
| 1051 | * @work: smb work containing smb request buffer |
| 1052 | * |
| 1053 | * Return: 0 |
| 1054 | */ |
| 1055 | int smb2_handle_negotiate(struct ksmbd_work *work) |
| 1056 | { |
| 1057 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 1058 | struct smb2_negotiate_req *req = work->request_buf; |
| 1059 | struct smb2_negotiate_rsp *rsp = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1060 | int rc = 0; |
| 1061 | __le32 status; |
| 1062 | |
| 1063 | ksmbd_debug(SMB, "Received negotiate request\n"); |
| 1064 | conn->need_neg = false; |
| 1065 | if (ksmbd_conn_good(work)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 1066 | pr_err("conn->tcp_status is already in CifsGood State\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1067 | work->send_no_response = 1; |
| 1068 | return rc; |
| 1069 | } |
| 1070 | |
| 1071 | if (req->DialectCount == 0) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 1072 | pr_err("malformed packet\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1073 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 1074 | rc = -EINVAL; |
| 1075 | goto err_out; |
| 1076 | } |
| 1077 | |
| 1078 | conn->cli_cap = le32_to_cpu(req->Capabilities); |
| 1079 | switch (conn->dialect) { |
| 1080 | case SMB311_PROT_ID: |
| 1081 | conn->preauth_info = |
| 1082 | kzalloc(sizeof(struct preauth_integrity_info), |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1083 | GFP_KERNEL); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1084 | if (!conn->preauth_info) { |
| 1085 | rc = -ENOMEM; |
| 1086 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 1087 | goto err_out; |
| 1088 | } |
| 1089 | |
| 1090 | status = deassemble_neg_contexts(conn, req); |
| 1091 | if (status != STATUS_SUCCESS) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 1092 | pr_err("deassemble_neg_contexts error(0x%x)\n", |
| 1093 | status); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1094 | rsp->hdr.Status = status; |
| 1095 | rc = -EINVAL; |
| 1096 | goto err_out; |
| 1097 | } |
| 1098 | |
| 1099 | rc = init_smb3_11_server(conn); |
| 1100 | if (rc < 0) { |
| 1101 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 1102 | goto err_out; |
| 1103 | } |
| 1104 | |
| 1105 | ksmbd_gen_preauth_integrity_hash(conn, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1106 | work->request_buf, |
| 1107 | conn->preauth_info->Preauth_HashValue); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1108 | rsp->NegotiateContextOffset = |
| 1109 | cpu_to_le32(OFFSET_OF_NEG_CONTEXT); |
| 1110 | assemble_neg_contexts(conn, rsp); |
| 1111 | break; |
| 1112 | case SMB302_PROT_ID: |
| 1113 | init_smb3_02_server(conn); |
| 1114 | break; |
| 1115 | case SMB30_PROT_ID: |
| 1116 | init_smb3_0_server(conn); |
| 1117 | break; |
| 1118 | case SMB21_PROT_ID: |
| 1119 | init_smb2_1_server(conn); |
| 1120 | break; |
| 1121 | case SMB20_PROT_ID: |
| 1122 | rc = init_smb2_0_server(conn); |
| 1123 | if (rc) { |
| 1124 | rsp->hdr.Status = STATUS_NOT_SUPPORTED; |
| 1125 | goto err_out; |
| 1126 | } |
| 1127 | break; |
| 1128 | case SMB2X_PROT_ID: |
| 1129 | case BAD_PROT_ID: |
| 1130 | default: |
| 1131 | ksmbd_debug(SMB, "Server dialect :0x%x not supported\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1132 | conn->dialect); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1133 | rsp->hdr.Status = STATUS_NOT_SUPPORTED; |
| 1134 | rc = -EINVAL; |
| 1135 | goto err_out; |
| 1136 | } |
| 1137 | rsp->Capabilities = cpu_to_le32(conn->vals->capabilities); |
| 1138 | |
| 1139 | /* For stats */ |
| 1140 | conn->connection_type = conn->dialect; |
| 1141 | |
| 1142 | rsp->MaxTransactSize = cpu_to_le32(conn->vals->max_trans_size); |
| 1143 | rsp->MaxReadSize = cpu_to_le32(conn->vals->max_read_size); |
| 1144 | rsp->MaxWriteSize = cpu_to_le32(conn->vals->max_write_size); |
| 1145 | |
| 1146 | if (conn->dialect > SMB20_PROT_ID) { |
| 1147 | memcpy(conn->ClientGUID, req->ClientGUID, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1148 | SMB2_CLIENT_GUID_SIZE); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1149 | conn->cli_sec_mode = le16_to_cpu(req->SecurityMode); |
| 1150 | } |
| 1151 | |
| 1152 | rsp->StructureSize = cpu_to_le16(65); |
| 1153 | rsp->DialectRevision = cpu_to_le16(conn->dialect); |
| 1154 | /* Not setting conn guid rsp->ServerGUID, as it |
| 1155 | * not used by client for identifying server |
| 1156 | */ |
| 1157 | memset(rsp->ServerGUID, 0, SMB2_CLIENT_GUID_SIZE); |
| 1158 | |
| 1159 | rsp->SystemTime = cpu_to_le64(ksmbd_systime()); |
| 1160 | rsp->ServerStartTime = 0; |
| 1161 | ksmbd_debug(SMB, "negotiate context offset %d, count %d\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1162 | le32_to_cpu(rsp->NegotiateContextOffset), |
| 1163 | le16_to_cpu(rsp->NegotiateContextCount)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1164 | |
| 1165 | rsp->SecurityBufferOffset = cpu_to_le16(128); |
| 1166 | rsp->SecurityBufferLength = cpu_to_le16(AUTH_GSS_LENGTH); |
| 1167 | ksmbd_copy_gss_neg_header(((char *)(&rsp->hdr) + |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1168 | sizeof(rsp->hdr.smb2_buf_length)) + |
| 1169 | le16_to_cpu(rsp->SecurityBufferOffset)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1170 | inc_rfc1001_len(rsp, sizeof(struct smb2_negotiate_rsp) - |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1171 | sizeof(struct smb2_hdr) - sizeof(rsp->Buffer) + |
| 1172 | AUTH_GSS_LENGTH); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1173 | rsp->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED_LE; |
| 1174 | conn->use_spnego = true; |
| 1175 | |
| 1176 | if ((server_conf.signing == KSMBD_CONFIG_OPT_AUTO || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1177 | server_conf.signing == KSMBD_CONFIG_OPT_DISABLED) && |
| 1178 | req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED_LE) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1179 | conn->sign = true; |
| 1180 | else if (server_conf.signing == KSMBD_CONFIG_OPT_MANDATORY) { |
| 1181 | server_conf.enforced_signing = true; |
| 1182 | rsp->SecurityMode |= SMB2_NEGOTIATE_SIGNING_REQUIRED_LE; |
| 1183 | conn->sign = true; |
| 1184 | } |
| 1185 | |
| 1186 | conn->srv_sec_mode = le16_to_cpu(rsp->SecurityMode); |
| 1187 | ksmbd_conn_set_need_negotiate(work); |
| 1188 | |
| 1189 | err_out: |
| 1190 | if (rc < 0) |
| 1191 | smb2_set_err_rsp(work); |
| 1192 | |
| 1193 | return rc; |
| 1194 | } |
| 1195 | |
| 1196 | static int alloc_preauth_hash(struct ksmbd_session *sess, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1197 | struct ksmbd_conn *conn) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1198 | { |
| 1199 | if (sess->Preauth_HashValue) |
| 1200 | return 0; |
| 1201 | |
kernel test robot | 86f5297 | 2021-04-02 12:17:24 +0900 | [diff] [blame] | 1202 | sess->Preauth_HashValue = kmemdup(conn->preauth_info->Preauth_HashValue, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1203 | PREAUTH_HASHVALUE_SIZE, GFP_KERNEL); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1204 | if (!sess->Preauth_HashValue) |
| 1205 | return -ENOMEM; |
| 1206 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1207 | return 0; |
| 1208 | } |
| 1209 | |
| 1210 | static int generate_preauth_hash(struct ksmbd_work *work) |
| 1211 | { |
| 1212 | struct ksmbd_conn *conn = work->conn; |
| 1213 | struct ksmbd_session *sess = work->sess; |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1214 | u8 *preauth_hash; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1215 | |
| 1216 | if (conn->dialect != SMB311_PROT_ID) |
| 1217 | return 0; |
| 1218 | |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1219 | if (conn->binding) { |
| 1220 | struct preauth_session *preauth_sess; |
| 1221 | |
| 1222 | preauth_sess = ksmbd_preauth_session_lookup(conn, sess->id); |
| 1223 | if (!preauth_sess) { |
| 1224 | preauth_sess = ksmbd_preauth_session_alloc(conn, sess->id); |
| 1225 | if (!preauth_sess) |
| 1226 | return -ENOMEM; |
| 1227 | } |
| 1228 | |
| 1229 | preauth_hash = preauth_sess->Preauth_HashValue; |
| 1230 | } else { |
| 1231 | if (!sess->Preauth_HashValue) |
| 1232 | if (alloc_preauth_hash(sess, conn)) |
| 1233 | return -ENOMEM; |
| 1234 | preauth_hash = sess->Preauth_HashValue; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1235 | } |
| 1236 | |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1237 | ksmbd_gen_preauth_integrity_hash(conn, work->request_buf, preauth_hash); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1238 | return 0; |
| 1239 | } |
| 1240 | |
| 1241 | static int decode_negotiation_token(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1242 | struct negotiate_message *negblob) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1243 | { |
| 1244 | struct ksmbd_conn *conn = work->conn; |
| 1245 | struct smb2_sess_setup_req *req; |
| 1246 | int sz; |
| 1247 | |
| 1248 | if (!conn->use_spnego) |
| 1249 | return -EINVAL; |
| 1250 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 1251 | req = work->request_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1252 | sz = le16_to_cpu(req->SecurityBufferLength); |
| 1253 | |
Hyunchul Lee | fad4161 | 2021-04-19 17:26:15 +0900 | [diff] [blame] | 1254 | if (ksmbd_decode_negTokenInit((char *)negblob, sz, conn)) { |
| 1255 | if (ksmbd_decode_negTokenTarg((char *)negblob, sz, conn)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1256 | conn->auth_mechs |= KSMBD_AUTH_NTLMSSP; |
| 1257 | conn->preferred_auth_mech = KSMBD_AUTH_NTLMSSP; |
| 1258 | conn->use_spnego = false; |
| 1259 | } |
| 1260 | } |
| 1261 | return 0; |
| 1262 | } |
| 1263 | |
| 1264 | static int ntlm_negotiate(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1265 | struct negotiate_message *negblob) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1266 | { |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 1267 | struct smb2_sess_setup_req *req = work->request_buf; |
| 1268 | struct smb2_sess_setup_rsp *rsp = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1269 | struct challenge_message *chgblob; |
| 1270 | unsigned char *spnego_blob = NULL; |
| 1271 | u16 spnego_blob_len; |
| 1272 | char *neg_blob; |
| 1273 | int sz, rc; |
| 1274 | |
| 1275 | ksmbd_debug(SMB, "negotiate phase\n"); |
| 1276 | sz = le16_to_cpu(req->SecurityBufferLength); |
| 1277 | rc = ksmbd_decode_ntlmssp_neg_blob(negblob, sz, work->sess); |
| 1278 | if (rc) |
| 1279 | return rc; |
| 1280 | |
| 1281 | sz = le16_to_cpu(rsp->SecurityBufferOffset); |
| 1282 | chgblob = |
| 1283 | (struct challenge_message *)((char *)&rsp->hdr.ProtocolId + sz); |
| 1284 | memset(chgblob, 0, sizeof(struct challenge_message)); |
| 1285 | |
| 1286 | if (!work->conn->use_spnego) { |
| 1287 | sz = ksmbd_build_ntlmssp_challenge_blob(chgblob, work->sess); |
| 1288 | if (sz < 0) |
| 1289 | return -ENOMEM; |
| 1290 | |
| 1291 | rsp->SecurityBufferLength = cpu_to_le16(sz); |
| 1292 | return 0; |
| 1293 | } |
| 1294 | |
| 1295 | sz = sizeof(struct challenge_message); |
| 1296 | sz += (strlen(ksmbd_netbios_name()) * 2 + 1 + 4) * 6; |
| 1297 | |
| 1298 | neg_blob = kzalloc(sz, GFP_KERNEL); |
| 1299 | if (!neg_blob) |
| 1300 | return -ENOMEM; |
| 1301 | |
| 1302 | chgblob = (struct challenge_message *)neg_blob; |
| 1303 | sz = ksmbd_build_ntlmssp_challenge_blob(chgblob, work->sess); |
| 1304 | if (sz < 0) { |
| 1305 | rc = -ENOMEM; |
| 1306 | goto out; |
| 1307 | } |
| 1308 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1309 | rc = build_spnego_ntlmssp_neg_blob(&spnego_blob, &spnego_blob_len, |
| 1310 | neg_blob, sz); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1311 | if (rc) { |
| 1312 | rc = -ENOMEM; |
| 1313 | goto out; |
| 1314 | } |
| 1315 | |
| 1316 | sz = le16_to_cpu(rsp->SecurityBufferOffset); |
| 1317 | memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len); |
| 1318 | rsp->SecurityBufferLength = cpu_to_le16(spnego_blob_len); |
| 1319 | |
| 1320 | out: |
| 1321 | kfree(spnego_blob); |
| 1322 | kfree(neg_blob); |
| 1323 | return rc; |
| 1324 | } |
| 1325 | |
| 1326 | static struct authenticate_message *user_authblob(struct ksmbd_conn *conn, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1327 | struct smb2_sess_setup_req *req) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1328 | { |
| 1329 | int sz; |
| 1330 | |
| 1331 | if (conn->use_spnego && conn->mechToken) |
| 1332 | return (struct authenticate_message *)conn->mechToken; |
| 1333 | |
| 1334 | sz = le16_to_cpu(req->SecurityBufferOffset); |
| 1335 | return (struct authenticate_message *)((char *)&req->hdr.ProtocolId |
| 1336 | + sz); |
| 1337 | } |
| 1338 | |
| 1339 | static struct ksmbd_user *session_user(struct ksmbd_conn *conn, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1340 | struct smb2_sess_setup_req *req) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1341 | { |
| 1342 | struct authenticate_message *authblob; |
| 1343 | struct ksmbd_user *user; |
| 1344 | char *name; |
| 1345 | int sz; |
| 1346 | |
| 1347 | authblob = user_authblob(conn, req); |
| 1348 | sz = le32_to_cpu(authblob->UserName.BufferOffset); |
| 1349 | name = smb_strndup_from_utf16((const char *)authblob + sz, |
| 1350 | le16_to_cpu(authblob->UserName.Length), |
| 1351 | true, |
| 1352 | conn->local_nls); |
| 1353 | if (IS_ERR(name)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 1354 | pr_err("cannot allocate memory\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1355 | return NULL; |
| 1356 | } |
| 1357 | |
| 1358 | ksmbd_debug(SMB, "session setup request for user %s\n", name); |
| 1359 | user = ksmbd_login_user(name); |
| 1360 | kfree(name); |
| 1361 | return user; |
| 1362 | } |
| 1363 | |
| 1364 | static int ntlm_authenticate(struct ksmbd_work *work) |
| 1365 | { |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 1366 | struct smb2_sess_setup_req *req = work->request_buf; |
| 1367 | struct smb2_sess_setup_rsp *rsp = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1368 | struct ksmbd_conn *conn = work->conn; |
| 1369 | struct ksmbd_session *sess = work->sess; |
| 1370 | struct channel *chann = NULL; |
| 1371 | struct ksmbd_user *user; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1372 | u64 prev_id; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1373 | int sz, rc; |
| 1374 | |
| 1375 | ksmbd_debug(SMB, "authenticate phase\n"); |
| 1376 | if (conn->use_spnego) { |
| 1377 | unsigned char *spnego_blob; |
| 1378 | u16 spnego_blob_len; |
| 1379 | |
| 1380 | rc = build_spnego_ntlmssp_auth_blob(&spnego_blob, |
| 1381 | &spnego_blob_len, |
| 1382 | 0); |
| 1383 | if (rc) |
| 1384 | return -ENOMEM; |
| 1385 | |
| 1386 | sz = le16_to_cpu(rsp->SecurityBufferOffset); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1387 | memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1388 | rsp->SecurityBufferLength = cpu_to_le16(spnego_blob_len); |
| 1389 | kfree(spnego_blob); |
| 1390 | inc_rfc1001_len(rsp, spnego_blob_len - 1); |
| 1391 | } |
| 1392 | |
| 1393 | user = session_user(conn, req); |
| 1394 | if (!user) { |
| 1395 | ksmbd_debug(SMB, "Unknown user name or an error\n"); |
Namjae Jeon | 58090b1 | 2021-07-16 14:52:09 +0900 | [diff] [blame] | 1396 | return -EPERM; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | /* Check for previous session */ |
| 1400 | prev_id = le64_to_cpu(req->PreviousSessionId); |
| 1401 | if (prev_id && prev_id != sess->id) |
| 1402 | destroy_previous_session(user, prev_id); |
| 1403 | |
| 1404 | if (sess->state == SMB2_SESSION_VALID) { |
| 1405 | /* |
| 1406 | * Reuse session if anonymous try to connect |
| 1407 | * on reauthetication. |
| 1408 | */ |
| 1409 | if (ksmbd_anonymous_user(user)) { |
| 1410 | ksmbd_free_user(user); |
| 1411 | return 0; |
| 1412 | } |
| 1413 | ksmbd_free_user(sess->user); |
| 1414 | } |
| 1415 | |
| 1416 | sess->user = user; |
| 1417 | if (user_guest(sess->user)) { |
| 1418 | if (conn->sign) { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1419 | ksmbd_debug(SMB, "Guest login not allowed when signing enabled\n"); |
Namjae Jeon | 58090b1 | 2021-07-16 14:52:09 +0900 | [diff] [blame] | 1420 | return -EPERM; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1421 | } |
| 1422 | |
| 1423 | rsp->SessionFlags = SMB2_SESSION_FLAG_IS_GUEST_LE; |
| 1424 | } else { |
| 1425 | struct authenticate_message *authblob; |
| 1426 | |
| 1427 | authblob = user_authblob(conn, req); |
| 1428 | sz = le16_to_cpu(req->SecurityBufferLength); |
| 1429 | rc = ksmbd_decode_ntlmssp_auth_blob(authblob, sz, sess); |
| 1430 | if (rc) { |
| 1431 | set_user_flag(sess->user, KSMBD_USER_FLAG_BAD_PASSWORD); |
| 1432 | ksmbd_debug(SMB, "authentication failed\n"); |
Namjae Jeon | 58090b1 | 2021-07-16 14:52:09 +0900 | [diff] [blame] | 1433 | return -EPERM; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | /* |
| 1437 | * If session state is SMB2_SESSION_VALID, We can assume |
| 1438 | * that it is reauthentication. And the user/password |
| 1439 | * has been verified, so return it here. |
| 1440 | */ |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1441 | if (sess->state == SMB2_SESSION_VALID) { |
| 1442 | if (conn->binding) |
| 1443 | goto binding_session; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1444 | return 0; |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1445 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1446 | |
| 1447 | if ((conn->sign || server_conf.enforced_signing) || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1448 | (req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1449 | sess->sign = true; |
| 1450 | |
| 1451 | if (conn->vals->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION && |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1452 | conn->ops->generate_encryptionkey && |
| 1453 | !(req->Flags & SMB2_SESSION_REQ_FLAG_BINDING)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1454 | rc = conn->ops->generate_encryptionkey(sess); |
| 1455 | if (rc) { |
| 1456 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1457 | "SMB3 encryption key generation failed\n"); |
Namjae Jeon | 58090b1 | 2021-07-16 14:52:09 +0900 | [diff] [blame] | 1458 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1459 | } |
| 1460 | sess->enc = true; |
| 1461 | rsp->SessionFlags = SMB2_SESSION_FLAG_ENCRYPT_DATA_LE; |
| 1462 | /* |
| 1463 | * signing is disable if encryption is enable |
| 1464 | * on this session |
| 1465 | */ |
| 1466 | sess->sign = false; |
| 1467 | } |
| 1468 | } |
| 1469 | |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1470 | binding_session: |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1471 | if (conn->dialect >= SMB30_PROT_ID) { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1472 | chann = lookup_chann_list(sess, conn); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1473 | if (!chann) { |
| 1474 | chann = kmalloc(sizeof(struct channel), GFP_KERNEL); |
| 1475 | if (!chann) |
| 1476 | return -ENOMEM; |
| 1477 | |
| 1478 | chann->conn = conn; |
| 1479 | INIT_LIST_HEAD(&chann->chann_list); |
| 1480 | list_add(&chann->chann_list, &sess->ksmbd_chann_list); |
| 1481 | } |
| 1482 | } |
| 1483 | |
| 1484 | if (conn->ops->generate_signingkey) { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1485 | rc = conn->ops->generate_signingkey(sess, conn); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1486 | if (rc) { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1487 | ksmbd_debug(SMB, "SMB3 signing key generation failed\n"); |
Namjae Jeon | 58090b1 | 2021-07-16 14:52:09 +0900 | [diff] [blame] | 1488 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1489 | } |
| 1490 | } |
| 1491 | |
| 1492 | if (conn->dialect > SMB20_PROT_ID) { |
| 1493 | if (!ksmbd_conn_lookup_dialect(conn)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 1494 | pr_err("fail to verify the dialect\n"); |
Namjae Jeon | 58090b1 | 2021-07-16 14:52:09 +0900 | [diff] [blame] | 1495 | return -ENOENT; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1496 | } |
| 1497 | } |
| 1498 | return 0; |
| 1499 | } |
| 1500 | |
| 1501 | #ifdef CONFIG_SMB_SERVER_KERBEROS5 |
| 1502 | static int krb5_authenticate(struct ksmbd_work *work) |
| 1503 | { |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 1504 | struct smb2_sess_setup_req *req = work->request_buf; |
| 1505 | struct smb2_sess_setup_rsp *rsp = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1506 | struct ksmbd_conn *conn = work->conn; |
| 1507 | struct ksmbd_session *sess = work->sess; |
| 1508 | char *in_blob, *out_blob; |
| 1509 | struct channel *chann = NULL; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1510 | u64 prev_sess_id; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1511 | int in_len, out_len; |
| 1512 | int retval; |
| 1513 | |
| 1514 | in_blob = (char *)&req->hdr.ProtocolId + |
| 1515 | le16_to_cpu(req->SecurityBufferOffset); |
| 1516 | in_len = le16_to_cpu(req->SecurityBufferLength); |
| 1517 | out_blob = (char *)&rsp->hdr.ProtocolId + |
| 1518 | le16_to_cpu(rsp->SecurityBufferOffset); |
| 1519 | out_len = work->response_sz - |
| 1520 | offsetof(struct smb2_hdr, smb2_buf_length) - |
| 1521 | le16_to_cpu(rsp->SecurityBufferOffset); |
| 1522 | |
| 1523 | /* Check previous session */ |
| 1524 | prev_sess_id = le64_to_cpu(req->PreviousSessionId); |
| 1525 | if (prev_sess_id && prev_sess_id != sess->id) |
| 1526 | destroy_previous_session(sess->user, prev_sess_id); |
| 1527 | |
| 1528 | if (sess->state == SMB2_SESSION_VALID) |
| 1529 | ksmbd_free_user(sess->user); |
| 1530 | |
| 1531 | retval = ksmbd_krb5_authenticate(sess, in_blob, in_len, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1532 | out_blob, &out_len); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1533 | if (retval) { |
| 1534 | ksmbd_debug(SMB, "krb5 authentication failed\n"); |
Namjae Jeon | 58090b1 | 2021-07-16 14:52:09 +0900 | [diff] [blame] | 1535 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1536 | } |
| 1537 | rsp->SecurityBufferLength = cpu_to_le16(out_len); |
| 1538 | inc_rfc1001_len(rsp, out_len - 1); |
| 1539 | |
| 1540 | if ((conn->sign || server_conf.enforced_signing) || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1541 | (req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1542 | sess->sign = true; |
| 1543 | |
| 1544 | if ((conn->vals->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION) && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1545 | conn->ops->generate_encryptionkey) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1546 | retval = conn->ops->generate_encryptionkey(sess); |
| 1547 | if (retval) { |
| 1548 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1549 | "SMB3 encryption key generation failed\n"); |
Namjae Jeon | 58090b1 | 2021-07-16 14:52:09 +0900 | [diff] [blame] | 1550 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1551 | } |
| 1552 | sess->enc = true; |
| 1553 | rsp->SessionFlags = SMB2_SESSION_FLAG_ENCRYPT_DATA_LE; |
| 1554 | sess->sign = false; |
| 1555 | } |
| 1556 | |
| 1557 | if (conn->dialect >= SMB30_PROT_ID) { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1558 | chann = lookup_chann_list(sess, conn); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1559 | if (!chann) { |
| 1560 | chann = kmalloc(sizeof(struct channel), GFP_KERNEL); |
| 1561 | if (!chann) |
| 1562 | return -ENOMEM; |
| 1563 | |
| 1564 | chann->conn = conn; |
| 1565 | INIT_LIST_HEAD(&chann->chann_list); |
| 1566 | list_add(&chann->chann_list, &sess->ksmbd_chann_list); |
| 1567 | } |
| 1568 | } |
| 1569 | |
| 1570 | if (conn->ops->generate_signingkey) { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1571 | retval = conn->ops->generate_signingkey(sess, conn); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1572 | if (retval) { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1573 | ksmbd_debug(SMB, "SMB3 signing key generation failed\n"); |
Namjae Jeon | 58090b1 | 2021-07-16 14:52:09 +0900 | [diff] [blame] | 1574 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1575 | } |
| 1576 | } |
| 1577 | |
| 1578 | if (conn->dialect > SMB20_PROT_ID) { |
| 1579 | if (!ksmbd_conn_lookup_dialect(conn)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 1580 | pr_err("fail to verify the dialect\n"); |
Namjae Jeon | 58090b1 | 2021-07-16 14:52:09 +0900 | [diff] [blame] | 1581 | return -ENOENT; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1582 | } |
| 1583 | } |
| 1584 | return 0; |
| 1585 | } |
| 1586 | #else |
| 1587 | static int krb5_authenticate(struct ksmbd_work *work) |
| 1588 | { |
| 1589 | return -EOPNOTSUPP; |
| 1590 | } |
| 1591 | #endif |
| 1592 | |
| 1593 | int smb2_sess_setup(struct ksmbd_work *work) |
| 1594 | { |
| 1595 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 1596 | struct smb2_sess_setup_req *req = work->request_buf; |
| 1597 | struct smb2_sess_setup_rsp *rsp = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1598 | struct ksmbd_session *sess; |
| 1599 | struct negotiate_message *negblob; |
| 1600 | int rc = 0; |
| 1601 | |
| 1602 | ksmbd_debug(SMB, "Received request for session setup\n"); |
| 1603 | |
| 1604 | rsp->StructureSize = cpu_to_le16(9); |
| 1605 | rsp->SessionFlags = 0; |
| 1606 | rsp->SecurityBufferOffset = cpu_to_le16(72); |
| 1607 | rsp->SecurityBufferLength = 0; |
| 1608 | inc_rfc1001_len(rsp, 9); |
| 1609 | |
| 1610 | if (!req->hdr.SessionId) { |
| 1611 | sess = ksmbd_smb2_session_create(); |
| 1612 | if (!sess) { |
| 1613 | rc = -ENOMEM; |
| 1614 | goto out_err; |
| 1615 | } |
| 1616 | rsp->hdr.SessionId = cpu_to_le64(sess->id); |
| 1617 | ksmbd_session_register(conn, sess); |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1618 | } else if (conn->dialect >= SMB30_PROT_ID && |
| 1619 | (server_conf.flags & KSMBD_GLOBAL_FLAG_SMB3_MULTICHANNEL) && |
| 1620 | req->Flags & SMB2_SESSION_REQ_FLAG_BINDING) { |
| 1621 | u64 sess_id = le64_to_cpu(req->hdr.SessionId); |
| 1622 | |
| 1623 | sess = ksmbd_session_lookup_slowpath(sess_id); |
| 1624 | if (!sess) { |
| 1625 | rc = -ENOENT; |
| 1626 | goto out_err; |
| 1627 | } |
| 1628 | |
| 1629 | if (conn->dialect != sess->conn->dialect) { |
| 1630 | rc = -EINVAL; |
| 1631 | goto out_err; |
| 1632 | } |
| 1633 | |
| 1634 | if (!(req->hdr.Flags & SMB2_FLAGS_SIGNED)) { |
| 1635 | rc = -EINVAL; |
| 1636 | goto out_err; |
| 1637 | } |
| 1638 | |
| 1639 | if (strncmp(conn->ClientGUID, sess->conn->ClientGUID, |
| 1640 | SMB2_CLIENT_GUID_SIZE)) { |
| 1641 | rc = -ENOENT; |
| 1642 | goto out_err; |
| 1643 | } |
| 1644 | |
| 1645 | if (sess->state == SMB2_SESSION_IN_PROGRESS) { |
| 1646 | rc = -EACCES; |
| 1647 | goto out_err; |
| 1648 | } |
| 1649 | |
| 1650 | if (sess->state == SMB2_SESSION_EXPIRED) { |
| 1651 | rc = -EFAULT; |
| 1652 | goto out_err; |
| 1653 | } |
| 1654 | |
| 1655 | if (ksmbd_session_lookup(conn, sess_id)) { |
| 1656 | rc = -EACCES; |
| 1657 | goto out_err; |
| 1658 | } |
| 1659 | |
| 1660 | conn->binding = true; |
| 1661 | } else if ((conn->dialect < SMB30_PROT_ID || |
| 1662 | server_conf.flags & KSMBD_GLOBAL_FLAG_SMB3_MULTICHANNEL) && |
| 1663 | (req->Flags & SMB2_SESSION_REQ_FLAG_BINDING)) { |
Colin Ian King | 4951a84 | 2021-07-06 13:05:01 +0100 | [diff] [blame] | 1664 | sess = NULL; |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1665 | rc = -EACCES; |
| 1666 | goto out_err; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1667 | } else { |
| 1668 | sess = ksmbd_session_lookup(conn, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1669 | le64_to_cpu(req->hdr.SessionId)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1670 | if (!sess) { |
| 1671 | rc = -ENOENT; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1672 | goto out_err; |
| 1673 | } |
| 1674 | } |
| 1675 | work->sess = sess; |
| 1676 | |
| 1677 | if (sess->state == SMB2_SESSION_EXPIRED) |
| 1678 | sess->state = SMB2_SESSION_IN_PROGRESS; |
| 1679 | |
| 1680 | negblob = (struct negotiate_message *)((char *)&req->hdr.ProtocolId + |
| 1681 | le16_to_cpu(req->SecurityBufferOffset)); |
| 1682 | |
| 1683 | if (decode_negotiation_token(work, negblob) == 0) { |
| 1684 | if (conn->mechToken) |
| 1685 | negblob = (struct negotiate_message *)conn->mechToken; |
| 1686 | } |
| 1687 | |
| 1688 | if (server_conf.auth_mechs & conn->auth_mechs) { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1689 | rc = generate_preauth_hash(work); |
| 1690 | if (rc) |
| 1691 | goto out_err; |
| 1692 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1693 | if (conn->preferred_auth_mech & |
| 1694 | (KSMBD_AUTH_KRB5 | KSMBD_AUTH_MSKRB5)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1695 | rc = krb5_authenticate(work); |
| 1696 | if (rc) { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1697 | rc = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1698 | goto out_err; |
| 1699 | } |
| 1700 | |
| 1701 | ksmbd_conn_set_good(work); |
| 1702 | sess->state = SMB2_SESSION_VALID; |
Muhammad Usama Anjum | 822bc8e | 2021-04-02 09:25:35 +0900 | [diff] [blame] | 1703 | kfree(sess->Preauth_HashValue); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1704 | sess->Preauth_HashValue = NULL; |
| 1705 | } else if (conn->preferred_auth_mech == KSMBD_AUTH_NTLMSSP) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1706 | if (negblob->MessageType == NtLmNegotiate) { |
| 1707 | rc = ntlm_negotiate(work, negblob); |
| 1708 | if (rc) |
| 1709 | goto out_err; |
| 1710 | rsp->hdr.Status = |
| 1711 | STATUS_MORE_PROCESSING_REQUIRED; |
| 1712 | /* |
| 1713 | * Note: here total size -1 is done as an |
| 1714 | * adjustment for 0 size blob |
| 1715 | */ |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1716 | inc_rfc1001_len(rsp, le16_to_cpu(rsp->SecurityBufferLength) - 1); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1717 | |
| 1718 | } else if (negblob->MessageType == NtLmAuthenticate) { |
| 1719 | rc = ntlm_authenticate(work); |
| 1720 | if (rc) |
| 1721 | goto out_err; |
| 1722 | |
| 1723 | ksmbd_conn_set_good(work); |
| 1724 | sess->state = SMB2_SESSION_VALID; |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1725 | if (conn->binding) { |
| 1726 | struct preauth_session *preauth_sess; |
| 1727 | |
| 1728 | preauth_sess = |
| 1729 | ksmbd_preauth_session_lookup(conn, sess->id); |
| 1730 | if (preauth_sess) { |
| 1731 | list_del(&preauth_sess->preauth_entry); |
| 1732 | kfree(preauth_sess); |
| 1733 | } |
| 1734 | } |
Muhammad Usama Anjum | 822bc8e | 2021-04-02 09:25:35 +0900 | [diff] [blame] | 1735 | kfree(sess->Preauth_HashValue); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1736 | sess->Preauth_HashValue = NULL; |
| 1737 | } |
| 1738 | } else { |
| 1739 | /* TODO: need one more negotiation */ |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 1740 | pr_err("Not support the preferred authentication\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1741 | rc = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1742 | } |
| 1743 | } else { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 1744 | pr_err("Not support authentication\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1745 | rc = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1746 | } |
| 1747 | |
| 1748 | out_err: |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1749 | if (rc == -EINVAL) |
| 1750 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 1751 | else if (rc == -ENOENT) |
| 1752 | rsp->hdr.Status = STATUS_USER_SESSION_DELETED; |
| 1753 | else if (rc == -EACCES) |
| 1754 | rsp->hdr.Status = STATUS_REQUEST_NOT_ACCEPTED; |
| 1755 | else if (rc == -EFAULT) |
| 1756 | rsp->hdr.Status = STATUS_NETWORK_SESSION_EXPIRED; |
Namjae Jeon | 58090b1 | 2021-07-16 14:52:09 +0900 | [diff] [blame] | 1757 | else if (rc == -ENOMEM) |
| 1758 | rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES; |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1759 | else if (rc) |
| 1760 | rsp->hdr.Status = STATUS_LOGON_FAILURE; |
| 1761 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1762 | if (conn->use_spnego && conn->mechToken) { |
| 1763 | kfree(conn->mechToken); |
| 1764 | conn->mechToken = NULL; |
| 1765 | } |
| 1766 | |
| 1767 | if (rc < 0 && sess) { |
| 1768 | ksmbd_session_destroy(sess); |
| 1769 | work->sess = NULL; |
| 1770 | } |
| 1771 | |
| 1772 | return rc; |
| 1773 | } |
| 1774 | |
| 1775 | /** |
| 1776 | * smb2_tree_connect() - handler for smb2 tree connect command |
| 1777 | * @work: smb work containing smb request buffer |
| 1778 | * |
| 1779 | * Return: 0 on success, otherwise error |
| 1780 | */ |
| 1781 | int smb2_tree_connect(struct ksmbd_work *work) |
| 1782 | { |
| 1783 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 1784 | struct smb2_tree_connect_req *req = work->request_buf; |
| 1785 | struct smb2_tree_connect_rsp *rsp = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1786 | struct ksmbd_session *sess = work->sess; |
| 1787 | char *treename = NULL, *name = NULL; |
| 1788 | struct ksmbd_tree_conn_status status; |
| 1789 | struct ksmbd_share_config *share; |
| 1790 | int rc = -EINVAL; |
| 1791 | |
| 1792 | treename = smb_strndup_from_utf16(req->Buffer, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1793 | le16_to_cpu(req->PathLength), true, |
| 1794 | conn->local_nls); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1795 | if (IS_ERR(treename)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 1796 | pr_err("treename is NULL\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1797 | status.ret = KSMBD_TREE_CONN_STATUS_ERROR; |
| 1798 | goto out_err1; |
| 1799 | } |
| 1800 | |
Stephen Rothwell | 36ba386 | 2021-03-17 17:01:15 +0900 | [diff] [blame] | 1801 | name = ksmbd_extract_sharename(treename); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1802 | if (IS_ERR(name)) { |
| 1803 | status.ret = KSMBD_TREE_CONN_STATUS_ERROR; |
| 1804 | goto out_err1; |
| 1805 | } |
| 1806 | |
| 1807 | ksmbd_debug(SMB, "tree connect request for tree %s treename %s\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1808 | name, treename); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1809 | |
| 1810 | status = ksmbd_tree_conn_connect(sess, name); |
| 1811 | if (status.ret == KSMBD_TREE_CONN_STATUS_OK) |
| 1812 | rsp->hdr.Id.SyncId.TreeId = cpu_to_le32(status.tree_conn->id); |
| 1813 | else |
| 1814 | goto out_err1; |
| 1815 | |
| 1816 | share = status.tree_conn->share_conf; |
| 1817 | if (test_share_config_flag(share, KSMBD_SHARE_FLAG_PIPE)) { |
| 1818 | ksmbd_debug(SMB, "IPC share path request\n"); |
| 1819 | rsp->ShareType = SMB2_SHARE_TYPE_PIPE; |
| 1820 | rsp->MaximalAccess = FILE_READ_DATA_LE | FILE_READ_EA_LE | |
| 1821 | FILE_EXECUTE_LE | FILE_READ_ATTRIBUTES_LE | |
| 1822 | FILE_DELETE_LE | FILE_READ_CONTROL_LE | |
| 1823 | FILE_WRITE_DAC_LE | FILE_WRITE_OWNER_LE | |
| 1824 | FILE_SYNCHRONIZE_LE; |
| 1825 | } else { |
| 1826 | rsp->ShareType = SMB2_SHARE_TYPE_DISK; |
| 1827 | rsp->MaximalAccess = FILE_READ_DATA_LE | FILE_READ_EA_LE | |
| 1828 | FILE_EXECUTE_LE | FILE_READ_ATTRIBUTES_LE; |
| 1829 | if (test_tree_conn_flag(status.tree_conn, |
| 1830 | KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
| 1831 | rsp->MaximalAccess |= FILE_WRITE_DATA_LE | |
| 1832 | FILE_APPEND_DATA_LE | FILE_WRITE_EA_LE | |
Wan Jiabing | 3aefd54 | 2021-06-07 12:54:32 +0800 | [diff] [blame] | 1833 | FILE_DELETE_LE | FILE_WRITE_ATTRIBUTES_LE | |
| 1834 | FILE_DELETE_CHILD_LE | FILE_READ_CONTROL_LE | |
| 1835 | FILE_WRITE_DAC_LE | FILE_WRITE_OWNER_LE | |
| 1836 | FILE_SYNCHRONIZE_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1837 | } |
| 1838 | } |
| 1839 | |
| 1840 | status.tree_conn->maximal_access = le32_to_cpu(rsp->MaximalAccess); |
| 1841 | if (conn->posix_ext_supported) |
| 1842 | status.tree_conn->posix_extensions = true; |
| 1843 | |
| 1844 | out_err1: |
| 1845 | rsp->StructureSize = cpu_to_le16(16); |
| 1846 | rsp->Capabilities = 0; |
| 1847 | rsp->Reserved = 0; |
| 1848 | /* default manual caching */ |
| 1849 | rsp->ShareFlags = SMB2_SHAREFLAG_MANUAL_CACHING; |
| 1850 | inc_rfc1001_len(rsp, 16); |
| 1851 | |
| 1852 | if (!IS_ERR(treename)) |
| 1853 | kfree(treename); |
| 1854 | if (!IS_ERR(name)) |
| 1855 | kfree(name); |
| 1856 | |
| 1857 | switch (status.ret) { |
| 1858 | case KSMBD_TREE_CONN_STATUS_OK: |
| 1859 | rsp->hdr.Status = STATUS_SUCCESS; |
| 1860 | rc = 0; |
| 1861 | break; |
| 1862 | case KSMBD_TREE_CONN_STATUS_NO_SHARE: |
| 1863 | rsp->hdr.Status = STATUS_BAD_NETWORK_PATH; |
| 1864 | break; |
| 1865 | case -ENOMEM: |
| 1866 | case KSMBD_TREE_CONN_STATUS_NOMEM: |
| 1867 | rsp->hdr.Status = STATUS_NO_MEMORY; |
| 1868 | break; |
| 1869 | case KSMBD_TREE_CONN_STATUS_ERROR: |
| 1870 | case KSMBD_TREE_CONN_STATUS_TOO_MANY_CONNS: |
| 1871 | case KSMBD_TREE_CONN_STATUS_TOO_MANY_SESSIONS: |
| 1872 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 1873 | break; |
| 1874 | case -EINVAL: |
| 1875 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 1876 | break; |
| 1877 | default: |
| 1878 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 1879 | } |
| 1880 | |
| 1881 | return rc; |
| 1882 | } |
| 1883 | |
| 1884 | /** |
| 1885 | * smb2_create_open_flags() - convert smb open flags to unix open flags |
| 1886 | * @file_present: is file already present |
| 1887 | * @access: file access flags |
| 1888 | * @disposition: file disposition flags |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 1889 | * @may_flags: set with MAY_ flags |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1890 | * |
| 1891 | * Return: file open flags |
| 1892 | */ |
| 1893 | static int smb2_create_open_flags(bool file_present, __le32 access, |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 1894 | __le32 disposition, |
| 1895 | int *may_flags) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1896 | { |
| 1897 | int oflags = O_NONBLOCK | O_LARGEFILE; |
| 1898 | |
| 1899 | if (access & FILE_READ_DESIRED_ACCESS_LE && |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 1900 | access & FILE_WRITE_DESIRE_ACCESS_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1901 | oflags |= O_RDWR; |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 1902 | *may_flags = MAY_OPEN | MAY_READ | MAY_WRITE; |
| 1903 | } else if (access & FILE_WRITE_DESIRE_ACCESS_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1904 | oflags |= O_WRONLY; |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 1905 | *may_flags = MAY_OPEN | MAY_WRITE; |
| 1906 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1907 | oflags |= O_RDONLY; |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 1908 | *may_flags = MAY_OPEN | MAY_READ; |
| 1909 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1910 | |
| 1911 | if (access == FILE_READ_ATTRIBUTES_LE) |
| 1912 | oflags |= O_PATH; |
| 1913 | |
| 1914 | if (file_present) { |
| 1915 | switch (disposition & FILE_CREATE_MASK_LE) { |
| 1916 | case FILE_OPEN_LE: |
| 1917 | case FILE_CREATE_LE: |
| 1918 | break; |
| 1919 | case FILE_SUPERSEDE_LE: |
| 1920 | case FILE_OVERWRITE_LE: |
| 1921 | case FILE_OVERWRITE_IF_LE: |
| 1922 | oflags |= O_TRUNC; |
| 1923 | break; |
| 1924 | default: |
| 1925 | break; |
| 1926 | } |
| 1927 | } else { |
| 1928 | switch (disposition & FILE_CREATE_MASK_LE) { |
| 1929 | case FILE_SUPERSEDE_LE: |
| 1930 | case FILE_CREATE_LE: |
| 1931 | case FILE_OPEN_IF_LE: |
| 1932 | case FILE_OVERWRITE_IF_LE: |
| 1933 | oflags |= O_CREAT; |
| 1934 | break; |
| 1935 | case FILE_OPEN_LE: |
| 1936 | case FILE_OVERWRITE_LE: |
| 1937 | oflags &= ~O_CREAT; |
| 1938 | break; |
| 1939 | default: |
| 1940 | break; |
| 1941 | } |
| 1942 | } |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 1943 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1944 | return oflags; |
| 1945 | } |
| 1946 | |
| 1947 | /** |
| 1948 | * smb2_tree_disconnect() - handler for smb tree connect request |
| 1949 | * @work: smb work containing request buffer |
| 1950 | * |
| 1951 | * Return: 0 |
| 1952 | */ |
| 1953 | int smb2_tree_disconnect(struct ksmbd_work *work) |
| 1954 | { |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 1955 | struct smb2_tree_disconnect_rsp *rsp = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1956 | struct ksmbd_session *sess = work->sess; |
| 1957 | struct ksmbd_tree_connect *tcon = work->tcon; |
| 1958 | |
| 1959 | rsp->StructureSize = cpu_to_le16(4); |
| 1960 | inc_rfc1001_len(rsp, 4); |
| 1961 | |
| 1962 | ksmbd_debug(SMB, "request\n"); |
| 1963 | |
| 1964 | if (!tcon) { |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 1965 | struct smb2_tree_disconnect_req *req = work->request_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1966 | |
| 1967 | ksmbd_debug(SMB, "Invalid tid %d\n", req->hdr.Id.SyncId.TreeId); |
| 1968 | rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED; |
| 1969 | smb2_set_err_rsp(work); |
| 1970 | return 0; |
| 1971 | } |
| 1972 | |
| 1973 | ksmbd_close_tree_conn_fds(work); |
| 1974 | ksmbd_tree_conn_disconnect(sess, tcon); |
| 1975 | return 0; |
| 1976 | } |
| 1977 | |
| 1978 | /** |
| 1979 | * smb2_session_logoff() - handler for session log off request |
| 1980 | * @work: smb work containing request buffer |
| 1981 | * |
| 1982 | * Return: 0 |
| 1983 | */ |
| 1984 | int smb2_session_logoff(struct ksmbd_work *work) |
| 1985 | { |
| 1986 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 1987 | struct smb2_logoff_rsp *rsp = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1988 | struct ksmbd_session *sess = work->sess; |
| 1989 | |
| 1990 | rsp->StructureSize = cpu_to_le16(4); |
| 1991 | inc_rfc1001_len(rsp, 4); |
| 1992 | |
| 1993 | ksmbd_debug(SMB, "request\n"); |
| 1994 | |
| 1995 | /* Got a valid session, set connection state */ |
| 1996 | WARN_ON(sess->conn != conn); |
| 1997 | |
| 1998 | /* setting CifsExiting here may race with start_tcp_sess */ |
| 1999 | ksmbd_conn_set_need_reconnect(work); |
| 2000 | ksmbd_close_session_fds(work); |
| 2001 | ksmbd_conn_wait_idle(conn); |
| 2002 | |
| 2003 | if (ksmbd_tree_conn_session_logoff(sess)) { |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 2004 | struct smb2_logoff_req *req = work->request_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2005 | |
| 2006 | ksmbd_debug(SMB, "Invalid tid %d\n", req->hdr.Id.SyncId.TreeId); |
| 2007 | rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED; |
| 2008 | smb2_set_err_rsp(work); |
| 2009 | return 0; |
| 2010 | } |
| 2011 | |
| 2012 | ksmbd_destroy_file_table(&sess->file_table); |
| 2013 | sess->state = SMB2_SESSION_EXPIRED; |
| 2014 | |
| 2015 | ksmbd_free_user(sess->user); |
| 2016 | sess->user = NULL; |
| 2017 | |
| 2018 | /* let start_tcp_sess free connection info now */ |
| 2019 | ksmbd_conn_set_need_negotiate(work); |
| 2020 | return 0; |
| 2021 | } |
| 2022 | |
| 2023 | /** |
| 2024 | * create_smb2_pipe() - create IPC pipe |
| 2025 | * @work: smb work containing request buffer |
| 2026 | * |
| 2027 | * Return: 0 on success, otherwise error |
| 2028 | */ |
| 2029 | static noinline int create_smb2_pipe(struct ksmbd_work *work) |
| 2030 | { |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 2031 | struct smb2_create_rsp *rsp = work->response_buf; |
| 2032 | struct smb2_create_req *req = work->request_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2033 | int id; |
| 2034 | int err; |
| 2035 | char *name; |
| 2036 | |
| 2037 | name = smb_strndup_from_utf16(req->Buffer, le16_to_cpu(req->NameLength), |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2038 | 1, work->conn->local_nls); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2039 | if (IS_ERR(name)) { |
| 2040 | rsp->hdr.Status = STATUS_NO_MEMORY; |
| 2041 | err = PTR_ERR(name); |
| 2042 | goto out; |
| 2043 | } |
| 2044 | |
| 2045 | id = ksmbd_session_rpc_open(work->sess, name); |
Marios Makassikis | 79caa96 | 2021-05-06 11:38:35 +0900 | [diff] [blame] | 2046 | if (id < 0) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2047 | pr_err("Unable to open RPC pipe: %d\n", id); |
Marios Makassikis | 79caa96 | 2021-05-06 11:38:35 +0900 | [diff] [blame] | 2048 | err = id; |
| 2049 | goto out; |
| 2050 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2051 | |
Marios Makassikis | 79caa96 | 2021-05-06 11:38:35 +0900 | [diff] [blame] | 2052 | rsp->hdr.Status = STATUS_SUCCESS; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2053 | rsp->StructureSize = cpu_to_le16(89); |
| 2054 | rsp->OplockLevel = SMB2_OPLOCK_LEVEL_NONE; |
| 2055 | rsp->Reserved = 0; |
| 2056 | rsp->CreateAction = cpu_to_le32(FILE_OPENED); |
| 2057 | |
| 2058 | rsp->CreationTime = cpu_to_le64(0); |
| 2059 | rsp->LastAccessTime = cpu_to_le64(0); |
| 2060 | rsp->ChangeTime = cpu_to_le64(0); |
| 2061 | rsp->AllocationSize = cpu_to_le64(0); |
| 2062 | rsp->EndofFile = cpu_to_le64(0); |
| 2063 | rsp->FileAttributes = ATTR_NORMAL_LE; |
| 2064 | rsp->Reserved2 = 0; |
| 2065 | rsp->VolatileFileId = cpu_to_le64(id); |
| 2066 | rsp->PersistentFileId = 0; |
| 2067 | rsp->CreateContextsOffset = 0; |
| 2068 | rsp->CreateContextsLength = 0; |
| 2069 | |
| 2070 | inc_rfc1001_len(rsp, 88); /* StructureSize - 1*/ |
| 2071 | kfree(name); |
| 2072 | return 0; |
| 2073 | |
| 2074 | out: |
Marios Makassikis | 79caa96 | 2021-05-06 11:38:35 +0900 | [diff] [blame] | 2075 | switch (err) { |
| 2076 | case -EINVAL: |
| 2077 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 2078 | break; |
| 2079 | case -ENOSPC: |
| 2080 | case -ENOMEM: |
| 2081 | rsp->hdr.Status = STATUS_NO_MEMORY; |
| 2082 | break; |
| 2083 | } |
| 2084 | |
| 2085 | if (!IS_ERR(name)) |
| 2086 | kfree(name); |
| 2087 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2088 | smb2_set_err_rsp(work); |
| 2089 | return err; |
| 2090 | } |
| 2091 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2092 | /** |
| 2093 | * smb2_set_ea() - handler for setting extended attributes using set |
| 2094 | * info command |
| 2095 | * @eabuf: set info command buffer |
| 2096 | * @path: dentry path for get ea |
| 2097 | * |
| 2098 | * Return: 0 on success, otherwise error |
| 2099 | */ |
| 2100 | static int smb2_set_ea(struct smb2_ea_info *eabuf, struct path *path) |
| 2101 | { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2102 | struct user_namespace *user_ns = mnt_user_ns(path->mnt); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2103 | char *attr_name = NULL, *value; |
| 2104 | int rc = 0; |
| 2105 | int next = 0; |
| 2106 | |
| 2107 | attr_name = kmalloc(XATTR_NAME_MAX + 1, GFP_KERNEL); |
| 2108 | if (!attr_name) |
| 2109 | return -ENOMEM; |
| 2110 | |
| 2111 | do { |
| 2112 | if (!eabuf->EaNameLength) |
| 2113 | goto next; |
| 2114 | |
| 2115 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2116 | "name : <%s>, name_len : %u, value_len : %u, next : %u\n", |
| 2117 | eabuf->name, eabuf->EaNameLength, |
| 2118 | le16_to_cpu(eabuf->EaValueLength), |
| 2119 | le32_to_cpu(eabuf->NextEntryOffset)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2120 | |
| 2121 | if (eabuf->EaNameLength > |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2122 | (XATTR_NAME_MAX - XATTR_USER_PREFIX_LEN)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2123 | rc = -EINVAL; |
| 2124 | break; |
| 2125 | } |
| 2126 | |
| 2127 | memcpy(attr_name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN); |
| 2128 | memcpy(&attr_name[XATTR_USER_PREFIX_LEN], eabuf->name, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2129 | eabuf->EaNameLength); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2130 | attr_name[XATTR_USER_PREFIX_LEN + eabuf->EaNameLength] = '\0'; |
| 2131 | value = (char *)&eabuf->name + eabuf->EaNameLength + 1; |
| 2132 | |
| 2133 | if (!eabuf->EaValueLength) { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2134 | rc = ksmbd_vfs_casexattr_len(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2135 | path->dentry, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2136 | attr_name, |
| 2137 | XATTR_USER_PREFIX_LEN + |
| 2138 | eabuf->EaNameLength); |
| 2139 | |
| 2140 | /* delete the EA only when it exits */ |
| 2141 | if (rc > 0) { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2142 | rc = ksmbd_vfs_remove_xattr(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2143 | path->dentry, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2144 | attr_name); |
| 2145 | |
| 2146 | if (rc < 0) { |
| 2147 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2148 | "remove xattr failed(%d)\n", |
| 2149 | rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2150 | break; |
| 2151 | } |
| 2152 | } |
| 2153 | |
| 2154 | /* if the EA doesn't exist, just do nothing. */ |
| 2155 | rc = 0; |
| 2156 | } else { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2157 | rc = ksmbd_vfs_setxattr(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2158 | path->dentry, attr_name, value, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2159 | le16_to_cpu(eabuf->EaValueLength), 0); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2160 | if (rc < 0) { |
| 2161 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2162 | "ksmbd_vfs_setxattr is failed(%d)\n", |
| 2163 | rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2164 | break; |
| 2165 | } |
| 2166 | } |
| 2167 | |
| 2168 | next: |
| 2169 | next = le32_to_cpu(eabuf->NextEntryOffset); |
| 2170 | eabuf = (struct smb2_ea_info *)((char *)eabuf + next); |
| 2171 | } while (next != 0); |
| 2172 | |
| 2173 | kfree(attr_name); |
| 2174 | return rc; |
| 2175 | } |
| 2176 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2177 | static noinline int smb2_set_stream_name_xattr(struct path *path, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2178 | struct ksmbd_file *fp, |
| 2179 | char *stream_name, int s_type) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2180 | { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2181 | struct user_namespace *user_ns = mnt_user_ns(path->mnt); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2182 | size_t xattr_stream_size; |
| 2183 | char *xattr_stream_name; |
| 2184 | int rc; |
| 2185 | |
| 2186 | rc = ksmbd_vfs_xattr_stream_name(stream_name, |
| 2187 | &xattr_stream_name, |
| 2188 | &xattr_stream_size, |
| 2189 | s_type); |
| 2190 | if (rc) |
| 2191 | return rc; |
| 2192 | |
| 2193 | fp->stream.name = xattr_stream_name; |
| 2194 | fp->stream.size = xattr_stream_size; |
| 2195 | |
| 2196 | /* Check if there is stream prefix in xattr space */ |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2197 | rc = ksmbd_vfs_casexattr_len(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2198 | path->dentry, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2199 | xattr_stream_name, |
| 2200 | xattr_stream_size); |
| 2201 | if (rc >= 0) |
| 2202 | return 0; |
| 2203 | |
| 2204 | if (fp->cdoption == FILE_OPEN_LE) { |
| 2205 | ksmbd_debug(SMB, "XATTR stream name lookup failed: %d\n", rc); |
| 2206 | return -EBADF; |
| 2207 | } |
| 2208 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2209 | rc = ksmbd_vfs_setxattr(user_ns, path->dentry, |
| 2210 | xattr_stream_name, NULL, 0, 0); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2211 | if (rc < 0) |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2212 | pr_err("Failed to store XATTR stream name :%d\n", rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2213 | return 0; |
| 2214 | } |
| 2215 | |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 2216 | static int smb2_remove_smb_xattrs(struct path *path) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2217 | { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2218 | struct user_namespace *user_ns = mnt_user_ns(path->mnt); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2219 | char *name, *xattr_list = NULL; |
| 2220 | ssize_t xattr_list_len; |
| 2221 | int err = 0; |
| 2222 | |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 2223 | xattr_list_len = ksmbd_vfs_listxattr(path->dentry, &xattr_list); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2224 | if (xattr_list_len < 0) { |
| 2225 | goto out; |
| 2226 | } else if (!xattr_list_len) { |
| 2227 | ksmbd_debug(SMB, "empty xattr in the file\n"); |
| 2228 | goto out; |
| 2229 | } |
| 2230 | |
| 2231 | for (name = xattr_list; name - xattr_list < xattr_list_len; |
| 2232 | name += strlen(name) + 1) { |
| 2233 | ksmbd_debug(SMB, "%s, len %zd\n", name, strlen(name)); |
| 2234 | |
| 2235 | if (strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2236 | strncmp(&name[XATTR_USER_PREFIX_LEN], DOS_ATTRIBUTE_PREFIX, |
| 2237 | DOS_ATTRIBUTE_PREFIX_LEN) && |
| 2238 | strncmp(&name[XATTR_USER_PREFIX_LEN], STREAM_PREFIX, STREAM_PREFIX_LEN)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2239 | continue; |
| 2240 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2241 | err = ksmbd_vfs_remove_xattr(user_ns, path->dentry, name); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2242 | if (err) |
| 2243 | ksmbd_debug(SMB, "remove xattr failed : %s\n", name); |
| 2244 | } |
| 2245 | out: |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 2246 | kvfree(xattr_list); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2247 | return err; |
| 2248 | } |
| 2249 | |
| 2250 | static int smb2_create_truncate(struct path *path) |
| 2251 | { |
| 2252 | int rc = vfs_truncate(path, 0); |
| 2253 | |
| 2254 | if (rc) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2255 | pr_err("vfs_truncate failed, rc %d\n", rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2256 | return rc; |
| 2257 | } |
| 2258 | |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 2259 | rc = smb2_remove_smb_xattrs(path); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2260 | if (rc == -EOPNOTSUPP) |
| 2261 | rc = 0; |
| 2262 | if (rc) |
| 2263 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2264 | "ksmbd_truncate_stream_name_xattr failed, rc %d\n", |
| 2265 | rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2266 | return rc; |
| 2267 | } |
| 2268 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2269 | static void smb2_new_xattrs(struct ksmbd_tree_connect *tcon, struct path *path, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2270 | struct ksmbd_file *fp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2271 | { |
| 2272 | struct xattr_dos_attrib da = {0}; |
| 2273 | int rc; |
| 2274 | |
| 2275 | if (!test_share_config_flag(tcon->share_conf, |
| 2276 | KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) |
| 2277 | return; |
| 2278 | |
| 2279 | da.version = 4; |
| 2280 | da.attr = le32_to_cpu(fp->f_ci->m_fattr); |
| 2281 | da.itime = da.create_time = fp->create_time; |
| 2282 | da.flags = XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME | |
| 2283 | XATTR_DOSINFO_ITIME; |
| 2284 | |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2285 | rc = ksmbd_vfs_set_dos_attrib_xattr(mnt_user_ns(path->mnt), |
| 2286 | path->dentry, &da); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2287 | if (rc) |
| 2288 | ksmbd_debug(SMB, "failed to store file attribute into xattr\n"); |
| 2289 | } |
| 2290 | |
| 2291 | static void smb2_update_xattrs(struct ksmbd_tree_connect *tcon, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2292 | struct path *path, struct ksmbd_file *fp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2293 | { |
| 2294 | struct xattr_dos_attrib da; |
| 2295 | int rc; |
| 2296 | |
| 2297 | fp->f_ci->m_fattr &= ~(ATTR_HIDDEN_LE | ATTR_SYSTEM_LE); |
| 2298 | |
| 2299 | /* get FileAttributes from XATTR_NAME_DOS_ATTRIBUTE */ |
| 2300 | if (!test_share_config_flag(tcon->share_conf, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2301 | KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2302 | return; |
| 2303 | |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2304 | rc = ksmbd_vfs_get_dos_attrib_xattr(mnt_user_ns(path->mnt), |
| 2305 | path->dentry, &da); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2306 | if (rc > 0) { |
| 2307 | fp->f_ci->m_fattr = cpu_to_le32(da.attr); |
| 2308 | fp->create_time = da.create_time; |
| 2309 | fp->itime = da.itime; |
| 2310 | } |
| 2311 | } |
| 2312 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2313 | 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] | 2314 | int open_flags, umode_t posix_mode, bool is_dir) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2315 | { |
| 2316 | struct ksmbd_tree_connect *tcon = work->tcon; |
| 2317 | struct ksmbd_share_config *share = tcon->share_conf; |
| 2318 | umode_t mode; |
| 2319 | int rc; |
| 2320 | |
| 2321 | if (!(open_flags & O_CREAT)) |
| 2322 | return -EBADF; |
| 2323 | |
| 2324 | ksmbd_debug(SMB, "file does not exist, so creating\n"); |
| 2325 | if (is_dir == true) { |
| 2326 | ksmbd_debug(SMB, "creating directory\n"); |
| 2327 | |
| 2328 | mode = share_config_directory_mode(share, posix_mode); |
| 2329 | rc = ksmbd_vfs_mkdir(work, name, mode); |
| 2330 | if (rc) |
| 2331 | return rc; |
| 2332 | } else { |
| 2333 | ksmbd_debug(SMB, "creating regular file\n"); |
| 2334 | |
| 2335 | mode = share_config_create_mode(share, posix_mode); |
| 2336 | rc = ksmbd_vfs_create(work, name, mode); |
| 2337 | if (rc) |
| 2338 | return rc; |
| 2339 | } |
| 2340 | |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame^] | 2341 | rc = ksmbd_vfs_kern_path(work, name, 0, path, 0); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2342 | if (rc) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2343 | pr_err("cannot get linux path (%s), err = %d\n", |
| 2344 | name, rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2345 | return rc; |
| 2346 | } |
| 2347 | return 0; |
| 2348 | } |
| 2349 | |
| 2350 | static int smb2_create_sd_buffer(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2351 | struct smb2_create_req *req, |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 2352 | struct path *path) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2353 | { |
| 2354 | struct create_context *context; |
Hyunchul Lee | 21dd1fd | 2021-07-09 17:06:34 +0900 | [diff] [blame] | 2355 | struct create_sd_buf_req *sd_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2356 | |
| 2357 | if (!req->CreateContextsOffset) |
Hyunchul Lee | 21dd1fd | 2021-07-09 17:06:34 +0900 | [diff] [blame] | 2358 | return -ENOENT; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2359 | |
| 2360 | /* Parse SD BUFFER create contexts */ |
| 2361 | context = smb2_find_context_vals(req, SMB2_CREATE_SD_BUFFER); |
Hyunchul Lee | 21dd1fd | 2021-07-09 17:06:34 +0900 | [diff] [blame] | 2362 | if (!context) |
| 2363 | return -ENOENT; |
| 2364 | else if (IS_ERR(context)) |
| 2365 | return PTR_ERR(context); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2366 | |
Hyunchul Lee | 21dd1fd | 2021-07-09 17:06:34 +0900 | [diff] [blame] | 2367 | ksmbd_debug(SMB, |
| 2368 | "Set ACLs using SMB2_CREATE_SD_BUFFER context\n"); |
| 2369 | sd_buf = (struct create_sd_buf_req *)context; |
| 2370 | return set_info_sec(work->conn, work->tcon, path, &sd_buf->ntsd, |
| 2371 | le32_to_cpu(sd_buf->ccontext.DataLength), true); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2372 | } |
| 2373 | |
Christian Brauner | 43205ca | 2021-08-23 17:13:50 +0200 | [diff] [blame] | 2374 | static void ksmbd_acls_fattr(struct smb_fattr *fattr, |
| 2375 | struct user_namespace *mnt_userns, |
| 2376 | struct inode *inode) |
Namjae Jeon | 3d47e54 | 2021-04-20 14:25:35 +0900 | [diff] [blame] | 2377 | { |
Christian Brauner | 43205ca | 2021-08-23 17:13:50 +0200 | [diff] [blame] | 2378 | fattr->cf_uid = i_uid_into_mnt(mnt_userns, inode); |
| 2379 | fattr->cf_gid = i_gid_into_mnt(mnt_userns, inode); |
Namjae Jeon | 3d47e54 | 2021-04-20 14:25:35 +0900 | [diff] [blame] | 2380 | fattr->cf_mode = inode->i_mode; |
Namjae Jeon | 777cad1 | 2021-08-13 08:15:33 +0900 | [diff] [blame] | 2381 | fattr->cf_acls = NULL; |
Namjae Jeon | 3d47e54 | 2021-04-20 14:25:35 +0900 | [diff] [blame] | 2382 | fattr->cf_dacls = NULL; |
| 2383 | |
Namjae Jeon | 777cad1 | 2021-08-13 08:15:33 +0900 | [diff] [blame] | 2384 | if (IS_ENABLED(CONFIG_FS_POSIX_ACL)) { |
| 2385 | fattr->cf_acls = get_acl(inode, ACL_TYPE_ACCESS); |
| 2386 | if (S_ISDIR(inode->i_mode)) |
| 2387 | fattr->cf_dacls = get_acl(inode, ACL_TYPE_DEFAULT); |
| 2388 | } |
Namjae Jeon | 3d47e54 | 2021-04-20 14:25:35 +0900 | [diff] [blame] | 2389 | } |
| 2390 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2391 | /** |
| 2392 | * smb2_open() - handler for smb file open request |
| 2393 | * @work: smb work containing request buffer |
| 2394 | * |
| 2395 | * Return: 0 on success, otherwise error |
| 2396 | */ |
| 2397 | int smb2_open(struct ksmbd_work *work) |
| 2398 | { |
| 2399 | struct ksmbd_conn *conn = work->conn; |
| 2400 | struct ksmbd_session *sess = work->sess; |
| 2401 | struct ksmbd_tree_connect *tcon = work->tcon; |
| 2402 | struct smb2_create_req *req; |
| 2403 | struct smb2_create_rsp *rsp, *rsp_org; |
| 2404 | struct path path; |
| 2405 | struct ksmbd_share_config *share = tcon->share_conf; |
| 2406 | struct ksmbd_file *fp = NULL; |
| 2407 | struct file *filp = NULL; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2408 | struct user_namespace *user_ns = NULL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2409 | struct kstat stat; |
| 2410 | struct create_context *context; |
| 2411 | struct lease_ctx_info *lc = NULL; |
| 2412 | struct create_ea_buf_req *ea_buf = NULL; |
| 2413 | struct oplock_info *opinfo; |
| 2414 | __le32 *next_ptr = NULL; |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 2415 | 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^] | 2416 | int rc = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2417 | int contxt_cnt = 0, query_disk_id = 0; |
| 2418 | int maximal_access_ctxt = 0, posix_ctxt = 0; |
| 2419 | int s_type = 0; |
| 2420 | int next_off = 0; |
| 2421 | char *name = NULL; |
| 2422 | char *stream_name = NULL; |
| 2423 | bool file_present = false, created = false, already_permitted = false; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2424 | int share_ret, need_truncate = 0; |
| 2425 | u64 time; |
| 2426 | umode_t posix_mode = 0; |
| 2427 | __le32 daccess, maximal_access = 0; |
| 2428 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 2429 | rsp_org = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2430 | WORK_BUFFERS(work, req, rsp); |
| 2431 | |
| 2432 | if (req->hdr.NextCommand && !work->next_smb2_rcv_hdr_off && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2433 | (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2434 | ksmbd_debug(SMB, "invalid flag in chained command\n"); |
| 2435 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 2436 | smb2_set_err_rsp(work); |
| 2437 | return -EINVAL; |
| 2438 | } |
| 2439 | |
| 2440 | if (test_share_config_flag(share, KSMBD_SHARE_FLAG_PIPE)) { |
| 2441 | ksmbd_debug(SMB, "IPC pipe create request\n"); |
| 2442 | return create_smb2_pipe(work); |
| 2443 | } |
| 2444 | |
| 2445 | if (req->NameLength) { |
| 2446 | if ((req->CreateOptions & FILE_DIRECTORY_FILE_LE) && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2447 | *(char *)req->Buffer == '\\') { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2448 | pr_err("not allow directory name included leading slash\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2449 | rc = -EINVAL; |
| 2450 | goto err_out1; |
| 2451 | } |
| 2452 | |
| 2453 | name = smb2_get_name(share, |
| 2454 | req->Buffer, |
| 2455 | le16_to_cpu(req->NameLength), |
| 2456 | work->conn->local_nls); |
| 2457 | if (IS_ERR(name)) { |
| 2458 | rc = PTR_ERR(name); |
| 2459 | if (rc != -ENOMEM) |
| 2460 | rc = -ENOENT; |
Dan Carpenter | 8b99f35 | 2021-08-02 08:14:03 +0900 | [diff] [blame] | 2461 | name = NULL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2462 | goto err_out1; |
| 2463 | } |
| 2464 | |
| 2465 | ksmbd_debug(SMB, "converted name = %s\n", name); |
| 2466 | if (strchr(name, ':')) { |
| 2467 | if (!test_share_config_flag(work->tcon->share_conf, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2468 | KSMBD_SHARE_FLAG_STREAMS)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2469 | rc = -EBADF; |
| 2470 | goto err_out1; |
| 2471 | } |
| 2472 | rc = parse_stream_name(name, &stream_name, &s_type); |
| 2473 | if (rc < 0) |
| 2474 | goto err_out1; |
| 2475 | } |
| 2476 | |
| 2477 | rc = ksmbd_validate_filename(name); |
| 2478 | if (rc < 0) |
| 2479 | goto err_out1; |
| 2480 | |
| 2481 | if (ksmbd_share_veto_filename(share, name)) { |
| 2482 | rc = -ENOENT; |
| 2483 | ksmbd_debug(SMB, "Reject open(), vetoed file: %s\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2484 | name); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2485 | goto err_out1; |
| 2486 | } |
| 2487 | } else { |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame^] | 2488 | name = kstrdup("", GFP_KERNEL); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2489 | if (!name) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2490 | rc = -ENOMEM; |
| 2491 | goto err_out1; |
| 2492 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2493 | } |
| 2494 | |
| 2495 | req_op_level = req->RequestedOplockLevel; |
Namjae Jeon | 73f9dad | 2021-04-16 14:12:06 +0900 | [diff] [blame] | 2496 | if (req_op_level == SMB2_OPLOCK_LEVEL_LEASE) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2497 | lc = parse_lease_state(req); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2498 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2499 | if (le32_to_cpu(req->ImpersonationLevel) > le32_to_cpu(IL_DELEGATE_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2500 | pr_err("Invalid impersonationlevel : 0x%x\n", |
| 2501 | le32_to_cpu(req->ImpersonationLevel)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2502 | rc = -EIO; |
| 2503 | rsp->hdr.Status = STATUS_BAD_IMPERSONATION_LEVEL; |
| 2504 | goto err_out1; |
| 2505 | } |
| 2506 | |
| 2507 | if (req->CreateOptions && !(req->CreateOptions & CREATE_OPTIONS_MASK)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2508 | pr_err("Invalid create options : 0x%x\n", |
| 2509 | le32_to_cpu(req->CreateOptions)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2510 | rc = -EINVAL; |
| 2511 | goto err_out1; |
| 2512 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2513 | if (req->CreateOptions & FILE_SEQUENTIAL_ONLY_LE && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2514 | req->CreateOptions & FILE_RANDOM_ACCESS_LE) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2515 | req->CreateOptions = ~(FILE_SEQUENTIAL_ONLY_LE); |
| 2516 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2517 | if (req->CreateOptions & |
| 2518 | (FILE_OPEN_BY_FILE_ID_LE | CREATE_TREE_CONNECTION | |
| 2519 | FILE_RESERVE_OPFILTER_LE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2520 | rc = -EOPNOTSUPP; |
| 2521 | goto err_out1; |
| 2522 | } |
| 2523 | |
| 2524 | if (req->CreateOptions & FILE_DIRECTORY_FILE_LE) { |
| 2525 | if (req->CreateOptions & FILE_NON_DIRECTORY_FILE_LE) { |
| 2526 | rc = -EINVAL; |
| 2527 | goto err_out1; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2528 | } else if (req->CreateOptions & FILE_NO_COMPRESSION_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2529 | req->CreateOptions = ~(FILE_NO_COMPRESSION_LE); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2530 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2531 | } |
| 2532 | } |
| 2533 | |
| 2534 | if (le32_to_cpu(req->CreateDisposition) > |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2535 | le32_to_cpu(FILE_OVERWRITE_IF_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2536 | pr_err("Invalid create disposition : 0x%x\n", |
| 2537 | le32_to_cpu(req->CreateDisposition)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2538 | rc = -EINVAL; |
| 2539 | goto err_out1; |
| 2540 | } |
| 2541 | |
| 2542 | if (!(req->DesiredAccess & DESIRED_ACCESS_MASK)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2543 | pr_err("Invalid desired access : 0x%x\n", |
| 2544 | le32_to_cpu(req->DesiredAccess)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2545 | rc = -EACCES; |
| 2546 | goto err_out1; |
| 2547 | } |
| 2548 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2549 | if (req->FileAttributes && !(req->FileAttributes & ATTR_MASK_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2550 | pr_err("Invalid file attribute : 0x%x\n", |
| 2551 | le32_to_cpu(req->FileAttributes)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2552 | rc = -EINVAL; |
| 2553 | goto err_out1; |
| 2554 | } |
| 2555 | |
| 2556 | if (req->CreateContextsOffset) { |
| 2557 | /* Parse non-durable handle create contexts */ |
| 2558 | context = smb2_find_context_vals(req, SMB2_CREATE_EA_BUFFER); |
Namjae Jeon | f19b396 | 2021-07-13 09:59:34 +0900 | [diff] [blame] | 2559 | if (IS_ERR(context)) { |
| 2560 | rc = PTR_ERR(context); |
| 2561 | goto err_out1; |
| 2562 | } else if (context) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2563 | ea_buf = (struct create_ea_buf_req *)context; |
| 2564 | if (req->CreateOptions & FILE_NO_EA_KNOWLEDGE_LE) { |
| 2565 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 2566 | rc = -EACCES; |
| 2567 | goto err_out1; |
| 2568 | } |
| 2569 | } |
| 2570 | |
| 2571 | context = smb2_find_context_vals(req, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2572 | SMB2_CREATE_QUERY_MAXIMAL_ACCESS_REQUEST); |
Namjae Jeon | f19b396 | 2021-07-13 09:59:34 +0900 | [diff] [blame] | 2573 | if (IS_ERR(context)) { |
| 2574 | rc = PTR_ERR(context); |
| 2575 | goto err_out1; |
| 2576 | } else if (context) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2577 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2578 | "get query maximal access context\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2579 | maximal_access_ctxt = 1; |
| 2580 | } |
| 2581 | |
| 2582 | context = smb2_find_context_vals(req, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2583 | SMB2_CREATE_TIMEWARP_REQUEST); |
Namjae Jeon | f19b396 | 2021-07-13 09:59:34 +0900 | [diff] [blame] | 2584 | if (IS_ERR(context)) { |
| 2585 | rc = PTR_ERR(context); |
| 2586 | goto err_out1; |
| 2587 | } else if (context) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2588 | ksmbd_debug(SMB, "get timewarp context\n"); |
| 2589 | rc = -EBADF; |
| 2590 | goto err_out1; |
| 2591 | } |
| 2592 | |
| 2593 | if (tcon->posix_extensions) { |
| 2594 | context = smb2_find_context_vals(req, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2595 | SMB2_CREATE_TAG_POSIX); |
Namjae Jeon | f19b396 | 2021-07-13 09:59:34 +0900 | [diff] [blame] | 2596 | if (IS_ERR(context)) { |
| 2597 | rc = PTR_ERR(context); |
| 2598 | goto err_out1; |
| 2599 | } else if (context) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2600 | struct create_posix *posix = |
| 2601 | (struct create_posix *)context; |
| 2602 | ksmbd_debug(SMB, "get posix context\n"); |
| 2603 | |
| 2604 | posix_mode = le32_to_cpu(posix->Mode); |
| 2605 | posix_ctxt = 1; |
| 2606 | } |
| 2607 | } |
| 2608 | } |
| 2609 | |
| 2610 | if (ksmbd_override_fsids(work)) { |
| 2611 | rc = -ENOMEM; |
| 2612 | goto err_out1; |
| 2613 | } |
| 2614 | |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame^] | 2615 | rc = ksmbd_vfs_kern_path(work, name, LOOKUP_NO_SYMLINKS, &path, 1); |
Namjae Jeon | 4ea4779 | 2021-09-21 14:19:33 +0900 | [diff] [blame] | 2616 | if (!rc) { |
| 2617 | if (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2618 | /* |
| 2619 | * If file exists with under flags, return access |
| 2620 | * denied error. |
| 2621 | */ |
| 2622 | if (req->CreateDisposition == FILE_OVERWRITE_IF_LE || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2623 | req->CreateDisposition == FILE_OPEN_IF_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2624 | rc = -EACCES; |
| 2625 | path_put(&path); |
| 2626 | goto err_out; |
| 2627 | } |
| 2628 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2629 | if (!test_tree_conn_flag(tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2630 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2631 | "User does not have write permission\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2632 | rc = -EACCES; |
| 2633 | path_put(&path); |
| 2634 | goto err_out; |
| 2635 | } |
Namjae Jeon | 4ea4779 | 2021-09-21 14:19:33 +0900 | [diff] [blame] | 2636 | } else if (d_is_symlink(path.dentry)) { |
| 2637 | rc = -EACCES; |
| 2638 | path_put(&path); |
| 2639 | goto err_out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2640 | } |
| 2641 | } |
| 2642 | |
| 2643 | if (rc) { |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame^] | 2644 | if (rc != -ENOENT) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2645 | goto err_out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2646 | 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] | 2647 | name, rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2648 | rc = 0; |
| 2649 | } else { |
| 2650 | file_present = true; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2651 | user_ns = mnt_user_ns(path.mnt); |
| 2652 | generic_fillattr(user_ns, d_inode(path.dentry), &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2653 | } |
| 2654 | if (stream_name) { |
| 2655 | if (req->CreateOptions & FILE_DIRECTORY_FILE_LE) { |
| 2656 | if (s_type == DATA_STREAM) { |
| 2657 | rc = -EIO; |
| 2658 | rsp->hdr.Status = STATUS_NOT_A_DIRECTORY; |
| 2659 | } |
| 2660 | } else { |
| 2661 | if (S_ISDIR(stat.mode) && s_type == DATA_STREAM) { |
| 2662 | rc = -EIO; |
| 2663 | rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY; |
| 2664 | } |
| 2665 | } |
| 2666 | |
| 2667 | if (req->CreateOptions & FILE_DIRECTORY_FILE_LE && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2668 | req->FileAttributes & ATTR_NORMAL_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2669 | rsp->hdr.Status = STATUS_NOT_A_DIRECTORY; |
| 2670 | rc = -EIO; |
| 2671 | } |
| 2672 | |
| 2673 | if (rc < 0) |
| 2674 | goto err_out; |
| 2675 | } |
| 2676 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2677 | if (file_present && req->CreateOptions & FILE_NON_DIRECTORY_FILE_LE && |
| 2678 | S_ISDIR(stat.mode) && !(req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2679 | ksmbd_debug(SMB, "open() argument is a directory: %s, %x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2680 | name, req->CreateOptions); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2681 | rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY; |
| 2682 | rc = -EIO; |
| 2683 | goto err_out; |
| 2684 | } |
| 2685 | |
| 2686 | if (file_present && (req->CreateOptions & FILE_DIRECTORY_FILE_LE) && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2687 | !(req->CreateDisposition == FILE_CREATE_LE) && |
| 2688 | !S_ISDIR(stat.mode)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2689 | rsp->hdr.Status = STATUS_NOT_A_DIRECTORY; |
| 2690 | rc = -EIO; |
| 2691 | goto err_out; |
| 2692 | } |
| 2693 | |
| 2694 | if (!stream_name && file_present && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2695 | req->CreateDisposition == FILE_CREATE_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2696 | rc = -EEXIST; |
| 2697 | goto err_out; |
| 2698 | } |
| 2699 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2700 | daccess = smb_map_generic_desired_access(req->DesiredAccess); |
| 2701 | |
| 2702 | if (file_present && !(req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) { |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 2703 | rc = smb_check_perm_dacl(conn, &path, &daccess, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2704 | sess->user->uid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2705 | if (rc) |
| 2706 | goto err_out; |
| 2707 | } |
| 2708 | |
| 2709 | if (daccess & FILE_MAXIMAL_ACCESS_LE) { |
| 2710 | if (!file_present) { |
| 2711 | daccess = cpu_to_le32(GENERIC_ALL_FLAGS); |
| 2712 | } else { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2713 | rc = ksmbd_vfs_query_maximal_access(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2714 | path.dentry, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2715 | &daccess); |
| 2716 | if (rc) |
| 2717 | goto err_out; |
| 2718 | already_permitted = true; |
| 2719 | } |
| 2720 | maximal_access = daccess; |
| 2721 | } |
| 2722 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2723 | open_flags = smb2_create_open_flags(file_present, daccess, |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 2724 | req->CreateDisposition, |
| 2725 | &may_flags); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2726 | |
| 2727 | if (!test_tree_conn_flag(tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
| 2728 | if (open_flags & O_CREAT) { |
| 2729 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2730 | "User does not have write permission\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2731 | rc = -EACCES; |
| 2732 | goto err_out; |
| 2733 | } |
| 2734 | } |
| 2735 | |
| 2736 | /*create file if not present */ |
| 2737 | if (!file_present) { |
| 2738 | rc = smb2_creat(work, &path, name, open_flags, posix_mode, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2739 | req->CreateOptions & FILE_DIRECTORY_FILE_LE); |
Marios Makassikis | d337a44 | 2021-07-27 09:24:51 +0900 | [diff] [blame] | 2740 | if (rc) { |
| 2741 | if (rc == -ENOENT) { |
| 2742 | rc = -EIO; |
| 2743 | rsp->hdr.Status = STATUS_OBJECT_PATH_NOT_FOUND; |
| 2744 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2745 | goto err_out; |
Marios Makassikis | d337a44 | 2021-07-27 09:24:51 +0900 | [diff] [blame] | 2746 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2747 | |
| 2748 | created = true; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2749 | user_ns = mnt_user_ns(path.mnt); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2750 | if (ea_buf) { |
| 2751 | rc = smb2_set_ea(&ea_buf->ea, &path); |
| 2752 | if (rc == -EOPNOTSUPP) |
| 2753 | rc = 0; |
| 2754 | else if (rc) |
| 2755 | goto err_out; |
| 2756 | } |
| 2757 | } else if (!already_permitted) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2758 | /* FILE_READ_ATTRIBUTE is allowed without inode_permission, |
| 2759 | * because execute(search) permission on a parent directory, |
| 2760 | * is already granted. |
| 2761 | */ |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2762 | if (daccess & ~(FILE_READ_ATTRIBUTES_LE | FILE_READ_CONTROL_LE)) { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2763 | rc = inode_permission(user_ns, |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 2764 | d_inode(path.dentry), |
| 2765 | may_flags); |
Namjae Jeon | ff1d572 | 2021-04-13 13:18:10 +0900 | [diff] [blame] | 2766 | if (rc) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2767 | goto err_out; |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 2768 | |
| 2769 | if ((daccess & FILE_DELETE_LE) || |
| 2770 | (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2771 | rc = ksmbd_vfs_may_delete(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2772 | path.dentry); |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 2773 | if (rc) |
| 2774 | goto err_out; |
| 2775 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2776 | } |
| 2777 | } |
| 2778 | |
| 2779 | rc = ksmbd_query_inode_status(d_inode(path.dentry->d_parent)); |
| 2780 | if (rc == KSMBD_INODE_STATUS_PENDING_DELETE) { |
| 2781 | rc = -EBUSY; |
| 2782 | goto err_out; |
| 2783 | } |
| 2784 | |
| 2785 | rc = 0; |
| 2786 | filp = dentry_open(&path, open_flags, current_cred()); |
| 2787 | if (IS_ERR(filp)) { |
| 2788 | rc = PTR_ERR(filp); |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2789 | pr_err("dentry open for dir failed, rc %d\n", rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2790 | goto err_out; |
| 2791 | } |
| 2792 | |
| 2793 | if (file_present) { |
| 2794 | if (!(open_flags & O_TRUNC)) |
| 2795 | file_info = FILE_OPENED; |
| 2796 | else |
| 2797 | file_info = FILE_OVERWRITTEN; |
| 2798 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2799 | if ((req->CreateDisposition & FILE_CREATE_MASK_LE) == |
| 2800 | FILE_SUPERSEDE_LE) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2801 | file_info = FILE_SUPERSEDED; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2802 | } else if (open_flags & O_CREAT) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2803 | file_info = FILE_CREATED; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2804 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2805 | |
| 2806 | ksmbd_vfs_set_fadvise(filp, req->CreateOptions); |
| 2807 | |
| 2808 | /* Obtain Volatile-ID */ |
| 2809 | fp = ksmbd_open_fd(work, filp); |
| 2810 | if (IS_ERR(fp)) { |
| 2811 | fput(filp); |
| 2812 | rc = PTR_ERR(fp); |
| 2813 | fp = NULL; |
| 2814 | goto err_out; |
| 2815 | } |
| 2816 | |
| 2817 | /* Get Persistent-ID */ |
| 2818 | ksmbd_open_durable_fd(fp); |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 2819 | if (!has_file_id(fp->persistent_id)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2820 | rc = -ENOMEM; |
| 2821 | goto err_out; |
| 2822 | } |
| 2823 | |
| 2824 | fp->filename = name; |
| 2825 | fp->cdoption = req->CreateDisposition; |
| 2826 | fp->daccess = daccess; |
| 2827 | fp->saccess = req->ShareAccess; |
| 2828 | fp->coption = req->CreateOptions; |
| 2829 | |
| 2830 | /* Set default windows and posix acls if creating new file */ |
| 2831 | if (created) { |
| 2832 | int posix_acl_rc; |
Namjae Jeon | fba08fa | 2021-04-15 10:29:39 +0900 | [diff] [blame] | 2833 | struct inode *inode = d_inode(path.dentry); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2834 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2835 | posix_acl_rc = ksmbd_vfs_inherit_posix_acl(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2836 | inode, |
| 2837 | d_inode(path.dentry->d_parent)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2838 | if (posix_acl_rc) |
| 2839 | ksmbd_debug(SMB, "inherit posix acl failed : %d\n", posix_acl_rc); |
| 2840 | |
| 2841 | if (test_share_config_flag(work->tcon->share_conf, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2842 | KSMBD_SHARE_FLAG_ACL_XATTR)) { |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 2843 | rc = smb_inherit_dacl(conn, &path, sess->user->uid, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2844 | sess->user->gid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2845 | } |
| 2846 | |
| 2847 | if (rc) { |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 2848 | rc = smb2_create_sd_buffer(work, req, &path); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2849 | if (rc) { |
| 2850 | if (posix_acl_rc) |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2851 | ksmbd_vfs_set_init_posix_acl(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2852 | inode); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2853 | |
| 2854 | if (test_share_config_flag(work->tcon->share_conf, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2855 | KSMBD_SHARE_FLAG_ACL_XATTR)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2856 | struct smb_fattr fattr; |
| 2857 | struct smb_ntsd *pntsd; |
Namjae Jeon | 3d47e54 | 2021-04-20 14:25:35 +0900 | [diff] [blame] | 2858 | int pntsd_size, ace_num = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2859 | |
Christian Brauner | 43205ca | 2021-08-23 17:13:50 +0200 | [diff] [blame] | 2860 | ksmbd_acls_fattr(&fattr, user_ns, inode); |
Marios Makassikis | e6b1059 | 2021-04-15 10:24:56 +0900 | [diff] [blame] | 2861 | if (fattr.cf_acls) |
| 2862 | ace_num = fattr.cf_acls->a_count; |
Namjae Jeon | 3d47e54 | 2021-04-20 14:25:35 +0900 | [diff] [blame] | 2863 | if (fattr.cf_dacls) |
| 2864 | ace_num += fattr.cf_dacls->a_count; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2865 | |
| 2866 | pntsd = kmalloc(sizeof(struct smb_ntsd) + |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2867 | sizeof(struct smb_sid) * 3 + |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2868 | sizeof(struct smb_acl) + |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2869 | sizeof(struct smb_ace) * ace_num * 2, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2870 | GFP_KERNEL); |
| 2871 | if (!pntsd) |
| 2872 | goto err_out; |
| 2873 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2874 | rc = build_sec_desc(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2875 | pntsd, NULL, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2876 | OWNER_SECINFO | |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2877 | GROUP_SECINFO | |
| 2878 | DACL_SECINFO, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2879 | &pntsd_size, &fattr); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2880 | posix_acl_release(fattr.cf_acls); |
| 2881 | posix_acl_release(fattr.cf_dacls); |
| 2882 | |
| 2883 | rc = ksmbd_vfs_set_sd_xattr(conn, |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2884 | user_ns, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2885 | path.dentry, |
| 2886 | pntsd, |
| 2887 | pntsd_size); |
Namjae Jeon | 3d47e54 | 2021-04-20 14:25:35 +0900 | [diff] [blame] | 2888 | kfree(pntsd); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2889 | if (rc) |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2890 | pr_err("failed to store ntacl in xattr : %d\n", |
| 2891 | rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2892 | } |
| 2893 | } |
| 2894 | } |
| 2895 | rc = 0; |
| 2896 | } |
| 2897 | |
| 2898 | if (stream_name) { |
| 2899 | rc = smb2_set_stream_name_xattr(&path, |
| 2900 | fp, |
| 2901 | stream_name, |
| 2902 | s_type); |
| 2903 | if (rc) |
| 2904 | goto err_out; |
| 2905 | file_info = FILE_CREATED; |
| 2906 | } |
| 2907 | |
| 2908 | fp->attrib_only = !(req->DesiredAccess & ~(FILE_READ_ATTRIBUTES_LE | |
| 2909 | FILE_WRITE_ATTRIBUTES_LE | FILE_SYNCHRONIZE_LE)); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2910 | if (!S_ISDIR(file_inode(filp)->i_mode) && open_flags & O_TRUNC && |
| 2911 | !fp->attrib_only && !stream_name) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2912 | smb_break_all_oplock(work, fp); |
| 2913 | need_truncate = 1; |
| 2914 | } |
| 2915 | |
| 2916 | /* fp should be searchable through ksmbd_inode.m_fp_list |
| 2917 | * after daccess, saccess, attrib_only, and stream are |
| 2918 | * initialized. |
| 2919 | */ |
| 2920 | write_lock(&fp->f_ci->m_lock); |
| 2921 | list_add(&fp->node, &fp->f_ci->m_fp_list); |
| 2922 | write_unlock(&fp->f_ci->m_lock); |
| 2923 | |
| 2924 | rc = ksmbd_vfs_getattr(&path, &stat); |
| 2925 | if (rc) { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2926 | generic_fillattr(user_ns, d_inode(path.dentry), &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2927 | rc = 0; |
| 2928 | } |
| 2929 | |
| 2930 | /* Check delete pending among previous fp before oplock break */ |
| 2931 | if (ksmbd_inode_pending_delete(fp)) { |
| 2932 | rc = -EBUSY; |
| 2933 | goto err_out; |
| 2934 | } |
| 2935 | |
| 2936 | share_ret = ksmbd_smb_check_shared_mode(fp->filp, fp); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2937 | if (!test_share_config_flag(work->tcon->share_conf, KSMBD_SHARE_FLAG_OPLOCKS) || |
| 2938 | (req_op_level == SMB2_OPLOCK_LEVEL_LEASE && |
| 2939 | !(conn->vals->capabilities & SMB2_GLOBAL_CAP_LEASING))) { |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 2940 | if (share_ret < 0 && !S_ISDIR(file_inode(fp->filp)->i_mode)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2941 | rc = share_ret; |
| 2942 | goto err_out; |
| 2943 | } |
| 2944 | } else { |
| 2945 | if (req_op_level == SMB2_OPLOCK_LEVEL_LEASE) { |
| 2946 | req_op_level = smb2_map_lease_to_oplock(lc->req_state); |
| 2947 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2948 | "lease req for(%s) req oplock state 0x%x, lease state 0x%x\n", |
| 2949 | name, req_op_level, lc->req_state); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2950 | rc = find_same_lease_key(sess, fp->f_ci, lc); |
| 2951 | if (rc) |
| 2952 | goto err_out; |
| 2953 | } else if (open_flags == O_RDONLY && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2954 | (req_op_level == SMB2_OPLOCK_LEVEL_BATCH || |
| 2955 | req_op_level == SMB2_OPLOCK_LEVEL_EXCLUSIVE)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2956 | req_op_level = SMB2_OPLOCK_LEVEL_II; |
| 2957 | |
| 2958 | rc = smb_grant_oplock(work, req_op_level, |
| 2959 | fp->persistent_id, fp, |
| 2960 | le32_to_cpu(req->hdr.Id.SyncId.TreeId), |
| 2961 | lc, share_ret); |
| 2962 | if (rc < 0) |
| 2963 | goto err_out; |
| 2964 | } |
| 2965 | |
| 2966 | if (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE) |
| 2967 | ksmbd_fd_set_delete_on_close(fp, file_info); |
| 2968 | |
| 2969 | if (need_truncate) { |
| 2970 | rc = smb2_create_truncate(&path); |
| 2971 | if (rc) |
| 2972 | goto err_out; |
| 2973 | } |
| 2974 | |
| 2975 | if (req->CreateContextsOffset) { |
| 2976 | struct create_alloc_size_req *az_req; |
| 2977 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2978 | az_req = (struct create_alloc_size_req *)smb2_find_context_vals(req, |
| 2979 | SMB2_CREATE_ALLOCATION_SIZE); |
Namjae Jeon | f19b396 | 2021-07-13 09:59:34 +0900 | [diff] [blame] | 2980 | if (IS_ERR(az_req)) { |
| 2981 | rc = PTR_ERR(az_req); |
| 2982 | goto err_out; |
| 2983 | } else if (az_req) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2984 | loff_t alloc_size = le64_to_cpu(az_req->AllocationSize); |
| 2985 | int err; |
| 2986 | |
| 2987 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2988 | "request smb2 create allocate size : %llu\n", |
| 2989 | alloc_size); |
Namjae Jeon | e8c0619 | 2021-06-22 11:06:11 +0900 | [diff] [blame] | 2990 | smb_break_all_levII_oplock(work, fp, 1); |
| 2991 | err = vfs_fallocate(fp->filp, FALLOC_FL_KEEP_SIZE, 0, |
| 2992 | alloc_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2993 | if (err < 0) |
| 2994 | ksmbd_debug(SMB, |
Namjae Jeon | e8c0619 | 2021-06-22 11:06:11 +0900 | [diff] [blame] | 2995 | "vfs_fallocate is failed : %d\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2996 | err); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2997 | } |
| 2998 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2999 | context = smb2_find_context_vals(req, SMB2_CREATE_QUERY_ON_DISK_ID); |
Namjae Jeon | f19b396 | 2021-07-13 09:59:34 +0900 | [diff] [blame] | 3000 | if (IS_ERR(context)) { |
| 3001 | rc = PTR_ERR(context); |
| 3002 | goto err_out; |
| 3003 | } else if (context) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3004 | ksmbd_debug(SMB, "get query on disk id context\n"); |
| 3005 | query_disk_id = 1; |
| 3006 | } |
| 3007 | } |
| 3008 | |
| 3009 | if (stat.result_mask & STATX_BTIME) |
| 3010 | fp->create_time = ksmbd_UnixTimeToNT(stat.btime); |
| 3011 | else |
| 3012 | fp->create_time = ksmbd_UnixTimeToNT(stat.ctime); |
| 3013 | if (req->FileAttributes || fp->f_ci->m_fattr == 0) |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3014 | fp->f_ci->m_fattr = |
| 3015 | 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] | 3016 | |
| 3017 | if (!created) |
| 3018 | smb2_update_xattrs(tcon, &path, fp); |
| 3019 | else |
| 3020 | smb2_new_xattrs(tcon, &path, fp); |
| 3021 | |
| 3022 | memcpy(fp->client_guid, conn->ClientGUID, SMB2_CLIENT_GUID_SIZE); |
| 3023 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 3024 | generic_fillattr(user_ns, file_inode(fp->filp), |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 3025 | &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3026 | |
| 3027 | rsp->StructureSize = cpu_to_le16(89); |
| 3028 | rcu_read_lock(); |
| 3029 | opinfo = rcu_dereference(fp->f_opinfo); |
| 3030 | rsp->OplockLevel = opinfo != NULL ? opinfo->level : 0; |
| 3031 | rcu_read_unlock(); |
| 3032 | rsp->Reserved = 0; |
| 3033 | rsp->CreateAction = cpu_to_le32(file_info); |
| 3034 | rsp->CreationTime = cpu_to_le64(fp->create_time); |
| 3035 | time = ksmbd_UnixTimeToNT(stat.atime); |
| 3036 | rsp->LastAccessTime = cpu_to_le64(time); |
| 3037 | time = ksmbd_UnixTimeToNT(stat.mtime); |
| 3038 | rsp->LastWriteTime = cpu_to_le64(time); |
| 3039 | time = ksmbd_UnixTimeToNT(stat.ctime); |
| 3040 | rsp->ChangeTime = cpu_to_le64(time); |
| 3041 | rsp->AllocationSize = S_ISDIR(stat.mode) ? 0 : |
| 3042 | cpu_to_le64(stat.blocks << 9); |
| 3043 | rsp->EndofFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size); |
| 3044 | rsp->FileAttributes = fp->f_ci->m_fattr; |
| 3045 | |
| 3046 | rsp->Reserved2 = 0; |
| 3047 | |
| 3048 | rsp->PersistentFileId = cpu_to_le64(fp->persistent_id); |
| 3049 | rsp->VolatileFileId = cpu_to_le64(fp->volatile_id); |
| 3050 | |
| 3051 | rsp->CreateContextsOffset = 0; |
| 3052 | rsp->CreateContextsLength = 0; |
| 3053 | inc_rfc1001_len(rsp_org, 88); /* StructureSize - 1*/ |
| 3054 | |
| 3055 | /* If lease is request send lease context response */ |
| 3056 | if (opinfo && opinfo->is_lease) { |
| 3057 | struct create_context *lease_ccontext; |
| 3058 | |
| 3059 | ksmbd_debug(SMB, "lease granted on(%s) lease state 0x%x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3060 | name, opinfo->o_lease->state); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3061 | rsp->OplockLevel = SMB2_OPLOCK_LEVEL_LEASE; |
| 3062 | |
| 3063 | lease_ccontext = (struct create_context *)rsp->Buffer; |
| 3064 | contxt_cnt++; |
| 3065 | create_lease_buf(rsp->Buffer, opinfo->o_lease); |
| 3066 | le32_add_cpu(&rsp->CreateContextsLength, |
| 3067 | conn->vals->create_lease_size); |
| 3068 | inc_rfc1001_len(rsp_org, conn->vals->create_lease_size); |
| 3069 | next_ptr = &lease_ccontext->Next; |
| 3070 | next_off = conn->vals->create_lease_size; |
| 3071 | } |
| 3072 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3073 | if (maximal_access_ctxt) { |
| 3074 | struct create_context *mxac_ccontext; |
| 3075 | |
| 3076 | if (maximal_access == 0) |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 3077 | ksmbd_vfs_query_maximal_access(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 3078 | path.dentry, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3079 | &maximal_access); |
| 3080 | mxac_ccontext = (struct create_context *)(rsp->Buffer + |
| 3081 | le32_to_cpu(rsp->CreateContextsLength)); |
| 3082 | contxt_cnt++; |
| 3083 | create_mxac_rsp_buf(rsp->Buffer + |
| 3084 | le32_to_cpu(rsp->CreateContextsLength), |
| 3085 | le32_to_cpu(maximal_access)); |
| 3086 | le32_add_cpu(&rsp->CreateContextsLength, |
| 3087 | conn->vals->create_mxac_size); |
| 3088 | inc_rfc1001_len(rsp_org, conn->vals->create_mxac_size); |
| 3089 | if (next_ptr) |
| 3090 | *next_ptr = cpu_to_le32(next_off); |
| 3091 | next_ptr = &mxac_ccontext->Next; |
| 3092 | next_off = conn->vals->create_mxac_size; |
| 3093 | } |
| 3094 | |
| 3095 | if (query_disk_id) { |
| 3096 | struct create_context *disk_id_ccontext; |
| 3097 | |
| 3098 | disk_id_ccontext = (struct create_context *)(rsp->Buffer + |
| 3099 | le32_to_cpu(rsp->CreateContextsLength)); |
| 3100 | contxt_cnt++; |
| 3101 | create_disk_id_rsp_buf(rsp->Buffer + |
| 3102 | le32_to_cpu(rsp->CreateContextsLength), |
| 3103 | stat.ino, tcon->id); |
| 3104 | le32_add_cpu(&rsp->CreateContextsLength, |
| 3105 | conn->vals->create_disk_id_size); |
| 3106 | inc_rfc1001_len(rsp_org, conn->vals->create_disk_id_size); |
| 3107 | if (next_ptr) |
| 3108 | *next_ptr = cpu_to_le32(next_off); |
| 3109 | next_ptr = &disk_id_ccontext->Next; |
| 3110 | next_off = conn->vals->create_disk_id_size; |
| 3111 | } |
| 3112 | |
| 3113 | if (posix_ctxt) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3114 | contxt_cnt++; |
| 3115 | create_posix_rsp_buf(rsp->Buffer + |
| 3116 | le32_to_cpu(rsp->CreateContextsLength), |
| 3117 | fp); |
| 3118 | le32_add_cpu(&rsp->CreateContextsLength, |
| 3119 | conn->vals->create_posix_size); |
| 3120 | inc_rfc1001_len(rsp_org, conn->vals->create_posix_size); |
| 3121 | if (next_ptr) |
| 3122 | *next_ptr = cpu_to_le32(next_off); |
| 3123 | } |
| 3124 | |
| 3125 | if (contxt_cnt > 0) { |
| 3126 | rsp->CreateContextsOffset = |
| 3127 | cpu_to_le32(offsetof(struct smb2_create_rsp, Buffer) |
| 3128 | - 4); |
| 3129 | } |
| 3130 | |
| 3131 | err_out: |
| 3132 | if (file_present || created) |
| 3133 | path_put(&path); |
| 3134 | ksmbd_revert_fsids(work); |
| 3135 | err_out1: |
| 3136 | if (rc) { |
| 3137 | if (rc == -EINVAL) |
| 3138 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 3139 | else if (rc == -EOPNOTSUPP) |
| 3140 | rsp->hdr.Status = STATUS_NOT_SUPPORTED; |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame^] | 3141 | else if (rc == -EACCES || rc == -ESTALE || rc == -EXDEV) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3142 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 3143 | else if (rc == -ENOENT) |
| 3144 | rsp->hdr.Status = STATUS_OBJECT_NAME_INVALID; |
| 3145 | else if (rc == -EPERM) |
| 3146 | rsp->hdr.Status = STATUS_SHARING_VIOLATION; |
| 3147 | else if (rc == -EBUSY) |
| 3148 | rsp->hdr.Status = STATUS_DELETE_PENDING; |
| 3149 | else if (rc == -EBADF) |
| 3150 | rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND; |
| 3151 | else if (rc == -ENOEXEC) |
| 3152 | rsp->hdr.Status = STATUS_DUPLICATE_OBJECTID; |
| 3153 | else if (rc == -ENXIO) |
| 3154 | rsp->hdr.Status = STATUS_NO_SUCH_DEVICE; |
| 3155 | else if (rc == -EEXIST) |
| 3156 | rsp->hdr.Status = STATUS_OBJECT_NAME_COLLISION; |
| 3157 | else if (rc == -EMFILE) |
| 3158 | rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES; |
| 3159 | if (!rsp->hdr.Status) |
| 3160 | rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR; |
| 3161 | |
| 3162 | if (!fp || !fp->filename) |
| 3163 | kfree(name); |
| 3164 | if (fp) |
| 3165 | ksmbd_fd_put(work, fp); |
| 3166 | smb2_set_err_rsp(work); |
| 3167 | ksmbd_debug(SMB, "Error response: %x\n", rsp->hdr.Status); |
| 3168 | } |
| 3169 | |
| 3170 | kfree(lc); |
| 3171 | |
| 3172 | return 0; |
| 3173 | } |
| 3174 | |
| 3175 | static int readdir_info_level_struct_sz(int info_level) |
| 3176 | { |
| 3177 | switch (info_level) { |
| 3178 | case FILE_FULL_DIRECTORY_INFORMATION: |
| 3179 | return sizeof(struct file_full_directory_info); |
| 3180 | case FILE_BOTH_DIRECTORY_INFORMATION: |
| 3181 | return sizeof(struct file_both_directory_info); |
| 3182 | case FILE_DIRECTORY_INFORMATION: |
| 3183 | return sizeof(struct file_directory_info); |
| 3184 | case FILE_NAMES_INFORMATION: |
| 3185 | return sizeof(struct file_names_info); |
| 3186 | case FILEID_FULL_DIRECTORY_INFORMATION: |
| 3187 | return sizeof(struct file_id_full_dir_info); |
| 3188 | case FILEID_BOTH_DIRECTORY_INFORMATION: |
| 3189 | return sizeof(struct file_id_both_directory_info); |
| 3190 | case SMB_FIND_FILE_POSIX_INFO: |
| 3191 | return sizeof(struct smb2_posix_info); |
| 3192 | default: |
| 3193 | return -EOPNOTSUPP; |
| 3194 | } |
| 3195 | } |
| 3196 | |
| 3197 | static int dentry_name(struct ksmbd_dir_info *d_info, int info_level) |
| 3198 | { |
| 3199 | switch (info_level) { |
| 3200 | case FILE_FULL_DIRECTORY_INFORMATION: |
| 3201 | { |
| 3202 | struct file_full_directory_info *ffdinfo; |
| 3203 | |
| 3204 | ffdinfo = (struct file_full_directory_info *)d_info->rptr; |
| 3205 | d_info->rptr += le32_to_cpu(ffdinfo->NextEntryOffset); |
| 3206 | d_info->name = ffdinfo->FileName; |
| 3207 | d_info->name_len = le32_to_cpu(ffdinfo->FileNameLength); |
| 3208 | return 0; |
| 3209 | } |
| 3210 | case FILE_BOTH_DIRECTORY_INFORMATION: |
| 3211 | { |
| 3212 | struct file_both_directory_info *fbdinfo; |
| 3213 | |
| 3214 | fbdinfo = (struct file_both_directory_info *)d_info->rptr; |
| 3215 | d_info->rptr += le32_to_cpu(fbdinfo->NextEntryOffset); |
| 3216 | d_info->name = fbdinfo->FileName; |
| 3217 | d_info->name_len = le32_to_cpu(fbdinfo->FileNameLength); |
| 3218 | return 0; |
| 3219 | } |
| 3220 | case FILE_DIRECTORY_INFORMATION: |
| 3221 | { |
| 3222 | struct file_directory_info *fdinfo; |
| 3223 | |
| 3224 | fdinfo = (struct file_directory_info *)d_info->rptr; |
| 3225 | d_info->rptr += le32_to_cpu(fdinfo->NextEntryOffset); |
| 3226 | d_info->name = fdinfo->FileName; |
| 3227 | d_info->name_len = le32_to_cpu(fdinfo->FileNameLength); |
| 3228 | return 0; |
| 3229 | } |
| 3230 | case FILE_NAMES_INFORMATION: |
| 3231 | { |
| 3232 | struct file_names_info *fninfo; |
| 3233 | |
| 3234 | fninfo = (struct file_names_info *)d_info->rptr; |
| 3235 | d_info->rptr += le32_to_cpu(fninfo->NextEntryOffset); |
| 3236 | d_info->name = fninfo->FileName; |
| 3237 | d_info->name_len = le32_to_cpu(fninfo->FileNameLength); |
| 3238 | return 0; |
| 3239 | } |
| 3240 | case FILEID_FULL_DIRECTORY_INFORMATION: |
| 3241 | { |
| 3242 | struct file_id_full_dir_info *dinfo; |
| 3243 | |
| 3244 | dinfo = (struct file_id_full_dir_info *)d_info->rptr; |
| 3245 | d_info->rptr += le32_to_cpu(dinfo->NextEntryOffset); |
| 3246 | d_info->name = dinfo->FileName; |
| 3247 | d_info->name_len = le32_to_cpu(dinfo->FileNameLength); |
| 3248 | return 0; |
| 3249 | } |
| 3250 | case FILEID_BOTH_DIRECTORY_INFORMATION: |
| 3251 | { |
| 3252 | struct file_id_both_directory_info *fibdinfo; |
| 3253 | |
| 3254 | fibdinfo = (struct file_id_both_directory_info *)d_info->rptr; |
| 3255 | d_info->rptr += le32_to_cpu(fibdinfo->NextEntryOffset); |
| 3256 | d_info->name = fibdinfo->FileName; |
| 3257 | d_info->name_len = le32_to_cpu(fibdinfo->FileNameLength); |
| 3258 | return 0; |
| 3259 | } |
| 3260 | case SMB_FIND_FILE_POSIX_INFO: |
| 3261 | { |
| 3262 | struct smb2_posix_info *posix_info; |
| 3263 | |
| 3264 | posix_info = (struct smb2_posix_info *)d_info->rptr; |
| 3265 | d_info->rptr += le32_to_cpu(posix_info->NextEntryOffset); |
| 3266 | d_info->name = posix_info->name; |
| 3267 | d_info->name_len = le32_to_cpu(posix_info->name_len); |
| 3268 | return 0; |
| 3269 | } |
| 3270 | default: |
| 3271 | return -EINVAL; |
| 3272 | } |
| 3273 | } |
| 3274 | |
| 3275 | /** |
| 3276 | * smb2_populate_readdir_entry() - encode directory entry in smb2 response |
| 3277 | * buffer |
| 3278 | * @conn: connection instance |
| 3279 | * @info_level: smb information level |
| 3280 | * @d_info: structure included variables for query dir |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 3281 | * @user_ns: user namespace |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3282 | * @ksmbd_kstat: ksmbd wrapper of dirent stat information |
| 3283 | * |
| 3284 | * if directory has many entries, find first can't read it fully. |
| 3285 | * find next might be called multiple times to read remaining dir entries |
| 3286 | * |
| 3287 | * Return: 0 on success, otherwise error |
| 3288 | */ |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3289 | 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] | 3290 | struct ksmbd_dir_info *d_info, |
| 3291 | struct ksmbd_kstat *ksmbd_kstat) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3292 | { |
| 3293 | int next_entry_offset = 0; |
| 3294 | char *conv_name; |
| 3295 | int conv_len; |
| 3296 | void *kstat; |
Namjae Jeon | dac0ec6 | 2021-07-07 14:57:24 +0900 | [diff] [blame] | 3297 | int struct_sz, rc = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3298 | |
| 3299 | conv_name = ksmbd_convert_dir_info_name(d_info, |
| 3300 | conn->local_nls, |
| 3301 | &conv_len); |
| 3302 | if (!conv_name) |
| 3303 | return -ENOMEM; |
| 3304 | |
| 3305 | /* Somehow the name has only terminating NULL bytes */ |
| 3306 | if (conv_len < 0) { |
Namjae Jeon | dac0ec6 | 2021-07-07 14:57:24 +0900 | [diff] [blame] | 3307 | rc = -EINVAL; |
| 3308 | goto free_conv_name; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3309 | } |
| 3310 | |
| 3311 | struct_sz = readdir_info_level_struct_sz(info_level); |
| 3312 | next_entry_offset = ALIGN(struct_sz - 1 + conv_len, |
| 3313 | KSMBD_DIR_INFO_ALIGNMENT); |
| 3314 | |
| 3315 | if (next_entry_offset > d_info->out_buf_len) { |
| 3316 | d_info->out_buf_len = 0; |
Namjae Jeon | dac0ec6 | 2021-07-07 14:57:24 +0900 | [diff] [blame] | 3317 | rc = -ENOSPC; |
| 3318 | goto free_conv_name; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3319 | } |
| 3320 | |
| 3321 | kstat = d_info->wptr; |
| 3322 | if (info_level != FILE_NAMES_INFORMATION) |
| 3323 | kstat = ksmbd_vfs_init_kstat(&d_info->wptr, ksmbd_kstat); |
| 3324 | |
| 3325 | switch (info_level) { |
| 3326 | case FILE_FULL_DIRECTORY_INFORMATION: |
| 3327 | { |
| 3328 | struct file_full_directory_info *ffdinfo; |
| 3329 | |
| 3330 | ffdinfo = (struct file_full_directory_info *)kstat; |
| 3331 | ffdinfo->FileNameLength = cpu_to_le32(conv_len); |
| 3332 | ffdinfo->EaSize = |
| 3333 | smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode); |
| 3334 | if (ffdinfo->EaSize) |
| 3335 | ffdinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE; |
| 3336 | if (d_info->hide_dot_file && d_info->name[0] == '.') |
| 3337 | ffdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE; |
| 3338 | memcpy(ffdinfo->FileName, conv_name, conv_len); |
| 3339 | ffdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3340 | break; |
| 3341 | } |
| 3342 | case FILE_BOTH_DIRECTORY_INFORMATION: |
| 3343 | { |
| 3344 | struct file_both_directory_info *fbdinfo; |
| 3345 | |
| 3346 | fbdinfo = (struct file_both_directory_info *)kstat; |
| 3347 | fbdinfo->FileNameLength = cpu_to_le32(conv_len); |
| 3348 | fbdinfo->EaSize = |
| 3349 | smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode); |
| 3350 | if (fbdinfo->EaSize) |
| 3351 | fbdinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE; |
| 3352 | fbdinfo->ShortNameLength = 0; |
| 3353 | fbdinfo->Reserved = 0; |
| 3354 | if (d_info->hide_dot_file && d_info->name[0] == '.') |
| 3355 | fbdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE; |
| 3356 | memcpy(fbdinfo->FileName, conv_name, conv_len); |
| 3357 | fbdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3358 | break; |
| 3359 | } |
| 3360 | case FILE_DIRECTORY_INFORMATION: |
| 3361 | { |
| 3362 | struct file_directory_info *fdinfo; |
| 3363 | |
| 3364 | fdinfo = (struct file_directory_info *)kstat; |
| 3365 | fdinfo->FileNameLength = cpu_to_le32(conv_len); |
| 3366 | if (d_info->hide_dot_file && d_info->name[0] == '.') |
| 3367 | fdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE; |
| 3368 | memcpy(fdinfo->FileName, conv_name, conv_len); |
| 3369 | fdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3370 | break; |
| 3371 | } |
| 3372 | case FILE_NAMES_INFORMATION: |
| 3373 | { |
| 3374 | struct file_names_info *fninfo; |
| 3375 | |
| 3376 | fninfo = (struct file_names_info *)kstat; |
| 3377 | fninfo->FileNameLength = cpu_to_le32(conv_len); |
| 3378 | memcpy(fninfo->FileName, conv_name, conv_len); |
| 3379 | fninfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3380 | break; |
| 3381 | } |
| 3382 | case FILEID_FULL_DIRECTORY_INFORMATION: |
| 3383 | { |
| 3384 | struct file_id_full_dir_info *dinfo; |
| 3385 | |
| 3386 | dinfo = (struct file_id_full_dir_info *)kstat; |
| 3387 | dinfo->FileNameLength = cpu_to_le32(conv_len); |
| 3388 | dinfo->EaSize = |
| 3389 | smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode); |
| 3390 | if (dinfo->EaSize) |
| 3391 | dinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE; |
| 3392 | dinfo->Reserved = 0; |
| 3393 | dinfo->UniqueId = cpu_to_le64(ksmbd_kstat->kstat->ino); |
| 3394 | if (d_info->hide_dot_file && d_info->name[0] == '.') |
| 3395 | dinfo->ExtFileAttributes |= ATTR_HIDDEN_LE; |
| 3396 | memcpy(dinfo->FileName, conv_name, conv_len); |
| 3397 | dinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3398 | break; |
| 3399 | } |
| 3400 | case FILEID_BOTH_DIRECTORY_INFORMATION: |
| 3401 | { |
| 3402 | struct file_id_both_directory_info *fibdinfo; |
| 3403 | |
| 3404 | fibdinfo = (struct file_id_both_directory_info *)kstat; |
| 3405 | fibdinfo->FileNameLength = cpu_to_le32(conv_len); |
| 3406 | fibdinfo->EaSize = |
| 3407 | smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode); |
| 3408 | if (fibdinfo->EaSize) |
| 3409 | fibdinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE; |
| 3410 | fibdinfo->UniqueId = cpu_to_le64(ksmbd_kstat->kstat->ino); |
| 3411 | fibdinfo->ShortNameLength = 0; |
| 3412 | fibdinfo->Reserved = 0; |
| 3413 | fibdinfo->Reserved2 = cpu_to_le16(0); |
| 3414 | if (d_info->hide_dot_file && d_info->name[0] == '.') |
| 3415 | fibdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE; |
| 3416 | memcpy(fibdinfo->FileName, conv_name, conv_len); |
| 3417 | fibdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3418 | break; |
| 3419 | } |
| 3420 | case SMB_FIND_FILE_POSIX_INFO: |
| 3421 | { |
| 3422 | struct smb2_posix_info *posix_info; |
| 3423 | u64 time; |
| 3424 | |
| 3425 | posix_info = (struct smb2_posix_info *)kstat; |
| 3426 | posix_info->Ignored = 0; |
| 3427 | posix_info->CreationTime = cpu_to_le64(ksmbd_kstat->create_time); |
| 3428 | time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->ctime); |
| 3429 | posix_info->ChangeTime = cpu_to_le64(time); |
| 3430 | time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->atime); |
| 3431 | posix_info->LastAccessTime = cpu_to_le64(time); |
| 3432 | time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->mtime); |
| 3433 | posix_info->LastWriteTime = cpu_to_le64(time); |
| 3434 | posix_info->EndOfFile = cpu_to_le64(ksmbd_kstat->kstat->size); |
| 3435 | posix_info->AllocationSize = cpu_to_le64(ksmbd_kstat->kstat->blocks << 9); |
| 3436 | posix_info->DeviceId = cpu_to_le32(ksmbd_kstat->kstat->rdev); |
| 3437 | posix_info->HardLinks = cpu_to_le32(ksmbd_kstat->kstat->nlink); |
| 3438 | posix_info->Mode = cpu_to_le32(ksmbd_kstat->kstat->mode); |
| 3439 | posix_info->Inode = cpu_to_le64(ksmbd_kstat->kstat->ino); |
| 3440 | posix_info->DosAttributes = |
| 3441 | S_ISDIR(ksmbd_kstat->kstat->mode) ? ATTR_DIRECTORY_LE : ATTR_ARCHIVE_LE; |
| 3442 | if (d_info->hide_dot_file && d_info->name[0] == '.') |
| 3443 | posix_info->DosAttributes |= ATTR_HIDDEN_LE; |
Christian Brauner | 475d6f9 | 2021-08-23 17:13:48 +0200 | [diff] [blame] | 3444 | id_to_sid(from_kuid_munged(&init_user_ns, ksmbd_kstat->kstat->uid), |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3445 | SIDNFS_USER, (struct smb_sid *)&posix_info->SidBuffer[0]); |
Christian Brauner | 475d6f9 | 2021-08-23 17:13:48 +0200 | [diff] [blame] | 3446 | id_to_sid(from_kgid_munged(&init_user_ns, ksmbd_kstat->kstat->gid), |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3447 | SIDNFS_GROUP, (struct smb_sid *)&posix_info->SidBuffer[20]); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3448 | memcpy(posix_info->name, conv_name, conv_len); |
| 3449 | posix_info->name_len = cpu_to_le32(conv_len); |
| 3450 | posix_info->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3451 | break; |
| 3452 | } |
| 3453 | |
| 3454 | } /* switch (info_level) */ |
| 3455 | |
| 3456 | d_info->last_entry_offset = d_info->data_count; |
| 3457 | d_info->data_count += next_entry_offset; |
Marios Makassikis | e7735c8 | 2021-05-06 11:40:02 +0900 | [diff] [blame] | 3458 | d_info->out_buf_len -= next_entry_offset; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3459 | d_info->wptr += next_entry_offset; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3460 | |
| 3461 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3462 | "info_level : %d, buf_len :%d, next_offset : %d, data_count : %d\n", |
| 3463 | info_level, d_info->out_buf_len, |
| 3464 | next_entry_offset, d_info->data_count); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3465 | |
Namjae Jeon | dac0ec6 | 2021-07-07 14:57:24 +0900 | [diff] [blame] | 3466 | free_conv_name: |
| 3467 | kfree(conv_name); |
| 3468 | return rc; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3469 | } |
| 3470 | |
| 3471 | struct smb2_query_dir_private { |
| 3472 | struct ksmbd_work *work; |
| 3473 | char *search_pattern; |
| 3474 | struct ksmbd_file *dir_fp; |
| 3475 | |
| 3476 | struct ksmbd_dir_info *d_info; |
| 3477 | int info_level; |
| 3478 | }; |
| 3479 | |
| 3480 | static void lock_dir(struct ksmbd_file *dir_fp) |
| 3481 | { |
| 3482 | struct dentry *dir = dir_fp->filp->f_path.dentry; |
| 3483 | |
| 3484 | inode_lock_nested(d_inode(dir), I_MUTEX_PARENT); |
| 3485 | } |
| 3486 | |
| 3487 | static void unlock_dir(struct ksmbd_file *dir_fp) |
| 3488 | { |
| 3489 | struct dentry *dir = dir_fp->filp->f_path.dentry; |
| 3490 | |
| 3491 | inode_unlock(d_inode(dir)); |
| 3492 | } |
| 3493 | |
| 3494 | static int process_query_dir_entries(struct smb2_query_dir_private *priv) |
| 3495 | { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 3496 | 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] | 3497 | struct kstat kstat; |
| 3498 | struct ksmbd_kstat ksmbd_kstat; |
| 3499 | int rc; |
| 3500 | int i; |
| 3501 | |
| 3502 | for (i = 0; i < priv->d_info->num_entry; i++) { |
| 3503 | struct dentry *dent; |
| 3504 | |
| 3505 | if (dentry_name(priv->d_info, priv->info_level)) |
| 3506 | return -EINVAL; |
| 3507 | |
| 3508 | lock_dir(priv->dir_fp); |
Christian Brauner | da1e7ad | 2021-08-23 17:13:47 +0200 | [diff] [blame] | 3509 | dent = lookup_one(user_ns, priv->d_info->name, |
| 3510 | priv->dir_fp->filp->f_path.dentry, |
| 3511 | priv->d_info->name_len); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3512 | unlock_dir(priv->dir_fp); |
| 3513 | |
| 3514 | if (IS_ERR(dent)) { |
| 3515 | ksmbd_debug(SMB, "Cannot lookup `%s' [%ld]\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3516 | priv->d_info->name, |
| 3517 | PTR_ERR(dent)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3518 | continue; |
| 3519 | } |
| 3520 | if (unlikely(d_is_negative(dent))) { |
| 3521 | dput(dent); |
| 3522 | ksmbd_debug(SMB, "Negative dentry `%s'\n", |
| 3523 | priv->d_info->name); |
| 3524 | continue; |
| 3525 | } |
| 3526 | |
| 3527 | ksmbd_kstat.kstat = &kstat; |
| 3528 | if (priv->info_level != FILE_NAMES_INFORMATION) |
| 3529 | ksmbd_vfs_fill_dentry_attrs(priv->work, |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 3530 | user_ns, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3531 | dent, |
| 3532 | &ksmbd_kstat); |
| 3533 | |
| 3534 | rc = smb2_populate_readdir_entry(priv->work->conn, |
| 3535 | priv->info_level, |
| 3536 | priv->d_info, |
| 3537 | &ksmbd_kstat); |
| 3538 | dput(dent); |
| 3539 | if (rc) |
| 3540 | return rc; |
| 3541 | } |
| 3542 | return 0; |
| 3543 | } |
| 3544 | |
| 3545 | static int reserve_populate_dentry(struct ksmbd_dir_info *d_info, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3546 | int info_level) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3547 | { |
| 3548 | int struct_sz; |
| 3549 | int conv_len; |
| 3550 | int next_entry_offset; |
| 3551 | |
| 3552 | struct_sz = readdir_info_level_struct_sz(info_level); |
| 3553 | if (struct_sz == -EOPNOTSUPP) |
| 3554 | return -EOPNOTSUPP; |
| 3555 | |
| 3556 | conv_len = (d_info->name_len + 1) * 2; |
| 3557 | next_entry_offset = ALIGN(struct_sz - 1 + conv_len, |
| 3558 | KSMBD_DIR_INFO_ALIGNMENT); |
| 3559 | |
| 3560 | if (next_entry_offset > d_info->out_buf_len) { |
| 3561 | d_info->out_buf_len = 0; |
| 3562 | return -ENOSPC; |
| 3563 | } |
| 3564 | |
| 3565 | switch (info_level) { |
| 3566 | case FILE_FULL_DIRECTORY_INFORMATION: |
| 3567 | { |
| 3568 | struct file_full_directory_info *ffdinfo; |
| 3569 | |
| 3570 | ffdinfo = (struct file_full_directory_info *)d_info->wptr; |
| 3571 | memcpy(ffdinfo->FileName, d_info->name, d_info->name_len); |
| 3572 | ffdinfo->FileName[d_info->name_len] = 0x00; |
| 3573 | ffdinfo->FileNameLength = cpu_to_le32(d_info->name_len); |
| 3574 | ffdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3575 | break; |
| 3576 | } |
| 3577 | case FILE_BOTH_DIRECTORY_INFORMATION: |
| 3578 | { |
| 3579 | struct file_both_directory_info *fbdinfo; |
| 3580 | |
| 3581 | fbdinfo = (struct file_both_directory_info *)d_info->wptr; |
| 3582 | memcpy(fbdinfo->FileName, d_info->name, d_info->name_len); |
| 3583 | fbdinfo->FileName[d_info->name_len] = 0x00; |
| 3584 | fbdinfo->FileNameLength = cpu_to_le32(d_info->name_len); |
| 3585 | fbdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3586 | break; |
| 3587 | } |
| 3588 | case FILE_DIRECTORY_INFORMATION: |
| 3589 | { |
| 3590 | struct file_directory_info *fdinfo; |
| 3591 | |
| 3592 | fdinfo = (struct file_directory_info *)d_info->wptr; |
| 3593 | memcpy(fdinfo->FileName, d_info->name, d_info->name_len); |
| 3594 | fdinfo->FileName[d_info->name_len] = 0x00; |
| 3595 | fdinfo->FileNameLength = cpu_to_le32(d_info->name_len); |
| 3596 | fdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3597 | break; |
| 3598 | } |
| 3599 | case FILE_NAMES_INFORMATION: |
| 3600 | { |
| 3601 | struct file_names_info *fninfo; |
| 3602 | |
| 3603 | fninfo = (struct file_names_info *)d_info->wptr; |
| 3604 | memcpy(fninfo->FileName, d_info->name, d_info->name_len); |
| 3605 | fninfo->FileName[d_info->name_len] = 0x00; |
| 3606 | fninfo->FileNameLength = cpu_to_le32(d_info->name_len); |
| 3607 | fninfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3608 | break; |
| 3609 | } |
| 3610 | case FILEID_FULL_DIRECTORY_INFORMATION: |
| 3611 | { |
| 3612 | struct file_id_full_dir_info *dinfo; |
| 3613 | |
| 3614 | dinfo = (struct file_id_full_dir_info *)d_info->wptr; |
| 3615 | memcpy(dinfo->FileName, d_info->name, d_info->name_len); |
| 3616 | dinfo->FileName[d_info->name_len] = 0x00; |
| 3617 | dinfo->FileNameLength = cpu_to_le32(d_info->name_len); |
| 3618 | dinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3619 | break; |
| 3620 | } |
| 3621 | case FILEID_BOTH_DIRECTORY_INFORMATION: |
| 3622 | { |
| 3623 | struct file_id_both_directory_info *fibdinfo; |
| 3624 | |
| 3625 | fibdinfo = (struct file_id_both_directory_info *)d_info->wptr; |
| 3626 | memcpy(fibdinfo->FileName, d_info->name, d_info->name_len); |
| 3627 | fibdinfo->FileName[d_info->name_len] = 0x00; |
| 3628 | fibdinfo->FileNameLength = cpu_to_le32(d_info->name_len); |
| 3629 | fibdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3630 | break; |
| 3631 | } |
| 3632 | case SMB_FIND_FILE_POSIX_INFO: |
| 3633 | { |
| 3634 | struct smb2_posix_info *posix_info; |
| 3635 | |
| 3636 | posix_info = (struct smb2_posix_info *)d_info->wptr; |
| 3637 | memcpy(posix_info->name, d_info->name, d_info->name_len); |
| 3638 | posix_info->name[d_info->name_len] = 0x00; |
| 3639 | posix_info->name_len = cpu_to_le32(d_info->name_len); |
| 3640 | posix_info->NextEntryOffset = |
| 3641 | cpu_to_le32(next_entry_offset); |
| 3642 | break; |
| 3643 | } |
| 3644 | } /* switch (info_level) */ |
| 3645 | |
| 3646 | d_info->num_entry++; |
| 3647 | d_info->out_buf_len -= next_entry_offset; |
| 3648 | d_info->wptr += next_entry_offset; |
| 3649 | return 0; |
| 3650 | } |
| 3651 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3652 | static int __query_dir(struct dir_context *ctx, const char *name, int namlen, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3653 | loff_t offset, u64 ino, unsigned int d_type) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3654 | { |
| 3655 | struct ksmbd_readdir_data *buf; |
| 3656 | struct smb2_query_dir_private *priv; |
| 3657 | struct ksmbd_dir_info *d_info; |
| 3658 | int rc; |
| 3659 | |
| 3660 | buf = container_of(ctx, struct ksmbd_readdir_data, ctx); |
| 3661 | priv = buf->private; |
| 3662 | d_info = priv->d_info; |
| 3663 | |
| 3664 | /* dot and dotdot entries are already reserved */ |
| 3665 | if (!strcmp(".", name) || !strcmp("..", name)) |
| 3666 | return 0; |
| 3667 | if (ksmbd_share_veto_filename(priv->work->tcon->share_conf, name)) |
| 3668 | return 0; |
Namjae Jeon | b24c933 | 2021-03-21 17:32:19 +0900 | [diff] [blame] | 3669 | if (!match_pattern(name, namlen, priv->search_pattern)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3670 | return 0; |
| 3671 | |
| 3672 | d_info->name = name; |
| 3673 | d_info->name_len = namlen; |
| 3674 | rc = reserve_populate_dentry(d_info, priv->info_level); |
| 3675 | if (rc) |
| 3676 | return rc; |
| 3677 | if (d_info->flags & SMB2_RETURN_SINGLE_ENTRY) { |
| 3678 | d_info->out_buf_len = 0; |
| 3679 | return 0; |
| 3680 | } |
| 3681 | return 0; |
| 3682 | } |
| 3683 | |
| 3684 | static void restart_ctx(struct dir_context *ctx) |
| 3685 | { |
| 3686 | ctx->pos = 0; |
| 3687 | } |
| 3688 | |
| 3689 | static int verify_info_level(int info_level) |
| 3690 | { |
| 3691 | switch (info_level) { |
| 3692 | case FILE_FULL_DIRECTORY_INFORMATION: |
| 3693 | case FILE_BOTH_DIRECTORY_INFORMATION: |
| 3694 | case FILE_DIRECTORY_INFORMATION: |
| 3695 | case FILE_NAMES_INFORMATION: |
| 3696 | case FILEID_FULL_DIRECTORY_INFORMATION: |
| 3697 | case FILEID_BOTH_DIRECTORY_INFORMATION: |
| 3698 | case SMB_FIND_FILE_POSIX_INFO: |
| 3699 | break; |
| 3700 | default: |
| 3701 | return -EOPNOTSUPP; |
| 3702 | } |
| 3703 | |
| 3704 | return 0; |
| 3705 | } |
| 3706 | |
| 3707 | int smb2_query_dir(struct ksmbd_work *work) |
| 3708 | { |
| 3709 | struct ksmbd_conn *conn = work->conn; |
| 3710 | struct smb2_query_directory_req *req; |
| 3711 | struct smb2_query_directory_rsp *rsp, *rsp_org; |
| 3712 | struct ksmbd_share_config *share = work->tcon->share_conf; |
| 3713 | struct ksmbd_file *dir_fp = NULL; |
| 3714 | struct ksmbd_dir_info d_info; |
| 3715 | int rc = 0; |
| 3716 | char *srch_ptr = NULL; |
| 3717 | unsigned char srch_flag; |
| 3718 | int buffer_sz; |
| 3719 | struct smb2_query_dir_private query_dir_private = {NULL, }; |
| 3720 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 3721 | rsp_org = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3722 | WORK_BUFFERS(work, req, rsp); |
| 3723 | |
| 3724 | if (ksmbd_override_fsids(work)) { |
| 3725 | rsp->hdr.Status = STATUS_NO_MEMORY; |
| 3726 | smb2_set_err_rsp(work); |
| 3727 | return -ENOMEM; |
| 3728 | } |
| 3729 | |
| 3730 | rc = verify_info_level(req->FileInformationClass); |
| 3731 | if (rc) { |
| 3732 | rc = -EFAULT; |
| 3733 | goto err_out2; |
| 3734 | } |
| 3735 | |
| 3736 | dir_fp = ksmbd_lookup_fd_slow(work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3737 | le64_to_cpu(req->VolatileFileId), |
| 3738 | le64_to_cpu(req->PersistentFileId)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3739 | if (!dir_fp) { |
| 3740 | rc = -EBADF; |
| 3741 | goto err_out2; |
| 3742 | } |
| 3743 | |
| 3744 | if (!(dir_fp->daccess & FILE_LIST_DIRECTORY_LE) || |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 3745 | inode_permission(file_mnt_user_ns(dir_fp->filp), |
| 3746 | file_inode(dir_fp->filp), |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3747 | MAY_READ | MAY_EXEC)) { |
Namjae Jeon | 493fa2f | 2021-06-29 09:22:16 +0900 | [diff] [blame] | 3748 | pr_err("no right to enumerate directory (%pd)\n", |
| 3749 | dir_fp->filp->f_path.dentry); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3750 | rc = -EACCES; |
| 3751 | goto err_out2; |
| 3752 | } |
| 3753 | |
| 3754 | if (!S_ISDIR(file_inode(dir_fp->filp)->i_mode)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 3755 | pr_err("can't do query dir for a file\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3756 | rc = -EINVAL; |
| 3757 | goto err_out2; |
| 3758 | } |
| 3759 | |
| 3760 | srch_flag = req->Flags; |
| 3761 | srch_ptr = smb_strndup_from_utf16(req->Buffer, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3762 | le16_to_cpu(req->FileNameLength), 1, |
| 3763 | conn->local_nls); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3764 | if (IS_ERR(srch_ptr)) { |
| 3765 | ksmbd_debug(SMB, "Search Pattern not found\n"); |
| 3766 | rc = -EINVAL; |
| 3767 | goto err_out2; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3768 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3769 | ksmbd_debug(SMB, "Search pattern is %s\n", srch_ptr); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3770 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3771 | |
| 3772 | ksmbd_debug(SMB, "Directory name is %s\n", dir_fp->filename); |
| 3773 | |
| 3774 | if (srch_flag & SMB2_REOPEN || srch_flag & SMB2_RESTART_SCANS) { |
| 3775 | ksmbd_debug(SMB, "Restart directory scan\n"); |
| 3776 | generic_file_llseek(dir_fp->filp, 0, SEEK_SET); |
| 3777 | restart_ctx(&dir_fp->readdir_data.ctx); |
| 3778 | } |
| 3779 | |
| 3780 | memset(&d_info, 0, sizeof(struct ksmbd_dir_info)); |
| 3781 | d_info.wptr = (char *)rsp->Buffer; |
| 3782 | d_info.rptr = (char *)rsp->Buffer; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3783 | d_info.out_buf_len = (work->response_sz - (get_rfc1002_len(rsp_org) + 4)); |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3784 | d_info.out_buf_len = min_t(int, d_info.out_buf_len, le32_to_cpu(req->OutputBufferLength)) - |
| 3785 | sizeof(struct smb2_query_directory_rsp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3786 | d_info.flags = srch_flag; |
| 3787 | |
| 3788 | /* |
| 3789 | * reserve dot and dotdot entries in head of buffer |
| 3790 | * in first response |
| 3791 | */ |
| 3792 | rc = ksmbd_populate_dot_dotdot_entries(work, req->FileInformationClass, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3793 | dir_fp, &d_info, srch_ptr, |
| 3794 | smb2_populate_readdir_entry); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3795 | if (rc == -ENOSPC) |
| 3796 | rc = 0; |
| 3797 | else if (rc) |
| 3798 | goto err_out; |
| 3799 | |
| 3800 | if (test_share_config_flag(share, KSMBD_SHARE_FLAG_HIDE_DOT_FILES)) |
| 3801 | d_info.hide_dot_file = true; |
| 3802 | |
| 3803 | buffer_sz = d_info.out_buf_len; |
| 3804 | d_info.rptr = d_info.wptr; |
| 3805 | query_dir_private.work = work; |
| 3806 | query_dir_private.search_pattern = srch_ptr; |
| 3807 | query_dir_private.dir_fp = dir_fp; |
| 3808 | query_dir_private.d_info = &d_info; |
| 3809 | query_dir_private.info_level = req->FileInformationClass; |
| 3810 | dir_fp->readdir_data.private = &query_dir_private; |
| 3811 | set_ctx_actor(&dir_fp->readdir_data.ctx, __query_dir); |
| 3812 | |
Namjae Jeon | e8c0619 | 2021-06-22 11:06:11 +0900 | [diff] [blame] | 3813 | rc = iterate_dir(dir_fp->filp, &dir_fp->readdir_data.ctx); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3814 | if (rc == 0) |
| 3815 | restart_ctx(&dir_fp->readdir_data.ctx); |
| 3816 | if (rc == -ENOSPC) |
| 3817 | rc = 0; |
| 3818 | if (rc) |
| 3819 | goto err_out; |
| 3820 | |
| 3821 | d_info.wptr = d_info.rptr; |
| 3822 | d_info.out_buf_len = buffer_sz; |
| 3823 | rc = process_query_dir_entries(&query_dir_private); |
| 3824 | if (rc) |
| 3825 | goto err_out; |
| 3826 | |
| 3827 | if (!d_info.data_count && d_info.out_buf_len >= 0) { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3828 | if (srch_flag & SMB2_RETURN_SINGLE_ENTRY && !is_asterisk(srch_ptr)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3829 | rsp->hdr.Status = STATUS_NO_SUCH_FILE; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3830 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3831 | dir_fp->dot_dotdot[0] = dir_fp->dot_dotdot[1] = 0; |
| 3832 | rsp->hdr.Status = STATUS_NO_MORE_FILES; |
| 3833 | } |
| 3834 | rsp->StructureSize = cpu_to_le16(9); |
| 3835 | rsp->OutputBufferOffset = cpu_to_le16(0); |
| 3836 | rsp->OutputBufferLength = cpu_to_le32(0); |
| 3837 | rsp->Buffer[0] = 0; |
| 3838 | inc_rfc1001_len(rsp_org, 9); |
| 3839 | } else { |
| 3840 | ((struct file_directory_info *) |
| 3841 | ((char *)rsp->Buffer + d_info.last_entry_offset)) |
| 3842 | ->NextEntryOffset = 0; |
| 3843 | |
| 3844 | rsp->StructureSize = cpu_to_le16(9); |
| 3845 | rsp->OutputBufferOffset = cpu_to_le16(72); |
| 3846 | rsp->OutputBufferLength = cpu_to_le32(d_info.data_count); |
| 3847 | inc_rfc1001_len(rsp_org, 8 + d_info.data_count); |
| 3848 | } |
| 3849 | |
| 3850 | kfree(srch_ptr); |
| 3851 | ksmbd_fd_put(work, dir_fp); |
| 3852 | ksmbd_revert_fsids(work); |
| 3853 | return 0; |
| 3854 | |
| 3855 | err_out: |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 3856 | pr_err("error while processing smb2 query dir rc = %d\n", rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3857 | kfree(srch_ptr); |
| 3858 | |
| 3859 | err_out2: |
| 3860 | if (rc == -EINVAL) |
| 3861 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 3862 | else if (rc == -EACCES) |
| 3863 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 3864 | else if (rc == -ENOENT) |
| 3865 | rsp->hdr.Status = STATUS_NO_SUCH_FILE; |
| 3866 | else if (rc == -EBADF) |
| 3867 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 3868 | else if (rc == -ENOMEM) |
| 3869 | rsp->hdr.Status = STATUS_NO_MEMORY; |
| 3870 | else if (rc == -EFAULT) |
| 3871 | rsp->hdr.Status = STATUS_INVALID_INFO_CLASS; |
| 3872 | if (!rsp->hdr.Status) |
| 3873 | rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR; |
| 3874 | |
| 3875 | smb2_set_err_rsp(work); |
| 3876 | ksmbd_fd_put(work, dir_fp); |
| 3877 | ksmbd_revert_fsids(work); |
| 3878 | return 0; |
| 3879 | } |
| 3880 | |
| 3881 | /** |
| 3882 | * buffer_check_err() - helper function to check buffer errors |
| 3883 | * @reqOutputBufferLength: max buffer length expected in command response |
| 3884 | * @rsp: query info response buffer contains output buffer length |
| 3885 | * @infoclass_size: query info class response buffer size |
| 3886 | * |
| 3887 | * Return: 0 on success, otherwise error |
| 3888 | */ |
| 3889 | static int buffer_check_err(int reqOutputBufferLength, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3890 | struct smb2_query_info_rsp *rsp, int infoclass_size) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3891 | { |
| 3892 | if (reqOutputBufferLength < le32_to_cpu(rsp->OutputBufferLength)) { |
| 3893 | if (reqOutputBufferLength < infoclass_size) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 3894 | pr_err("Invalid Buffer Size Requested\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3895 | rsp->hdr.Status = STATUS_INFO_LENGTH_MISMATCH; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3896 | rsp->hdr.smb2_buf_length = cpu_to_be32(sizeof(struct smb2_hdr) - 4); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3897 | return -EINVAL; |
| 3898 | } |
| 3899 | |
| 3900 | ksmbd_debug(SMB, "Buffer Overflow\n"); |
| 3901 | rsp->hdr.Status = STATUS_BUFFER_OVERFLOW; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3902 | rsp->hdr.smb2_buf_length = cpu_to_be32(sizeof(struct smb2_hdr) - 4 + |
| 3903 | reqOutputBufferLength); |
| 3904 | rsp->OutputBufferLength = cpu_to_le32(reqOutputBufferLength); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3905 | } |
| 3906 | return 0; |
| 3907 | } |
| 3908 | |
| 3909 | static void get_standard_info_pipe(struct smb2_query_info_rsp *rsp) |
| 3910 | { |
| 3911 | struct smb2_file_standard_info *sinfo; |
| 3912 | |
| 3913 | sinfo = (struct smb2_file_standard_info *)rsp->Buffer; |
| 3914 | |
| 3915 | sinfo->AllocationSize = cpu_to_le64(4096); |
| 3916 | sinfo->EndOfFile = cpu_to_le64(0); |
| 3917 | sinfo->NumberOfLinks = cpu_to_le32(1); |
| 3918 | sinfo->DeletePending = 1; |
| 3919 | sinfo->Directory = 0; |
| 3920 | rsp->OutputBufferLength = |
| 3921 | cpu_to_le32(sizeof(struct smb2_file_standard_info)); |
| 3922 | inc_rfc1001_len(rsp, sizeof(struct smb2_file_standard_info)); |
| 3923 | } |
| 3924 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3925 | static void get_internal_info_pipe(struct smb2_query_info_rsp *rsp, u64 num) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3926 | { |
| 3927 | struct smb2_file_internal_info *file_info; |
| 3928 | |
| 3929 | file_info = (struct smb2_file_internal_info *)rsp->Buffer; |
| 3930 | |
| 3931 | /* any unique number */ |
| 3932 | file_info->IndexNumber = cpu_to_le64(num | (1ULL << 63)); |
| 3933 | rsp->OutputBufferLength = |
| 3934 | cpu_to_le32(sizeof(struct smb2_file_internal_info)); |
| 3935 | inc_rfc1001_len(rsp, sizeof(struct smb2_file_internal_info)); |
| 3936 | } |
| 3937 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3938 | static int smb2_get_info_file_pipe(struct ksmbd_session *sess, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3939 | struct smb2_query_info_req *req, |
| 3940 | struct smb2_query_info_rsp *rsp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3941 | { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3942 | u64 id; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3943 | int rc; |
| 3944 | |
| 3945 | /* |
| 3946 | * Windows can sometime send query file info request on |
| 3947 | * pipe without opening it, checking error condition here |
| 3948 | */ |
| 3949 | id = le64_to_cpu(req->VolatileFileId); |
| 3950 | if (!ksmbd_session_rpc_method(sess, id)) |
| 3951 | return -ENOENT; |
| 3952 | |
| 3953 | ksmbd_debug(SMB, "FileInfoClass %u, FileId 0x%llx\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3954 | req->FileInfoClass, le64_to_cpu(req->VolatileFileId)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3955 | |
| 3956 | switch (req->FileInfoClass) { |
| 3957 | case FILE_STANDARD_INFORMATION: |
| 3958 | get_standard_info_pipe(rsp); |
| 3959 | rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength), |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3960 | rsp, FILE_STANDARD_INFORMATION_SIZE); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3961 | break; |
| 3962 | case FILE_INTERNAL_INFORMATION: |
| 3963 | get_internal_info_pipe(rsp, id); |
| 3964 | rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength), |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3965 | rsp, FILE_INTERNAL_INFORMATION_SIZE); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3966 | break; |
| 3967 | default: |
| 3968 | ksmbd_debug(SMB, "smb2_info_file_pipe for %u not supported\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3969 | req->FileInfoClass); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3970 | rc = -EOPNOTSUPP; |
| 3971 | } |
| 3972 | return rc; |
| 3973 | } |
| 3974 | |
| 3975 | /** |
| 3976 | * smb2_get_ea() - handler for smb2 get extended attribute command |
| 3977 | * @work: smb work containing query info command buffer |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 3978 | * @fp: ksmbd_file pointer |
| 3979 | * @req: get extended attribute request |
| 3980 | * @rsp: response buffer pointer |
| 3981 | * @rsp_org: base response buffer pointer in case of chained response |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3982 | * |
| 3983 | * Return: 0 on success, otherwise error |
| 3984 | */ |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3985 | 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] | 3986 | struct smb2_query_info_req *req, |
| 3987 | struct smb2_query_info_rsp *rsp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3988 | { |
| 3989 | struct smb2_ea_info *eainfo, *prev_eainfo; |
| 3990 | char *name, *ptr, *xattr_list = NULL, *buf; |
| 3991 | int rc, name_len, value_len, xattr_list_len, idx; |
| 3992 | ssize_t buf_free_len, alignment_bytes, next_offset, rsp_data_cnt = 0; |
| 3993 | struct smb2_ea_info_req *ea_req = NULL; |
| 3994 | struct path *path; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 3995 | struct user_namespace *user_ns = file_mnt_user_ns(fp->filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3996 | |
| 3997 | if (!(fp->daccess & FILE_READ_EA_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 3998 | pr_err("Not permitted to read ext attr : 0x%x\n", |
| 3999 | fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4000 | return -EACCES; |
| 4001 | } |
| 4002 | |
| 4003 | path = &fp->filp->f_path; |
| 4004 | /* single EA entry is requested with given user.* name */ |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4005 | if (req->InputBufferLength) { |
Namjae Jeon | 6d56262 | 2021-09-18 18:45:12 +0900 | [diff] [blame] | 4006 | if (le32_to_cpu(req->InputBufferLength) < |
| 4007 | sizeof(struct smb2_ea_info_req)) |
| 4008 | return -EINVAL; |
| 4009 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4010 | ea_req = (struct smb2_ea_info_req *)req->Buffer; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4011 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4012 | /* need to send all EAs, if no specific EA is requested*/ |
| 4013 | if (le32_to_cpu(req->Flags) & SL_RETURN_SINGLE_ENTRY) |
| 4014 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4015 | "All EAs are requested but need to send single EA entry in rsp flags 0x%x\n", |
| 4016 | le32_to_cpu(req->Flags)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4017 | } |
| 4018 | |
| 4019 | buf_free_len = work->response_sz - |
| 4020 | (get_rfc1002_len(rsp_org) + 4) - |
| 4021 | sizeof(struct smb2_query_info_rsp); |
| 4022 | |
| 4023 | if (le32_to_cpu(req->OutputBufferLength) < buf_free_len) |
| 4024 | buf_free_len = le32_to_cpu(req->OutputBufferLength); |
| 4025 | |
| 4026 | rc = ksmbd_vfs_listxattr(path->dentry, &xattr_list); |
| 4027 | if (rc < 0) { |
| 4028 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 4029 | goto out; |
| 4030 | } else if (!rc) { /* there is no EA in the file */ |
| 4031 | ksmbd_debug(SMB, "no ea data in the file\n"); |
| 4032 | goto done; |
| 4033 | } |
| 4034 | xattr_list_len = rc; |
| 4035 | |
| 4036 | ptr = (char *)rsp->Buffer; |
| 4037 | eainfo = (struct smb2_ea_info *)ptr; |
| 4038 | prev_eainfo = eainfo; |
| 4039 | idx = 0; |
| 4040 | |
| 4041 | while (idx < xattr_list_len) { |
| 4042 | name = xattr_list + idx; |
| 4043 | name_len = strlen(name); |
| 4044 | |
| 4045 | ksmbd_debug(SMB, "%s, len %d\n", name, name_len); |
| 4046 | idx += name_len + 1; |
| 4047 | |
| 4048 | /* |
| 4049 | * CIFS does not support EA other than user.* namespace, |
| 4050 | * still keep the framework generic, to list other attrs |
| 4051 | * in future. |
| 4052 | */ |
| 4053 | if (strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) |
| 4054 | continue; |
| 4055 | |
| 4056 | if (!strncmp(&name[XATTR_USER_PREFIX_LEN], STREAM_PREFIX, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4057 | STREAM_PREFIX_LEN)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4058 | continue; |
| 4059 | |
| 4060 | if (req->InputBufferLength && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4061 | strncmp(&name[XATTR_USER_PREFIX_LEN], ea_req->name, |
| 4062 | ea_req->EaNameLength)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4063 | continue; |
| 4064 | |
| 4065 | if (!strncmp(&name[XATTR_USER_PREFIX_LEN], |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4066 | DOS_ATTRIBUTE_PREFIX, DOS_ATTRIBUTE_PREFIX_LEN)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4067 | continue; |
| 4068 | |
| 4069 | if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) |
| 4070 | name_len -= XATTR_USER_PREFIX_LEN; |
| 4071 | |
| 4072 | ptr = (char *)(&eainfo->name + name_len + 1); |
| 4073 | buf_free_len -= (offsetof(struct smb2_ea_info, name) + |
| 4074 | name_len + 1); |
| 4075 | /* bailout if xattr can't fit in buf_free_len */ |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 4076 | value_len = ksmbd_vfs_getxattr(user_ns, path->dentry, |
| 4077 | name, &buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4078 | if (value_len <= 0) { |
| 4079 | rc = -ENOENT; |
| 4080 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 4081 | goto out; |
| 4082 | } |
| 4083 | |
| 4084 | buf_free_len -= value_len; |
| 4085 | if (buf_free_len < 0) { |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 4086 | kfree(buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4087 | break; |
| 4088 | } |
| 4089 | |
| 4090 | memcpy(ptr, buf, value_len); |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 4091 | kfree(buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4092 | |
| 4093 | ptr += value_len; |
| 4094 | eainfo->Flags = 0; |
| 4095 | eainfo->EaNameLength = name_len; |
| 4096 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4097 | if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4098 | memcpy(eainfo->name, &name[XATTR_USER_PREFIX_LEN], |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4099 | name_len); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4100 | else |
| 4101 | memcpy(eainfo->name, name, name_len); |
| 4102 | |
| 4103 | eainfo->name[name_len] = '\0'; |
| 4104 | eainfo->EaValueLength = cpu_to_le16(value_len); |
| 4105 | next_offset = offsetof(struct smb2_ea_info, name) + |
| 4106 | name_len + 1 + value_len; |
| 4107 | |
| 4108 | /* align next xattr entry at 4 byte bundary */ |
| 4109 | alignment_bytes = ((next_offset + 3) & ~3) - next_offset; |
| 4110 | if (alignment_bytes) { |
| 4111 | memset(ptr, '\0', alignment_bytes); |
| 4112 | ptr += alignment_bytes; |
| 4113 | next_offset += alignment_bytes; |
| 4114 | buf_free_len -= alignment_bytes; |
| 4115 | } |
| 4116 | eainfo->NextEntryOffset = cpu_to_le32(next_offset); |
| 4117 | prev_eainfo = eainfo; |
| 4118 | eainfo = (struct smb2_ea_info *)ptr; |
| 4119 | rsp_data_cnt += next_offset; |
| 4120 | |
| 4121 | if (req->InputBufferLength) { |
| 4122 | ksmbd_debug(SMB, "single entry requested\n"); |
| 4123 | break; |
| 4124 | } |
| 4125 | } |
| 4126 | |
| 4127 | /* no more ea entries */ |
| 4128 | prev_eainfo->NextEntryOffset = 0; |
| 4129 | done: |
| 4130 | rc = 0; |
| 4131 | if (rsp_data_cnt == 0) |
| 4132 | rsp->hdr.Status = STATUS_NO_EAS_ON_FILE; |
| 4133 | rsp->OutputBufferLength = cpu_to_le32(rsp_data_cnt); |
| 4134 | inc_rfc1001_len(rsp_org, rsp_data_cnt); |
| 4135 | out: |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 4136 | kvfree(xattr_list); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4137 | return rc; |
| 4138 | } |
| 4139 | |
| 4140 | static void get_file_access_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4141 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4142 | { |
| 4143 | struct smb2_file_access_info *file_info; |
| 4144 | |
| 4145 | file_info = (struct smb2_file_access_info *)rsp->Buffer; |
| 4146 | file_info->AccessFlags = fp->daccess; |
| 4147 | rsp->OutputBufferLength = |
| 4148 | cpu_to_le32(sizeof(struct smb2_file_access_info)); |
| 4149 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_access_info)); |
| 4150 | } |
| 4151 | |
| 4152 | static int get_file_basic_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4153 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4154 | { |
| 4155 | struct smb2_file_all_info *basic_info; |
| 4156 | struct kstat stat; |
| 4157 | u64 time; |
| 4158 | |
| 4159 | if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4160 | pr_err("no right to read the attributes : 0x%x\n", |
| 4161 | fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4162 | return -EACCES; |
| 4163 | } |
| 4164 | |
| 4165 | basic_info = (struct smb2_file_all_info *)rsp->Buffer; |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 4166 | generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp), |
| 4167 | &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4168 | basic_info->CreationTime = cpu_to_le64(fp->create_time); |
| 4169 | time = ksmbd_UnixTimeToNT(stat.atime); |
| 4170 | basic_info->LastAccessTime = cpu_to_le64(time); |
| 4171 | time = ksmbd_UnixTimeToNT(stat.mtime); |
| 4172 | basic_info->LastWriteTime = cpu_to_le64(time); |
| 4173 | time = ksmbd_UnixTimeToNT(stat.ctime); |
| 4174 | basic_info->ChangeTime = cpu_to_le64(time); |
| 4175 | basic_info->Attributes = fp->f_ci->m_fattr; |
| 4176 | basic_info->Pad1 = 0; |
| 4177 | rsp->OutputBufferLength = |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4178 | cpu_to_le32(offsetof(struct smb2_file_all_info, AllocationSize)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4179 | inc_rfc1001_len(rsp_org, offsetof(struct smb2_file_all_info, |
| 4180 | AllocationSize)); |
| 4181 | return 0; |
| 4182 | } |
| 4183 | |
| 4184 | static unsigned long long get_allocation_size(struct inode *inode, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4185 | struct kstat *stat) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4186 | { |
| 4187 | unsigned long long alloc_size = 0; |
| 4188 | |
| 4189 | if (!S_ISDIR(stat->mode)) { |
| 4190 | if ((inode->i_blocks << 9) <= stat->size) |
| 4191 | alloc_size = stat->size; |
| 4192 | else |
| 4193 | alloc_size = inode->i_blocks << 9; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4194 | } |
| 4195 | |
| 4196 | return alloc_size; |
| 4197 | } |
| 4198 | |
| 4199 | static void get_file_standard_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4200 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4201 | { |
| 4202 | struct smb2_file_standard_info *sinfo; |
| 4203 | unsigned int delete_pending; |
| 4204 | struct inode *inode; |
| 4205 | struct kstat stat; |
| 4206 | |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 4207 | inode = file_inode(fp->filp); |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 4208 | generic_fillattr(file_mnt_user_ns(fp->filp), inode, &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4209 | |
| 4210 | sinfo = (struct smb2_file_standard_info *)rsp->Buffer; |
| 4211 | delete_pending = ksmbd_inode_pending_delete(fp); |
| 4212 | |
| 4213 | sinfo->AllocationSize = cpu_to_le64(get_allocation_size(inode, &stat)); |
| 4214 | sinfo->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size); |
| 4215 | sinfo->NumberOfLinks = cpu_to_le32(get_nlink(&stat) - delete_pending); |
| 4216 | sinfo->DeletePending = delete_pending; |
| 4217 | sinfo->Directory = S_ISDIR(stat.mode) ? 1 : 0; |
| 4218 | rsp->OutputBufferLength = |
| 4219 | cpu_to_le32(sizeof(struct smb2_file_standard_info)); |
| 4220 | inc_rfc1001_len(rsp_org, |
| 4221 | sizeof(struct smb2_file_standard_info)); |
| 4222 | } |
| 4223 | |
| 4224 | static void get_file_alignment_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4225 | void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4226 | { |
| 4227 | struct smb2_file_alignment_info *file_info; |
| 4228 | |
| 4229 | file_info = (struct smb2_file_alignment_info *)rsp->Buffer; |
| 4230 | file_info->AlignmentRequirement = 0; |
| 4231 | rsp->OutputBufferLength = |
| 4232 | cpu_to_le32(sizeof(struct smb2_file_alignment_info)); |
| 4233 | inc_rfc1001_len(rsp_org, |
| 4234 | sizeof(struct smb2_file_alignment_info)); |
| 4235 | } |
| 4236 | |
| 4237 | static int get_file_all_info(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4238 | struct smb2_query_info_rsp *rsp, |
| 4239 | struct ksmbd_file *fp, |
| 4240 | void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4241 | { |
| 4242 | struct ksmbd_conn *conn = work->conn; |
| 4243 | struct smb2_file_all_info *file_info; |
| 4244 | unsigned int delete_pending; |
| 4245 | struct inode *inode; |
| 4246 | struct kstat stat; |
| 4247 | int conv_len; |
| 4248 | char *filename; |
| 4249 | u64 time; |
| 4250 | |
| 4251 | if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) { |
| 4252 | ksmbd_debug(SMB, "no right to read the attributes : 0x%x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4253 | fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4254 | return -EACCES; |
| 4255 | } |
| 4256 | |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame^] | 4257 | filename = convert_to_nt_pathname(fp->filename); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4258 | if (!filename) |
| 4259 | return -ENOMEM; |
| 4260 | |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 4261 | inode = file_inode(fp->filp); |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 4262 | generic_fillattr(file_mnt_user_ns(fp->filp), inode, &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4263 | |
| 4264 | ksmbd_debug(SMB, "filename = %s\n", filename); |
| 4265 | delete_pending = ksmbd_inode_pending_delete(fp); |
| 4266 | file_info = (struct smb2_file_all_info *)rsp->Buffer; |
| 4267 | |
| 4268 | file_info->CreationTime = cpu_to_le64(fp->create_time); |
| 4269 | time = ksmbd_UnixTimeToNT(stat.atime); |
| 4270 | file_info->LastAccessTime = cpu_to_le64(time); |
| 4271 | time = ksmbd_UnixTimeToNT(stat.mtime); |
| 4272 | file_info->LastWriteTime = cpu_to_le64(time); |
| 4273 | time = ksmbd_UnixTimeToNT(stat.ctime); |
| 4274 | file_info->ChangeTime = cpu_to_le64(time); |
| 4275 | file_info->Attributes = fp->f_ci->m_fattr; |
| 4276 | file_info->Pad1 = 0; |
| 4277 | file_info->AllocationSize = |
| 4278 | cpu_to_le64(get_allocation_size(inode, &stat)); |
| 4279 | file_info->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size); |
| 4280 | file_info->NumberOfLinks = |
| 4281 | cpu_to_le32(get_nlink(&stat) - delete_pending); |
| 4282 | file_info->DeletePending = delete_pending; |
| 4283 | file_info->Directory = S_ISDIR(stat.mode) ? 1 : 0; |
| 4284 | file_info->Pad2 = 0; |
| 4285 | file_info->IndexNumber = cpu_to_le64(stat.ino); |
| 4286 | file_info->EASize = 0; |
| 4287 | file_info->AccessFlags = fp->daccess; |
| 4288 | file_info->CurrentByteOffset = cpu_to_le64(fp->filp->f_pos); |
| 4289 | file_info->Mode = fp->coption; |
| 4290 | file_info->AlignmentRequirement = 0; |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4291 | conv_len = smbConvertToUTF16((__le16 *)file_info->FileName, filename, |
| 4292 | PATH_MAX, conn->local_nls, 0); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4293 | conv_len *= 2; |
| 4294 | file_info->FileNameLength = cpu_to_le32(conv_len); |
| 4295 | rsp->OutputBufferLength = |
| 4296 | cpu_to_le32(sizeof(struct smb2_file_all_info) + conv_len - 1); |
| 4297 | kfree(filename); |
| 4298 | inc_rfc1001_len(rsp_org, le32_to_cpu(rsp->OutputBufferLength)); |
| 4299 | return 0; |
| 4300 | } |
| 4301 | |
| 4302 | static void get_file_alternate_info(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4303 | struct smb2_query_info_rsp *rsp, |
| 4304 | struct ksmbd_file *fp, |
| 4305 | void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4306 | { |
| 4307 | struct ksmbd_conn *conn = work->conn; |
| 4308 | struct smb2_file_alt_name_info *file_info; |
Namjae Jeon | 493fa2f | 2021-06-29 09:22:16 +0900 | [diff] [blame] | 4309 | struct dentry *dentry = fp->filp->f_path.dentry; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4310 | int conv_len; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4311 | |
Namjae Jeon | 493fa2f | 2021-06-29 09:22:16 +0900 | [diff] [blame] | 4312 | spin_lock(&dentry->d_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4313 | file_info = (struct smb2_file_alt_name_info *)rsp->Buffer; |
| 4314 | conv_len = ksmbd_extract_shortname(conn, |
Namjae Jeon | 493fa2f | 2021-06-29 09:22:16 +0900 | [diff] [blame] | 4315 | dentry->d_name.name, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4316 | file_info->FileName); |
Namjae Jeon | 493fa2f | 2021-06-29 09:22:16 +0900 | [diff] [blame] | 4317 | spin_unlock(&dentry->d_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4318 | file_info->FileNameLength = cpu_to_le32(conv_len); |
| 4319 | rsp->OutputBufferLength = |
| 4320 | cpu_to_le32(sizeof(struct smb2_file_alt_name_info) + conv_len); |
| 4321 | inc_rfc1001_len(rsp_org, le32_to_cpu(rsp->OutputBufferLength)); |
| 4322 | } |
| 4323 | |
| 4324 | static void get_file_stream_info(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4325 | struct smb2_query_info_rsp *rsp, |
| 4326 | struct ksmbd_file *fp, |
| 4327 | void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4328 | { |
| 4329 | struct ksmbd_conn *conn = work->conn; |
| 4330 | struct smb2_file_stream_info *file_info; |
| 4331 | char *stream_name, *xattr_list = NULL, *stream_buf; |
| 4332 | struct kstat stat; |
| 4333 | struct path *path = &fp->filp->f_path; |
| 4334 | ssize_t xattr_list_len; |
| 4335 | int nbytes = 0, streamlen, stream_name_len, next, idx = 0; |
| 4336 | |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 4337 | generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp), |
| 4338 | &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4339 | file_info = (struct smb2_file_stream_info *)rsp->Buffer; |
| 4340 | |
| 4341 | xattr_list_len = ksmbd_vfs_listxattr(path->dentry, &xattr_list); |
| 4342 | if (xattr_list_len < 0) { |
| 4343 | goto out; |
| 4344 | } else if (!xattr_list_len) { |
| 4345 | ksmbd_debug(SMB, "empty xattr in the file\n"); |
| 4346 | goto out; |
| 4347 | } |
| 4348 | |
| 4349 | while (idx < xattr_list_len) { |
| 4350 | stream_name = xattr_list + idx; |
| 4351 | streamlen = strlen(stream_name); |
| 4352 | idx += streamlen + 1; |
| 4353 | |
| 4354 | ksmbd_debug(SMB, "%s, len %d\n", stream_name, streamlen); |
| 4355 | |
| 4356 | if (strncmp(&stream_name[XATTR_USER_PREFIX_LEN], |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4357 | STREAM_PREFIX, STREAM_PREFIX_LEN)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4358 | continue; |
| 4359 | |
| 4360 | stream_name_len = streamlen - (XATTR_USER_PREFIX_LEN + |
| 4361 | STREAM_PREFIX_LEN); |
| 4362 | streamlen = stream_name_len; |
| 4363 | |
| 4364 | /* plus : size */ |
| 4365 | streamlen += 1; |
| 4366 | stream_buf = kmalloc(streamlen + 1, GFP_KERNEL); |
| 4367 | if (!stream_buf) |
| 4368 | break; |
| 4369 | |
| 4370 | streamlen = snprintf(stream_buf, streamlen + 1, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4371 | ":%s", &stream_name[XATTR_NAME_STREAM_LEN]); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4372 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4373 | file_info = (struct smb2_file_stream_info *)&rsp->Buffer[nbytes]; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4374 | streamlen = smbConvertToUTF16((__le16 *)file_info->StreamName, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4375 | stream_buf, streamlen, |
| 4376 | conn->local_nls, 0); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4377 | streamlen *= 2; |
| 4378 | kfree(stream_buf); |
| 4379 | file_info->StreamNameLength = cpu_to_le32(streamlen); |
| 4380 | file_info->StreamSize = cpu_to_le64(stream_name_len); |
| 4381 | file_info->StreamAllocationSize = cpu_to_le64(stream_name_len); |
| 4382 | |
| 4383 | next = sizeof(struct smb2_file_stream_info) + streamlen; |
| 4384 | nbytes += next; |
| 4385 | file_info->NextEntryOffset = cpu_to_le32(next); |
| 4386 | } |
| 4387 | |
Namjae Jeon | 9f632331 | 2021-09-18 21:02:39 +0900 | [diff] [blame] | 4388 | if (!S_ISDIR(stat.mode)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4389 | file_info = (struct smb2_file_stream_info *) |
| 4390 | &rsp->Buffer[nbytes]; |
| 4391 | streamlen = smbConvertToUTF16((__le16 *)file_info->StreamName, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4392 | "::$DATA", 7, conn->local_nls, 0); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4393 | streamlen *= 2; |
| 4394 | file_info->StreamNameLength = cpu_to_le32(streamlen); |
Namjae Jeon | 9f632331 | 2021-09-18 21:02:39 +0900 | [diff] [blame] | 4395 | file_info->StreamSize = 0; |
| 4396 | file_info->StreamAllocationSize = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4397 | nbytes += sizeof(struct smb2_file_stream_info) + streamlen; |
| 4398 | } |
| 4399 | |
| 4400 | /* last entry offset should be 0 */ |
| 4401 | file_info->NextEntryOffset = 0; |
| 4402 | out: |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 4403 | kvfree(xattr_list); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4404 | |
| 4405 | rsp->OutputBufferLength = cpu_to_le32(nbytes); |
| 4406 | inc_rfc1001_len(rsp_org, nbytes); |
| 4407 | } |
| 4408 | |
| 4409 | static void get_file_internal_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4410 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4411 | { |
| 4412 | struct smb2_file_internal_info *file_info; |
| 4413 | struct kstat stat; |
| 4414 | |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 4415 | generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp), |
| 4416 | &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4417 | file_info = (struct smb2_file_internal_info *)rsp->Buffer; |
| 4418 | file_info->IndexNumber = cpu_to_le64(stat.ino); |
| 4419 | rsp->OutputBufferLength = |
| 4420 | cpu_to_le32(sizeof(struct smb2_file_internal_info)); |
| 4421 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_internal_info)); |
| 4422 | } |
| 4423 | |
| 4424 | 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] | 4425 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4426 | { |
| 4427 | struct smb2_file_ntwrk_info *file_info; |
| 4428 | struct inode *inode; |
| 4429 | struct kstat stat; |
| 4430 | u64 time; |
| 4431 | |
| 4432 | if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4433 | pr_err("no right to read the attributes : 0x%x\n", |
| 4434 | fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4435 | return -EACCES; |
| 4436 | } |
| 4437 | |
| 4438 | file_info = (struct smb2_file_ntwrk_info *)rsp->Buffer; |
| 4439 | |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 4440 | inode = file_inode(fp->filp); |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 4441 | generic_fillattr(file_mnt_user_ns(fp->filp), inode, &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4442 | |
| 4443 | file_info->CreationTime = cpu_to_le64(fp->create_time); |
| 4444 | time = ksmbd_UnixTimeToNT(stat.atime); |
| 4445 | file_info->LastAccessTime = cpu_to_le64(time); |
| 4446 | time = ksmbd_UnixTimeToNT(stat.mtime); |
| 4447 | file_info->LastWriteTime = cpu_to_le64(time); |
| 4448 | time = ksmbd_UnixTimeToNT(stat.ctime); |
| 4449 | file_info->ChangeTime = cpu_to_le64(time); |
| 4450 | file_info->Attributes = fp->f_ci->m_fattr; |
| 4451 | file_info->AllocationSize = |
| 4452 | cpu_to_le64(get_allocation_size(inode, &stat)); |
| 4453 | file_info->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size); |
| 4454 | file_info->Reserved = cpu_to_le32(0); |
| 4455 | rsp->OutputBufferLength = |
| 4456 | cpu_to_le32(sizeof(struct smb2_file_ntwrk_info)); |
| 4457 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_ntwrk_info)); |
| 4458 | return 0; |
| 4459 | } |
| 4460 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4461 | 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] | 4462 | { |
| 4463 | struct smb2_file_ea_info *file_info; |
| 4464 | |
| 4465 | file_info = (struct smb2_file_ea_info *)rsp->Buffer; |
| 4466 | file_info->EASize = 0; |
| 4467 | rsp->OutputBufferLength = |
| 4468 | cpu_to_le32(sizeof(struct smb2_file_ea_info)); |
| 4469 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_ea_info)); |
| 4470 | } |
| 4471 | |
| 4472 | static void get_file_position_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4473 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4474 | { |
| 4475 | struct smb2_file_pos_info *file_info; |
| 4476 | |
| 4477 | file_info = (struct smb2_file_pos_info *)rsp->Buffer; |
| 4478 | file_info->CurrentByteOffset = cpu_to_le64(fp->filp->f_pos); |
| 4479 | rsp->OutputBufferLength = |
| 4480 | cpu_to_le32(sizeof(struct smb2_file_pos_info)); |
| 4481 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_pos_info)); |
| 4482 | } |
| 4483 | |
| 4484 | static void get_file_mode_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4485 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4486 | { |
| 4487 | struct smb2_file_mode_info *file_info; |
| 4488 | |
| 4489 | file_info = (struct smb2_file_mode_info *)rsp->Buffer; |
| 4490 | file_info->Mode = fp->coption & FILE_MODE_INFO_MASK; |
| 4491 | rsp->OutputBufferLength = |
| 4492 | cpu_to_le32(sizeof(struct smb2_file_mode_info)); |
| 4493 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_mode_info)); |
| 4494 | } |
| 4495 | |
| 4496 | static void get_file_compression_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4497 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4498 | { |
| 4499 | struct smb2_file_comp_info *file_info; |
| 4500 | struct kstat stat; |
| 4501 | |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 4502 | generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp), |
| 4503 | &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4504 | |
| 4505 | file_info = (struct smb2_file_comp_info *)rsp->Buffer; |
| 4506 | file_info->CompressedFileSize = cpu_to_le64(stat.blocks << 9); |
| 4507 | file_info->CompressionFormat = COMPRESSION_FORMAT_NONE; |
| 4508 | file_info->CompressionUnitShift = 0; |
| 4509 | file_info->ChunkShift = 0; |
| 4510 | file_info->ClusterShift = 0; |
| 4511 | memset(&file_info->Reserved[0], 0, 3); |
| 4512 | |
| 4513 | rsp->OutputBufferLength = |
| 4514 | cpu_to_le32(sizeof(struct smb2_file_comp_info)); |
| 4515 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_comp_info)); |
| 4516 | } |
| 4517 | |
| 4518 | 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] | 4519 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4520 | { |
| 4521 | struct smb2_file_attr_tag_info *file_info; |
| 4522 | |
| 4523 | if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4524 | pr_err("no right to read the attributes : 0x%x\n", |
| 4525 | fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4526 | return -EACCES; |
| 4527 | } |
| 4528 | |
| 4529 | file_info = (struct smb2_file_attr_tag_info *)rsp->Buffer; |
| 4530 | file_info->FileAttributes = fp->f_ci->m_fattr; |
| 4531 | file_info->ReparseTag = 0; |
| 4532 | rsp->OutputBufferLength = |
| 4533 | cpu_to_le32(sizeof(struct smb2_file_attr_tag_info)); |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4534 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_attr_tag_info)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4535 | return 0; |
| 4536 | } |
| 4537 | |
| 4538 | static int find_file_posix_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4539 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4540 | { |
| 4541 | struct smb311_posix_qinfo *file_info; |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 4542 | struct inode *inode = file_inode(fp->filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4543 | u64 time; |
| 4544 | |
| 4545 | file_info = (struct smb311_posix_qinfo *)rsp->Buffer; |
| 4546 | file_info->CreationTime = cpu_to_le64(fp->create_time); |
| 4547 | time = ksmbd_UnixTimeToNT(inode->i_atime); |
| 4548 | file_info->LastAccessTime = cpu_to_le64(time); |
| 4549 | time = ksmbd_UnixTimeToNT(inode->i_mtime); |
| 4550 | file_info->LastWriteTime = cpu_to_le64(time); |
| 4551 | time = ksmbd_UnixTimeToNT(inode->i_ctime); |
| 4552 | file_info->ChangeTime = cpu_to_le64(time); |
| 4553 | file_info->DosAttributes = fp->f_ci->m_fattr; |
| 4554 | file_info->Inode = cpu_to_le64(inode->i_ino); |
| 4555 | file_info->EndOfFile = cpu_to_le64(inode->i_size); |
| 4556 | file_info->AllocationSize = cpu_to_le64(inode->i_blocks << 9); |
| 4557 | file_info->HardLinks = cpu_to_le32(inode->i_nlink); |
| 4558 | file_info->Mode = cpu_to_le32(inode->i_mode); |
| 4559 | file_info->DeviceId = cpu_to_le32(inode->i_rdev); |
| 4560 | rsp->OutputBufferLength = |
| 4561 | cpu_to_le32(sizeof(struct smb311_posix_qinfo)); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4562 | inc_rfc1001_len(rsp_org, sizeof(struct smb311_posix_qinfo)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4563 | return 0; |
| 4564 | } |
| 4565 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4566 | static int smb2_get_info_file(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4567 | struct smb2_query_info_req *req, |
| 4568 | struct smb2_query_info_rsp *rsp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4569 | { |
| 4570 | struct ksmbd_file *fp; |
| 4571 | int fileinfoclass = 0; |
| 4572 | int rc = 0; |
| 4573 | int file_infoclass_size; |
| 4574 | unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID; |
| 4575 | |
| 4576 | if (test_share_config_flag(work->tcon->share_conf, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4577 | KSMBD_SHARE_FLAG_PIPE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4578 | /* smb2 info file called for pipe */ |
| 4579 | return smb2_get_info_file_pipe(work->sess, req, rsp); |
| 4580 | } |
| 4581 | |
| 4582 | if (work->next_smb2_rcv_hdr_off) { |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 4583 | if (!has_file_id(le64_to_cpu(req->VolatileFileId))) { |
| 4584 | ksmbd_debug(SMB, "Compound request set FID = %llu\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4585 | work->compound_fid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4586 | id = work->compound_fid; |
| 4587 | pid = work->compound_pfid; |
| 4588 | } |
| 4589 | } |
| 4590 | |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 4591 | if (!has_file_id(id)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4592 | id = le64_to_cpu(req->VolatileFileId); |
| 4593 | pid = le64_to_cpu(req->PersistentFileId); |
| 4594 | } |
| 4595 | |
| 4596 | fp = ksmbd_lookup_fd_slow(work, id, pid); |
| 4597 | if (!fp) |
| 4598 | return -ENOENT; |
| 4599 | |
| 4600 | fileinfoclass = req->FileInfoClass; |
| 4601 | |
| 4602 | switch (fileinfoclass) { |
| 4603 | case FILE_ACCESS_INFORMATION: |
| 4604 | get_file_access_info(rsp, fp, rsp_org); |
| 4605 | file_infoclass_size = FILE_ACCESS_INFORMATION_SIZE; |
| 4606 | break; |
| 4607 | |
| 4608 | case FILE_BASIC_INFORMATION: |
| 4609 | rc = get_file_basic_info(rsp, fp, rsp_org); |
| 4610 | file_infoclass_size = FILE_BASIC_INFORMATION_SIZE; |
| 4611 | break; |
| 4612 | |
| 4613 | case FILE_STANDARD_INFORMATION: |
| 4614 | get_file_standard_info(rsp, fp, rsp_org); |
| 4615 | file_infoclass_size = FILE_STANDARD_INFORMATION_SIZE; |
| 4616 | break; |
| 4617 | |
| 4618 | case FILE_ALIGNMENT_INFORMATION: |
| 4619 | get_file_alignment_info(rsp, rsp_org); |
| 4620 | file_infoclass_size = FILE_ALIGNMENT_INFORMATION_SIZE; |
| 4621 | break; |
| 4622 | |
| 4623 | case FILE_ALL_INFORMATION: |
| 4624 | rc = get_file_all_info(work, rsp, fp, rsp_org); |
| 4625 | file_infoclass_size = FILE_ALL_INFORMATION_SIZE; |
| 4626 | break; |
| 4627 | |
| 4628 | case FILE_ALTERNATE_NAME_INFORMATION: |
| 4629 | get_file_alternate_info(work, rsp, fp, rsp_org); |
| 4630 | file_infoclass_size = FILE_ALTERNATE_NAME_INFORMATION_SIZE; |
| 4631 | break; |
| 4632 | |
| 4633 | case FILE_STREAM_INFORMATION: |
| 4634 | get_file_stream_info(work, rsp, fp, rsp_org); |
| 4635 | file_infoclass_size = FILE_STREAM_INFORMATION_SIZE; |
| 4636 | break; |
| 4637 | |
| 4638 | case FILE_INTERNAL_INFORMATION: |
| 4639 | get_file_internal_info(rsp, fp, rsp_org); |
| 4640 | file_infoclass_size = FILE_INTERNAL_INFORMATION_SIZE; |
| 4641 | break; |
| 4642 | |
| 4643 | case FILE_NETWORK_OPEN_INFORMATION: |
| 4644 | rc = get_file_network_open_info(rsp, fp, rsp_org); |
| 4645 | file_infoclass_size = FILE_NETWORK_OPEN_INFORMATION_SIZE; |
| 4646 | break; |
| 4647 | |
| 4648 | case FILE_EA_INFORMATION: |
| 4649 | get_file_ea_info(rsp, rsp_org); |
| 4650 | file_infoclass_size = FILE_EA_INFORMATION_SIZE; |
| 4651 | break; |
| 4652 | |
| 4653 | case FILE_FULL_EA_INFORMATION: |
| 4654 | rc = smb2_get_ea(work, fp, req, rsp, rsp_org); |
| 4655 | file_infoclass_size = FILE_FULL_EA_INFORMATION_SIZE; |
| 4656 | break; |
| 4657 | |
| 4658 | case FILE_POSITION_INFORMATION: |
| 4659 | get_file_position_info(rsp, fp, rsp_org); |
| 4660 | file_infoclass_size = FILE_POSITION_INFORMATION_SIZE; |
| 4661 | break; |
| 4662 | |
| 4663 | case FILE_MODE_INFORMATION: |
| 4664 | get_file_mode_info(rsp, fp, rsp_org); |
| 4665 | file_infoclass_size = FILE_MODE_INFORMATION_SIZE; |
| 4666 | break; |
| 4667 | |
| 4668 | case FILE_COMPRESSION_INFORMATION: |
| 4669 | get_file_compression_info(rsp, fp, rsp_org); |
| 4670 | file_infoclass_size = FILE_COMPRESSION_INFORMATION_SIZE; |
| 4671 | break; |
| 4672 | |
| 4673 | case FILE_ATTRIBUTE_TAG_INFORMATION: |
| 4674 | rc = get_file_attribute_tag_info(rsp, fp, rsp_org); |
| 4675 | file_infoclass_size = FILE_ATTRIBUTE_TAG_INFORMATION_SIZE; |
| 4676 | break; |
| 4677 | case SMB_FIND_FILE_POSIX_INFO: |
| 4678 | if (!work->tcon->posix_extensions) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4679 | 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] | 4680 | rc = -EOPNOTSUPP; |
| 4681 | } else { |
| 4682 | rc = find_file_posix_info(rsp, fp, rsp_org); |
| 4683 | file_infoclass_size = sizeof(struct smb311_posix_qinfo); |
| 4684 | } |
| 4685 | break; |
| 4686 | default: |
| 4687 | ksmbd_debug(SMB, "fileinfoclass %d not supported yet\n", |
| 4688 | fileinfoclass); |
| 4689 | rc = -EOPNOTSUPP; |
| 4690 | } |
| 4691 | if (!rc) |
| 4692 | rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength), |
| 4693 | rsp, |
| 4694 | file_infoclass_size); |
| 4695 | ksmbd_fd_put(work, fp); |
| 4696 | return rc; |
| 4697 | } |
| 4698 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4699 | static int smb2_get_info_filesystem(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4700 | struct smb2_query_info_req *req, |
| 4701 | struct smb2_query_info_rsp *rsp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4702 | { |
| 4703 | struct ksmbd_session *sess = work->sess; |
| 4704 | struct ksmbd_conn *conn = sess->conn; |
| 4705 | struct ksmbd_share_config *share = work->tcon->share_conf; |
| 4706 | int fsinfoclass = 0; |
| 4707 | struct kstatfs stfs; |
| 4708 | struct path path; |
| 4709 | int rc = 0, len; |
| 4710 | int fs_infoclass_size = 0; |
| 4711 | |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame^] | 4712 | rc = kern_path(share->path, LOOKUP_NO_SYMLINKS, &path); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4713 | if (rc) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4714 | pr_err("cannot create vfs path\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4715 | return -EIO; |
| 4716 | } |
| 4717 | |
| 4718 | rc = vfs_statfs(&path, &stfs); |
| 4719 | if (rc) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4720 | pr_err("cannot do stat of path %s\n", share->path); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4721 | path_put(&path); |
| 4722 | return -EIO; |
| 4723 | } |
| 4724 | |
| 4725 | fsinfoclass = req->FileInfoClass; |
| 4726 | |
| 4727 | switch (fsinfoclass) { |
| 4728 | case FS_DEVICE_INFORMATION: |
| 4729 | { |
| 4730 | struct filesystem_device_info *info; |
| 4731 | |
| 4732 | info = (struct filesystem_device_info *)rsp->Buffer; |
| 4733 | |
| 4734 | info->DeviceType = cpu_to_le32(stfs.f_type); |
| 4735 | info->DeviceCharacteristics = cpu_to_le32(0x00000020); |
| 4736 | rsp->OutputBufferLength = cpu_to_le32(8); |
| 4737 | inc_rfc1001_len(rsp_org, 8); |
| 4738 | fs_infoclass_size = FS_DEVICE_INFORMATION_SIZE; |
| 4739 | break; |
| 4740 | } |
| 4741 | case FS_ATTRIBUTE_INFORMATION: |
| 4742 | { |
| 4743 | struct filesystem_attribute_info *info; |
| 4744 | size_t sz; |
| 4745 | |
| 4746 | info = (struct filesystem_attribute_info *)rsp->Buffer; |
| 4747 | info->Attributes = cpu_to_le32(FILE_SUPPORTS_OBJECT_IDS | |
| 4748 | FILE_PERSISTENT_ACLS | |
| 4749 | FILE_UNICODE_ON_DISK | |
| 4750 | FILE_CASE_PRESERVED_NAMES | |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 4751 | FILE_CASE_SENSITIVE_SEARCH | |
| 4752 | FILE_SUPPORTS_BLOCK_REFCOUNTING); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4753 | |
| 4754 | info->Attributes |= cpu_to_le32(server_conf.share_fake_fscaps); |
| 4755 | |
| 4756 | info->MaxPathNameComponentLength = cpu_to_le32(stfs.f_namelen); |
| 4757 | len = smbConvertToUTF16((__le16 *)info->FileSystemName, |
| 4758 | "NTFS", PATH_MAX, conn->local_nls, 0); |
| 4759 | len = len * 2; |
| 4760 | info->FileSystemNameLen = cpu_to_le32(len); |
| 4761 | sz = sizeof(struct filesystem_attribute_info) - 2 + len; |
| 4762 | rsp->OutputBufferLength = cpu_to_le32(sz); |
| 4763 | inc_rfc1001_len(rsp_org, sz); |
| 4764 | fs_infoclass_size = FS_ATTRIBUTE_INFORMATION_SIZE; |
| 4765 | break; |
| 4766 | } |
| 4767 | case FS_VOLUME_INFORMATION: |
| 4768 | { |
| 4769 | struct filesystem_vol_info *info; |
| 4770 | size_t sz; |
| 4771 | |
| 4772 | info = (struct filesystem_vol_info *)(rsp->Buffer); |
| 4773 | info->VolumeCreationTime = 0; |
| 4774 | /* Taking dummy value of serial number*/ |
| 4775 | info->SerialNumber = cpu_to_le32(0xbc3ac512); |
| 4776 | len = smbConvertToUTF16((__le16 *)info->VolumeLabel, |
| 4777 | share->name, PATH_MAX, |
| 4778 | conn->local_nls, 0); |
| 4779 | len = len * 2; |
| 4780 | info->VolumeLabelSize = cpu_to_le32(len); |
| 4781 | info->Reserved = 0; |
| 4782 | sz = sizeof(struct filesystem_vol_info) - 2 + len; |
| 4783 | rsp->OutputBufferLength = cpu_to_le32(sz); |
| 4784 | inc_rfc1001_len(rsp_org, sz); |
| 4785 | fs_infoclass_size = FS_VOLUME_INFORMATION_SIZE; |
| 4786 | break; |
| 4787 | } |
| 4788 | case FS_SIZE_INFORMATION: |
| 4789 | { |
| 4790 | struct filesystem_info *info; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4791 | |
| 4792 | info = (struct filesystem_info *)(rsp->Buffer); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4793 | info->TotalAllocationUnits = cpu_to_le64(stfs.f_blocks); |
| 4794 | info->FreeAllocationUnits = cpu_to_le64(stfs.f_bfree); |
Namjae Jeon | ee81cae | 2021-06-26 22:32:34 +0900 | [diff] [blame] | 4795 | info->SectorsPerAllocationUnit = cpu_to_le32(1); |
| 4796 | info->BytesPerSector = cpu_to_le32(stfs.f_bsize); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4797 | rsp->OutputBufferLength = cpu_to_le32(24); |
| 4798 | inc_rfc1001_len(rsp_org, 24); |
| 4799 | fs_infoclass_size = FS_SIZE_INFORMATION_SIZE; |
| 4800 | break; |
| 4801 | } |
| 4802 | case FS_FULL_SIZE_INFORMATION: |
| 4803 | { |
| 4804 | struct smb2_fs_full_size_info *info; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4805 | |
| 4806 | info = (struct smb2_fs_full_size_info *)(rsp->Buffer); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4807 | info->TotalAllocationUnits = cpu_to_le64(stfs.f_blocks); |
| 4808 | info->CallerAvailableAllocationUnits = |
| 4809 | cpu_to_le64(stfs.f_bavail); |
| 4810 | info->ActualAvailableAllocationUnits = |
| 4811 | cpu_to_le64(stfs.f_bfree); |
Namjae Jeon | ee81cae | 2021-06-26 22:32:34 +0900 | [diff] [blame] | 4812 | info->SectorsPerAllocationUnit = cpu_to_le32(1); |
| 4813 | info->BytesPerSector = cpu_to_le32(stfs.f_bsize); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4814 | rsp->OutputBufferLength = cpu_to_le32(32); |
| 4815 | inc_rfc1001_len(rsp_org, 32); |
| 4816 | fs_infoclass_size = FS_FULL_SIZE_INFORMATION_SIZE; |
| 4817 | break; |
| 4818 | } |
| 4819 | case FS_OBJECT_ID_INFORMATION: |
| 4820 | { |
| 4821 | struct object_id_info *info; |
| 4822 | |
| 4823 | info = (struct object_id_info *)(rsp->Buffer); |
| 4824 | |
| 4825 | if (!user_guest(sess->user)) |
| 4826 | memcpy(info->objid, user_passkey(sess->user), 16); |
| 4827 | else |
| 4828 | memset(info->objid, 0, 16); |
| 4829 | |
| 4830 | info->extended_info.magic = cpu_to_le32(EXTENDED_INFO_MAGIC); |
| 4831 | info->extended_info.version = cpu_to_le32(1); |
| 4832 | info->extended_info.release = cpu_to_le32(1); |
| 4833 | info->extended_info.rel_date = 0; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4834 | 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] | 4835 | rsp->OutputBufferLength = cpu_to_le32(64); |
| 4836 | inc_rfc1001_len(rsp_org, 64); |
| 4837 | fs_infoclass_size = FS_OBJECT_ID_INFORMATION_SIZE; |
| 4838 | break; |
| 4839 | } |
| 4840 | case FS_SECTOR_SIZE_INFORMATION: |
| 4841 | { |
| 4842 | struct smb3_fs_ss_info *info; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4843 | |
| 4844 | info = (struct smb3_fs_ss_info *)(rsp->Buffer); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4845 | |
Namjae Jeon | 131bac1 | 2021-06-22 16:20:47 +0900 | [diff] [blame] | 4846 | info->LogicalBytesPerSector = cpu_to_le32(stfs.f_bsize); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4847 | info->PhysicalBytesPerSectorForAtomicity = |
Namjae Jeon | 131bac1 | 2021-06-22 16:20:47 +0900 | [diff] [blame] | 4848 | cpu_to_le32(stfs.f_bsize); |
| 4849 | info->PhysicalBytesPerSectorForPerf = cpu_to_le32(stfs.f_bsize); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4850 | info->FSEffPhysicalBytesPerSectorForAtomicity = |
Namjae Jeon | 131bac1 | 2021-06-22 16:20:47 +0900 | [diff] [blame] | 4851 | cpu_to_le32(stfs.f_bsize); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4852 | info->Flags = cpu_to_le32(SSINFO_FLAGS_ALIGNED_DEVICE | |
| 4853 | SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE); |
| 4854 | info->ByteOffsetForSectorAlignment = 0; |
| 4855 | info->ByteOffsetForPartitionAlignment = 0; |
| 4856 | rsp->OutputBufferLength = cpu_to_le32(28); |
| 4857 | inc_rfc1001_len(rsp_org, 28); |
| 4858 | fs_infoclass_size = FS_SECTOR_SIZE_INFORMATION_SIZE; |
| 4859 | break; |
| 4860 | } |
| 4861 | case FS_CONTROL_INFORMATION: |
| 4862 | { |
| 4863 | /* |
| 4864 | * TODO : The current implementation is based on |
| 4865 | * test result with win7(NTFS) server. It's need to |
| 4866 | * modify this to get valid Quota values |
| 4867 | * from Linux kernel |
| 4868 | */ |
| 4869 | struct smb2_fs_control_info *info; |
| 4870 | |
| 4871 | info = (struct smb2_fs_control_info *)(rsp->Buffer); |
| 4872 | info->FreeSpaceStartFiltering = 0; |
| 4873 | info->FreeSpaceThreshold = 0; |
| 4874 | info->FreeSpaceStopFiltering = 0; |
| 4875 | info->DefaultQuotaThreshold = cpu_to_le64(SMB2_NO_FID); |
| 4876 | info->DefaultQuotaLimit = cpu_to_le64(SMB2_NO_FID); |
| 4877 | info->Padding = 0; |
| 4878 | rsp->OutputBufferLength = cpu_to_le32(48); |
| 4879 | inc_rfc1001_len(rsp_org, 48); |
| 4880 | fs_infoclass_size = FS_CONTROL_INFORMATION_SIZE; |
| 4881 | break; |
| 4882 | } |
| 4883 | case FS_POSIX_INFORMATION: |
| 4884 | { |
| 4885 | struct filesystem_posix_info *info; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4886 | |
| 4887 | if (!work->tcon->posix_extensions) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4888 | 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] | 4889 | rc = -EOPNOTSUPP; |
| 4890 | } else { |
| 4891 | info = (struct filesystem_posix_info *)(rsp->Buffer); |
Namjae Jeon | ee81cae | 2021-06-26 22:32:34 +0900 | [diff] [blame] | 4892 | info->OptimalTransferSize = cpu_to_le32(stfs.f_bsize); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4893 | info->BlockSize = cpu_to_le32(stfs.f_bsize); |
| 4894 | info->TotalBlocks = cpu_to_le64(stfs.f_blocks); |
| 4895 | info->BlocksAvail = cpu_to_le64(stfs.f_bfree); |
| 4896 | info->UserBlocksAvail = cpu_to_le64(stfs.f_bavail); |
| 4897 | info->TotalFileNodes = cpu_to_le64(stfs.f_files); |
| 4898 | info->FreeFileNodes = cpu_to_le64(stfs.f_ffree); |
| 4899 | rsp->OutputBufferLength = cpu_to_le32(56); |
| 4900 | inc_rfc1001_len(rsp_org, 56); |
| 4901 | fs_infoclass_size = FS_POSIX_INFORMATION_SIZE; |
| 4902 | } |
| 4903 | break; |
| 4904 | } |
| 4905 | default: |
| 4906 | path_put(&path); |
| 4907 | return -EOPNOTSUPP; |
| 4908 | } |
| 4909 | rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength), |
| 4910 | rsp, |
| 4911 | fs_infoclass_size); |
| 4912 | path_put(&path); |
| 4913 | return rc; |
| 4914 | } |
| 4915 | |
| 4916 | static int smb2_get_info_sec(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4917 | struct smb2_query_info_req *req, |
| 4918 | struct smb2_query_info_rsp *rsp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4919 | { |
| 4920 | struct ksmbd_file *fp; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 4921 | struct user_namespace *user_ns; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4922 | struct smb_ntsd *pntsd = (struct smb_ntsd *)rsp->Buffer, *ppntsd = NULL; |
| 4923 | struct smb_fattr fattr = {{0}}; |
| 4924 | struct inode *inode; |
| 4925 | __u32 secdesclen; |
| 4926 | unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID; |
| 4927 | int addition_info = le32_to_cpu(req->AdditionalInformation); |
| 4928 | int rc; |
| 4929 | |
Namjae Jeon | e294f78 | 2021-06-28 15:26:37 +0900 | [diff] [blame] | 4930 | if (addition_info & ~(OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO | |
| 4931 | PROTECTED_DACL_SECINFO | |
| 4932 | UNPROTECTED_DACL_SECINFO)) { |
| 4933 | pr_err("Unsupported addition info: 0x%x)\n", |
| 4934 | addition_info); |
Sebastian Gottschall | ced2b26 | 2021-04-27 15:33:54 +0900 | [diff] [blame] | 4935 | |
| 4936 | pntsd->revision = cpu_to_le16(1); |
| 4937 | pntsd->type = cpu_to_le16(SELF_RELATIVE | DACL_PROTECTED); |
| 4938 | pntsd->osidoffset = 0; |
| 4939 | pntsd->gsidoffset = 0; |
| 4940 | pntsd->sacloffset = 0; |
| 4941 | pntsd->dacloffset = 0; |
| 4942 | |
| 4943 | secdesclen = sizeof(struct smb_ntsd); |
| 4944 | rsp->OutputBufferLength = cpu_to_le32(secdesclen); |
| 4945 | inc_rfc1001_len(rsp_org, secdesclen); |
| 4946 | |
| 4947 | return 0; |
| 4948 | } |
| 4949 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4950 | if (work->next_smb2_rcv_hdr_off) { |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 4951 | if (!has_file_id(le64_to_cpu(req->VolatileFileId))) { |
| 4952 | ksmbd_debug(SMB, "Compound request set FID = %llu\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4953 | work->compound_fid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4954 | id = work->compound_fid; |
| 4955 | pid = work->compound_pfid; |
| 4956 | } |
| 4957 | } |
| 4958 | |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 4959 | if (!has_file_id(id)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4960 | id = le64_to_cpu(req->VolatileFileId); |
| 4961 | pid = le64_to_cpu(req->PersistentFileId); |
| 4962 | } |
| 4963 | |
| 4964 | fp = ksmbd_lookup_fd_slow(work, id, pid); |
| 4965 | if (!fp) |
| 4966 | return -ENOENT; |
| 4967 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 4968 | user_ns = file_mnt_user_ns(fp->filp); |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 4969 | inode = file_inode(fp->filp); |
Christian Brauner | 43205ca | 2021-08-23 17:13:50 +0200 | [diff] [blame] | 4970 | ksmbd_acls_fattr(&fattr, user_ns, inode); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4971 | |
| 4972 | if (test_share_config_flag(work->tcon->share_conf, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4973 | KSMBD_SHARE_FLAG_ACL_XATTR)) |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 4974 | ksmbd_vfs_get_sd_xattr(work->conn, user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 4975 | fp->filp->f_path.dentry, &ppntsd); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4976 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 4977 | rc = build_sec_desc(user_ns, pntsd, ppntsd, addition_info, |
| 4978 | &secdesclen, &fattr); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4979 | posix_acl_release(fattr.cf_acls); |
| 4980 | posix_acl_release(fattr.cf_dacls); |
| 4981 | kfree(ppntsd); |
| 4982 | ksmbd_fd_put(work, fp); |
| 4983 | if (rc) |
| 4984 | return rc; |
| 4985 | |
| 4986 | rsp->OutputBufferLength = cpu_to_le32(secdesclen); |
| 4987 | inc_rfc1001_len(rsp_org, secdesclen); |
| 4988 | return 0; |
| 4989 | } |
| 4990 | |
| 4991 | /** |
| 4992 | * smb2_query_info() - handler for smb2 query info command |
| 4993 | * @work: smb work containing query info request buffer |
| 4994 | * |
| 4995 | * Return: 0 on success, otherwise error |
| 4996 | */ |
| 4997 | int smb2_query_info(struct ksmbd_work *work) |
| 4998 | { |
| 4999 | struct smb2_query_info_req *req; |
| 5000 | struct smb2_query_info_rsp *rsp, *rsp_org; |
| 5001 | int rc = 0; |
| 5002 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 5003 | rsp_org = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5004 | WORK_BUFFERS(work, req, rsp); |
| 5005 | |
| 5006 | ksmbd_debug(SMB, "GOT query info request\n"); |
| 5007 | |
| 5008 | switch (req->InfoType) { |
| 5009 | case SMB2_O_INFO_FILE: |
| 5010 | ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILE\n"); |
| 5011 | rc = smb2_get_info_file(work, req, rsp, (void *)rsp_org); |
| 5012 | break; |
| 5013 | case SMB2_O_INFO_FILESYSTEM: |
| 5014 | ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILESYSTEM\n"); |
| 5015 | rc = smb2_get_info_filesystem(work, req, rsp, (void *)rsp_org); |
| 5016 | break; |
| 5017 | case SMB2_O_INFO_SECURITY: |
| 5018 | ksmbd_debug(SMB, "GOT SMB2_O_INFO_SECURITY\n"); |
| 5019 | rc = smb2_get_info_sec(work, req, rsp, (void *)rsp_org); |
| 5020 | break; |
| 5021 | default: |
| 5022 | ksmbd_debug(SMB, "InfoType %d not supported yet\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5023 | req->InfoType); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5024 | rc = -EOPNOTSUPP; |
| 5025 | } |
| 5026 | |
| 5027 | if (rc < 0) { |
| 5028 | if (rc == -EACCES) |
| 5029 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 5030 | else if (rc == -ENOENT) |
| 5031 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 5032 | else if (rc == -EIO) |
| 5033 | rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR; |
| 5034 | else if (rc == -EOPNOTSUPP || rsp->hdr.Status == 0) |
| 5035 | rsp->hdr.Status = STATUS_INVALID_INFO_CLASS; |
| 5036 | smb2_set_err_rsp(work); |
| 5037 | |
| 5038 | ksmbd_debug(SMB, "error while processing smb2 query rc = %d\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5039 | rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5040 | return rc; |
| 5041 | } |
| 5042 | rsp->StructureSize = cpu_to_le16(9); |
| 5043 | rsp->OutputBufferOffset = cpu_to_le16(72); |
| 5044 | inc_rfc1001_len(rsp_org, 8); |
| 5045 | return 0; |
| 5046 | } |
| 5047 | |
| 5048 | /** |
| 5049 | * smb2_close_pipe() - handler for closing IPC pipe |
| 5050 | * @work: smb work containing close request buffer |
| 5051 | * |
| 5052 | * Return: 0 |
| 5053 | */ |
| 5054 | static noinline int smb2_close_pipe(struct ksmbd_work *work) |
| 5055 | { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5056 | u64 id; |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 5057 | struct smb2_close_req *req = work->request_buf; |
| 5058 | struct smb2_close_rsp *rsp = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5059 | |
| 5060 | id = le64_to_cpu(req->VolatileFileId); |
| 5061 | ksmbd_session_rpc_close(work->sess, id); |
| 5062 | |
| 5063 | rsp->StructureSize = cpu_to_le16(60); |
| 5064 | rsp->Flags = 0; |
| 5065 | rsp->Reserved = 0; |
| 5066 | rsp->CreationTime = 0; |
| 5067 | rsp->LastAccessTime = 0; |
| 5068 | rsp->LastWriteTime = 0; |
| 5069 | rsp->ChangeTime = 0; |
| 5070 | rsp->AllocationSize = 0; |
| 5071 | rsp->EndOfFile = 0; |
| 5072 | rsp->Attributes = 0; |
| 5073 | inc_rfc1001_len(rsp, 60); |
| 5074 | return 0; |
| 5075 | } |
| 5076 | |
| 5077 | /** |
| 5078 | * smb2_close() - handler for smb2 close file command |
| 5079 | * @work: smb work containing close request buffer |
| 5080 | * |
| 5081 | * Return: 0 |
| 5082 | */ |
| 5083 | int smb2_close(struct ksmbd_work *work) |
| 5084 | { |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 5085 | u64 volatile_id = KSMBD_NO_FID; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5086 | u64 sess_id; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5087 | struct smb2_close_req *req; |
| 5088 | struct smb2_close_rsp *rsp; |
| 5089 | struct smb2_close_rsp *rsp_org; |
| 5090 | struct ksmbd_conn *conn = work->conn; |
| 5091 | struct ksmbd_file *fp; |
| 5092 | struct inode *inode; |
| 5093 | u64 time; |
| 5094 | int err = 0; |
| 5095 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 5096 | rsp_org = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5097 | WORK_BUFFERS(work, req, rsp); |
| 5098 | |
| 5099 | if (test_share_config_flag(work->tcon->share_conf, |
| 5100 | KSMBD_SHARE_FLAG_PIPE)) { |
| 5101 | ksmbd_debug(SMB, "IPC pipe close request\n"); |
| 5102 | return smb2_close_pipe(work); |
| 5103 | } |
| 5104 | |
| 5105 | sess_id = le64_to_cpu(req->hdr.SessionId); |
| 5106 | if (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS) |
| 5107 | sess_id = work->compound_sid; |
| 5108 | |
| 5109 | work->compound_sid = 0; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5110 | if (check_session_id(conn, sess_id)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5111 | work->compound_sid = sess_id; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5112 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5113 | rsp->hdr.Status = STATUS_USER_SESSION_DELETED; |
| 5114 | if (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS) |
| 5115 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 5116 | err = -EBADF; |
| 5117 | goto out; |
| 5118 | } |
| 5119 | |
| 5120 | if (work->next_smb2_rcv_hdr_off && |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 5121 | !has_file_id(le64_to_cpu(req->VolatileFileId))) { |
| 5122 | if (!has_file_id(work->compound_fid)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5123 | /* file already closed, return FILE_CLOSED */ |
| 5124 | ksmbd_debug(SMB, "file already closed\n"); |
| 5125 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 5126 | err = -EBADF; |
| 5127 | goto out; |
| 5128 | } else { |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 5129 | ksmbd_debug(SMB, |
| 5130 | "Compound request set FID = %llu:%llu\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5131 | work->compound_fid, |
| 5132 | work->compound_pfid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5133 | volatile_id = work->compound_fid; |
| 5134 | |
| 5135 | /* file closed, stored id is not valid anymore */ |
| 5136 | work->compound_fid = KSMBD_NO_FID; |
| 5137 | work->compound_pfid = KSMBD_NO_FID; |
| 5138 | } |
| 5139 | } else { |
| 5140 | volatile_id = le64_to_cpu(req->VolatileFileId); |
| 5141 | } |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 5142 | ksmbd_debug(SMB, "volatile_id = %llu\n", volatile_id); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5143 | |
| 5144 | rsp->StructureSize = cpu_to_le16(60); |
| 5145 | rsp->Reserved = 0; |
| 5146 | |
| 5147 | if (req->Flags == SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB) { |
| 5148 | fp = ksmbd_lookup_fd_fast(work, volatile_id); |
| 5149 | if (!fp) { |
| 5150 | err = -ENOENT; |
| 5151 | goto out; |
| 5152 | } |
| 5153 | |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 5154 | inode = file_inode(fp->filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5155 | rsp->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB; |
| 5156 | rsp->AllocationSize = S_ISDIR(inode->i_mode) ? 0 : |
| 5157 | cpu_to_le64(inode->i_blocks << 9); |
| 5158 | rsp->EndOfFile = cpu_to_le64(inode->i_size); |
| 5159 | rsp->Attributes = fp->f_ci->m_fattr; |
| 5160 | rsp->CreationTime = cpu_to_le64(fp->create_time); |
| 5161 | time = ksmbd_UnixTimeToNT(inode->i_atime); |
| 5162 | rsp->LastAccessTime = cpu_to_le64(time); |
| 5163 | time = ksmbd_UnixTimeToNT(inode->i_mtime); |
| 5164 | rsp->LastWriteTime = cpu_to_le64(time); |
| 5165 | time = ksmbd_UnixTimeToNT(inode->i_ctime); |
| 5166 | rsp->ChangeTime = cpu_to_le64(time); |
| 5167 | ksmbd_fd_put(work, fp); |
| 5168 | } else { |
| 5169 | rsp->Flags = 0; |
| 5170 | rsp->AllocationSize = 0; |
| 5171 | rsp->EndOfFile = 0; |
| 5172 | rsp->Attributes = 0; |
| 5173 | rsp->CreationTime = 0; |
| 5174 | rsp->LastAccessTime = 0; |
| 5175 | rsp->LastWriteTime = 0; |
| 5176 | rsp->ChangeTime = 0; |
| 5177 | } |
| 5178 | |
| 5179 | err = ksmbd_close_fd(work, volatile_id); |
| 5180 | out: |
| 5181 | if (err) { |
| 5182 | if (rsp->hdr.Status == 0) |
| 5183 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 5184 | smb2_set_err_rsp(work); |
| 5185 | } else { |
| 5186 | inc_rfc1001_len(rsp_org, 60); |
| 5187 | } |
| 5188 | |
| 5189 | return 0; |
| 5190 | } |
| 5191 | |
| 5192 | /** |
| 5193 | * smb2_echo() - handler for smb2 echo(ping) command |
| 5194 | * @work: smb work containing echo request buffer |
| 5195 | * |
| 5196 | * Return: 0 |
| 5197 | */ |
| 5198 | int smb2_echo(struct ksmbd_work *work) |
| 5199 | { |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 5200 | struct smb2_echo_rsp *rsp = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5201 | |
| 5202 | rsp->StructureSize = cpu_to_le16(4); |
| 5203 | rsp->Reserved = 0; |
| 5204 | inc_rfc1001_len(rsp, 4); |
| 5205 | return 0; |
| 5206 | } |
| 5207 | |
Christian Brauner | da1e7ad | 2021-08-23 17:13:47 +0200 | [diff] [blame] | 5208 | static int smb2_rename(struct ksmbd_work *work, |
| 5209 | struct ksmbd_file *fp, |
| 5210 | struct user_namespace *user_ns, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5211 | struct smb2_file_rename_info *file_info, |
| 5212 | struct nls_table *local_nls) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5213 | { |
| 5214 | struct ksmbd_share_config *share = fp->tcon->share_conf; |
| 5215 | char *new_name = NULL, *abs_oldname = NULL, *old_name = NULL; |
| 5216 | char *pathname = NULL; |
| 5217 | struct path path; |
| 5218 | bool file_present = true; |
| 5219 | int rc; |
| 5220 | |
| 5221 | ksmbd_debug(SMB, "setting FILE_RENAME_INFO\n"); |
| 5222 | pathname = kmalloc(PATH_MAX, GFP_KERNEL); |
| 5223 | if (!pathname) |
| 5224 | return -ENOMEM; |
| 5225 | |
| 5226 | abs_oldname = d_path(&fp->filp->f_path, pathname, PATH_MAX); |
| 5227 | if (IS_ERR(abs_oldname)) { |
| 5228 | rc = -EINVAL; |
| 5229 | goto out; |
| 5230 | } |
| 5231 | old_name = strrchr(abs_oldname, '/'); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5232 | if (old_name && old_name[1] != '\0') { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5233 | old_name++; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5234 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5235 | ksmbd_debug(SMB, "can't get last component in path %s\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5236 | abs_oldname); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5237 | rc = -ENOENT; |
| 5238 | goto out; |
| 5239 | } |
| 5240 | |
| 5241 | new_name = smb2_get_name(share, |
| 5242 | file_info->FileName, |
| 5243 | le32_to_cpu(file_info->FileNameLength), |
| 5244 | local_nls); |
| 5245 | if (IS_ERR(new_name)) { |
| 5246 | rc = PTR_ERR(new_name); |
| 5247 | goto out; |
| 5248 | } |
| 5249 | |
| 5250 | if (strchr(new_name, ':')) { |
| 5251 | int s_type; |
| 5252 | char *xattr_stream_name, *stream_name = NULL; |
| 5253 | size_t xattr_stream_size; |
| 5254 | int len; |
| 5255 | |
| 5256 | rc = parse_stream_name(new_name, &stream_name, &s_type); |
| 5257 | if (rc < 0) |
| 5258 | goto out; |
| 5259 | |
| 5260 | len = strlen(new_name); |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame^] | 5261 | if (len > 0 && new_name[len - 1] != '/') { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5262 | pr_err("not allow base filename in rename\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5263 | rc = -ESHARE; |
| 5264 | goto out; |
| 5265 | } |
| 5266 | |
| 5267 | rc = ksmbd_vfs_xattr_stream_name(stream_name, |
| 5268 | &xattr_stream_name, |
| 5269 | &xattr_stream_size, |
| 5270 | s_type); |
| 5271 | if (rc) |
| 5272 | goto out; |
| 5273 | |
Christian Brauner | da1e7ad | 2021-08-23 17:13:47 +0200 | [diff] [blame] | 5274 | rc = ksmbd_vfs_setxattr(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 5275 | fp->filp->f_path.dentry, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5276 | xattr_stream_name, |
| 5277 | NULL, 0, 0); |
| 5278 | if (rc < 0) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5279 | pr_err("failed to store stream name in xattr: %d\n", |
| 5280 | rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5281 | rc = -EINVAL; |
| 5282 | goto out; |
| 5283 | } |
| 5284 | |
| 5285 | goto out; |
| 5286 | } |
| 5287 | |
| 5288 | ksmbd_debug(SMB, "new name %s\n", new_name); |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame^] | 5289 | rc = ksmbd_vfs_kern_path(work, new_name, LOOKUP_NO_SYMLINKS, &path, 1); |
| 5290 | if (rc) { |
| 5291 | if (rc != -ENOENT) |
| 5292 | goto out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5293 | file_present = false; |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame^] | 5294 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5295 | path_put(&path); |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame^] | 5296 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5297 | |
| 5298 | if (ksmbd_share_veto_filename(share, new_name)) { |
| 5299 | rc = -ENOENT; |
| 5300 | ksmbd_debug(SMB, "Can't rename vetoed file: %s\n", new_name); |
| 5301 | goto out; |
| 5302 | } |
| 5303 | |
| 5304 | if (file_info->ReplaceIfExists) { |
| 5305 | if (file_present) { |
| 5306 | rc = ksmbd_vfs_remove_file(work, new_name); |
| 5307 | if (rc) { |
| 5308 | if (rc != -ENOTEMPTY) |
| 5309 | rc = -EINVAL; |
| 5310 | ksmbd_debug(SMB, "cannot delete %s, rc %d\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5311 | new_name, rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5312 | goto out; |
| 5313 | } |
| 5314 | } |
| 5315 | } else { |
| 5316 | if (file_present && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5317 | strncmp(old_name, path.dentry->d_name.name, strlen(old_name))) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5318 | rc = -EEXIST; |
| 5319 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5320 | "cannot rename already existing file\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5321 | goto out; |
| 5322 | } |
| 5323 | } |
| 5324 | |
| 5325 | rc = ksmbd_vfs_fp_rename(work, fp, new_name); |
| 5326 | out: |
| 5327 | kfree(pathname); |
| 5328 | if (!IS_ERR(new_name)) |
Marios Makassikis | 915f570 | 2021-04-13 13:25:57 +0900 | [diff] [blame] | 5329 | kfree(new_name); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5330 | return rc; |
| 5331 | } |
| 5332 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5333 | static int smb2_create_link(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5334 | struct ksmbd_share_config *share, |
| 5335 | struct smb2_file_link_info *file_info, |
| 5336 | struct file *filp, |
| 5337 | struct nls_table *local_nls) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5338 | { |
| 5339 | char *link_name = NULL, *target_name = NULL, *pathname = NULL; |
| 5340 | struct path path; |
| 5341 | bool file_present = true; |
| 5342 | int rc; |
| 5343 | |
| 5344 | ksmbd_debug(SMB, "setting FILE_LINK_INFORMATION\n"); |
| 5345 | pathname = kmalloc(PATH_MAX, GFP_KERNEL); |
| 5346 | if (!pathname) |
| 5347 | return -ENOMEM; |
| 5348 | |
| 5349 | link_name = smb2_get_name(share, |
| 5350 | file_info->FileName, |
| 5351 | le32_to_cpu(file_info->FileNameLength), |
| 5352 | local_nls); |
| 5353 | if (IS_ERR(link_name) || S_ISDIR(file_inode(filp)->i_mode)) { |
| 5354 | rc = -EINVAL; |
| 5355 | goto out; |
| 5356 | } |
| 5357 | |
| 5358 | ksmbd_debug(SMB, "link name is %s\n", link_name); |
| 5359 | target_name = d_path(&filp->f_path, pathname, PATH_MAX); |
| 5360 | if (IS_ERR(target_name)) { |
| 5361 | rc = -EINVAL; |
| 5362 | goto out; |
| 5363 | } |
| 5364 | |
| 5365 | ksmbd_debug(SMB, "target name is %s\n", target_name); |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame^] | 5366 | rc = ksmbd_vfs_kern_path(work, link_name, LOOKUP_NO_SYMLINKS, &path, 0); |
| 5367 | if (rc) { |
| 5368 | if (rc != -ENOENT) |
| 5369 | goto out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5370 | file_present = false; |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame^] | 5371 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5372 | path_put(&path); |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame^] | 5373 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5374 | |
| 5375 | if (file_info->ReplaceIfExists) { |
| 5376 | if (file_present) { |
| 5377 | rc = ksmbd_vfs_remove_file(work, link_name); |
| 5378 | if (rc) { |
| 5379 | rc = -EINVAL; |
| 5380 | ksmbd_debug(SMB, "cannot delete %s\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5381 | link_name); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5382 | goto out; |
| 5383 | } |
| 5384 | } |
| 5385 | } else { |
| 5386 | if (file_present) { |
| 5387 | rc = -EEXIST; |
| 5388 | ksmbd_debug(SMB, "link already exists\n"); |
| 5389 | goto out; |
| 5390 | } |
| 5391 | } |
| 5392 | |
| 5393 | rc = ksmbd_vfs_link(work, target_name, link_name); |
| 5394 | if (rc) |
| 5395 | rc = -EINVAL; |
| 5396 | out: |
| 5397 | if (!IS_ERR(link_name)) |
Marios Makassikis | 915f570 | 2021-04-13 13:25:57 +0900 | [diff] [blame] | 5398 | kfree(link_name); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5399 | kfree(pathname); |
| 5400 | return rc; |
| 5401 | } |
| 5402 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5403 | static int set_file_basic_info(struct ksmbd_file *fp, char *buf, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5404 | struct ksmbd_share_config *share) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5405 | { |
| 5406 | struct smb2_file_all_info *file_info; |
| 5407 | struct iattr attrs; |
Christian Brauner | db7fb6f | 2021-08-26 10:07:05 +0900 | [diff] [blame] | 5408 | struct timespec64 ctime; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5409 | struct file *filp; |
| 5410 | struct inode *inode; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 5411 | struct user_namespace *user_ns; |
Namjae Jeon | 4ffd526 | 2021-09-07 08:15:21 +0900 | [diff] [blame] | 5412 | int rc = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5413 | |
Marios Makassikis | 7adfd4f | 2021-04-27 15:30:22 +0900 | [diff] [blame] | 5414 | if (!(fp->daccess & FILE_WRITE_ATTRIBUTES_LE)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5415 | return -EACCES; |
| 5416 | |
| 5417 | file_info = (struct smb2_file_all_info *)buf; |
| 5418 | attrs.ia_valid = 0; |
| 5419 | filp = fp->filp; |
| 5420 | inode = file_inode(filp); |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 5421 | user_ns = file_mnt_user_ns(filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5422 | |
| 5423 | if (file_info->CreationTime) |
| 5424 | fp->create_time = le64_to_cpu(file_info->CreationTime); |
| 5425 | |
| 5426 | if (file_info->LastAccessTime) { |
| 5427 | attrs.ia_atime = ksmbd_NTtimeToUnix(file_info->LastAccessTime); |
| 5428 | attrs.ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET); |
| 5429 | } |
| 5430 | |
| 5431 | if (file_info->ChangeTime) { |
Christian Brauner | db7fb6f | 2021-08-26 10:07:05 +0900 | [diff] [blame] | 5432 | attrs.ia_ctime = ksmbd_NTtimeToUnix(file_info->ChangeTime); |
| 5433 | ctime = attrs.ia_ctime; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5434 | attrs.ia_valid |= ATTR_CTIME; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5435 | } else { |
Christian Brauner | db7fb6f | 2021-08-26 10:07:05 +0900 | [diff] [blame] | 5436 | ctime = inode->i_ctime; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5437 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5438 | |
| 5439 | if (file_info->LastWriteTime) { |
| 5440 | attrs.ia_mtime = ksmbd_NTtimeToUnix(file_info->LastWriteTime); |
| 5441 | attrs.ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET); |
| 5442 | } |
| 5443 | |
| 5444 | if (file_info->Attributes) { |
| 5445 | if (!S_ISDIR(inode->i_mode) && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5446 | file_info->Attributes & ATTR_DIRECTORY_LE) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5447 | pr_err("can't change a file to a directory\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5448 | return -EINVAL; |
| 5449 | } |
| 5450 | |
| 5451 | if (!(S_ISDIR(inode->i_mode) && file_info->Attributes == ATTR_NORMAL_LE)) |
| 5452 | fp->f_ci->m_fattr = file_info->Attributes | |
| 5453 | (fp->f_ci->m_fattr & ATTR_DIRECTORY_LE); |
| 5454 | } |
| 5455 | |
| 5456 | if (test_share_config_flag(share, KSMBD_SHARE_FLAG_STORE_DOS_ATTRS) && |
| 5457 | (file_info->CreationTime || file_info->Attributes)) { |
| 5458 | struct xattr_dos_attrib da = {0}; |
| 5459 | |
| 5460 | da.version = 4; |
| 5461 | da.itime = fp->itime; |
| 5462 | da.create_time = fp->create_time; |
| 5463 | da.attr = le32_to_cpu(fp->f_ci->m_fattr); |
| 5464 | da.flags = XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME | |
| 5465 | XATTR_DOSINFO_ITIME; |
| 5466 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 5467 | rc = ksmbd_vfs_set_dos_attrib_xattr(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 5468 | filp->f_path.dentry, &da); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5469 | if (rc) |
| 5470 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5471 | "failed to restore file attribute in EA\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5472 | rc = 0; |
| 5473 | } |
| 5474 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5475 | if (attrs.ia_valid) { |
| 5476 | struct dentry *dentry = filp->f_path.dentry; |
| 5477 | struct inode *inode = d_inode(dentry); |
| 5478 | |
| 5479 | if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) |
| 5480 | return -EACCES; |
| 5481 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5482 | inode_lock(inode); |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 5483 | rc = notify_change(user_ns, dentry, &attrs, NULL); |
Christian Brauner | db7fb6f | 2021-08-26 10:07:05 +0900 | [diff] [blame] | 5484 | if (!rc) { |
| 5485 | inode->i_ctime = ctime; |
| 5486 | mark_inode_dirty(inode); |
| 5487 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5488 | inode_unlock(inode); |
| 5489 | } |
Christian Brauner | eb5784f | 2021-08-23 17:13:55 +0200 | [diff] [blame] | 5490 | return rc; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5491 | } |
| 5492 | |
| 5493 | static int set_file_allocation_info(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5494 | struct ksmbd_file *fp, char *buf) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5495 | { |
| 5496 | /* |
| 5497 | * TODO : It's working fine only when store dos attributes |
| 5498 | * is not yes. need to implement a logic which works |
| 5499 | * properly with any smb.conf option |
| 5500 | */ |
| 5501 | |
| 5502 | struct smb2_file_alloc_info *file_alloc_info; |
| 5503 | loff_t alloc_blks; |
| 5504 | struct inode *inode; |
| 5505 | int rc; |
| 5506 | |
Marios Makassikis | a299669 | 2021-04-27 15:29:01 +0900 | [diff] [blame] | 5507 | if (!(fp->daccess & FILE_WRITE_DATA_LE)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5508 | return -EACCES; |
| 5509 | |
| 5510 | file_alloc_info = (struct smb2_file_alloc_info *)buf; |
| 5511 | alloc_blks = (le64_to_cpu(file_alloc_info->AllocationSize) + 511) >> 9; |
| 5512 | inode = file_inode(fp->filp); |
| 5513 | |
| 5514 | if (alloc_blks > inode->i_blocks) { |
Namjae Jeon | e8c0619 | 2021-06-22 11:06:11 +0900 | [diff] [blame] | 5515 | smb_break_all_levII_oplock(work, fp, 1); |
| 5516 | rc = vfs_fallocate(fp->filp, FALLOC_FL_KEEP_SIZE, 0, |
| 5517 | alloc_blks * 512); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5518 | if (rc && rc != -EOPNOTSUPP) { |
Namjae Jeon | e8c0619 | 2021-06-22 11:06:11 +0900 | [diff] [blame] | 5519 | pr_err("vfs_fallocate is failed : %d\n", rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5520 | return rc; |
| 5521 | } |
| 5522 | } else if (alloc_blks < inode->i_blocks) { |
| 5523 | loff_t size; |
| 5524 | |
| 5525 | /* |
| 5526 | * Allocation size could be smaller than original one |
| 5527 | * which means allocated blocks in file should be |
| 5528 | * deallocated. use truncate to cut out it, but inode |
| 5529 | * size is also updated with truncate offset. |
| 5530 | * inode size is retained by backup inode size. |
| 5531 | */ |
| 5532 | size = i_size_read(inode); |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame^] | 5533 | rc = ksmbd_vfs_truncate(work, fp, alloc_blks * 512); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5534 | if (rc) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5535 | pr_err("truncate failed! filename : %s, err %d\n", |
| 5536 | fp->filename, rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5537 | return rc; |
| 5538 | } |
| 5539 | if (size < alloc_blks * 512) |
| 5540 | i_size_write(inode, size); |
| 5541 | } |
| 5542 | return 0; |
| 5543 | } |
| 5544 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5545 | static int set_end_of_file_info(struct ksmbd_work *work, struct ksmbd_file *fp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5546 | char *buf) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5547 | { |
| 5548 | struct smb2_file_eof_info *file_eof_info; |
| 5549 | loff_t newsize; |
| 5550 | struct inode *inode; |
| 5551 | int rc; |
| 5552 | |
Marios Makassikis | a299669 | 2021-04-27 15:29:01 +0900 | [diff] [blame] | 5553 | if (!(fp->daccess & FILE_WRITE_DATA_LE)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5554 | return -EACCES; |
| 5555 | |
| 5556 | file_eof_info = (struct smb2_file_eof_info *)buf; |
| 5557 | newsize = le64_to_cpu(file_eof_info->EndOfFile); |
| 5558 | inode = file_inode(fp->filp); |
| 5559 | |
| 5560 | /* |
| 5561 | * If FILE_END_OF_FILE_INFORMATION of set_info_file is called |
| 5562 | * on FAT32 shared device, truncate execution time is too long |
| 5563 | * and network error could cause from windows client. because |
| 5564 | * truncate of some filesystem like FAT32 fill zero data in |
| 5565 | * truncated range. |
| 5566 | */ |
| 5567 | if (inode->i_sb->s_magic != MSDOS_SUPER_MAGIC) { |
| 5568 | ksmbd_debug(SMB, "filename : %s truncated to newsize %lld\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5569 | fp->filename, newsize); |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame^] | 5570 | rc = ksmbd_vfs_truncate(work, fp, newsize); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5571 | if (rc) { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5572 | ksmbd_debug(SMB, "truncate failed! filename : %s err %d\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5573 | fp->filename, rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5574 | if (rc != -EAGAIN) |
| 5575 | rc = -EBADF; |
| 5576 | return rc; |
| 5577 | } |
| 5578 | } |
| 5579 | return 0; |
| 5580 | } |
| 5581 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5582 | static int set_rename_info(struct ksmbd_work *work, struct ksmbd_file *fp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5583 | char *buf) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5584 | { |
Christian Brauner | da1e7ad | 2021-08-23 17:13:47 +0200 | [diff] [blame] | 5585 | struct user_namespace *user_ns; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5586 | struct ksmbd_file *parent_fp; |
Namjae Jeon | 12202c0 | 2021-06-29 09:23:56 +0900 | [diff] [blame] | 5587 | struct dentry *parent; |
| 5588 | struct dentry *dentry = fp->filp->f_path.dentry; |
| 5589 | int ret; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5590 | |
| 5591 | if (!(fp->daccess & FILE_DELETE_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5592 | pr_err("no right to delete : 0x%x\n", fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5593 | return -EACCES; |
| 5594 | } |
| 5595 | |
Christian Brauner | da1e7ad | 2021-08-23 17:13:47 +0200 | [diff] [blame] | 5596 | user_ns = file_mnt_user_ns(fp->filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5597 | if (ksmbd_stream_fd(fp)) |
| 5598 | goto next; |
| 5599 | |
Namjae Jeon | 12202c0 | 2021-06-29 09:23:56 +0900 | [diff] [blame] | 5600 | parent = dget_parent(dentry); |
Christian Brauner | da1e7ad | 2021-08-23 17:13:47 +0200 | [diff] [blame] | 5601 | ret = ksmbd_vfs_lock_parent(user_ns, parent, dentry); |
Namjae Jeon | 12202c0 | 2021-06-29 09:23:56 +0900 | [diff] [blame] | 5602 | if (ret) { |
| 5603 | dput(parent); |
| 5604 | return ret; |
| 5605 | } |
| 5606 | |
| 5607 | parent_fp = ksmbd_lookup_fd_inode(d_inode(parent)); |
| 5608 | inode_unlock(d_inode(parent)); |
| 5609 | dput(parent); |
| 5610 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5611 | if (parent_fp) { |
| 5612 | if (parent_fp->daccess & FILE_DELETE_LE) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5613 | pr_err("parent dir is opened with delete access\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5614 | return -ESHARE; |
| 5615 | } |
| 5616 | } |
| 5617 | next: |
Christian Brauner | da1e7ad | 2021-08-23 17:13:47 +0200 | [diff] [blame] | 5618 | return smb2_rename(work, fp, user_ns, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5619 | (struct smb2_file_rename_info *)buf, |
| 5620 | work->sess->conn->local_nls); |
| 5621 | } |
| 5622 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5623 | static int set_file_disposition_info(struct ksmbd_file *fp, char *buf) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5624 | { |
| 5625 | struct smb2_file_disposition_info *file_info; |
| 5626 | struct inode *inode; |
| 5627 | |
| 5628 | if (!(fp->daccess & FILE_DELETE_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5629 | pr_err("no right to delete : 0x%x\n", fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5630 | return -EACCES; |
| 5631 | } |
| 5632 | |
| 5633 | inode = file_inode(fp->filp); |
| 5634 | file_info = (struct smb2_file_disposition_info *)buf; |
| 5635 | if (file_info->DeletePending) { |
| 5636 | if (S_ISDIR(inode->i_mode) && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5637 | ksmbd_vfs_empty_dir(fp) == -ENOTEMPTY) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5638 | return -EBUSY; |
| 5639 | ksmbd_set_inode_pending_delete(fp); |
| 5640 | } else { |
| 5641 | ksmbd_clear_inode_pending_delete(fp); |
| 5642 | } |
| 5643 | return 0; |
| 5644 | } |
| 5645 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5646 | static int set_file_position_info(struct ksmbd_file *fp, char *buf) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5647 | { |
| 5648 | struct smb2_file_pos_info *file_info; |
| 5649 | loff_t current_byte_offset; |
Namjae Jeon | ee81cae | 2021-06-26 22:32:34 +0900 | [diff] [blame] | 5650 | unsigned long sector_size; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5651 | struct inode *inode; |
| 5652 | |
| 5653 | inode = file_inode(fp->filp); |
| 5654 | file_info = (struct smb2_file_pos_info *)buf; |
| 5655 | current_byte_offset = le64_to_cpu(file_info->CurrentByteOffset); |
Namjae Jeon | ee81cae | 2021-06-26 22:32:34 +0900 | [diff] [blame] | 5656 | sector_size = inode->i_sb->s_blocksize; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5657 | |
| 5658 | if (current_byte_offset < 0 || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5659 | (fp->coption == FILE_NO_INTERMEDIATE_BUFFERING_LE && |
| 5660 | current_byte_offset & (sector_size - 1))) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5661 | pr_err("CurrentByteOffset is not valid : %llu\n", |
| 5662 | current_byte_offset); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5663 | return -EINVAL; |
| 5664 | } |
| 5665 | |
| 5666 | fp->filp->f_pos = current_byte_offset; |
| 5667 | return 0; |
| 5668 | } |
| 5669 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5670 | static int set_file_mode_info(struct ksmbd_file *fp, char *buf) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5671 | { |
| 5672 | struct smb2_file_mode_info *file_info; |
| 5673 | __le32 mode; |
| 5674 | |
| 5675 | file_info = (struct smb2_file_mode_info *)buf; |
| 5676 | mode = file_info->Mode; |
| 5677 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5678 | if ((mode & ~FILE_MODE_INFO_MASK) || |
| 5679 | (mode & FILE_SYNCHRONOUS_IO_ALERT_LE && |
| 5680 | mode & FILE_SYNCHRONOUS_IO_NONALERT_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5681 | 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] | 5682 | return -EINVAL; |
| 5683 | } |
| 5684 | |
| 5685 | /* |
| 5686 | * TODO : need to implement consideration for |
| 5687 | * FILE_SYNCHRONOUS_IO_ALERT and FILE_SYNCHRONOUS_IO_NONALERT |
| 5688 | */ |
| 5689 | ksmbd_vfs_set_fadvise(fp->filp, mode); |
| 5690 | fp->coption = mode; |
| 5691 | return 0; |
| 5692 | } |
| 5693 | |
| 5694 | /** |
| 5695 | * smb2_set_info_file() - handler for smb2 set info command |
| 5696 | * @work: smb work containing set info command buffer |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 5697 | * @fp: ksmbd_file pointer |
| 5698 | * @info_class: smb2 set info class |
| 5699 | * @share: ksmbd_share_config pointer |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5700 | * |
| 5701 | * Return: 0 on success, otherwise error |
| 5702 | * TODO: need to implement an error handling for STATUS_INFO_LENGTH_MISMATCH |
| 5703 | */ |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5704 | static int smb2_set_info_file(struct ksmbd_work *work, struct ksmbd_file *fp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5705 | int info_class, char *buf, |
| 5706 | struct ksmbd_share_config *share) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5707 | { |
| 5708 | switch (info_class) { |
| 5709 | case FILE_BASIC_INFORMATION: |
| 5710 | return set_file_basic_info(fp, buf, share); |
| 5711 | |
| 5712 | case FILE_ALLOCATION_INFORMATION: |
| 5713 | return set_file_allocation_info(work, fp, buf); |
| 5714 | |
| 5715 | case FILE_END_OF_FILE_INFORMATION: |
| 5716 | return set_end_of_file_info(work, fp, buf); |
| 5717 | |
| 5718 | case FILE_RENAME_INFORMATION: |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5719 | if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5720 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5721 | "User does not have write permission\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5722 | return -EACCES; |
| 5723 | } |
| 5724 | return set_rename_info(work, fp, buf); |
| 5725 | |
| 5726 | case FILE_LINK_INFORMATION: |
| 5727 | return smb2_create_link(work, work->tcon->share_conf, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5728 | (struct smb2_file_link_info *)buf, fp->filp, |
| 5729 | work->sess->conn->local_nls); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5730 | |
| 5731 | case FILE_DISPOSITION_INFORMATION: |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5732 | if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5733 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5734 | "User does not have write permission\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5735 | return -EACCES; |
| 5736 | } |
| 5737 | return set_file_disposition_info(fp, buf); |
| 5738 | |
| 5739 | case FILE_FULL_EA_INFORMATION: |
| 5740 | { |
| 5741 | if (!(fp->daccess & FILE_WRITE_EA_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5742 | pr_err("Not permitted to write ext attr: 0x%x\n", |
| 5743 | fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5744 | return -EACCES; |
| 5745 | } |
| 5746 | |
| 5747 | return smb2_set_ea((struct smb2_ea_info *)buf, |
| 5748 | &fp->filp->f_path); |
| 5749 | } |
| 5750 | |
| 5751 | case FILE_POSITION_INFORMATION: |
| 5752 | return set_file_position_info(fp, buf); |
| 5753 | |
| 5754 | case FILE_MODE_INFORMATION: |
| 5755 | return set_file_mode_info(fp, buf); |
| 5756 | } |
| 5757 | |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5758 | pr_err("Unimplemented Fileinfoclass :%d\n", info_class); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5759 | return -EOPNOTSUPP; |
| 5760 | } |
| 5761 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5762 | 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] | 5763 | char *buffer, int buf_len) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5764 | { |
| 5765 | struct smb_ntsd *pntsd = (struct smb_ntsd *)buffer; |
| 5766 | |
| 5767 | fp->saccess |= FILE_SHARE_DELETE_LE; |
| 5768 | |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 5769 | 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] | 5770 | buf_len, false); |
| 5771 | } |
| 5772 | |
| 5773 | /** |
| 5774 | * smb2_set_info() - handler for smb2 set info command handler |
| 5775 | * @work: smb work containing set info request buffer |
| 5776 | * |
| 5777 | * Return: 0 on success, otherwise error |
| 5778 | */ |
| 5779 | int smb2_set_info(struct ksmbd_work *work) |
| 5780 | { |
| 5781 | struct smb2_set_info_req *req; |
| 5782 | struct smb2_set_info_rsp *rsp, *rsp_org; |
| 5783 | struct ksmbd_file *fp; |
| 5784 | int rc = 0; |
| 5785 | unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID; |
| 5786 | |
| 5787 | ksmbd_debug(SMB, "Received set info request\n"); |
| 5788 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 5789 | rsp_org = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5790 | if (work->next_smb2_rcv_hdr_off) { |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 5791 | req = ksmbd_req_buf_next(work); |
| 5792 | rsp = ksmbd_resp_buf_next(work); |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 5793 | if (!has_file_id(le64_to_cpu(req->VolatileFileId))) { |
| 5794 | ksmbd_debug(SMB, "Compound request set FID = %llu\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5795 | work->compound_fid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5796 | id = work->compound_fid; |
| 5797 | pid = work->compound_pfid; |
| 5798 | } |
| 5799 | } else { |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 5800 | req = work->request_buf; |
| 5801 | rsp = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5802 | } |
| 5803 | |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 5804 | if (!has_file_id(id)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5805 | id = le64_to_cpu(req->VolatileFileId); |
| 5806 | pid = le64_to_cpu(req->PersistentFileId); |
| 5807 | } |
| 5808 | |
| 5809 | fp = ksmbd_lookup_fd_slow(work, id, pid); |
| 5810 | if (!fp) { |
| 5811 | ksmbd_debug(SMB, "Invalid id for close: %u\n", id); |
| 5812 | rc = -ENOENT; |
| 5813 | goto err_out; |
| 5814 | } |
| 5815 | |
| 5816 | switch (req->InfoType) { |
| 5817 | case SMB2_O_INFO_FILE: |
| 5818 | ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILE\n"); |
| 5819 | rc = smb2_set_info_file(work, fp, req->FileInfoClass, |
| 5820 | req->Buffer, work->tcon->share_conf); |
| 5821 | break; |
| 5822 | case SMB2_O_INFO_SECURITY: |
| 5823 | ksmbd_debug(SMB, "GOT SMB2_O_INFO_SECURITY\n"); |
Namjae Jeon | e70e392 | 2021-08-21 23:26:01 +0900 | [diff] [blame] | 5824 | if (ksmbd_override_fsids(work)) { |
| 5825 | rc = -ENOMEM; |
| 5826 | goto err_out; |
| 5827 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5828 | rc = smb2_set_info_sec(fp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5829 | le32_to_cpu(req->AdditionalInformation), |
| 5830 | req->Buffer, |
| 5831 | le32_to_cpu(req->BufferLength)); |
Namjae Jeon | e70e392 | 2021-08-21 23:26:01 +0900 | [diff] [blame] | 5832 | ksmbd_revert_fsids(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5833 | break; |
| 5834 | default: |
| 5835 | rc = -EOPNOTSUPP; |
| 5836 | } |
| 5837 | |
| 5838 | if (rc < 0) |
| 5839 | goto err_out; |
| 5840 | |
| 5841 | rsp->StructureSize = cpu_to_le16(2); |
| 5842 | inc_rfc1001_len(rsp_org, 2); |
| 5843 | ksmbd_fd_put(work, fp); |
| 5844 | return 0; |
| 5845 | |
| 5846 | err_out: |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame^] | 5847 | if (rc == -EACCES || rc == -EPERM || rc == -EXDEV) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5848 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 5849 | else if (rc == -EINVAL) |
| 5850 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 5851 | else if (rc == -ESHARE) |
| 5852 | rsp->hdr.Status = STATUS_SHARING_VIOLATION; |
| 5853 | else if (rc == -ENOENT) |
| 5854 | rsp->hdr.Status = STATUS_OBJECT_NAME_INVALID; |
| 5855 | else if (rc == -EBUSY || rc == -ENOTEMPTY) |
| 5856 | rsp->hdr.Status = STATUS_DIRECTORY_NOT_EMPTY; |
| 5857 | else if (rc == -EAGAIN) |
| 5858 | rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT; |
Namjae Jeon | ff1d572 | 2021-04-13 13:18:10 +0900 | [diff] [blame] | 5859 | else if (rc == -EBADF || rc == -ESTALE) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5860 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 5861 | else if (rc == -EEXIST) |
| 5862 | rsp->hdr.Status = STATUS_OBJECT_NAME_COLLISION; |
| 5863 | else if (rsp->hdr.Status == 0 || rc == -EOPNOTSUPP) |
| 5864 | rsp->hdr.Status = STATUS_INVALID_INFO_CLASS; |
| 5865 | smb2_set_err_rsp(work); |
| 5866 | ksmbd_fd_put(work, fp); |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5867 | ksmbd_debug(SMB, "error while processing smb2 query rc = %d\n", rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5868 | return rc; |
| 5869 | } |
| 5870 | |
| 5871 | /** |
| 5872 | * smb2_read_pipe() - handler for smb2 read from IPC pipe |
| 5873 | * @work: smb work containing read IPC pipe command buffer |
| 5874 | * |
| 5875 | * Return: 0 on success, otherwise error |
| 5876 | */ |
| 5877 | static noinline int smb2_read_pipe(struct ksmbd_work *work) |
| 5878 | { |
| 5879 | int nbytes = 0, err; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5880 | u64 id; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5881 | struct ksmbd_rpc_command *rpc_resp; |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 5882 | struct smb2_read_req *req = work->request_buf; |
| 5883 | struct smb2_read_rsp *rsp = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5884 | |
| 5885 | id = le64_to_cpu(req->VolatileFileId); |
| 5886 | |
| 5887 | inc_rfc1001_len(rsp, 16); |
| 5888 | rpc_resp = ksmbd_rpc_read(work->sess, id); |
| 5889 | if (rpc_resp) { |
| 5890 | if (rpc_resp->flags != KSMBD_RPC_OK) { |
| 5891 | err = -EINVAL; |
| 5892 | goto out; |
| 5893 | } |
| 5894 | |
| 5895 | work->aux_payload_buf = |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 5896 | kvmalloc(rpc_resp->payload_sz, GFP_KERNEL | __GFP_ZERO); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5897 | if (!work->aux_payload_buf) { |
| 5898 | err = -ENOMEM; |
| 5899 | goto out; |
| 5900 | } |
| 5901 | |
| 5902 | memcpy(work->aux_payload_buf, rpc_resp->payload, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5903 | rpc_resp->payload_sz); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5904 | |
| 5905 | nbytes = rpc_resp->payload_sz; |
| 5906 | work->resp_hdr_sz = get_rfc1002_len(rsp) + 4; |
| 5907 | work->aux_payload_sz = nbytes; |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 5908 | kvfree(rpc_resp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5909 | } |
| 5910 | |
| 5911 | rsp->StructureSize = cpu_to_le16(17); |
| 5912 | rsp->DataOffset = 80; |
| 5913 | rsp->Reserved = 0; |
| 5914 | rsp->DataLength = cpu_to_le32(nbytes); |
| 5915 | rsp->DataRemaining = 0; |
| 5916 | rsp->Reserved2 = 0; |
| 5917 | inc_rfc1001_len(rsp, nbytes); |
| 5918 | return 0; |
| 5919 | |
| 5920 | out: |
| 5921 | rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR; |
| 5922 | smb2_set_err_rsp(work); |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 5923 | kvfree(rpc_resp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5924 | return err; |
| 5925 | } |
| 5926 | |
| 5927 | static ssize_t smb2_read_rdma_channel(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5928 | struct smb2_read_req *req, void *data_buf, |
| 5929 | size_t length) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5930 | { |
| 5931 | struct smb2_buffer_desc_v1 *desc = |
| 5932 | (struct smb2_buffer_desc_v1 *)&req->Buffer[0]; |
| 5933 | int err; |
| 5934 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5935 | if (work->conn->dialect == SMB30_PROT_ID && |
| 5936 | req->Channel != SMB2_CHANNEL_RDMA_V1) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5937 | return -EINVAL; |
| 5938 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5939 | if (req->ReadChannelInfoOffset == 0 || |
| 5940 | le16_to_cpu(req->ReadChannelInfoLength) < sizeof(*desc)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5941 | return -EINVAL; |
| 5942 | |
| 5943 | work->need_invalidate_rkey = |
| 5944 | (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE); |
| 5945 | work->remote_key = le32_to_cpu(desc->token); |
| 5946 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5947 | err = ksmbd_conn_rdma_write(work->conn, data_buf, length, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5948 | le32_to_cpu(desc->token), |
| 5949 | le64_to_cpu(desc->offset), |
| 5950 | le32_to_cpu(desc->length)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5951 | if (err) |
| 5952 | return err; |
| 5953 | |
| 5954 | return length; |
| 5955 | } |
| 5956 | |
| 5957 | /** |
| 5958 | * smb2_read() - handler for smb2 read from file |
| 5959 | * @work: smb work containing read command buffer |
| 5960 | * |
| 5961 | * Return: 0 on success, otherwise error |
| 5962 | */ |
| 5963 | int smb2_read(struct ksmbd_work *work) |
| 5964 | { |
| 5965 | struct ksmbd_conn *conn = work->conn; |
| 5966 | struct smb2_read_req *req; |
| 5967 | struct smb2_read_rsp *rsp, *rsp_org; |
| 5968 | struct ksmbd_file *fp; |
| 5969 | loff_t offset; |
| 5970 | size_t length, mincount; |
| 5971 | ssize_t nbytes = 0, remain_bytes = 0; |
| 5972 | int err = 0; |
| 5973 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 5974 | rsp_org = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5975 | WORK_BUFFERS(work, req, rsp); |
| 5976 | |
| 5977 | if (test_share_config_flag(work->tcon->share_conf, |
| 5978 | KSMBD_SHARE_FLAG_PIPE)) { |
| 5979 | ksmbd_debug(SMB, "IPC pipe read request\n"); |
| 5980 | return smb2_read_pipe(work); |
| 5981 | } |
| 5982 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5983 | fp = ksmbd_lookup_fd_slow(work, le64_to_cpu(req->VolatileFileId), |
| 5984 | le64_to_cpu(req->PersistentFileId)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5985 | if (!fp) { |
Marios Makassikis | a4382db | 2021-05-06 11:34:52 +0900 | [diff] [blame] | 5986 | err = -ENOENT; |
| 5987 | goto out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5988 | } |
| 5989 | |
| 5990 | if (!(fp->daccess & (FILE_READ_DATA_LE | FILE_READ_ATTRIBUTES_LE))) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5991 | pr_err("Not permitted to read : 0x%x\n", fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5992 | err = -EACCES; |
| 5993 | goto out; |
| 5994 | } |
| 5995 | |
| 5996 | offset = le64_to_cpu(req->Offset); |
| 5997 | length = le32_to_cpu(req->Length); |
| 5998 | mincount = le32_to_cpu(req->MinimumCount); |
| 5999 | |
| 6000 | if (length > conn->vals->max_read_size) { |
| 6001 | ksmbd_debug(SMB, "limiting read size to max size(%u)\n", |
| 6002 | conn->vals->max_read_size); |
| 6003 | err = -EINVAL; |
| 6004 | goto out; |
| 6005 | } |
| 6006 | |
Namjae Jeon | 493fa2f | 2021-06-29 09:22:16 +0900 | [diff] [blame] | 6007 | ksmbd_debug(SMB, "filename %pd, offset %lld, len %zu\n", |
| 6008 | fp->filp->f_path.dentry, offset, length); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6009 | |
Namjae Jeon | c30f4eb | 2021-06-18 10:17:37 +0900 | [diff] [blame] | 6010 | work->aux_payload_buf = kvmalloc(length, GFP_KERNEL | __GFP_ZERO); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6011 | if (!work->aux_payload_buf) { |
Dan Carpenter | c1ea111 | 2021-03-22 17:50:11 +0300 | [diff] [blame] | 6012 | err = -ENOMEM; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6013 | goto out; |
| 6014 | } |
| 6015 | |
| 6016 | nbytes = ksmbd_vfs_read(work, fp, length, &offset); |
| 6017 | if (nbytes < 0) { |
| 6018 | err = nbytes; |
| 6019 | goto out; |
| 6020 | } |
| 6021 | |
| 6022 | if ((nbytes == 0 && length != 0) || nbytes < mincount) { |
Namjae Jeon | c30f4eb | 2021-06-18 10:17:37 +0900 | [diff] [blame] | 6023 | kvfree(work->aux_payload_buf); |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 6024 | work->aux_payload_buf = NULL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6025 | rsp->hdr.Status = STATUS_END_OF_FILE; |
| 6026 | smb2_set_err_rsp(work); |
| 6027 | ksmbd_fd_put(work, fp); |
| 6028 | return 0; |
| 6029 | } |
| 6030 | |
| 6031 | ksmbd_debug(SMB, "nbytes %zu, offset %lld mincount %zu\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6032 | nbytes, offset, mincount); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6033 | |
| 6034 | if (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6035 | req->Channel == SMB2_CHANNEL_RDMA_V1) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6036 | /* write data to the client using rdma channel */ |
| 6037 | remain_bytes = smb2_read_rdma_channel(work, req, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6038 | work->aux_payload_buf, |
| 6039 | nbytes); |
Namjae Jeon | c30f4eb | 2021-06-18 10:17:37 +0900 | [diff] [blame] | 6040 | kvfree(work->aux_payload_buf); |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 6041 | work->aux_payload_buf = NULL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6042 | |
| 6043 | nbytes = 0; |
| 6044 | if (remain_bytes < 0) { |
| 6045 | err = (int)remain_bytes; |
| 6046 | goto out; |
| 6047 | } |
| 6048 | } |
| 6049 | |
| 6050 | rsp->StructureSize = cpu_to_le16(17); |
| 6051 | rsp->DataOffset = 80; |
| 6052 | rsp->Reserved = 0; |
| 6053 | rsp->DataLength = cpu_to_le32(nbytes); |
| 6054 | rsp->DataRemaining = cpu_to_le32(remain_bytes); |
| 6055 | rsp->Reserved2 = 0; |
| 6056 | inc_rfc1001_len(rsp_org, 16); |
| 6057 | work->resp_hdr_sz = get_rfc1002_len(rsp_org) + 4; |
| 6058 | work->aux_payload_sz = nbytes; |
| 6059 | inc_rfc1001_len(rsp_org, nbytes); |
| 6060 | ksmbd_fd_put(work, fp); |
| 6061 | return 0; |
| 6062 | |
| 6063 | out: |
| 6064 | if (err) { |
| 6065 | if (err == -EISDIR) |
| 6066 | rsp->hdr.Status = STATUS_INVALID_DEVICE_REQUEST; |
| 6067 | else if (err == -EAGAIN) |
| 6068 | rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT; |
| 6069 | else if (err == -ENOENT) |
| 6070 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 6071 | else if (err == -EACCES) |
| 6072 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 6073 | else if (err == -ESHARE) |
| 6074 | rsp->hdr.Status = STATUS_SHARING_VIOLATION; |
| 6075 | else if (err == -EINVAL) |
| 6076 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 6077 | else |
| 6078 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 6079 | |
| 6080 | smb2_set_err_rsp(work); |
| 6081 | } |
| 6082 | ksmbd_fd_put(work, fp); |
| 6083 | return err; |
| 6084 | } |
| 6085 | |
| 6086 | /** |
| 6087 | * smb2_write_pipe() - handler for smb2 write on IPC pipe |
| 6088 | * @work: smb work containing write IPC pipe command buffer |
| 6089 | * |
| 6090 | * Return: 0 on success, otherwise error |
| 6091 | */ |
| 6092 | static noinline int smb2_write_pipe(struct ksmbd_work *work) |
| 6093 | { |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 6094 | struct smb2_write_req *req = work->request_buf; |
| 6095 | struct smb2_write_rsp *rsp = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6096 | struct ksmbd_rpc_command *rpc_resp; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6097 | u64 id = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6098 | int err = 0, ret = 0; |
| 6099 | char *data_buf; |
| 6100 | size_t length; |
| 6101 | |
| 6102 | length = le32_to_cpu(req->Length); |
| 6103 | id = le64_to_cpu(req->VolatileFileId); |
| 6104 | |
| 6105 | if (le16_to_cpu(req->DataOffset) == |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6106 | (offsetof(struct smb2_write_req, Buffer) - 4)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6107 | data_buf = (char *)&req->Buffer[0]; |
| 6108 | } else { |
| 6109 | if ((le16_to_cpu(req->DataOffset) > get_rfc1002_len(req)) || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6110 | (le16_to_cpu(req->DataOffset) + length > get_rfc1002_len(req))) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 6111 | pr_err("invalid write data offset %u, smb_len %u\n", |
| 6112 | le16_to_cpu(req->DataOffset), |
| 6113 | get_rfc1002_len(req)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6114 | err = -EINVAL; |
| 6115 | goto out; |
| 6116 | } |
| 6117 | |
| 6118 | data_buf = (char *)(((char *)&req->hdr.ProtocolId) + |
| 6119 | le16_to_cpu(req->DataOffset)); |
| 6120 | } |
| 6121 | |
| 6122 | rpc_resp = ksmbd_rpc_write(work->sess, id, data_buf, length); |
| 6123 | if (rpc_resp) { |
| 6124 | if (rpc_resp->flags == KSMBD_RPC_ENOTIMPLEMENTED) { |
| 6125 | rsp->hdr.Status = STATUS_NOT_SUPPORTED; |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6126 | kvfree(rpc_resp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6127 | smb2_set_err_rsp(work); |
| 6128 | return -EOPNOTSUPP; |
| 6129 | } |
| 6130 | if (rpc_resp->flags != KSMBD_RPC_OK) { |
| 6131 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 6132 | smb2_set_err_rsp(work); |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6133 | kvfree(rpc_resp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6134 | return ret; |
| 6135 | } |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6136 | kvfree(rpc_resp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6137 | } |
| 6138 | |
| 6139 | rsp->StructureSize = cpu_to_le16(17); |
| 6140 | rsp->DataOffset = 0; |
| 6141 | rsp->Reserved = 0; |
| 6142 | rsp->DataLength = cpu_to_le32(length); |
| 6143 | rsp->DataRemaining = 0; |
| 6144 | rsp->Reserved2 = 0; |
| 6145 | inc_rfc1001_len(rsp, 16); |
| 6146 | return 0; |
| 6147 | out: |
| 6148 | if (err) { |
| 6149 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 6150 | smb2_set_err_rsp(work); |
| 6151 | } |
| 6152 | |
| 6153 | return err; |
| 6154 | } |
| 6155 | |
| 6156 | static ssize_t smb2_write_rdma_channel(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6157 | struct smb2_write_req *req, |
| 6158 | struct ksmbd_file *fp, |
| 6159 | loff_t offset, size_t length, bool sync) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6160 | { |
| 6161 | struct smb2_buffer_desc_v1 *desc; |
| 6162 | char *data_buf; |
| 6163 | int ret; |
| 6164 | ssize_t nbytes; |
| 6165 | |
| 6166 | desc = (struct smb2_buffer_desc_v1 *)&req->Buffer[0]; |
| 6167 | |
| 6168 | if (work->conn->dialect == SMB30_PROT_ID && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6169 | req->Channel != SMB2_CHANNEL_RDMA_V1) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6170 | return -EINVAL; |
| 6171 | |
| 6172 | if (req->Length != 0 || req->DataOffset != 0) |
| 6173 | return -EINVAL; |
| 6174 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6175 | if (req->WriteChannelInfoOffset == 0 || |
| 6176 | le16_to_cpu(req->WriteChannelInfoLength) < sizeof(*desc)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6177 | return -EINVAL; |
| 6178 | |
| 6179 | work->need_invalidate_rkey = |
| 6180 | (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE); |
| 6181 | work->remote_key = le32_to_cpu(desc->token); |
| 6182 | |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6183 | data_buf = kvmalloc(length, GFP_KERNEL | __GFP_ZERO); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6184 | if (!data_buf) |
| 6185 | return -ENOMEM; |
| 6186 | |
| 6187 | ret = ksmbd_conn_rdma_read(work->conn, data_buf, length, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6188 | le32_to_cpu(desc->token), |
| 6189 | le64_to_cpu(desc->offset), |
| 6190 | le32_to_cpu(desc->length)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6191 | if (ret < 0) { |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6192 | kvfree(data_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6193 | return ret; |
| 6194 | } |
| 6195 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6196 | ret = ksmbd_vfs_write(work, fp, data_buf, length, &offset, sync, &nbytes); |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6197 | kvfree(data_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6198 | if (ret < 0) |
| 6199 | return ret; |
| 6200 | |
| 6201 | return nbytes; |
| 6202 | } |
| 6203 | |
| 6204 | /** |
| 6205 | * smb2_write() - handler for smb2 write from file |
| 6206 | * @work: smb work containing write command buffer |
| 6207 | * |
| 6208 | * Return: 0 on success, otherwise error |
| 6209 | */ |
| 6210 | int smb2_write(struct ksmbd_work *work) |
| 6211 | { |
| 6212 | struct smb2_write_req *req; |
| 6213 | struct smb2_write_rsp *rsp, *rsp_org; |
Namjae Jeon | bcd62a3 | 2021-05-10 09:08:19 +0900 | [diff] [blame] | 6214 | struct ksmbd_file *fp = NULL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6215 | loff_t offset; |
| 6216 | size_t length; |
| 6217 | ssize_t nbytes; |
| 6218 | char *data_buf; |
| 6219 | bool writethrough = false; |
| 6220 | int err = 0; |
| 6221 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 6222 | rsp_org = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6223 | WORK_BUFFERS(work, req, rsp); |
| 6224 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6225 | 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] | 6226 | ksmbd_debug(SMB, "IPC pipe write request\n"); |
| 6227 | return smb2_write_pipe(work); |
| 6228 | } |
| 6229 | |
| 6230 | if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
| 6231 | ksmbd_debug(SMB, "User does not have write permission\n"); |
| 6232 | err = -EACCES; |
| 6233 | goto out; |
| 6234 | } |
| 6235 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6236 | fp = ksmbd_lookup_fd_slow(work, le64_to_cpu(req->VolatileFileId), |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6237 | le64_to_cpu(req->PersistentFileId)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6238 | if (!fp) { |
Marios Makassikis | a4382db | 2021-05-06 11:34:52 +0900 | [diff] [blame] | 6239 | err = -ENOENT; |
| 6240 | goto out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6241 | } |
| 6242 | |
| 6243 | if (!(fp->daccess & (FILE_WRITE_DATA_LE | FILE_READ_ATTRIBUTES_LE))) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 6244 | pr_err("Not permitted to write : 0x%x\n", fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6245 | err = -EACCES; |
| 6246 | goto out; |
| 6247 | } |
| 6248 | |
| 6249 | offset = le64_to_cpu(req->Offset); |
| 6250 | length = le32_to_cpu(req->Length); |
| 6251 | |
| 6252 | if (length > work->conn->vals->max_write_size) { |
| 6253 | ksmbd_debug(SMB, "limiting write size to max size(%u)\n", |
| 6254 | work->conn->vals->max_write_size); |
| 6255 | err = -EINVAL; |
| 6256 | goto out; |
| 6257 | } |
| 6258 | |
| 6259 | if (le32_to_cpu(req->Flags) & SMB2_WRITEFLAG_WRITE_THROUGH) |
| 6260 | writethrough = true; |
| 6261 | |
| 6262 | if (req->Channel != SMB2_CHANNEL_RDMA_V1 && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6263 | req->Channel != SMB2_CHANNEL_RDMA_V1_INVALIDATE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6264 | if (le16_to_cpu(req->DataOffset) == |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6265 | (offsetof(struct smb2_write_req, Buffer) - 4)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6266 | data_buf = (char *)&req->Buffer[0]; |
| 6267 | } else { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6268 | if ((le16_to_cpu(req->DataOffset) > get_rfc1002_len(req)) || |
| 6269 | (le16_to_cpu(req->DataOffset) + length > get_rfc1002_len(req))) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 6270 | pr_err("invalid write data offset %u, smb_len %u\n", |
| 6271 | le16_to_cpu(req->DataOffset), |
| 6272 | get_rfc1002_len(req)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6273 | err = -EINVAL; |
| 6274 | goto out; |
| 6275 | } |
| 6276 | |
| 6277 | data_buf = (char *)(((char *)&req->hdr.ProtocolId) + |
| 6278 | le16_to_cpu(req->DataOffset)); |
| 6279 | } |
| 6280 | |
| 6281 | ksmbd_debug(SMB, "flags %u\n", le32_to_cpu(req->Flags)); |
| 6282 | if (le32_to_cpu(req->Flags) & SMB2_WRITEFLAG_WRITE_THROUGH) |
| 6283 | writethrough = true; |
| 6284 | |
Namjae Jeon | 493fa2f | 2021-06-29 09:22:16 +0900 | [diff] [blame] | 6285 | ksmbd_debug(SMB, "filename %pd, offset %lld, len %zu\n", |
| 6286 | fp->filp->f_path.dentry, offset, length); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6287 | err = ksmbd_vfs_write(work, fp, data_buf, length, &offset, |
| 6288 | writethrough, &nbytes); |
| 6289 | if (err < 0) |
| 6290 | goto out; |
| 6291 | } else { |
| 6292 | /* read data from the client using rdma channel, and |
| 6293 | * write the data. |
| 6294 | */ |
| 6295 | nbytes = smb2_write_rdma_channel(work, req, fp, offset, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6296 | le32_to_cpu(req->RemainingBytes), |
| 6297 | writethrough); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6298 | if (nbytes < 0) { |
| 6299 | err = (int)nbytes; |
| 6300 | goto out; |
| 6301 | } |
| 6302 | } |
| 6303 | |
| 6304 | rsp->StructureSize = cpu_to_le16(17); |
| 6305 | rsp->DataOffset = 0; |
| 6306 | rsp->Reserved = 0; |
| 6307 | rsp->DataLength = cpu_to_le32(nbytes); |
| 6308 | rsp->DataRemaining = 0; |
| 6309 | rsp->Reserved2 = 0; |
| 6310 | inc_rfc1001_len(rsp_org, 16); |
| 6311 | ksmbd_fd_put(work, fp); |
| 6312 | return 0; |
| 6313 | |
| 6314 | out: |
| 6315 | if (err == -EAGAIN) |
| 6316 | rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT; |
| 6317 | else if (err == -ENOSPC || err == -EFBIG) |
| 6318 | rsp->hdr.Status = STATUS_DISK_FULL; |
| 6319 | else if (err == -ENOENT) |
| 6320 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 6321 | else if (err == -EACCES) |
| 6322 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 6323 | else if (err == -ESHARE) |
| 6324 | rsp->hdr.Status = STATUS_SHARING_VIOLATION; |
| 6325 | else if (err == -EINVAL) |
| 6326 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 6327 | else |
| 6328 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 6329 | |
| 6330 | smb2_set_err_rsp(work); |
| 6331 | ksmbd_fd_put(work, fp); |
| 6332 | return err; |
| 6333 | } |
| 6334 | |
| 6335 | /** |
| 6336 | * smb2_flush() - handler for smb2 flush file - fsync |
| 6337 | * @work: smb work containing flush command buffer |
| 6338 | * |
| 6339 | * Return: 0 on success, otherwise error |
| 6340 | */ |
| 6341 | int smb2_flush(struct ksmbd_work *work) |
| 6342 | { |
| 6343 | struct smb2_flush_req *req; |
| 6344 | struct smb2_flush_rsp *rsp, *rsp_org; |
| 6345 | int err; |
| 6346 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 6347 | rsp_org = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6348 | WORK_BUFFERS(work, req, rsp); |
| 6349 | |
| 6350 | ksmbd_debug(SMB, "SMB2_FLUSH called for fid %llu\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6351 | le64_to_cpu(req->VolatileFileId)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6352 | |
| 6353 | err = ksmbd_vfs_fsync(work, |
| 6354 | le64_to_cpu(req->VolatileFileId), |
| 6355 | le64_to_cpu(req->PersistentFileId)); |
| 6356 | if (err) |
| 6357 | goto out; |
| 6358 | |
| 6359 | rsp->StructureSize = cpu_to_le16(4); |
| 6360 | rsp->Reserved = 0; |
| 6361 | inc_rfc1001_len(rsp_org, 4); |
| 6362 | return 0; |
| 6363 | |
| 6364 | out: |
| 6365 | if (err) { |
| 6366 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 6367 | smb2_set_err_rsp(work); |
| 6368 | } |
| 6369 | |
| 6370 | return err; |
| 6371 | } |
| 6372 | |
| 6373 | /** |
| 6374 | * smb2_cancel() - handler for smb2 cancel command |
| 6375 | * @work: smb work containing cancel command buffer |
| 6376 | * |
| 6377 | * Return: 0 on success, otherwise error |
| 6378 | */ |
| 6379 | int smb2_cancel(struct ksmbd_work *work) |
| 6380 | { |
| 6381 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 6382 | struct smb2_hdr *hdr = work->request_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6383 | struct smb2_hdr *chdr; |
| 6384 | struct ksmbd_work *cancel_work = NULL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6385 | int canceled = 0; |
| 6386 | struct list_head *command_list; |
| 6387 | |
| 6388 | 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] | 6389 | hdr->MessageId, hdr->Flags); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6390 | |
| 6391 | if (hdr->Flags & SMB2_FLAGS_ASYNC_COMMAND) { |
| 6392 | command_list = &conn->async_requests; |
| 6393 | |
| 6394 | spin_lock(&conn->request_lock); |
Namjae Jeon | 6f3d5ee | 2021-06-18 10:28:52 +0900 | [diff] [blame] | 6395 | list_for_each_entry(cancel_work, command_list, |
| 6396 | async_request_entry) { |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 6397 | chdr = cancel_work->request_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6398 | |
| 6399 | if (cancel_work->async_id != |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6400 | le64_to_cpu(hdr->Id.AsyncId)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6401 | continue; |
| 6402 | |
| 6403 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6404 | "smb2 with AsyncId %llu cancelled command = 0x%x\n", |
| 6405 | le64_to_cpu(hdr->Id.AsyncId), |
| 6406 | le16_to_cpu(chdr->Command)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6407 | canceled = 1; |
| 6408 | break; |
| 6409 | } |
| 6410 | spin_unlock(&conn->request_lock); |
| 6411 | } else { |
| 6412 | command_list = &conn->requests; |
| 6413 | |
| 6414 | spin_lock(&conn->request_lock); |
Namjae Jeon | 6f3d5ee | 2021-06-18 10:28:52 +0900 | [diff] [blame] | 6415 | list_for_each_entry(cancel_work, command_list, request_entry) { |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 6416 | chdr = cancel_work->request_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6417 | |
| 6418 | if (chdr->MessageId != hdr->MessageId || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6419 | cancel_work == work) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6420 | continue; |
| 6421 | |
| 6422 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6423 | "smb2 with mid %llu cancelled command = 0x%x\n", |
| 6424 | le64_to_cpu(hdr->MessageId), |
| 6425 | le16_to_cpu(chdr->Command)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6426 | canceled = 1; |
| 6427 | break; |
| 6428 | } |
| 6429 | spin_unlock(&conn->request_lock); |
| 6430 | } |
| 6431 | |
| 6432 | if (canceled) { |
| 6433 | cancel_work->state = KSMBD_WORK_CANCELLED; |
| 6434 | if (cancel_work->cancel_fn) |
| 6435 | cancel_work->cancel_fn(cancel_work->cancel_argv); |
| 6436 | } |
| 6437 | |
| 6438 | /* For SMB2_CANCEL command itself send no response*/ |
| 6439 | work->send_no_response = 1; |
| 6440 | return 0; |
| 6441 | } |
| 6442 | |
| 6443 | struct file_lock *smb_flock_init(struct file *f) |
| 6444 | { |
| 6445 | struct file_lock *fl; |
| 6446 | |
| 6447 | fl = locks_alloc_lock(); |
| 6448 | if (!fl) |
| 6449 | goto out; |
| 6450 | |
| 6451 | locks_init_lock(fl); |
| 6452 | |
| 6453 | fl->fl_owner = f; |
| 6454 | fl->fl_pid = current->tgid; |
| 6455 | fl->fl_file = f; |
| 6456 | fl->fl_flags = FL_POSIX; |
| 6457 | fl->fl_ops = NULL; |
| 6458 | fl->fl_lmops = NULL; |
| 6459 | |
| 6460 | out: |
| 6461 | return fl; |
| 6462 | } |
| 6463 | |
| 6464 | static int smb2_set_flock_flags(struct file_lock *flock, int flags) |
| 6465 | { |
| 6466 | int cmd = -EINVAL; |
| 6467 | |
| 6468 | /* Checking for wrong flag combination during lock request*/ |
| 6469 | switch (flags) { |
| 6470 | case SMB2_LOCKFLAG_SHARED: |
| 6471 | ksmbd_debug(SMB, "received shared request\n"); |
| 6472 | cmd = F_SETLKW; |
| 6473 | flock->fl_type = F_RDLCK; |
| 6474 | flock->fl_flags |= FL_SLEEP; |
| 6475 | break; |
| 6476 | case SMB2_LOCKFLAG_EXCLUSIVE: |
| 6477 | ksmbd_debug(SMB, "received exclusive request\n"); |
| 6478 | cmd = F_SETLKW; |
| 6479 | flock->fl_type = F_WRLCK; |
| 6480 | flock->fl_flags |= FL_SLEEP; |
| 6481 | break; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6482 | case SMB2_LOCKFLAG_SHARED | SMB2_LOCKFLAG_FAIL_IMMEDIATELY: |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6483 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6484 | "received shared & fail immediately request\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6485 | cmd = F_SETLK; |
| 6486 | flock->fl_type = F_RDLCK; |
| 6487 | break; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6488 | case SMB2_LOCKFLAG_EXCLUSIVE | SMB2_LOCKFLAG_FAIL_IMMEDIATELY: |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6489 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6490 | "received exclusive & fail immediately request\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6491 | cmd = F_SETLK; |
| 6492 | flock->fl_type = F_WRLCK; |
| 6493 | break; |
| 6494 | case SMB2_LOCKFLAG_UNLOCK: |
| 6495 | ksmbd_debug(SMB, "received unlock request\n"); |
| 6496 | flock->fl_type = F_UNLCK; |
| 6497 | cmd = 0; |
| 6498 | break; |
| 6499 | } |
| 6500 | |
| 6501 | return cmd; |
| 6502 | } |
| 6503 | |
| 6504 | static struct ksmbd_lock *smb2_lock_init(struct file_lock *flock, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6505 | unsigned int cmd, int flags, |
| 6506 | struct list_head *lock_list) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6507 | { |
| 6508 | struct ksmbd_lock *lock; |
| 6509 | |
| 6510 | lock = kzalloc(sizeof(struct ksmbd_lock), GFP_KERNEL); |
| 6511 | if (!lock) |
| 6512 | return NULL; |
| 6513 | |
| 6514 | lock->cmd = cmd; |
| 6515 | lock->fl = flock; |
| 6516 | lock->start = flock->fl_start; |
| 6517 | lock->end = flock->fl_end; |
| 6518 | lock->flags = flags; |
| 6519 | if (lock->start == lock->end) |
| 6520 | lock->zero_len = 1; |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6521 | INIT_LIST_HEAD(&lock->clist); |
| 6522 | INIT_LIST_HEAD(&lock->flist); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6523 | INIT_LIST_HEAD(&lock->llist); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6524 | list_add_tail(&lock->llist, lock_list); |
| 6525 | |
| 6526 | return lock; |
| 6527 | } |
| 6528 | |
| 6529 | static void smb2_remove_blocked_lock(void **argv) |
| 6530 | { |
| 6531 | struct file_lock *flock = (struct file_lock *)argv[0]; |
| 6532 | |
| 6533 | ksmbd_vfs_posix_lock_unblock(flock); |
| 6534 | wake_up(&flock->fl_wait); |
| 6535 | } |
| 6536 | |
| 6537 | static inline bool lock_defer_pending(struct file_lock *fl) |
| 6538 | { |
| 6539 | /* check pending lock waiters */ |
| 6540 | return waitqueue_active(&fl->fl_wait); |
| 6541 | } |
| 6542 | |
| 6543 | /** |
| 6544 | * smb2_lock() - handler for smb2 file lock command |
| 6545 | * @work: smb work containing lock command buffer |
| 6546 | * |
| 6547 | * Return: 0 on success, otherwise error |
| 6548 | */ |
| 6549 | int smb2_lock(struct ksmbd_work *work) |
| 6550 | { |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 6551 | struct smb2_lock_req *req = work->request_buf; |
| 6552 | struct smb2_lock_rsp *rsp = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6553 | struct smb2_lock_element *lock_ele; |
| 6554 | struct ksmbd_file *fp = NULL; |
| 6555 | struct file_lock *flock = NULL; |
| 6556 | struct file *filp = NULL; |
| 6557 | int lock_count; |
| 6558 | int flags = 0; |
| 6559 | int cmd = 0; |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6560 | int err = -EIO, i, rc = 0; |
Namjae Jeon | 50bf80a | 2021-05-14 12:20:07 +0900 | [diff] [blame] | 6561 | u64 lock_start, lock_length; |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6562 | struct ksmbd_lock *smb_lock = NULL, *cmp_lock, *tmp, *tmp2; |
| 6563 | struct ksmbd_conn *conn; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6564 | int nolock = 0; |
| 6565 | LIST_HEAD(lock_list); |
| 6566 | LIST_HEAD(rollback_list); |
| 6567 | int prior_lock = 0; |
| 6568 | |
| 6569 | ksmbd_debug(SMB, "Received lock request\n"); |
| 6570 | fp = ksmbd_lookup_fd_slow(work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6571 | le64_to_cpu(req->VolatileFileId), |
| 6572 | le64_to_cpu(req->PersistentFileId)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6573 | if (!fp) { |
| 6574 | ksmbd_debug(SMB, "Invalid file id for lock : %llu\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6575 | le64_to_cpu(req->VolatileFileId)); |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6576 | err = -ENOENT; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6577 | goto out2; |
| 6578 | } |
| 6579 | |
| 6580 | filp = fp->filp; |
| 6581 | lock_count = le16_to_cpu(req->LockCount); |
| 6582 | lock_ele = req->locks; |
| 6583 | |
| 6584 | ksmbd_debug(SMB, "lock count is %d\n", lock_count); |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6585 | if (!lock_count) { |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6586 | err = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6587 | goto out2; |
| 6588 | } |
| 6589 | |
| 6590 | for (i = 0; i < lock_count; i++) { |
| 6591 | flags = le32_to_cpu(lock_ele[i].Flags); |
| 6592 | |
| 6593 | flock = smb_flock_init(filp); |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6594 | if (!flock) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6595 | goto out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6596 | |
| 6597 | cmd = smb2_set_flock_flags(flock, flags); |
| 6598 | |
Namjae Jeon | 50bf80a | 2021-05-14 12:20:07 +0900 | [diff] [blame] | 6599 | lock_start = le64_to_cpu(lock_ele[i].Offset); |
| 6600 | lock_length = le64_to_cpu(lock_ele[i].Length); |
| 6601 | if (lock_start > U64_MAX - lock_length) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 6602 | pr_err("Invalid lock range requested\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6603 | rsp->hdr.Status = STATUS_INVALID_LOCK_RANGE; |
| 6604 | goto out; |
| 6605 | } |
| 6606 | |
Namjae Jeon | 50bf80a | 2021-05-14 12:20:07 +0900 | [diff] [blame] | 6607 | if (lock_start > OFFSET_MAX) |
| 6608 | flock->fl_start = OFFSET_MAX; |
| 6609 | else |
| 6610 | flock->fl_start = lock_start; |
| 6611 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6612 | lock_length = le64_to_cpu(lock_ele[i].Length); |
Namjae Jeon | 50bf80a | 2021-05-14 12:20:07 +0900 | [diff] [blame] | 6613 | if (lock_length > OFFSET_MAX - flock->fl_start) |
| 6614 | lock_length = OFFSET_MAX - flock->fl_start; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6615 | |
| 6616 | flock->fl_end = flock->fl_start + lock_length; |
| 6617 | |
| 6618 | if (flock->fl_end < flock->fl_start) { |
| 6619 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6620 | "the end offset(%llx) is smaller than the start offset(%llx)\n", |
| 6621 | flock->fl_end, flock->fl_start); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6622 | rsp->hdr.Status = STATUS_INVALID_LOCK_RANGE; |
| 6623 | goto out; |
| 6624 | } |
| 6625 | |
| 6626 | /* Check conflict locks in one request */ |
| 6627 | list_for_each_entry(cmp_lock, &lock_list, llist) { |
| 6628 | if (cmp_lock->fl->fl_start <= flock->fl_start && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6629 | cmp_lock->fl->fl_end >= flock->fl_end) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6630 | if (cmp_lock->fl->fl_type != F_UNLCK && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6631 | flock->fl_type != F_UNLCK) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 6632 | pr_err("conflict two locks in one request\n"); |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6633 | err = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6634 | goto out; |
| 6635 | } |
| 6636 | } |
| 6637 | } |
| 6638 | |
| 6639 | smb_lock = smb2_lock_init(flock, cmd, flags, &lock_list); |
| 6640 | if (!smb_lock) { |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6641 | err = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6642 | goto out; |
| 6643 | } |
| 6644 | } |
| 6645 | |
| 6646 | list_for_each_entry_safe(smb_lock, tmp, &lock_list, llist) { |
| 6647 | if (smb_lock->cmd < 0) { |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6648 | err = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6649 | goto out; |
| 6650 | } |
| 6651 | |
| 6652 | if (!(smb_lock->flags & SMB2_LOCKFLAG_MASK)) { |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6653 | err = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6654 | goto out; |
| 6655 | } |
| 6656 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6657 | if ((prior_lock & (SMB2_LOCKFLAG_EXCLUSIVE | SMB2_LOCKFLAG_SHARED) && |
| 6658 | smb_lock->flags & SMB2_LOCKFLAG_UNLOCK) || |
| 6659 | (prior_lock == SMB2_LOCKFLAG_UNLOCK && |
| 6660 | !(smb_lock->flags & SMB2_LOCKFLAG_UNLOCK))) { |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6661 | err = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6662 | goto out; |
| 6663 | } |
| 6664 | |
| 6665 | prior_lock = smb_lock->flags; |
| 6666 | |
| 6667 | if (!(smb_lock->flags & SMB2_LOCKFLAG_UNLOCK) && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6668 | !(smb_lock->flags & SMB2_LOCKFLAG_FAIL_IMMEDIATELY)) |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6669 | goto no_check_cl; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6670 | |
| 6671 | nolock = 1; |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6672 | /* check locks in connection list */ |
| 6673 | read_lock(&conn_list_lock); |
| 6674 | list_for_each_entry(conn, &conn_list, conns_list) { |
| 6675 | spin_lock(&conn->llist_lock); |
| 6676 | list_for_each_entry_safe(cmp_lock, tmp2, &conn->lock_list, clist) { |
| 6677 | if (file_inode(cmp_lock->fl->fl_file) != |
| 6678 | file_inode(smb_lock->fl->fl_file)) |
| 6679 | continue; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6680 | |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6681 | if (smb_lock->fl->fl_type == F_UNLCK) { |
| 6682 | if (cmp_lock->fl->fl_file == smb_lock->fl->fl_file && |
| 6683 | cmp_lock->start == smb_lock->start && |
| 6684 | cmp_lock->end == smb_lock->end && |
| 6685 | !lock_defer_pending(cmp_lock->fl)) { |
| 6686 | nolock = 0; |
| 6687 | list_del(&cmp_lock->flist); |
| 6688 | list_del(&cmp_lock->clist); |
| 6689 | spin_unlock(&conn->llist_lock); |
| 6690 | read_unlock(&conn_list_lock); |
| 6691 | |
| 6692 | locks_free_lock(cmp_lock->fl); |
| 6693 | kfree(cmp_lock); |
| 6694 | goto out_check_cl; |
| 6695 | } |
| 6696 | continue; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6697 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6698 | |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6699 | if (cmp_lock->fl->fl_file == smb_lock->fl->fl_file) { |
| 6700 | if (smb_lock->flags & SMB2_LOCKFLAG_SHARED) |
| 6701 | continue; |
| 6702 | } else { |
| 6703 | if (cmp_lock->flags & SMB2_LOCKFLAG_SHARED) |
| 6704 | continue; |
| 6705 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6706 | |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6707 | /* check zero byte lock range */ |
| 6708 | if (cmp_lock->zero_len && !smb_lock->zero_len && |
| 6709 | cmp_lock->start > smb_lock->start && |
| 6710 | cmp_lock->start < smb_lock->end) { |
| 6711 | spin_unlock(&conn->llist_lock); |
| 6712 | read_unlock(&conn_list_lock); |
| 6713 | pr_err("previous lock conflict with zero byte lock range\n"); |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6714 | goto out; |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6715 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6716 | |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6717 | if (smb_lock->zero_len && !cmp_lock->zero_len && |
| 6718 | smb_lock->start > cmp_lock->start && |
| 6719 | smb_lock->start < cmp_lock->end) { |
| 6720 | spin_unlock(&conn->llist_lock); |
| 6721 | read_unlock(&conn_list_lock); |
| 6722 | pr_err("current lock conflict with zero byte lock range\n"); |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6723 | goto out; |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6724 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6725 | |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6726 | if (((cmp_lock->start <= smb_lock->start && |
| 6727 | cmp_lock->end > smb_lock->start) || |
| 6728 | (cmp_lock->start < smb_lock->end && |
| 6729 | cmp_lock->end >= smb_lock->end)) && |
| 6730 | !cmp_lock->zero_len && !smb_lock->zero_len) { |
| 6731 | spin_unlock(&conn->llist_lock); |
| 6732 | read_unlock(&conn_list_lock); |
| 6733 | pr_err("Not allow lock operation on exclusive lock range\n"); |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6734 | goto out; |
| 6735 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6736 | } |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6737 | spin_unlock(&conn->llist_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6738 | } |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6739 | read_unlock(&conn_list_lock); |
| 6740 | out_check_cl: |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6741 | if (smb_lock->fl->fl_type == F_UNLCK && nolock) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 6742 | pr_err("Try to unlock nolocked range\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6743 | rsp->hdr.Status = STATUS_RANGE_NOT_LOCKED; |
| 6744 | goto out; |
| 6745 | } |
| 6746 | |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6747 | no_check_cl: |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6748 | if (smb_lock->zero_len) { |
| 6749 | err = 0; |
| 6750 | goto skip; |
| 6751 | } |
| 6752 | |
| 6753 | flock = smb_lock->fl; |
| 6754 | list_del(&smb_lock->llist); |
| 6755 | retry: |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6756 | rc = vfs_lock_file(filp, smb_lock->cmd, flock, NULL); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6757 | skip: |
| 6758 | if (flags & SMB2_LOCKFLAG_UNLOCK) { |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6759 | if (!rc) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6760 | ksmbd_debug(SMB, "File unlocked\n"); |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6761 | } else if (rc == -ENOENT) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6762 | rsp->hdr.Status = STATUS_NOT_LOCKED; |
| 6763 | goto out; |
| 6764 | } |
| 6765 | locks_free_lock(flock); |
| 6766 | kfree(smb_lock); |
| 6767 | } else { |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6768 | if (rc == FILE_LOCK_DEFERRED) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6769 | void **argv; |
| 6770 | |
| 6771 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6772 | "would have to wait for getting lock\n"); |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6773 | spin_lock(&work->conn->llist_lock); |
| 6774 | list_add_tail(&smb_lock->clist, |
| 6775 | &work->conn->lock_list); |
| 6776 | spin_unlock(&work->conn->llist_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6777 | list_add(&smb_lock->llist, &rollback_list); |
| 6778 | |
| 6779 | argv = kmalloc(sizeof(void *), GFP_KERNEL); |
| 6780 | if (!argv) { |
| 6781 | err = -ENOMEM; |
| 6782 | goto out; |
| 6783 | } |
| 6784 | argv[0] = flock; |
| 6785 | |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6786 | rc = setup_async_work(work, |
| 6787 | smb2_remove_blocked_lock, |
| 6788 | argv); |
| 6789 | if (rc) { |
| 6790 | err = -ENOMEM; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6791 | goto out; |
| 6792 | } |
| 6793 | spin_lock(&fp->f_lock); |
| 6794 | list_add(&work->fp_entry, &fp->blocked_works); |
| 6795 | spin_unlock(&fp->f_lock); |
| 6796 | |
| 6797 | smb2_send_interim_resp(work, STATUS_PENDING); |
| 6798 | |
Hyunchul Lee | 45a64e8 | 2021-07-10 09:34:20 +0900 | [diff] [blame] | 6799 | ksmbd_vfs_posix_lock_wait(flock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6800 | |
Hyunchul Lee | d4075ab | 2021-06-25 07:02:10 +0900 | [diff] [blame] | 6801 | if (work->state != KSMBD_WORK_ACTIVE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6802 | list_del(&smb_lock->llist); |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6803 | spin_lock(&work->conn->llist_lock); |
| 6804 | list_del(&smb_lock->clist); |
| 6805 | spin_unlock(&work->conn->llist_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6806 | locks_free_lock(flock); |
| 6807 | |
Hyunchul Lee | d4075ab | 2021-06-25 07:02:10 +0900 | [diff] [blame] | 6808 | if (work->state == KSMBD_WORK_CANCELLED) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6809 | spin_lock(&fp->f_lock); |
| 6810 | list_del(&work->fp_entry); |
| 6811 | spin_unlock(&fp->f_lock); |
| 6812 | rsp->hdr.Status = |
| 6813 | STATUS_CANCELLED; |
| 6814 | kfree(smb_lock); |
| 6815 | smb2_send_interim_resp(work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6816 | STATUS_CANCELLED); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6817 | work->send_no_response = 1; |
| 6818 | goto out; |
| 6819 | } |
| 6820 | init_smb2_rsp_hdr(work); |
| 6821 | smb2_set_err_rsp(work); |
| 6822 | rsp->hdr.Status = |
| 6823 | STATUS_RANGE_NOT_LOCKED; |
| 6824 | kfree(smb_lock); |
| 6825 | goto out2; |
| 6826 | } |
| 6827 | |
| 6828 | list_del(&smb_lock->llist); |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6829 | spin_lock(&work->conn->llist_lock); |
| 6830 | list_del(&smb_lock->clist); |
| 6831 | spin_unlock(&work->conn->llist_lock); |
| 6832 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6833 | spin_lock(&fp->f_lock); |
| 6834 | list_del(&work->fp_entry); |
| 6835 | spin_unlock(&fp->f_lock); |
| 6836 | goto retry; |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6837 | } else if (!rc) { |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6838 | spin_lock(&work->conn->llist_lock); |
| 6839 | list_add_tail(&smb_lock->clist, |
| 6840 | &work->conn->lock_list); |
| 6841 | list_add_tail(&smb_lock->flist, |
| 6842 | &fp->lock_list); |
| 6843 | spin_unlock(&work->conn->llist_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6844 | list_add(&smb_lock->llist, &rollback_list); |
| 6845 | ksmbd_debug(SMB, "successful in taking lock\n"); |
| 6846 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6847 | goto out; |
| 6848 | } |
| 6849 | } |
| 6850 | } |
| 6851 | |
| 6852 | if (atomic_read(&fp->f_ci->op_count) > 1) |
| 6853 | smb_break_all_oplock(work, fp); |
| 6854 | |
| 6855 | rsp->StructureSize = cpu_to_le16(4); |
| 6856 | ksmbd_debug(SMB, "successful in taking lock\n"); |
| 6857 | rsp->hdr.Status = STATUS_SUCCESS; |
| 6858 | rsp->Reserved = 0; |
| 6859 | inc_rfc1001_len(rsp, 4); |
| 6860 | ksmbd_fd_put(work, fp); |
Namjae Jeon | 96ad4ec | 2021-07-13 17:17:28 +0900 | [diff] [blame] | 6861 | return 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6862 | |
| 6863 | out: |
| 6864 | list_for_each_entry_safe(smb_lock, tmp, &lock_list, llist) { |
| 6865 | locks_free_lock(smb_lock->fl); |
| 6866 | list_del(&smb_lock->llist); |
| 6867 | kfree(smb_lock); |
| 6868 | } |
| 6869 | |
| 6870 | list_for_each_entry_safe(smb_lock, tmp, &rollback_list, llist) { |
| 6871 | struct file_lock *rlock = NULL; |
| 6872 | |
| 6873 | rlock = smb_flock_init(filp); |
| 6874 | rlock->fl_type = F_UNLCK; |
| 6875 | rlock->fl_start = smb_lock->start; |
| 6876 | rlock->fl_end = smb_lock->end; |
| 6877 | |
Namjae Jeon | 96ad4ec | 2021-07-13 17:17:28 +0900 | [diff] [blame] | 6878 | rc = vfs_lock_file(filp, 0, rlock, NULL); |
| 6879 | if (rc) |
| 6880 | pr_err("rollback unlock fail : %d\n", rc); |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6881 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6882 | list_del(&smb_lock->llist); |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6883 | spin_lock(&work->conn->llist_lock); |
| 6884 | if (!list_empty(&smb_lock->flist)) |
| 6885 | list_del(&smb_lock->flist); |
| 6886 | list_del(&smb_lock->clist); |
| 6887 | spin_unlock(&work->conn->llist_lock); |
| 6888 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6889 | locks_free_lock(smb_lock->fl); |
| 6890 | locks_free_lock(rlock); |
| 6891 | kfree(smb_lock); |
| 6892 | } |
| 6893 | out2: |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6894 | ksmbd_debug(SMB, "failed in taking lock(flags : %x), err : %d\n", flags, err); |
| 6895 | |
| 6896 | if (!rsp->hdr.Status) { |
| 6897 | if (err == -EINVAL) |
| 6898 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 6899 | else if (err == -ENOMEM) |
| 6900 | rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES; |
| 6901 | else if (err == -ENOENT) |
| 6902 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 6903 | else |
| 6904 | rsp->hdr.Status = STATUS_LOCK_NOT_GRANTED; |
| 6905 | } |
| 6906 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6907 | smb2_set_err_rsp(work); |
| 6908 | ksmbd_fd_put(work, fp); |
Namjae Jeon | 96ad4ec | 2021-07-13 17:17:28 +0900 | [diff] [blame] | 6909 | return err; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6910 | } |
| 6911 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6912 | static int fsctl_copychunk(struct ksmbd_work *work, struct smb2_ioctl_req *req, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6913 | struct smb2_ioctl_rsp *rsp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6914 | { |
| 6915 | struct copychunk_ioctl_req *ci_req; |
| 6916 | struct copychunk_ioctl_rsp *ci_rsp; |
| 6917 | struct ksmbd_file *src_fp = NULL, *dst_fp = NULL; |
| 6918 | struct srv_copychunk *chunks; |
| 6919 | unsigned int i, chunk_count, chunk_count_written = 0; |
| 6920 | unsigned int chunk_size_written = 0; |
| 6921 | loff_t total_size_written = 0; |
| 6922 | int ret, cnt_code; |
| 6923 | |
| 6924 | cnt_code = le32_to_cpu(req->CntCode); |
| 6925 | ci_req = (struct copychunk_ioctl_req *)&req->Buffer[0]; |
| 6926 | ci_rsp = (struct copychunk_ioctl_rsp *)&rsp->Buffer[0]; |
| 6927 | |
| 6928 | rsp->VolatileFileId = req->VolatileFileId; |
| 6929 | rsp->PersistentFileId = req->PersistentFileId; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6930 | ci_rsp->ChunksWritten = |
| 6931 | cpu_to_le32(ksmbd_server_side_copy_max_chunk_count()); |
| 6932 | ci_rsp->ChunkBytesWritten = |
| 6933 | cpu_to_le32(ksmbd_server_side_copy_max_chunk_size()); |
| 6934 | ci_rsp->TotalBytesWritten = |
| 6935 | cpu_to_le32(ksmbd_server_side_copy_max_total_size()); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6936 | |
| 6937 | chunks = (struct srv_copychunk *)&ci_req->Chunks[0]; |
| 6938 | chunk_count = le32_to_cpu(ci_req->ChunkCount); |
| 6939 | total_size_written = 0; |
| 6940 | |
| 6941 | /* verify the SRV_COPYCHUNK_COPY packet */ |
| 6942 | if (chunk_count > ksmbd_server_side_copy_max_chunk_count() || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6943 | le32_to_cpu(req->InputCount) < |
| 6944 | offsetof(struct copychunk_ioctl_req, Chunks) + |
| 6945 | chunk_count * sizeof(struct srv_copychunk)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6946 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 6947 | return -EINVAL; |
| 6948 | } |
| 6949 | |
| 6950 | for (i = 0; i < chunk_count; i++) { |
| 6951 | if (le32_to_cpu(chunks[i].Length) == 0 || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6952 | 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] | 6953 | break; |
| 6954 | total_size_written += le32_to_cpu(chunks[i].Length); |
| 6955 | } |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6956 | |
| 6957 | if (i < chunk_count || |
| 6958 | total_size_written > ksmbd_server_side_copy_max_total_size()) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6959 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 6960 | return -EINVAL; |
| 6961 | } |
| 6962 | |
| 6963 | src_fp = ksmbd_lookup_foreign_fd(work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6964 | le64_to_cpu(ci_req->ResumeKey[0])); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6965 | dst_fp = ksmbd_lookup_fd_slow(work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6966 | le64_to_cpu(req->VolatileFileId), |
| 6967 | le64_to_cpu(req->PersistentFileId)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6968 | ret = -EINVAL; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6969 | if (!src_fp || |
| 6970 | src_fp->persistent_id != le64_to_cpu(ci_req->ResumeKey[1])) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6971 | rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND; |
| 6972 | goto out; |
| 6973 | } |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6974 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6975 | if (!dst_fp) { |
| 6976 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 6977 | goto out; |
| 6978 | } |
| 6979 | |
| 6980 | /* |
| 6981 | * FILE_READ_DATA should only be included in |
| 6982 | * the FSCTL_COPYCHUNK case |
| 6983 | */ |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6984 | if (cnt_code == FSCTL_COPYCHUNK && |
| 6985 | !(dst_fp->daccess & (FILE_READ_DATA_LE | FILE_GENERIC_READ_LE))) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6986 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 6987 | goto out; |
| 6988 | } |
| 6989 | |
| 6990 | ret = ksmbd_vfs_copy_file_ranges(work, src_fp, dst_fp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6991 | chunks, chunk_count, |
| 6992 | &chunk_count_written, |
| 6993 | &chunk_size_written, |
| 6994 | &total_size_written); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6995 | if (ret < 0) { |
| 6996 | if (ret == -EACCES) |
| 6997 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 6998 | if (ret == -EAGAIN) |
| 6999 | rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT; |
| 7000 | else if (ret == -EBADF) |
| 7001 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 7002 | else if (ret == -EFBIG || ret == -ENOSPC) |
| 7003 | rsp->hdr.Status = STATUS_DISK_FULL; |
| 7004 | else if (ret == -EINVAL) |
| 7005 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 7006 | else if (ret == -EISDIR) |
| 7007 | rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY; |
| 7008 | else if (ret == -E2BIG) |
| 7009 | rsp->hdr.Status = STATUS_INVALID_VIEW_SIZE; |
| 7010 | else |
| 7011 | rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR; |
| 7012 | } |
| 7013 | |
| 7014 | ci_rsp->ChunksWritten = cpu_to_le32(chunk_count_written); |
| 7015 | ci_rsp->ChunkBytesWritten = cpu_to_le32(chunk_size_written); |
| 7016 | ci_rsp->TotalBytesWritten = cpu_to_le32(total_size_written); |
| 7017 | out: |
| 7018 | ksmbd_fd_put(work, src_fp); |
| 7019 | ksmbd_fd_put(work, dst_fp); |
| 7020 | return ret; |
| 7021 | } |
| 7022 | |
| 7023 | static __be32 idev_ipv4_address(struct in_device *idev) |
| 7024 | { |
| 7025 | __be32 addr = 0; |
| 7026 | |
| 7027 | struct in_ifaddr *ifa; |
| 7028 | |
| 7029 | rcu_read_lock(); |
| 7030 | in_dev_for_each_ifa_rcu(ifa, idev) { |
| 7031 | if (ifa->ifa_flags & IFA_F_SECONDARY) |
| 7032 | continue; |
| 7033 | |
| 7034 | addr = ifa->ifa_address; |
| 7035 | break; |
| 7036 | } |
| 7037 | rcu_read_unlock(); |
| 7038 | return addr; |
| 7039 | } |
| 7040 | |
| 7041 | static int fsctl_query_iface_info_ioctl(struct ksmbd_conn *conn, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7042 | struct smb2_ioctl_req *req, |
| 7043 | struct smb2_ioctl_rsp *rsp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7044 | { |
| 7045 | struct network_interface_info_ioctl_rsp *nii_rsp = NULL; |
| 7046 | int nbytes = 0; |
| 7047 | struct net_device *netdev; |
| 7048 | struct sockaddr_storage_rsp *sockaddr_storage; |
| 7049 | unsigned int flags; |
| 7050 | unsigned long long speed; |
Namjae Jeon | f1abdb7 | 2021-07-27 13:25:13 +0900 | [diff] [blame] | 7051 | struct sockaddr_in6 *csin6 = (struct sockaddr_in6 *)&conn->peer_addr; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7052 | |
| 7053 | rtnl_lock(); |
| 7054 | for_each_netdev(&init_net, netdev) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7055 | if (netdev->type == ARPHRD_LOOPBACK) |
| 7056 | continue; |
| 7057 | |
| 7058 | flags = dev_get_flags(netdev); |
| 7059 | if (!(flags & IFF_RUNNING)) |
| 7060 | continue; |
| 7061 | |
| 7062 | nii_rsp = (struct network_interface_info_ioctl_rsp *) |
| 7063 | &rsp->Buffer[nbytes]; |
| 7064 | nii_rsp->IfIndex = cpu_to_le32(netdev->ifindex); |
| 7065 | |
Hyunchul Lee | 03d8d4f | 2021-07-13 16:09:34 +0900 | [diff] [blame] | 7066 | nii_rsp->Capability = 0; |
Hyunchul Lee | 03d8d4f | 2021-07-13 16:09:34 +0900 | [diff] [blame] | 7067 | if (ksmbd_rdma_capable_netdev(netdev)) |
| 7068 | nii_rsp->Capability |= cpu_to_le32(RDMA_CAPABLE); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7069 | |
| 7070 | nii_rsp->Next = cpu_to_le32(152); |
| 7071 | nii_rsp->Reserved = 0; |
| 7072 | |
| 7073 | if (netdev->ethtool_ops->get_link_ksettings) { |
| 7074 | struct ethtool_link_ksettings cmd; |
| 7075 | |
| 7076 | netdev->ethtool_ops->get_link_ksettings(netdev, &cmd); |
| 7077 | speed = cmd.base.speed; |
| 7078 | } else { |
Per Forlin | d475866 | 2021-08-30 13:23:04 +0900 | [diff] [blame] | 7079 | ksmbd_debug(SMB, "%s %s\n", netdev->name, |
| 7080 | "speed is unknown, defaulting to 1Gb/sec"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7081 | speed = SPEED_1000; |
| 7082 | } |
| 7083 | |
| 7084 | speed *= 1000000; |
| 7085 | nii_rsp->LinkSpeed = cpu_to_le64(speed); |
| 7086 | |
| 7087 | sockaddr_storage = (struct sockaddr_storage_rsp *) |
| 7088 | nii_rsp->SockAddr_Storage; |
| 7089 | memset(sockaddr_storage, 0, 128); |
| 7090 | |
Namjae Jeon | f1abdb7 | 2021-07-27 13:25:13 +0900 | [diff] [blame] | 7091 | if (conn->peer_addr.ss_family == PF_INET || |
| 7092 | ipv6_addr_v4mapped(&csin6->sin6_addr)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7093 | struct in_device *idev; |
| 7094 | |
| 7095 | sockaddr_storage->Family = cpu_to_le16(INTERNETWORK); |
| 7096 | sockaddr_storage->addr4.Port = 0; |
| 7097 | |
| 7098 | idev = __in_dev_get_rtnl(netdev); |
| 7099 | if (!idev) |
| 7100 | continue; |
| 7101 | sockaddr_storage->addr4.IPv4address = |
| 7102 | idev_ipv4_address(idev); |
| 7103 | } else { |
| 7104 | struct inet6_dev *idev6; |
| 7105 | struct inet6_ifaddr *ifa; |
| 7106 | __u8 *ipv6_addr = sockaddr_storage->addr6.IPv6address; |
| 7107 | |
| 7108 | sockaddr_storage->Family = cpu_to_le16(INTERNETWORKV6); |
| 7109 | sockaddr_storage->addr6.Port = 0; |
| 7110 | sockaddr_storage->addr6.FlowInfo = 0; |
| 7111 | |
| 7112 | idev6 = __in6_dev_get(netdev); |
| 7113 | if (!idev6) |
| 7114 | continue; |
| 7115 | |
| 7116 | list_for_each_entry(ifa, &idev6->addr_list, if_list) { |
| 7117 | if (ifa->flags & (IFA_F_TENTATIVE | |
| 7118 | IFA_F_DEPRECATED)) |
| 7119 | continue; |
| 7120 | memcpy(ipv6_addr, ifa->addr.s6_addr, 16); |
| 7121 | break; |
| 7122 | } |
| 7123 | sockaddr_storage->addr6.ScopeId = 0; |
| 7124 | } |
| 7125 | |
| 7126 | nbytes += sizeof(struct network_interface_info_ioctl_rsp); |
| 7127 | } |
| 7128 | rtnl_unlock(); |
| 7129 | |
| 7130 | /* zero if this is last one */ |
| 7131 | if (nii_rsp) |
| 7132 | nii_rsp->Next = 0; |
| 7133 | |
| 7134 | if (!nbytes) { |
| 7135 | rsp->hdr.Status = STATUS_BUFFER_TOO_SMALL; |
| 7136 | return -EINVAL; |
| 7137 | } |
| 7138 | |
| 7139 | rsp->PersistentFileId = cpu_to_le64(SMB2_NO_FID); |
| 7140 | rsp->VolatileFileId = cpu_to_le64(SMB2_NO_FID); |
| 7141 | return nbytes; |
| 7142 | } |
| 7143 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7144 | static int fsctl_validate_negotiate_info(struct ksmbd_conn *conn, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7145 | struct validate_negotiate_info_req *neg_req, |
| 7146 | struct validate_negotiate_info_rsp *neg_rsp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7147 | { |
| 7148 | int ret = 0; |
| 7149 | int dialect; |
| 7150 | |
| 7151 | dialect = ksmbd_lookup_dialect_by_id(neg_req->Dialects, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7152 | neg_req->DialectCount); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7153 | if (dialect == BAD_PROT_ID || dialect != conn->dialect) { |
| 7154 | ret = -EINVAL; |
| 7155 | goto err_out; |
| 7156 | } |
| 7157 | |
| 7158 | if (strncmp(neg_req->Guid, conn->ClientGUID, SMB2_CLIENT_GUID_SIZE)) { |
| 7159 | ret = -EINVAL; |
| 7160 | goto err_out; |
| 7161 | } |
| 7162 | |
| 7163 | if (le16_to_cpu(neg_req->SecurityMode) != conn->cli_sec_mode) { |
| 7164 | ret = -EINVAL; |
| 7165 | goto err_out; |
| 7166 | } |
| 7167 | |
| 7168 | if (le32_to_cpu(neg_req->Capabilities) != conn->cli_cap) { |
| 7169 | ret = -EINVAL; |
| 7170 | goto err_out; |
| 7171 | } |
| 7172 | |
| 7173 | neg_rsp->Capabilities = cpu_to_le32(conn->vals->capabilities); |
| 7174 | memset(neg_rsp->Guid, 0, SMB2_CLIENT_GUID_SIZE); |
| 7175 | neg_rsp->SecurityMode = cpu_to_le16(conn->srv_sec_mode); |
| 7176 | neg_rsp->Dialect = cpu_to_le16(conn->dialect); |
| 7177 | err_out: |
| 7178 | return ret; |
| 7179 | } |
| 7180 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7181 | static int fsctl_query_allocated_ranges(struct ksmbd_work *work, u64 id, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7182 | struct file_allocated_range_buffer *qar_req, |
| 7183 | struct file_allocated_range_buffer *qar_rsp, |
| 7184 | int in_count, int *out_count) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7185 | { |
| 7186 | struct ksmbd_file *fp; |
| 7187 | loff_t start, length; |
| 7188 | int ret = 0; |
| 7189 | |
| 7190 | *out_count = 0; |
| 7191 | if (in_count == 0) |
| 7192 | return -EINVAL; |
| 7193 | |
| 7194 | fp = ksmbd_lookup_fd_fast(work, id); |
| 7195 | if (!fp) |
| 7196 | return -ENOENT; |
| 7197 | |
| 7198 | start = le64_to_cpu(qar_req->file_offset); |
| 7199 | length = le64_to_cpu(qar_req->length); |
| 7200 | |
| 7201 | ret = ksmbd_vfs_fqar_lseek(fp, start, length, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7202 | qar_rsp, in_count, out_count); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7203 | if (ret && ret != -E2BIG) |
| 7204 | *out_count = 0; |
| 7205 | |
| 7206 | ksmbd_fd_put(work, fp); |
| 7207 | return ret; |
| 7208 | } |
| 7209 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7210 | static int fsctl_pipe_transceive(struct ksmbd_work *work, u64 id, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7211 | int out_buf_len, struct smb2_ioctl_req *req, |
| 7212 | struct smb2_ioctl_rsp *rsp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7213 | { |
| 7214 | struct ksmbd_rpc_command *rpc_resp; |
| 7215 | char *data_buf = (char *)&req->Buffer[0]; |
| 7216 | int nbytes = 0; |
| 7217 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7218 | rpc_resp = ksmbd_rpc_ioctl(work->sess, id, data_buf, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7219 | le32_to_cpu(req->InputCount)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7220 | if (rpc_resp) { |
| 7221 | if (rpc_resp->flags == KSMBD_RPC_SOME_NOT_MAPPED) { |
| 7222 | /* |
| 7223 | * set STATUS_SOME_NOT_MAPPED response |
| 7224 | * for unknown domain sid. |
| 7225 | */ |
| 7226 | rsp->hdr.Status = STATUS_SOME_NOT_MAPPED; |
| 7227 | } else if (rpc_resp->flags == KSMBD_RPC_ENOTIMPLEMENTED) { |
| 7228 | rsp->hdr.Status = STATUS_NOT_SUPPORTED; |
| 7229 | goto out; |
| 7230 | } else if (rpc_resp->flags != KSMBD_RPC_OK) { |
| 7231 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 7232 | goto out; |
| 7233 | } |
| 7234 | |
| 7235 | nbytes = rpc_resp->payload_sz; |
| 7236 | if (rpc_resp->payload_sz > out_buf_len) { |
| 7237 | rsp->hdr.Status = STATUS_BUFFER_OVERFLOW; |
| 7238 | nbytes = out_buf_len; |
| 7239 | } |
| 7240 | |
| 7241 | if (!rpc_resp->payload_sz) { |
| 7242 | rsp->hdr.Status = |
| 7243 | STATUS_UNEXPECTED_IO_ERROR; |
| 7244 | goto out; |
| 7245 | } |
| 7246 | |
| 7247 | memcpy((char *)rsp->Buffer, rpc_resp->payload, nbytes); |
| 7248 | } |
| 7249 | out: |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 7250 | kvfree(rpc_resp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7251 | return nbytes; |
| 7252 | } |
| 7253 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7254 | static inline int fsctl_set_sparse(struct ksmbd_work *work, u64 id, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7255 | struct file_sparse *sparse) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7256 | { |
| 7257 | struct ksmbd_file *fp; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 7258 | struct user_namespace *user_ns; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7259 | int ret = 0; |
| 7260 | __le32 old_fattr; |
| 7261 | |
| 7262 | fp = ksmbd_lookup_fd_fast(work, id); |
| 7263 | if (!fp) |
| 7264 | return -ENOENT; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 7265 | user_ns = file_mnt_user_ns(fp->filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7266 | |
| 7267 | old_fattr = fp->f_ci->m_fattr; |
| 7268 | if (sparse->SetSparse) |
| 7269 | fp->f_ci->m_fattr |= ATTR_SPARSE_FILE_LE; |
| 7270 | else |
| 7271 | fp->f_ci->m_fattr &= ~ATTR_SPARSE_FILE_LE; |
| 7272 | |
| 7273 | if (fp->f_ci->m_fattr != old_fattr && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7274 | test_share_config_flag(work->tcon->share_conf, |
| 7275 | KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7276 | struct xattr_dos_attrib da; |
| 7277 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 7278 | ret = ksmbd_vfs_get_dos_attrib_xattr(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 7279 | fp->filp->f_path.dentry, &da); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7280 | if (ret <= 0) |
| 7281 | goto out; |
| 7282 | |
| 7283 | da.attr = le32_to_cpu(fp->f_ci->m_fattr); |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 7284 | ret = ksmbd_vfs_set_dos_attrib_xattr(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 7285 | fp->filp->f_path.dentry, &da); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7286 | if (ret) |
| 7287 | fp->f_ci->m_fattr = old_fattr; |
| 7288 | } |
| 7289 | |
| 7290 | out: |
| 7291 | ksmbd_fd_put(work, fp); |
| 7292 | return ret; |
| 7293 | } |
| 7294 | |
| 7295 | static int fsctl_request_resume_key(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7296 | struct smb2_ioctl_req *req, |
| 7297 | struct resume_key_ioctl_rsp *key_rsp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7298 | { |
| 7299 | struct ksmbd_file *fp; |
| 7300 | |
| 7301 | fp = ksmbd_lookup_fd_slow(work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7302 | le64_to_cpu(req->VolatileFileId), |
| 7303 | le64_to_cpu(req->PersistentFileId)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7304 | if (!fp) |
| 7305 | return -ENOENT; |
| 7306 | |
| 7307 | memset(key_rsp, 0, sizeof(*key_rsp)); |
| 7308 | key_rsp->ResumeKey[0] = req->VolatileFileId; |
| 7309 | key_rsp->ResumeKey[1] = req->PersistentFileId; |
| 7310 | ksmbd_fd_put(work, fp); |
| 7311 | |
| 7312 | return 0; |
| 7313 | } |
| 7314 | |
| 7315 | /** |
| 7316 | * smb2_ioctl() - handler for smb2 ioctl command |
| 7317 | * @work: smb work containing ioctl command buffer |
| 7318 | * |
| 7319 | * Return: 0 on success, otherwise error |
| 7320 | */ |
| 7321 | int smb2_ioctl(struct ksmbd_work *work) |
| 7322 | { |
| 7323 | struct smb2_ioctl_req *req; |
| 7324 | struct smb2_ioctl_rsp *rsp, *rsp_org; |
| 7325 | int cnt_code, nbytes = 0; |
| 7326 | int out_buf_len; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7327 | u64 id = KSMBD_NO_FID; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7328 | struct ksmbd_conn *conn = work->conn; |
| 7329 | int ret = 0; |
| 7330 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 7331 | rsp_org = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7332 | if (work->next_smb2_rcv_hdr_off) { |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 7333 | req = ksmbd_req_buf_next(work); |
| 7334 | rsp = ksmbd_resp_buf_next(work); |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 7335 | if (!has_file_id(le64_to_cpu(req->VolatileFileId))) { |
| 7336 | ksmbd_debug(SMB, "Compound request set FID = %llu\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7337 | work->compound_fid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7338 | id = work->compound_fid; |
| 7339 | } |
| 7340 | } else { |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 7341 | req = work->request_buf; |
| 7342 | rsp = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7343 | } |
| 7344 | |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 7345 | if (!has_file_id(id)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7346 | id = le64_to_cpu(req->VolatileFileId); |
| 7347 | |
| 7348 | if (req->Flags != cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL)) { |
| 7349 | rsp->hdr.Status = STATUS_NOT_SUPPORTED; |
| 7350 | goto out; |
| 7351 | } |
| 7352 | |
| 7353 | cnt_code = le32_to_cpu(req->CntCode); |
| 7354 | out_buf_len = le32_to_cpu(req->MaxOutputResponse); |
| 7355 | out_buf_len = min(KSMBD_IPC_MAX_PAYLOAD, out_buf_len); |
| 7356 | |
| 7357 | switch (cnt_code) { |
| 7358 | case FSCTL_DFS_GET_REFERRALS: |
| 7359 | case FSCTL_DFS_GET_REFERRALS_EX: |
| 7360 | /* Not support DFS yet */ |
| 7361 | rsp->hdr.Status = STATUS_FS_DRIVER_REQUIRED; |
| 7362 | goto out; |
| 7363 | case FSCTL_CREATE_OR_GET_OBJECT_ID: |
| 7364 | { |
| 7365 | struct file_object_buf_type1_ioctl_rsp *obj_buf; |
| 7366 | |
| 7367 | nbytes = sizeof(struct file_object_buf_type1_ioctl_rsp); |
| 7368 | obj_buf = (struct file_object_buf_type1_ioctl_rsp *) |
| 7369 | &rsp->Buffer[0]; |
| 7370 | |
| 7371 | /* |
| 7372 | * TODO: This is dummy implementation to pass smbtorture |
| 7373 | * Need to check correct response later |
| 7374 | */ |
| 7375 | memset(obj_buf->ObjectId, 0x0, 16); |
| 7376 | memset(obj_buf->BirthVolumeId, 0x0, 16); |
| 7377 | memset(obj_buf->BirthObjectId, 0x0, 16); |
| 7378 | memset(obj_buf->DomainId, 0x0, 16); |
| 7379 | |
| 7380 | break; |
| 7381 | } |
| 7382 | case FSCTL_PIPE_TRANSCEIVE: |
| 7383 | nbytes = fsctl_pipe_transceive(work, id, out_buf_len, req, rsp); |
| 7384 | break; |
| 7385 | case FSCTL_VALIDATE_NEGOTIATE_INFO: |
| 7386 | if (conn->dialect < SMB30_PROT_ID) { |
| 7387 | ret = -EOPNOTSUPP; |
| 7388 | goto out; |
| 7389 | } |
| 7390 | |
| 7391 | ret = fsctl_validate_negotiate_info(conn, |
| 7392 | (struct validate_negotiate_info_req *)&req->Buffer[0], |
| 7393 | (struct validate_negotiate_info_rsp *)&rsp->Buffer[0]); |
| 7394 | if (ret < 0) |
| 7395 | goto out; |
| 7396 | |
| 7397 | nbytes = sizeof(struct validate_negotiate_info_rsp); |
| 7398 | rsp->PersistentFileId = cpu_to_le64(SMB2_NO_FID); |
| 7399 | rsp->VolatileFileId = cpu_to_le64(SMB2_NO_FID); |
| 7400 | break; |
| 7401 | case FSCTL_QUERY_NETWORK_INTERFACE_INFO: |
| 7402 | nbytes = fsctl_query_iface_info_ioctl(conn, req, rsp); |
| 7403 | if (nbytes < 0) |
| 7404 | goto out; |
| 7405 | break; |
| 7406 | case FSCTL_REQUEST_RESUME_KEY: |
| 7407 | if (out_buf_len < sizeof(struct resume_key_ioctl_rsp)) { |
| 7408 | ret = -EINVAL; |
| 7409 | goto out; |
| 7410 | } |
| 7411 | |
| 7412 | ret = fsctl_request_resume_key(work, req, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7413 | (struct resume_key_ioctl_rsp *)&rsp->Buffer[0]); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7414 | if (ret < 0) |
| 7415 | goto out; |
| 7416 | rsp->PersistentFileId = req->PersistentFileId; |
| 7417 | rsp->VolatileFileId = req->VolatileFileId; |
| 7418 | nbytes = sizeof(struct resume_key_ioctl_rsp); |
| 7419 | break; |
| 7420 | case FSCTL_COPYCHUNK: |
| 7421 | case FSCTL_COPYCHUNK_WRITE: |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7422 | if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7423 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7424 | "User does not have write permission\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7425 | ret = -EACCES; |
| 7426 | goto out; |
| 7427 | } |
| 7428 | |
| 7429 | if (out_buf_len < sizeof(struct copychunk_ioctl_rsp)) { |
| 7430 | ret = -EINVAL; |
| 7431 | goto out; |
| 7432 | } |
| 7433 | |
| 7434 | nbytes = sizeof(struct copychunk_ioctl_rsp); |
| 7435 | fsctl_copychunk(work, req, rsp); |
| 7436 | break; |
| 7437 | case FSCTL_SET_SPARSE: |
| 7438 | ret = fsctl_set_sparse(work, id, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7439 | (struct file_sparse *)&req->Buffer[0]); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7440 | if (ret < 0) |
| 7441 | goto out; |
| 7442 | break; |
| 7443 | case FSCTL_SET_ZERO_DATA: |
| 7444 | { |
| 7445 | struct file_zero_data_information *zero_data; |
| 7446 | struct ksmbd_file *fp; |
| 7447 | loff_t off, len; |
| 7448 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7449 | if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7450 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7451 | "User does not have write permission\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7452 | ret = -EACCES; |
| 7453 | goto out; |
| 7454 | } |
| 7455 | |
| 7456 | zero_data = |
| 7457 | (struct file_zero_data_information *)&req->Buffer[0]; |
| 7458 | |
| 7459 | fp = ksmbd_lookup_fd_fast(work, id); |
| 7460 | if (!fp) { |
| 7461 | ret = -ENOENT; |
| 7462 | goto out; |
| 7463 | } |
| 7464 | |
| 7465 | off = le64_to_cpu(zero_data->FileOffset); |
| 7466 | len = le64_to_cpu(zero_data->BeyondFinalZero) - off; |
| 7467 | |
| 7468 | ret = ksmbd_vfs_zero_data(work, fp, off, len); |
| 7469 | ksmbd_fd_put(work, fp); |
| 7470 | if (ret < 0) |
| 7471 | goto out; |
| 7472 | break; |
| 7473 | } |
| 7474 | case FSCTL_QUERY_ALLOCATED_RANGES: |
| 7475 | ret = fsctl_query_allocated_ranges(work, id, |
| 7476 | (struct file_allocated_range_buffer *)&req->Buffer[0], |
| 7477 | (struct file_allocated_range_buffer *)&rsp->Buffer[0], |
| 7478 | out_buf_len / |
| 7479 | sizeof(struct file_allocated_range_buffer), &nbytes); |
| 7480 | if (ret == -E2BIG) { |
| 7481 | rsp->hdr.Status = STATUS_BUFFER_OVERFLOW; |
| 7482 | } else if (ret < 0) { |
| 7483 | nbytes = 0; |
| 7484 | goto out; |
| 7485 | } |
| 7486 | |
| 7487 | nbytes *= sizeof(struct file_allocated_range_buffer); |
| 7488 | break; |
| 7489 | case FSCTL_GET_REPARSE_POINT: |
| 7490 | { |
| 7491 | struct reparse_data_buffer *reparse_ptr; |
| 7492 | struct ksmbd_file *fp; |
| 7493 | |
| 7494 | reparse_ptr = (struct reparse_data_buffer *)&rsp->Buffer[0]; |
| 7495 | fp = ksmbd_lookup_fd_fast(work, id); |
| 7496 | if (!fp) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 7497 | pr_err("not found fp!!\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7498 | ret = -ENOENT; |
| 7499 | goto out; |
| 7500 | } |
| 7501 | |
| 7502 | reparse_ptr->ReparseTag = |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 7503 | smb2_get_reparse_tag_special_file(file_inode(fp->filp)->i_mode); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7504 | reparse_ptr->ReparseDataLength = 0; |
| 7505 | ksmbd_fd_put(work, fp); |
| 7506 | nbytes = sizeof(struct reparse_data_buffer); |
| 7507 | break; |
| 7508 | } |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 7509 | case FSCTL_DUPLICATE_EXTENTS_TO_FILE: |
| 7510 | { |
| 7511 | struct ksmbd_file *fp_in, *fp_out = NULL; |
| 7512 | struct duplicate_extents_to_file *dup_ext; |
| 7513 | loff_t src_off, dst_off, length, cloned; |
| 7514 | |
| 7515 | dup_ext = (struct duplicate_extents_to_file *)&req->Buffer[0]; |
| 7516 | |
| 7517 | fp_in = ksmbd_lookup_fd_slow(work, dup_ext->VolatileFileHandle, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7518 | dup_ext->PersistentFileHandle); |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 7519 | if (!fp_in) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 7520 | pr_err("not found file handle in duplicate extent to file\n"); |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 7521 | ret = -ENOENT; |
| 7522 | goto out; |
| 7523 | } |
| 7524 | |
| 7525 | fp_out = ksmbd_lookup_fd_fast(work, id); |
| 7526 | if (!fp_out) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 7527 | pr_err("not found fp\n"); |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 7528 | ret = -ENOENT; |
| 7529 | goto dup_ext_out; |
| 7530 | } |
| 7531 | |
| 7532 | src_off = le64_to_cpu(dup_ext->SourceFileOffset); |
| 7533 | dst_off = le64_to_cpu(dup_ext->TargetFileOffset); |
| 7534 | length = le64_to_cpu(dup_ext->ByteCount); |
| 7535 | cloned = vfs_clone_file_range(fp_in->filp, src_off, fp_out->filp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7536 | dst_off, length, 0); |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 7537 | if (cloned == -EXDEV || cloned == -EOPNOTSUPP) { |
| 7538 | ret = -EOPNOTSUPP; |
| 7539 | goto dup_ext_out; |
| 7540 | } else if (cloned != length) { |
Namjae Jeon | f852477 | 2021-06-18 10:28:00 +0900 | [diff] [blame] | 7541 | cloned = vfs_copy_file_range(fp_in->filp, src_off, |
| 7542 | fp_out->filp, dst_off, length, 0); |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 7543 | if (cloned != length) { |
| 7544 | if (cloned < 0) |
| 7545 | ret = cloned; |
| 7546 | else |
| 7547 | ret = -EINVAL; |
| 7548 | } |
| 7549 | } |
| 7550 | |
| 7551 | dup_ext_out: |
| 7552 | ksmbd_fd_put(work, fp_in); |
| 7553 | ksmbd_fd_put(work, fp_out); |
| 7554 | if (ret < 0) |
| 7555 | goto out; |
| 7556 | break; |
| 7557 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7558 | default: |
| 7559 | ksmbd_debug(SMB, "not implemented yet ioctl command 0x%x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7560 | cnt_code); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7561 | ret = -EOPNOTSUPP; |
| 7562 | goto out; |
| 7563 | } |
| 7564 | |
| 7565 | rsp->CntCode = cpu_to_le32(cnt_code); |
| 7566 | rsp->InputCount = cpu_to_le32(0); |
| 7567 | rsp->InputOffset = cpu_to_le32(112); |
| 7568 | rsp->OutputOffset = cpu_to_le32(112); |
| 7569 | rsp->OutputCount = cpu_to_le32(nbytes); |
| 7570 | rsp->StructureSize = cpu_to_le16(49); |
| 7571 | rsp->Reserved = cpu_to_le16(0); |
| 7572 | rsp->Flags = cpu_to_le32(0); |
| 7573 | rsp->Reserved2 = cpu_to_le32(0); |
| 7574 | inc_rfc1001_len(rsp_org, 48 + nbytes); |
| 7575 | |
| 7576 | return 0; |
| 7577 | |
| 7578 | out: |
| 7579 | if (ret == -EACCES) |
| 7580 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 7581 | else if (ret == -ENOENT) |
| 7582 | rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND; |
| 7583 | else if (ret == -EOPNOTSUPP) |
| 7584 | rsp->hdr.Status = STATUS_NOT_SUPPORTED; |
| 7585 | else if (ret < 0 || rsp->hdr.Status == 0) |
| 7586 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 7587 | smb2_set_err_rsp(work); |
| 7588 | return 0; |
| 7589 | } |
| 7590 | |
| 7591 | /** |
| 7592 | * smb20_oplock_break_ack() - handler for smb2.0 oplock break command |
| 7593 | * @work: smb work containing oplock break command buffer |
| 7594 | * |
| 7595 | * Return: 0 |
| 7596 | */ |
| 7597 | static void smb20_oplock_break_ack(struct ksmbd_work *work) |
| 7598 | { |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 7599 | struct smb2_oplock_break *req = work->request_buf; |
| 7600 | struct smb2_oplock_break *rsp = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7601 | struct ksmbd_file *fp; |
| 7602 | struct oplock_info *opinfo = NULL; |
| 7603 | __le32 err = 0; |
| 7604 | int ret = 0; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7605 | u64 volatile_id, persistent_id; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7606 | char req_oplevel = 0, rsp_oplevel = 0; |
| 7607 | unsigned int oplock_change_type; |
| 7608 | |
| 7609 | volatile_id = le64_to_cpu(req->VolatileFid); |
| 7610 | persistent_id = le64_to_cpu(req->PersistentFid); |
| 7611 | req_oplevel = req->OplockLevel; |
| 7612 | ksmbd_debug(OPLOCK, "v_id %llu, p_id %llu request oplock level %d\n", |
| 7613 | volatile_id, persistent_id, req_oplevel); |
| 7614 | |
| 7615 | fp = ksmbd_lookup_fd_slow(work, volatile_id, persistent_id); |
| 7616 | if (!fp) { |
| 7617 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 7618 | smb2_set_err_rsp(work); |
| 7619 | return; |
| 7620 | } |
| 7621 | |
| 7622 | opinfo = opinfo_get(fp); |
| 7623 | if (!opinfo) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 7624 | pr_err("unexpected null oplock_info\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7625 | rsp->hdr.Status = STATUS_INVALID_OPLOCK_PROTOCOL; |
| 7626 | smb2_set_err_rsp(work); |
| 7627 | ksmbd_fd_put(work, fp); |
| 7628 | return; |
| 7629 | } |
| 7630 | |
| 7631 | if (opinfo->level == SMB2_OPLOCK_LEVEL_NONE) { |
| 7632 | rsp->hdr.Status = STATUS_INVALID_OPLOCK_PROTOCOL; |
| 7633 | goto err_out; |
| 7634 | } |
| 7635 | |
| 7636 | if (opinfo->op_state == OPLOCK_STATE_NONE) { |
| 7637 | ksmbd_debug(SMB, "unexpected oplock state 0x%x\n", opinfo->op_state); |
| 7638 | rsp->hdr.Status = STATUS_UNSUCCESSFUL; |
| 7639 | goto err_out; |
| 7640 | } |
| 7641 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7642 | if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE || |
| 7643 | opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) && |
| 7644 | (req_oplevel != SMB2_OPLOCK_LEVEL_II && |
| 7645 | req_oplevel != SMB2_OPLOCK_LEVEL_NONE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7646 | err = STATUS_INVALID_OPLOCK_PROTOCOL; |
| 7647 | oplock_change_type = OPLOCK_WRITE_TO_NONE; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7648 | } else if (opinfo->level == SMB2_OPLOCK_LEVEL_II && |
| 7649 | req_oplevel != SMB2_OPLOCK_LEVEL_NONE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7650 | err = STATUS_INVALID_OPLOCK_PROTOCOL; |
| 7651 | oplock_change_type = OPLOCK_READ_TO_NONE; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7652 | } else if (req_oplevel == SMB2_OPLOCK_LEVEL_II || |
| 7653 | req_oplevel == SMB2_OPLOCK_LEVEL_NONE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7654 | err = STATUS_INVALID_DEVICE_STATE; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7655 | if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE || |
| 7656 | opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) && |
| 7657 | req_oplevel == SMB2_OPLOCK_LEVEL_II) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7658 | oplock_change_type = OPLOCK_WRITE_TO_READ; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7659 | } else if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE || |
| 7660 | opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) && |
| 7661 | req_oplevel == SMB2_OPLOCK_LEVEL_NONE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7662 | oplock_change_type = OPLOCK_WRITE_TO_NONE; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7663 | } else if (opinfo->level == SMB2_OPLOCK_LEVEL_II && |
| 7664 | req_oplevel == SMB2_OPLOCK_LEVEL_NONE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7665 | oplock_change_type = OPLOCK_READ_TO_NONE; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7666 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7667 | oplock_change_type = 0; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7668 | } |
| 7669 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7670 | oplock_change_type = 0; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7671 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7672 | |
| 7673 | switch (oplock_change_type) { |
| 7674 | case OPLOCK_WRITE_TO_READ: |
| 7675 | ret = opinfo_write_to_read(opinfo); |
| 7676 | rsp_oplevel = SMB2_OPLOCK_LEVEL_II; |
| 7677 | break; |
| 7678 | case OPLOCK_WRITE_TO_NONE: |
| 7679 | ret = opinfo_write_to_none(opinfo); |
| 7680 | rsp_oplevel = SMB2_OPLOCK_LEVEL_NONE; |
| 7681 | break; |
| 7682 | case OPLOCK_READ_TO_NONE: |
| 7683 | ret = opinfo_read_to_none(opinfo); |
| 7684 | rsp_oplevel = SMB2_OPLOCK_LEVEL_NONE; |
| 7685 | break; |
| 7686 | default: |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 7687 | pr_err("unknown oplock change 0x%x -> 0x%x\n", |
| 7688 | opinfo->level, rsp_oplevel); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7689 | } |
| 7690 | |
| 7691 | if (ret < 0) { |
| 7692 | rsp->hdr.Status = err; |
| 7693 | goto err_out; |
| 7694 | } |
| 7695 | |
| 7696 | opinfo_put(opinfo); |
| 7697 | ksmbd_fd_put(work, fp); |
| 7698 | opinfo->op_state = OPLOCK_STATE_NONE; |
| 7699 | wake_up_interruptible_all(&opinfo->oplock_q); |
| 7700 | |
| 7701 | rsp->StructureSize = cpu_to_le16(24); |
| 7702 | rsp->OplockLevel = rsp_oplevel; |
| 7703 | rsp->Reserved = 0; |
| 7704 | rsp->Reserved2 = 0; |
| 7705 | rsp->VolatileFid = cpu_to_le64(volatile_id); |
| 7706 | rsp->PersistentFid = cpu_to_le64(persistent_id); |
| 7707 | inc_rfc1001_len(rsp, 24); |
| 7708 | return; |
| 7709 | |
| 7710 | err_out: |
| 7711 | opinfo->op_state = OPLOCK_STATE_NONE; |
| 7712 | wake_up_interruptible_all(&opinfo->oplock_q); |
| 7713 | |
| 7714 | opinfo_put(opinfo); |
| 7715 | ksmbd_fd_put(work, fp); |
| 7716 | smb2_set_err_rsp(work); |
| 7717 | } |
| 7718 | |
| 7719 | static int check_lease_state(struct lease *lease, __le32 req_state) |
| 7720 | { |
| 7721 | if ((lease->new_state == |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7722 | (SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_HANDLE_CACHING_LE)) && |
| 7723 | !(req_state & SMB2_LEASE_WRITE_CACHING_LE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7724 | lease->new_state = req_state; |
| 7725 | return 0; |
| 7726 | } |
| 7727 | |
| 7728 | if (lease->new_state == req_state) |
| 7729 | return 0; |
| 7730 | |
| 7731 | return 1; |
| 7732 | } |
| 7733 | |
| 7734 | /** |
| 7735 | * smb21_lease_break_ack() - handler for smb2.1 lease break command |
| 7736 | * @work: smb work containing lease break command buffer |
| 7737 | * |
| 7738 | * Return: 0 |
| 7739 | */ |
| 7740 | static void smb21_lease_break_ack(struct ksmbd_work *work) |
| 7741 | { |
| 7742 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 7743 | struct smb2_lease_ack *req = work->request_buf; |
| 7744 | struct smb2_lease_ack *rsp = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7745 | struct oplock_info *opinfo; |
| 7746 | __le32 err = 0; |
| 7747 | int ret = 0; |
| 7748 | unsigned int lease_change_type; |
| 7749 | __le32 lease_state; |
| 7750 | struct lease *lease; |
| 7751 | |
| 7752 | ksmbd_debug(OPLOCK, "smb21 lease break, lease state(0x%x)\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7753 | le32_to_cpu(req->LeaseState)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7754 | opinfo = lookup_lease_in_table(conn, req->LeaseKey); |
| 7755 | if (!opinfo) { |
| 7756 | ksmbd_debug(OPLOCK, "file not opened\n"); |
| 7757 | smb2_set_err_rsp(work); |
| 7758 | rsp->hdr.Status = STATUS_UNSUCCESSFUL; |
| 7759 | return; |
| 7760 | } |
| 7761 | lease = opinfo->o_lease; |
| 7762 | |
| 7763 | if (opinfo->op_state == OPLOCK_STATE_NONE) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 7764 | pr_err("unexpected lease break state 0x%x\n", |
| 7765 | opinfo->op_state); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7766 | rsp->hdr.Status = STATUS_UNSUCCESSFUL; |
| 7767 | goto err_out; |
| 7768 | } |
| 7769 | |
| 7770 | if (check_lease_state(lease, req->LeaseState)) { |
| 7771 | rsp->hdr.Status = STATUS_REQUEST_NOT_ACCEPTED; |
| 7772 | ksmbd_debug(OPLOCK, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7773 | "req lease state: 0x%x, expected state: 0x%x\n", |
| 7774 | req->LeaseState, lease->new_state); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7775 | goto err_out; |
| 7776 | } |
| 7777 | |
| 7778 | if (!atomic_read(&opinfo->breaking_cnt)) { |
| 7779 | rsp->hdr.Status = STATUS_UNSUCCESSFUL; |
| 7780 | goto err_out; |
| 7781 | } |
| 7782 | |
| 7783 | /* check for bad lease state */ |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7784 | if (req->LeaseState & |
| 7785 | (~(SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_HANDLE_CACHING_LE))) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7786 | err = STATUS_INVALID_OPLOCK_PROTOCOL; |
| 7787 | if (lease->state & SMB2_LEASE_WRITE_CACHING_LE) |
| 7788 | lease_change_type = OPLOCK_WRITE_TO_NONE; |
| 7789 | else |
| 7790 | lease_change_type = OPLOCK_READ_TO_NONE; |
| 7791 | ksmbd_debug(OPLOCK, "handle bad lease state 0x%x -> 0x%x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7792 | le32_to_cpu(lease->state), |
| 7793 | le32_to_cpu(req->LeaseState)); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7794 | } else if (lease->state == SMB2_LEASE_READ_CACHING_LE && |
| 7795 | req->LeaseState != SMB2_LEASE_NONE_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7796 | err = STATUS_INVALID_OPLOCK_PROTOCOL; |
| 7797 | lease_change_type = OPLOCK_READ_TO_NONE; |
| 7798 | ksmbd_debug(OPLOCK, "handle bad lease state 0x%x -> 0x%x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7799 | le32_to_cpu(lease->state), |
| 7800 | le32_to_cpu(req->LeaseState)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7801 | } else { |
| 7802 | /* valid lease state changes */ |
| 7803 | err = STATUS_INVALID_DEVICE_STATE; |
| 7804 | if (req->LeaseState == SMB2_LEASE_NONE_LE) { |
| 7805 | if (lease->state & SMB2_LEASE_WRITE_CACHING_LE) |
| 7806 | lease_change_type = OPLOCK_WRITE_TO_NONE; |
| 7807 | else |
| 7808 | lease_change_type = OPLOCK_READ_TO_NONE; |
| 7809 | } else if (req->LeaseState & SMB2_LEASE_READ_CACHING_LE) { |
| 7810 | if (lease->state & SMB2_LEASE_WRITE_CACHING_LE) |
| 7811 | lease_change_type = OPLOCK_WRITE_TO_READ; |
| 7812 | else |
| 7813 | lease_change_type = OPLOCK_READ_HANDLE_TO_READ; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7814 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7815 | lease_change_type = 0; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7816 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7817 | } |
| 7818 | |
| 7819 | switch (lease_change_type) { |
| 7820 | case OPLOCK_WRITE_TO_READ: |
| 7821 | ret = opinfo_write_to_read(opinfo); |
| 7822 | break; |
| 7823 | case OPLOCK_READ_HANDLE_TO_READ: |
| 7824 | ret = opinfo_read_handle_to_read(opinfo); |
| 7825 | break; |
| 7826 | case OPLOCK_WRITE_TO_NONE: |
| 7827 | ret = opinfo_write_to_none(opinfo); |
| 7828 | break; |
| 7829 | case OPLOCK_READ_TO_NONE: |
| 7830 | ret = opinfo_read_to_none(opinfo); |
| 7831 | break; |
| 7832 | default: |
| 7833 | ksmbd_debug(OPLOCK, "unknown lease change 0x%x -> 0x%x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7834 | le32_to_cpu(lease->state), |
| 7835 | le32_to_cpu(req->LeaseState)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7836 | } |
| 7837 | |
| 7838 | lease_state = lease->state; |
| 7839 | opinfo->op_state = OPLOCK_STATE_NONE; |
| 7840 | wake_up_interruptible_all(&opinfo->oplock_q); |
| 7841 | atomic_dec(&opinfo->breaking_cnt); |
| 7842 | wake_up_interruptible_all(&opinfo->oplock_brk); |
| 7843 | opinfo_put(opinfo); |
| 7844 | |
| 7845 | if (ret < 0) { |
| 7846 | rsp->hdr.Status = err; |
| 7847 | goto err_out; |
| 7848 | } |
| 7849 | |
| 7850 | rsp->StructureSize = cpu_to_le16(36); |
| 7851 | rsp->Reserved = 0; |
| 7852 | rsp->Flags = 0; |
| 7853 | memcpy(rsp->LeaseKey, req->LeaseKey, 16); |
| 7854 | rsp->LeaseState = lease_state; |
| 7855 | rsp->LeaseDuration = 0; |
| 7856 | inc_rfc1001_len(rsp, 36); |
| 7857 | return; |
| 7858 | |
| 7859 | err_out: |
| 7860 | opinfo->op_state = OPLOCK_STATE_NONE; |
| 7861 | wake_up_interruptible_all(&opinfo->oplock_q); |
| 7862 | atomic_dec(&opinfo->breaking_cnt); |
| 7863 | wake_up_interruptible_all(&opinfo->oplock_brk); |
| 7864 | |
| 7865 | opinfo_put(opinfo); |
| 7866 | smb2_set_err_rsp(work); |
| 7867 | } |
| 7868 | |
| 7869 | /** |
| 7870 | * smb2_oplock_break() - dispatcher for smb2.0 and 2.1 oplock/lease break |
| 7871 | * @work: smb work containing oplock/lease break command buffer |
| 7872 | * |
| 7873 | * Return: 0 |
| 7874 | */ |
| 7875 | int smb2_oplock_break(struct ksmbd_work *work) |
| 7876 | { |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 7877 | struct smb2_oplock_break *req = work->request_buf; |
| 7878 | struct smb2_oplock_break *rsp = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7879 | |
| 7880 | switch (le16_to_cpu(req->StructureSize)) { |
| 7881 | case OP_BREAK_STRUCT_SIZE_20: |
| 7882 | smb20_oplock_break_ack(work); |
| 7883 | break; |
| 7884 | case OP_BREAK_STRUCT_SIZE_21: |
| 7885 | smb21_lease_break_ack(work); |
| 7886 | break; |
| 7887 | default: |
| 7888 | ksmbd_debug(OPLOCK, "invalid break cmd %d\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7889 | le16_to_cpu(req->StructureSize)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7890 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 7891 | smb2_set_err_rsp(work); |
| 7892 | } |
| 7893 | |
| 7894 | return 0; |
| 7895 | } |
| 7896 | |
| 7897 | /** |
| 7898 | * smb2_notify() - handler for smb2 notify request |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 7899 | * @work: smb work containing notify command buffer |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7900 | * |
| 7901 | * Return: 0 |
| 7902 | */ |
| 7903 | int smb2_notify(struct ksmbd_work *work) |
| 7904 | { |
| 7905 | struct smb2_notify_req *req; |
| 7906 | struct smb2_notify_rsp *rsp; |
| 7907 | |
| 7908 | WORK_BUFFERS(work, req, rsp); |
| 7909 | |
| 7910 | if (work->next_smb2_rcv_hdr_off && req->hdr.NextCommand) { |
| 7911 | rsp->hdr.Status = STATUS_INTERNAL_ERROR; |
| 7912 | smb2_set_err_rsp(work); |
| 7913 | return 0; |
| 7914 | } |
| 7915 | |
| 7916 | smb2_set_err_rsp(work); |
| 7917 | rsp->hdr.Status = STATUS_NOT_IMPLEMENTED; |
| 7918 | return 0; |
| 7919 | } |
| 7920 | |
| 7921 | /** |
| 7922 | * smb2_is_sign_req() - handler for checking packet signing status |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 7923 | * @work: smb work containing notify command buffer |
| 7924 | * @command: SMB2 command id |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7925 | * |
| 7926 | * Return: true if packed is signed, false otherwise |
| 7927 | */ |
| 7928 | bool smb2_is_sign_req(struct ksmbd_work *work, unsigned int command) |
| 7929 | { |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 7930 | struct smb2_hdr *rcv_hdr2 = work->request_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7931 | |
| 7932 | if ((rcv_hdr2->Flags & SMB2_FLAGS_SIGNED) && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7933 | command != SMB2_NEGOTIATE_HE && |
| 7934 | command != SMB2_SESSION_SETUP_HE && |
| 7935 | command != SMB2_OPLOCK_BREAK_HE) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7936 | return true; |
| 7937 | |
kernel test robot | 5616015 | 2021-05-12 09:24:37 +0900 | [diff] [blame] | 7938 | return false; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7939 | } |
| 7940 | |
| 7941 | /** |
| 7942 | * smb2_check_sign_req() - handler for req packet sign processing |
| 7943 | * @work: smb work containing notify command buffer |
| 7944 | * |
| 7945 | * Return: 1 on success, 0 otherwise |
| 7946 | */ |
| 7947 | int smb2_check_sign_req(struct ksmbd_work *work) |
| 7948 | { |
| 7949 | struct smb2_hdr *hdr, *hdr_org; |
| 7950 | char signature_req[SMB2_SIGNATURE_SIZE]; |
| 7951 | char signature[SMB2_HMACSHA256_SIZE]; |
| 7952 | struct kvec iov[1]; |
| 7953 | size_t len; |
| 7954 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 7955 | hdr_org = hdr = work->request_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7956 | if (work->next_smb2_rcv_hdr_off) |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 7957 | hdr = ksmbd_req_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7958 | |
| 7959 | if (!hdr->NextCommand && !work->next_smb2_rcv_hdr_off) |
| 7960 | len = be32_to_cpu(hdr_org->smb2_buf_length); |
| 7961 | else if (hdr->NextCommand) |
| 7962 | len = le32_to_cpu(hdr->NextCommand); |
| 7963 | else |
| 7964 | len = be32_to_cpu(hdr_org->smb2_buf_length) - |
| 7965 | work->next_smb2_rcv_hdr_off; |
| 7966 | |
| 7967 | memcpy(signature_req, hdr->Signature, SMB2_SIGNATURE_SIZE); |
| 7968 | memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE); |
| 7969 | |
| 7970 | iov[0].iov_base = (char *)&hdr->ProtocolId; |
| 7971 | iov[0].iov_len = len; |
| 7972 | |
| 7973 | 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] | 7974 | signature)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7975 | return 0; |
| 7976 | |
| 7977 | if (memcmp(signature, signature_req, SMB2_SIGNATURE_SIZE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 7978 | pr_err("bad smb2 signature\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7979 | return 0; |
| 7980 | } |
| 7981 | |
| 7982 | return 1; |
| 7983 | } |
| 7984 | |
| 7985 | /** |
| 7986 | * smb2_set_sign_rsp() - handler for rsp packet sign processing |
| 7987 | * @work: smb work containing notify command buffer |
| 7988 | * |
| 7989 | */ |
| 7990 | void smb2_set_sign_rsp(struct ksmbd_work *work) |
| 7991 | { |
| 7992 | struct smb2_hdr *hdr, *hdr_org; |
| 7993 | struct smb2_hdr *req_hdr; |
| 7994 | char signature[SMB2_HMACSHA256_SIZE]; |
| 7995 | struct kvec iov[2]; |
| 7996 | size_t len; |
| 7997 | int n_vec = 1; |
| 7998 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 7999 | hdr_org = hdr = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8000 | if (work->next_smb2_rsp_hdr_off) |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 8001 | hdr = ksmbd_resp_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8002 | |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 8003 | req_hdr = ksmbd_req_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8004 | |
| 8005 | if (!work->next_smb2_rsp_hdr_off) { |
| 8006 | len = get_rfc1002_len(hdr_org); |
| 8007 | if (req_hdr->NextCommand) |
| 8008 | len = ALIGN(len, 8); |
| 8009 | } else { |
| 8010 | len = get_rfc1002_len(hdr_org) - work->next_smb2_rsp_hdr_off; |
| 8011 | len = ALIGN(len, 8); |
| 8012 | } |
| 8013 | |
| 8014 | if (req_hdr->NextCommand) |
| 8015 | hdr->NextCommand = cpu_to_le32(len); |
| 8016 | |
| 8017 | hdr->Flags |= SMB2_FLAGS_SIGNED; |
| 8018 | memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE); |
| 8019 | |
| 8020 | iov[0].iov_base = (char *)&hdr->ProtocolId; |
| 8021 | iov[0].iov_len = len; |
| 8022 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8023 | if (work->aux_payload_sz) { |
| 8024 | iov[0].iov_len -= work->aux_payload_sz; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8025 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8026 | iov[1].iov_base = work->aux_payload_buf; |
| 8027 | iov[1].iov_len = work->aux_payload_sz; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8028 | n_vec++; |
| 8029 | } |
| 8030 | |
| 8031 | 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] | 8032 | signature)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8033 | memcpy(hdr->Signature, signature, SMB2_SIGNATURE_SIZE); |
| 8034 | } |
| 8035 | |
| 8036 | /** |
| 8037 | * smb3_check_sign_req() - handler for req packet sign processing |
| 8038 | * @work: smb work containing notify command buffer |
| 8039 | * |
| 8040 | * Return: 1 on success, 0 otherwise |
| 8041 | */ |
| 8042 | int smb3_check_sign_req(struct ksmbd_work *work) |
| 8043 | { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 8044 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8045 | char *signing_key; |
| 8046 | struct smb2_hdr *hdr, *hdr_org; |
| 8047 | struct channel *chann; |
| 8048 | char signature_req[SMB2_SIGNATURE_SIZE]; |
| 8049 | char signature[SMB2_CMACAES_SIZE]; |
| 8050 | struct kvec iov[1]; |
| 8051 | size_t len; |
| 8052 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8053 | hdr_org = hdr = work->request_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8054 | if (work->next_smb2_rcv_hdr_off) |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 8055 | hdr = ksmbd_req_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8056 | |
| 8057 | if (!hdr->NextCommand && !work->next_smb2_rcv_hdr_off) |
| 8058 | len = be32_to_cpu(hdr_org->smb2_buf_length); |
| 8059 | else if (hdr->NextCommand) |
| 8060 | len = le32_to_cpu(hdr->NextCommand); |
| 8061 | else |
| 8062 | len = be32_to_cpu(hdr_org->smb2_buf_length) - |
| 8063 | work->next_smb2_rcv_hdr_off; |
| 8064 | |
| 8065 | if (le16_to_cpu(hdr->Command) == SMB2_SESSION_SETUP_HE) { |
| 8066 | signing_key = work->sess->smb3signingkey; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8067 | } else { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 8068 | chann = lookup_chann_list(work->sess, conn); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8069 | if (!chann) |
| 8070 | return 0; |
| 8071 | signing_key = chann->smb3signingkey; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8072 | } |
| 8073 | |
| 8074 | if (!signing_key) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 8075 | pr_err("SMB3 signing key is not generated\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8076 | return 0; |
| 8077 | } |
| 8078 | |
| 8079 | memcpy(signature_req, hdr->Signature, SMB2_SIGNATURE_SIZE); |
| 8080 | memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE); |
| 8081 | iov[0].iov_base = (char *)&hdr->ProtocolId; |
| 8082 | iov[0].iov_len = len; |
| 8083 | |
| 8084 | if (ksmbd_sign_smb3_pdu(conn, signing_key, iov, 1, signature)) |
| 8085 | return 0; |
| 8086 | |
| 8087 | if (memcmp(signature, signature_req, SMB2_SIGNATURE_SIZE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 8088 | pr_err("bad smb2 signature\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8089 | return 0; |
| 8090 | } |
| 8091 | |
| 8092 | return 1; |
| 8093 | } |
| 8094 | |
| 8095 | /** |
| 8096 | * smb3_set_sign_rsp() - handler for rsp packet sign processing |
| 8097 | * @work: smb work containing notify command buffer |
| 8098 | * |
| 8099 | */ |
| 8100 | void smb3_set_sign_rsp(struct ksmbd_work *work) |
| 8101 | { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 8102 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8103 | struct smb2_hdr *req_hdr; |
| 8104 | struct smb2_hdr *hdr, *hdr_org; |
| 8105 | struct channel *chann; |
| 8106 | char signature[SMB2_CMACAES_SIZE]; |
| 8107 | struct kvec iov[2]; |
| 8108 | int n_vec = 1; |
| 8109 | size_t len; |
| 8110 | char *signing_key; |
| 8111 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8112 | hdr_org = hdr = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8113 | if (work->next_smb2_rsp_hdr_off) |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 8114 | hdr = ksmbd_resp_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8115 | |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 8116 | req_hdr = ksmbd_req_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8117 | |
| 8118 | if (!work->next_smb2_rsp_hdr_off) { |
| 8119 | len = get_rfc1002_len(hdr_org); |
| 8120 | if (req_hdr->NextCommand) |
| 8121 | len = ALIGN(len, 8); |
| 8122 | } else { |
| 8123 | len = get_rfc1002_len(hdr_org) - work->next_smb2_rsp_hdr_off; |
| 8124 | len = ALIGN(len, 8); |
| 8125 | } |
| 8126 | |
Namjae Jeon | 08bdbc6 | 2021-07-27 09:30:29 +0900 | [diff] [blame] | 8127 | if (conn->binding == false && |
| 8128 | le16_to_cpu(hdr->Command) == SMB2_SESSION_SETUP_HE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8129 | signing_key = work->sess->smb3signingkey; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8130 | } else { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 8131 | chann = lookup_chann_list(work->sess, work->conn); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8132 | if (!chann) |
| 8133 | return; |
| 8134 | signing_key = chann->smb3signingkey; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8135 | } |
| 8136 | |
| 8137 | if (!signing_key) |
| 8138 | return; |
| 8139 | |
| 8140 | if (req_hdr->NextCommand) |
| 8141 | hdr->NextCommand = cpu_to_le32(len); |
| 8142 | |
| 8143 | hdr->Flags |= SMB2_FLAGS_SIGNED; |
| 8144 | memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE); |
| 8145 | iov[0].iov_base = (char *)&hdr->ProtocolId; |
| 8146 | iov[0].iov_len = len; |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8147 | if (work->aux_payload_sz) { |
| 8148 | iov[0].iov_len -= work->aux_payload_sz; |
| 8149 | iov[1].iov_base = work->aux_payload_buf; |
| 8150 | iov[1].iov_len = work->aux_payload_sz; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8151 | n_vec++; |
| 8152 | } |
| 8153 | |
| 8154 | if (!ksmbd_sign_smb3_pdu(conn, signing_key, iov, n_vec, signature)) |
| 8155 | memcpy(hdr->Signature, signature, SMB2_SIGNATURE_SIZE); |
| 8156 | } |
| 8157 | |
| 8158 | /** |
| 8159 | * smb3_preauth_hash_rsp() - handler for computing preauth hash on response |
| 8160 | * @work: smb work containing response buffer |
| 8161 | * |
| 8162 | */ |
| 8163 | void smb3_preauth_hash_rsp(struct ksmbd_work *work) |
| 8164 | { |
| 8165 | struct ksmbd_conn *conn = work->conn; |
| 8166 | struct ksmbd_session *sess = work->sess; |
| 8167 | struct smb2_hdr *req, *rsp; |
| 8168 | |
| 8169 | if (conn->dialect != SMB311_PROT_ID) |
| 8170 | return; |
| 8171 | |
| 8172 | WORK_BUFFERS(work, req, rsp); |
| 8173 | |
| 8174 | if (le16_to_cpu(req->Command) == SMB2_NEGOTIATE_HE) |
| 8175 | ksmbd_gen_preauth_integrity_hash(conn, (char *)rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 8176 | conn->preauth_info->Preauth_HashValue); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8177 | |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 8178 | if (le16_to_cpu(rsp->Command) == SMB2_SESSION_SETUP_HE && sess) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8179 | __u8 *hash_value; |
| 8180 | |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 8181 | if (conn->binding) { |
| 8182 | struct preauth_session *preauth_sess; |
| 8183 | |
| 8184 | preauth_sess = ksmbd_preauth_session_lookup(conn, sess->id); |
| 8185 | if (!preauth_sess) |
| 8186 | return; |
| 8187 | hash_value = preauth_sess->Preauth_HashValue; |
| 8188 | } else { |
| 8189 | hash_value = sess->Preauth_HashValue; |
| 8190 | if (!hash_value) |
| 8191 | return; |
| 8192 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8193 | ksmbd_gen_preauth_integrity_hash(conn, (char *)rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 8194 | hash_value); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8195 | } |
| 8196 | } |
| 8197 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 8198 | static void fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, char *old_buf, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 8199 | __le16 cipher_type) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8200 | { |
| 8201 | struct smb2_hdr *hdr = (struct smb2_hdr *)old_buf; |
| 8202 | unsigned int orig_len = get_rfc1002_len(old_buf); |
| 8203 | |
| 8204 | memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr)); |
| 8205 | tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM; |
| 8206 | tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len); |
| 8207 | tr_hdr->Flags = cpu_to_le16(0x01); |
Namjae Jeon | 5a0ca77 | 2021-05-06 11:43:37 +0900 | [diff] [blame] | 8208 | if (cipher_type == SMB2_ENCRYPTION_AES128_GCM || |
| 8209 | cipher_type == SMB2_ENCRYPTION_AES256_GCM) |
| 8210 | get_random_bytes(&tr_hdr->Nonce, SMB3_AES_GCM_NONCE); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8211 | else |
Namjae Jeon | 5a0ca77 | 2021-05-06 11:43:37 +0900 | [diff] [blame] | 8212 | get_random_bytes(&tr_hdr->Nonce, SMB3_AES_CCM_NONCE); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8213 | memcpy(&tr_hdr->SessionId, &hdr->SessionId, 8); |
| 8214 | inc_rfc1001_len(tr_hdr, sizeof(struct smb2_transform_hdr) - 4); |
| 8215 | inc_rfc1001_len(tr_hdr, orig_len); |
| 8216 | } |
| 8217 | |
| 8218 | int smb3_encrypt_resp(struct ksmbd_work *work) |
| 8219 | { |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8220 | char *buf = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8221 | struct smb2_transform_hdr *tr_hdr; |
| 8222 | struct kvec iov[3]; |
| 8223 | int rc = -ENOMEM; |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8224 | 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] | 8225 | |
| 8226 | if (ARRAY_SIZE(iov) < rq_nvec) |
| 8227 | return -ENOMEM; |
| 8228 | |
Namjae Jeon | 20ea7fd | 2021-03-30 12:40:47 +0900 | [diff] [blame] | 8229 | tr_hdr = kzalloc(sizeof(struct smb2_transform_hdr), GFP_KERNEL); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8230 | if (!tr_hdr) |
| 8231 | return rc; |
| 8232 | |
| 8233 | /* fill transform header */ |
| 8234 | fill_transform_hdr(tr_hdr, buf, work->conn->cipher_type); |
| 8235 | |
| 8236 | iov[0].iov_base = tr_hdr; |
| 8237 | iov[0].iov_len = sizeof(struct smb2_transform_hdr); |
| 8238 | buf_size += iov[0].iov_len - 4; |
| 8239 | |
| 8240 | iov[1].iov_base = buf + 4; |
| 8241 | iov[1].iov_len = get_rfc1002_len(buf); |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8242 | if (work->aux_payload_sz) { |
| 8243 | iov[1].iov_len = work->resp_hdr_sz - 4; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8244 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8245 | iov[2].iov_base = work->aux_payload_buf; |
| 8246 | iov[2].iov_len = work->aux_payload_sz; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8247 | buf_size += iov[2].iov_len; |
| 8248 | } |
| 8249 | buf_size += iov[1].iov_len; |
| 8250 | work->resp_hdr_sz = iov[1].iov_len; |
| 8251 | |
| 8252 | rc = ksmbd_crypt_message(work->conn, iov, rq_nvec, 1); |
| 8253 | if (rc) |
| 8254 | return rc; |
| 8255 | |
| 8256 | memmove(buf, iov[1].iov_base, iov[1].iov_len); |
| 8257 | tr_hdr->smb2_buf_length = cpu_to_be32(buf_size); |
| 8258 | work->tr_buf = tr_hdr; |
| 8259 | |
| 8260 | return rc; |
| 8261 | } |
| 8262 | |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 8263 | bool smb3_is_transform_hdr(void *buf) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8264 | { |
| 8265 | struct smb2_transform_hdr *trhdr = buf; |
| 8266 | |
| 8267 | return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM; |
| 8268 | } |
| 8269 | |
| 8270 | int smb3_decrypt_req(struct ksmbd_work *work) |
| 8271 | { |
| 8272 | struct ksmbd_conn *conn = work->conn; |
| 8273 | struct ksmbd_session *sess; |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8274 | char *buf = work->request_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8275 | struct smb2_hdr *hdr; |
| 8276 | unsigned int pdu_length = get_rfc1002_len(buf); |
| 8277 | struct kvec iov[2]; |
| 8278 | unsigned int buf_data_size = pdu_length + 4 - |
| 8279 | sizeof(struct smb2_transform_hdr); |
| 8280 | struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf; |
| 8281 | unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize); |
| 8282 | int rc = 0; |
| 8283 | |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 8284 | sess = ksmbd_session_lookup_all(conn, le64_to_cpu(tr_hdr->SessionId)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8285 | if (!sess) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 8286 | pr_err("invalid session id(%llx) in transform header\n", |
| 8287 | le64_to_cpu(tr_hdr->SessionId)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8288 | return -ECONNABORTED; |
| 8289 | } |
| 8290 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 8291 | if (pdu_length + 4 < |
| 8292 | sizeof(struct smb2_transform_hdr) + sizeof(struct smb2_hdr)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 8293 | pr_err("Transform message is too small (%u)\n", |
| 8294 | pdu_length); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8295 | return -ECONNABORTED; |
| 8296 | } |
| 8297 | |
| 8298 | if (pdu_length + 4 < orig_len + sizeof(struct smb2_transform_hdr)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 8299 | pr_err("Transform message is broken\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8300 | return -ECONNABORTED; |
| 8301 | } |
| 8302 | |
| 8303 | iov[0].iov_base = buf; |
| 8304 | iov[0].iov_len = sizeof(struct smb2_transform_hdr); |
| 8305 | iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr); |
| 8306 | iov[1].iov_len = buf_data_size; |
| 8307 | rc = ksmbd_crypt_message(conn, iov, 2, 0); |
| 8308 | if (rc) |
| 8309 | return rc; |
| 8310 | |
| 8311 | memmove(buf + 4, iov[1].iov_base, buf_data_size); |
| 8312 | hdr = (struct smb2_hdr *)buf; |
| 8313 | hdr->smb2_buf_length = cpu_to_be32(buf_data_size); |
| 8314 | |
| 8315 | return rc; |
| 8316 | } |
| 8317 | |
| 8318 | bool smb3_11_final_sess_setup_resp(struct ksmbd_work *work) |
| 8319 | { |
| 8320 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8321 | struct smb2_hdr *rsp = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8322 | |
| 8323 | if (conn->dialect < SMB30_PROT_ID) |
| 8324 | return false; |
| 8325 | |
| 8326 | if (work->next_smb2_rcv_hdr_off) |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 8327 | rsp = ksmbd_resp_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8328 | |
| 8329 | if (le16_to_cpu(rsp->Command) == SMB2_SESSION_SETUP_HE && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 8330 | rsp->Status == STATUS_SUCCESS) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8331 | return true; |
| 8332 | return false; |
| 8333 | } |