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" |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 16 | #include "smbfsctl.h" |
| 17 | #include "oplock.h" |
| 18 | #include "smbacl.h" |
| 19 | |
| 20 | #include "auth.h" |
| 21 | #include "asn1.h" |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 22 | #include "connection.h" |
| 23 | #include "transport_ipc.h" |
Hyunchul Lee | 03d8d4f | 2021-07-13 16:09:34 +0900 | [diff] [blame] | 24 | #include "transport_rdma.h" |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 25 | #include "vfs.h" |
| 26 | #include "vfs_cache.h" |
| 27 | #include "misc.h" |
| 28 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 29 | #include "server.h" |
| 30 | #include "smb_common.h" |
| 31 | #include "smbstatus.h" |
| 32 | #include "ksmbd_work.h" |
| 33 | #include "mgmt/user_config.h" |
| 34 | #include "mgmt/share_config.h" |
| 35 | #include "mgmt/tree_connect.h" |
| 36 | #include "mgmt/user_session.h" |
| 37 | #include "mgmt/ksmbd_ida.h" |
| 38 | #include "ndr.h" |
| 39 | |
| 40 | static void __wbuf(struct ksmbd_work *work, void **req, void **rsp) |
| 41 | { |
| 42 | if (work->next_smb2_rcv_hdr_off) { |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 43 | *req = ksmbd_req_buf_next(work); |
| 44 | *rsp = ksmbd_resp_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 45 | } else { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 46 | *req = smb2_get_msg(work->request_buf); |
| 47 | *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | |
| 51 | #define WORK_BUFFERS(w, rq, rs) __wbuf((w), (void **)&(rq), (void **)&(rs)) |
| 52 | |
| 53 | /** |
| 54 | * check_session_id() - check for valid session id in smb header |
| 55 | * @conn: connection instance |
| 56 | * @id: session id from smb header |
| 57 | * |
| 58 | * Return: 1 if valid session id, otherwise 0 |
| 59 | */ |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 60 | static inline bool check_session_id(struct ksmbd_conn *conn, u64 id) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 61 | { |
| 62 | struct ksmbd_session *sess; |
| 63 | |
| 64 | if (id == 0 || id == -1) |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 65 | return false; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 66 | |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 67 | sess = ksmbd_session_lookup_all(conn, id); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 68 | if (sess) |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 69 | return true; |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 70 | pr_err("Invalid user session id: %llu\n", id); |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 71 | return false; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 72 | } |
| 73 | |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 74 | 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] | 75 | { |
| 76 | struct channel *chann; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 77 | |
Namjae Jeon | 6f3d5ee | 2021-06-18 10:28:52 +0900 | [diff] [blame] | 78 | list_for_each_entry(chann, &sess->ksmbd_chann_list, chann_list) { |
Namjae Jeon | 560ac05 | 2021-06-22 16:16:45 +0900 | [diff] [blame] | 79 | if (chann->conn == conn) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 80 | return chann; |
| 81 | } |
| 82 | |
| 83 | return NULL; |
| 84 | } |
| 85 | |
| 86 | /** |
Namjae Jeon | 5ec3df8e | 2021-08-12 10:17:39 +0900 | [diff] [blame] | 87 | * smb2_get_ksmbd_tcon() - get tree connection information using a tree id. |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 88 | * @work: smb work |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 89 | * |
Namjae Jeon | 5ec3df8e | 2021-08-12 10:17:39 +0900 | [diff] [blame] | 90 | * Return: 0 if there is a tree connection matched or these are |
| 91 | * skipable commands, otherwise error |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 92 | */ |
| 93 | int smb2_get_ksmbd_tcon(struct ksmbd_work *work) |
| 94 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 95 | struct smb2_hdr *req_hdr = smb2_get_msg(work->request_buf); |
Ralph Boehme | 341b160 | 2021-10-05 07:03:40 +0200 | [diff] [blame] | 96 | unsigned int cmd = le16_to_cpu(req_hdr->Command); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 97 | int tree_id; |
| 98 | |
| 99 | work->tcon = NULL; |
Ralph Boehme | 341b160 | 2021-10-05 07:03:40 +0200 | [diff] [blame] | 100 | if (cmd == SMB2_TREE_CONNECT_HE || |
| 101 | cmd == SMB2_CANCEL_HE || |
| 102 | cmd == 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 | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 133 | err_rsp = smb2_get_msg(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 | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 153 | struct smb2_hdr *hdr = smb2_get_msg(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 | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 177 | struct smb2_hdr *hdr = smb2_get_msg(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 | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 203 | rcv_hdr = smb2_get_msg(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 | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 219 | rsp_hdr = smb2_get_msg(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; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 239 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 240 | *(__be32 *)work->response_buf = |
| 241 | cpu_to_be32(conn->vals->header_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 242 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 243 | rsp_hdr = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 244 | memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 245 | rsp_hdr->ProtocolId = SMB2_PROTO_NUMBER; |
| 246 | rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE; |
| 247 | rsp_hdr->CreditRequest = cpu_to_le16(2); |
| 248 | rsp_hdr->Command = SMB2_NEGOTIATE; |
| 249 | rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR); |
| 250 | rsp_hdr->NextCommand = 0; |
| 251 | rsp_hdr->MessageId = 0; |
| 252 | rsp_hdr->Id.SyncId.ProcessId = 0; |
| 253 | rsp_hdr->Id.SyncId.TreeId = 0; |
| 254 | rsp_hdr->SessionId = 0; |
| 255 | memset(rsp_hdr->Signature, 0, 16); |
| 256 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 257 | rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 258 | |
| 259 | WARN_ON(ksmbd_conn_good(work)); |
| 260 | |
| 261 | rsp->StructureSize = cpu_to_le16(65); |
| 262 | ksmbd_debug(SMB, "conn->dialect 0x%x\n", conn->dialect); |
| 263 | rsp->DialectRevision = cpu_to_le16(conn->dialect); |
| 264 | /* Not setting conn guid rsp->ServerGUID, as it |
| 265 | * not used by client for identifying connection |
| 266 | */ |
| 267 | rsp->Capabilities = cpu_to_le32(conn->vals->capabilities); |
| 268 | /* Default Max Message Size till SMB2.0, 64K*/ |
| 269 | rsp->MaxTransactSize = cpu_to_le32(conn->vals->max_trans_size); |
| 270 | rsp->MaxReadSize = cpu_to_le32(conn->vals->max_read_size); |
| 271 | rsp->MaxWriteSize = cpu_to_le32(conn->vals->max_write_size); |
| 272 | |
| 273 | rsp->SystemTime = cpu_to_le64(ksmbd_systime()); |
| 274 | rsp->ServerStartTime = 0; |
| 275 | |
| 276 | rsp->SecurityBufferOffset = cpu_to_le16(128); |
| 277 | rsp->SecurityBufferLength = cpu_to_le16(AUTH_GSS_LENGTH); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 278 | ksmbd_copy_gss_neg_header((char *)(&rsp->hdr) + |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 279 | le16_to_cpu(rsp->SecurityBufferOffset)); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 280 | inc_rfc1001_len(work->response_buf, |
| 281 | sizeof(struct smb2_negotiate_rsp) - |
| 282 | sizeof(struct smb2_hdr) - sizeof(rsp->Buffer) + |
| 283 | AUTH_GSS_LENGTH); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 284 | rsp->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED_LE; |
| 285 | if (server_conf.signing == KSMBD_CONFIG_OPT_MANDATORY) |
| 286 | rsp->SecurityMode |= SMB2_NEGOTIATE_SIGNING_REQUIRED_LE; |
| 287 | conn->use_spnego = true; |
| 288 | |
| 289 | ksmbd_conn_set_need_negotiate(work); |
| 290 | return 0; |
| 291 | } |
| 292 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 293 | /** |
| 294 | * smb2_set_rsp_credits() - set number of credits in response buffer |
| 295 | * @work: smb work containing smb response buffer |
| 296 | */ |
| 297 | int smb2_set_rsp_credits(struct ksmbd_work *work) |
| 298 | { |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 299 | struct smb2_hdr *req_hdr = ksmbd_req_buf_next(work); |
| 300 | struct smb2_hdr *hdr = ksmbd_resp_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 301 | struct ksmbd_conn *conn = work->conn; |
Hyunchul Lee | bf8acc9 | 2021-10-07 16:26:58 +0900 | [diff] [blame] | 302 | unsigned short credits_requested; |
| 303 | unsigned short credit_charge, credits_granted = 0; |
| 304 | unsigned short aux_max, aux_credits; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 305 | |
Hyunchul Lee | bf8acc9 | 2021-10-07 16:26:58 +0900 | [diff] [blame] | 306 | if (work->send_no_response) |
| 307 | return 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 308 | |
Hyunchul Lee | bf8acc9 | 2021-10-07 16:26:58 +0900 | [diff] [blame] | 309 | hdr->CreditCharge = req_hdr->CreditCharge; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 310 | |
Hyunchul Lee | bf8acc9 | 2021-10-07 16:26:58 +0900 | [diff] [blame] | 311 | if (conn->total_credits > conn->max_credits) { |
| 312 | hdr->CreditRequest = 0; |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 313 | pr_err("Total credits overflow: %d\n", conn->total_credits); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 314 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 315 | } |
| 316 | |
Hyunchul Lee | bf8acc9 | 2021-10-07 16:26:58 +0900 | [diff] [blame] | 317 | credit_charge = max_t(unsigned short, |
| 318 | le16_to_cpu(req_hdr->CreditCharge), 1); |
| 319 | credits_requested = max_t(unsigned short, |
| 320 | le16_to_cpu(req_hdr->CreditRequest), 1); |
| 321 | |
| 322 | /* according to smb2.credits smbtorture, Windows server |
| 323 | * 2016 or later grant up to 8192 credits at once. |
| 324 | * |
| 325 | * TODO: Need to adjuct CreditRequest value according to |
| 326 | * current cpu load |
| 327 | */ |
| 328 | aux_credits = credits_requested - 1; |
| 329 | if (hdr->Command == SMB2_NEGOTIATE) |
| 330 | aux_max = 0; |
| 331 | else |
| 332 | aux_max = conn->max_credits - credit_charge; |
| 333 | aux_credits = min_t(unsigned short, aux_credits, aux_max); |
| 334 | credits_granted = credit_charge + aux_credits; |
| 335 | |
| 336 | if (conn->max_credits - conn->total_credits < credits_granted) |
| 337 | credits_granted = conn->max_credits - |
| 338 | conn->total_credits; |
| 339 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 340 | conn->total_credits += credits_granted; |
| 341 | work->credits_granted += credits_granted; |
| 342 | |
| 343 | if (!req_hdr->NextCommand) { |
| 344 | /* Update CreditRequest in last request */ |
| 345 | hdr->CreditRequest = cpu_to_le16(work->credits_granted); |
| 346 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 347 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 348 | "credits: requested[%d] granted[%d] total_granted[%d]\n", |
| 349 | credits_requested, credits_granted, |
| 350 | conn->total_credits); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * init_chained_smb2_rsp() - initialize smb2 chained response |
| 356 | * @work: smb work containing smb response buffer |
| 357 | */ |
| 358 | static void init_chained_smb2_rsp(struct ksmbd_work *work) |
| 359 | { |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 360 | struct smb2_hdr *req = ksmbd_req_buf_next(work); |
| 361 | struct smb2_hdr *rsp = ksmbd_resp_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 362 | struct smb2_hdr *rsp_hdr; |
| 363 | struct smb2_hdr *rcv_hdr; |
| 364 | int next_hdr_offset = 0; |
| 365 | int len, new_len; |
| 366 | |
| 367 | /* Len of this response = updated RFC len - offset of previous cmd |
| 368 | * in the compound rsp |
| 369 | */ |
| 370 | |
| 371 | /* Storing the current local FID which may be needed by subsequent |
| 372 | * command in the compound request |
| 373 | */ |
| 374 | if (req->Command == SMB2_CREATE && rsp->Status == STATUS_SUCCESS) { |
| 375 | work->compound_fid = |
| 376 | le64_to_cpu(((struct smb2_create_rsp *)rsp)-> |
| 377 | VolatileFileId); |
| 378 | work->compound_pfid = |
| 379 | le64_to_cpu(((struct smb2_create_rsp *)rsp)-> |
| 380 | PersistentFileId); |
| 381 | work->compound_sid = le64_to_cpu(rsp->SessionId); |
| 382 | } |
| 383 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 384 | 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] | 385 | next_hdr_offset = le32_to_cpu(req->NextCommand); |
| 386 | |
| 387 | new_len = ALIGN(len, 8); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 388 | inc_rfc1001_len(work->response_buf, |
| 389 | sizeof(struct smb2_hdr) + new_len - len); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 390 | rsp->NextCommand = cpu_to_le32(new_len); |
| 391 | |
| 392 | work->next_smb2_rcv_hdr_off += next_hdr_offset; |
| 393 | work->next_smb2_rsp_hdr_off += new_len; |
| 394 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 395 | "Compound req new_len = %d rcv off = %d rsp off = %d\n", |
| 396 | new_len, work->next_smb2_rcv_hdr_off, |
| 397 | work->next_smb2_rsp_hdr_off); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 398 | |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 399 | rsp_hdr = ksmbd_resp_buf_next(work); |
| 400 | rcv_hdr = ksmbd_req_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 401 | |
| 402 | if (!(rcv_hdr->Flags & SMB2_FLAGS_RELATED_OPERATIONS)) { |
| 403 | ksmbd_debug(SMB, "related flag should be set\n"); |
| 404 | work->compound_fid = KSMBD_NO_FID; |
| 405 | work->compound_pfid = KSMBD_NO_FID; |
| 406 | } |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 407 | memset((char *)rsp_hdr, 0, sizeof(struct smb2_hdr) + 2); |
Namjae Jeon | 18a015b | 2021-09-22 21:00:57 +0900 | [diff] [blame] | 408 | rsp_hdr->ProtocolId = SMB2_PROTO_NUMBER; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 409 | rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE; |
| 410 | rsp_hdr->Command = rcv_hdr->Command; |
| 411 | |
| 412 | /* |
| 413 | * Message is response. We don't grant oplock yet. |
| 414 | */ |
| 415 | rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR | |
| 416 | SMB2_FLAGS_RELATED_OPERATIONS); |
| 417 | rsp_hdr->NextCommand = 0; |
| 418 | rsp_hdr->MessageId = rcv_hdr->MessageId; |
| 419 | rsp_hdr->Id.SyncId.ProcessId = rcv_hdr->Id.SyncId.ProcessId; |
| 420 | rsp_hdr->Id.SyncId.TreeId = rcv_hdr->Id.SyncId.TreeId; |
| 421 | rsp_hdr->SessionId = rcv_hdr->SessionId; |
| 422 | memcpy(rsp_hdr->Signature, rcv_hdr->Signature, 16); |
| 423 | } |
| 424 | |
| 425 | /** |
| 426 | * is_chained_smb2_message() - check for chained command |
| 427 | * @work: smb work containing smb request buffer |
| 428 | * |
| 429 | * Return: true if chained request, otherwise false |
| 430 | */ |
| 431 | bool is_chained_smb2_message(struct ksmbd_work *work) |
| 432 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 433 | struct smb2_hdr *hdr = smb2_get_msg(work->request_buf); |
Namjae Jeon | d72a9c1 | 2021-09-24 09:24:08 +0900 | [diff] [blame] | 434 | unsigned int len, next_cmd; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 435 | |
| 436 | if (hdr->ProtocolId != SMB2_PROTO_NUMBER) |
| 437 | return false; |
| 438 | |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 439 | hdr = ksmbd_req_buf_next(work); |
Namjae Jeon | d72a9c1 | 2021-09-24 09:24:08 +0900 | [diff] [blame] | 440 | next_cmd = le32_to_cpu(hdr->NextCommand); |
| 441 | if (next_cmd > 0) { |
| 442 | if ((u64)work->next_smb2_rcv_hdr_off + next_cmd + |
| 443 | __SMB2_HEADER_STRUCTURE_SIZE > |
| 444 | get_rfc1002_len(work->request_buf)) { |
| 445 | pr_err("next command(%u) offset exceeds smb msg size\n", |
| 446 | next_cmd); |
| 447 | return false; |
| 448 | } |
| 449 | |
Namjae Jeon | dbad630 | 2021-10-11 19:15:25 +0900 | [diff] [blame] | 450 | if ((u64)get_rfc1002_len(work->response_buf) + MAX_CIFS_SMALL_BUFFER_SIZE > |
| 451 | work->response_sz) { |
| 452 | pr_err("next response offset exceeds response buffer size\n"); |
| 453 | return false; |
| 454 | } |
| 455 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 456 | ksmbd_debug(SMB, "got SMB2 chained command\n"); |
| 457 | init_chained_smb2_rsp(work); |
| 458 | return true; |
| 459 | } else if (work->next_smb2_rcv_hdr_off) { |
| 460 | /* |
| 461 | * This is last request in chained command, |
| 462 | * align response to 8 byte |
| 463 | */ |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 464 | len = ALIGN(get_rfc1002_len(work->response_buf), 8); |
| 465 | len = len - get_rfc1002_len(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 466 | if (len) { |
| 467 | ksmbd_debug(SMB, "padding len %u\n", len); |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 468 | inc_rfc1001_len(work->response_buf, len); |
| 469 | if (work->aux_payload_sz) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 470 | work->aux_payload_sz += len; |
| 471 | } |
| 472 | } |
| 473 | return false; |
| 474 | } |
| 475 | |
| 476 | /** |
| 477 | * init_smb2_rsp_hdr() - initialize smb2 response |
| 478 | * @work: smb work containing smb request buffer |
| 479 | * |
| 480 | * Return: 0 |
| 481 | */ |
| 482 | int init_smb2_rsp_hdr(struct ksmbd_work *work) |
| 483 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 484 | struct smb2_hdr *rsp_hdr = smb2_get_msg(work->response_buf); |
| 485 | struct smb2_hdr *rcv_hdr = smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 486 | struct ksmbd_conn *conn = work->conn; |
| 487 | |
| 488 | memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 489 | *(__be32 *)work->response_buf = |
| 490 | cpu_to_be32(conn->vals->header_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 491 | rsp_hdr->ProtocolId = rcv_hdr->ProtocolId; |
| 492 | rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE; |
| 493 | rsp_hdr->Command = rcv_hdr->Command; |
| 494 | |
| 495 | /* |
| 496 | * Message is response. We don't grant oplock yet. |
| 497 | */ |
| 498 | rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR); |
| 499 | rsp_hdr->NextCommand = 0; |
| 500 | rsp_hdr->MessageId = rcv_hdr->MessageId; |
| 501 | rsp_hdr->Id.SyncId.ProcessId = rcv_hdr->Id.SyncId.ProcessId; |
| 502 | rsp_hdr->Id.SyncId.TreeId = rcv_hdr->Id.SyncId.TreeId; |
| 503 | rsp_hdr->SessionId = rcv_hdr->SessionId; |
| 504 | memcpy(rsp_hdr->Signature, rcv_hdr->Signature, 16); |
| 505 | |
| 506 | work->syncronous = true; |
| 507 | if (work->async_id) { |
Namjae Jeon | d40012a | 2021-04-13 13:06:30 +0900 | [diff] [blame] | 508 | ksmbd_release_id(&conn->async_ida, work->async_id); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 509 | work->async_id = 0; |
| 510 | } |
| 511 | |
| 512 | return 0; |
| 513 | } |
| 514 | |
| 515 | /** |
| 516 | * smb2_allocate_rsp_buf() - allocate smb2 response buffer |
| 517 | * @work: smb work containing smb request buffer |
| 518 | * |
| 519 | * Return: 0 on success, otherwise -ENOMEM |
| 520 | */ |
| 521 | int smb2_allocate_rsp_buf(struct ksmbd_work *work) |
| 522 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 523 | struct smb2_hdr *hdr = smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 524 | size_t small_sz = MAX_CIFS_SMALL_BUFFER_SIZE; |
Namjae Jeon | 4bc5947 | 2021-10-15 17:14:02 +0900 | [diff] [blame] | 525 | size_t large_sz = small_sz + work->conn->vals->max_trans_size; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 526 | size_t sz = small_sz; |
| 527 | int cmd = le16_to_cpu(hdr->Command); |
| 528 | |
Namjae Jeon | c30f4eb | 2021-06-18 10:17:37 +0900 | [diff] [blame] | 529 | if (cmd == SMB2_IOCTL_HE || cmd == SMB2_QUERY_DIRECTORY_HE) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 530 | sz = large_sz; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 531 | |
| 532 | if (cmd == SMB2_QUERY_INFO_HE) { |
| 533 | struct smb2_query_info_req *req; |
| 534 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 535 | req = smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 536 | if (req->InfoType == SMB2_O_INFO_FILE && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 537 | (req->FileInfoClass == FILE_FULL_EA_INFORMATION || |
Namjae Jeon | c30f4eb | 2021-06-18 10:17:37 +0900 | [diff] [blame] | 538 | req->FileInfoClass == FILE_ALL_INFORMATION)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 539 | sz = large_sz; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | /* allocate large response buf for chained commands */ |
| 543 | if (le32_to_cpu(hdr->NextCommand) > 0) |
| 544 | sz = large_sz; |
| 545 | |
Namjae Jeon | c30f4eb | 2021-06-18 10:17:37 +0900 | [diff] [blame] | 546 | work->response_buf = kvmalloc(sz, GFP_KERNEL | __GFP_ZERO); |
Namjae Jeon | 63c454f | 2021-04-20 14:24:28 +0900 | [diff] [blame] | 547 | if (!work->response_buf) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 548 | return -ENOMEM; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 549 | |
| 550 | work->response_sz = sz; |
| 551 | return 0; |
| 552 | } |
| 553 | |
| 554 | /** |
| 555 | * smb2_check_user_session() - check for valid session for a user |
| 556 | * @work: smb work containing smb request buffer |
| 557 | * |
| 558 | * Return: 0 on success, otherwise error |
| 559 | */ |
| 560 | int smb2_check_user_session(struct ksmbd_work *work) |
| 561 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 562 | struct smb2_hdr *req_hdr = smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 563 | struct ksmbd_conn *conn = work->conn; |
| 564 | unsigned int cmd = conn->ops->get_cmd_val(work); |
| 565 | unsigned long long sess_id; |
| 566 | |
| 567 | work->sess = NULL; |
| 568 | /* |
| 569 | * SMB2_ECHO, SMB2_NEGOTIATE, SMB2_SESSION_SETUP command do not |
| 570 | * require a session id, so no need to validate user session's for |
| 571 | * these commands. |
| 572 | */ |
| 573 | if (cmd == SMB2_ECHO_HE || cmd == SMB2_NEGOTIATE_HE || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 574 | cmd == SMB2_SESSION_SETUP_HE) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 575 | return 0; |
| 576 | |
| 577 | if (!ksmbd_conn_good(work)) |
| 578 | return -EINVAL; |
| 579 | |
| 580 | sess_id = le64_to_cpu(req_hdr->SessionId); |
| 581 | /* Check for validity of user session */ |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 582 | work->sess = ksmbd_session_lookup_all(conn, sess_id); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 583 | if (work->sess) |
| 584 | return 1; |
| 585 | ksmbd_debug(SMB, "Invalid user session, Uid %llu\n", sess_id); |
| 586 | return -EINVAL; |
| 587 | } |
| 588 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 589 | static void destroy_previous_session(struct ksmbd_user *user, u64 id) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 590 | { |
| 591 | struct ksmbd_session *prev_sess = ksmbd_session_lookup_slowpath(id); |
| 592 | struct ksmbd_user *prev_user; |
| 593 | |
| 594 | if (!prev_sess) |
| 595 | return; |
| 596 | |
| 597 | prev_user = prev_sess->user; |
| 598 | |
Marios Makassikis | 1fca803 | 2021-05-06 11:41:54 +0900 | [diff] [blame] | 599 | if (!prev_user || |
| 600 | strcmp(user->name, prev_user->name) || |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 601 | user->passkey_sz != prev_user->passkey_sz || |
| 602 | memcmp(user->passkey, prev_user->passkey, user->passkey_sz)) { |
| 603 | put_session(prev_sess); |
| 604 | return; |
| 605 | } |
| 606 | |
| 607 | put_session(prev_sess); |
| 608 | ksmbd_session_destroy(prev_sess); |
| 609 | } |
| 610 | |
| 611 | /** |
| 612 | * smb2_get_name() - get filename string from on the wire smb format |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 613 | * @share: ksmbd_share_config pointer |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 614 | * @src: source buffer |
| 615 | * @maxlen: maxlen of source string |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 616 | * @nls_table: nls_table pointer |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 617 | * |
| 618 | * Return: matching converted filename on success, otherwise error ptr |
| 619 | */ |
| 620 | static char * |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 621 | smb2_get_name(struct ksmbd_share_config *share, const char *src, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 622 | const int maxlen, struct nls_table *local_nls) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 623 | { |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 624 | char *name; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 625 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 626 | name = smb_strndup_from_utf16(src, maxlen, 1, local_nls); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 627 | if (IS_ERR(name)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 628 | pr_err("failed to get name %ld\n", PTR_ERR(name)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 629 | return name; |
| 630 | } |
| 631 | |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 632 | ksmbd_conv_path_to_unix(name); |
| 633 | ksmbd_strip_last_slash(name); |
| 634 | return name; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 635 | } |
| 636 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 637 | int setup_async_work(struct ksmbd_work *work, void (*fn)(void **), void **arg) |
| 638 | { |
| 639 | struct smb2_hdr *rsp_hdr; |
| 640 | struct ksmbd_conn *conn = work->conn; |
| 641 | int id; |
| 642 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 643 | rsp_hdr = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 644 | rsp_hdr->Flags |= SMB2_FLAGS_ASYNC_COMMAND; |
| 645 | |
Namjae Jeon | d40012a | 2021-04-13 13:06:30 +0900 | [diff] [blame] | 646 | id = ksmbd_acquire_async_msg_id(&conn->async_ida); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 647 | if (id < 0) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 648 | pr_err("Failed to alloc async message id\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 649 | return id; |
| 650 | } |
| 651 | work->syncronous = false; |
| 652 | work->async_id = id; |
| 653 | rsp_hdr->Id.AsyncId = cpu_to_le64(id); |
| 654 | |
| 655 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 656 | "Send interim Response to inform async request id : %d\n", |
| 657 | work->async_id); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 658 | |
| 659 | work->cancel_fn = fn; |
| 660 | work->cancel_argv = arg; |
| 661 | |
Namjae Jeon | 6c4e675 | 2021-06-07 09:08:45 +0900 | [diff] [blame] | 662 | if (list_empty(&work->async_request_entry)) { |
| 663 | spin_lock(&conn->request_lock); |
| 664 | list_add_tail(&work->async_request_entry, &conn->async_requests); |
| 665 | spin_unlock(&conn->request_lock); |
| 666 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 667 | |
| 668 | return 0; |
| 669 | } |
| 670 | |
| 671 | void smb2_send_interim_resp(struct ksmbd_work *work, __le32 status) |
| 672 | { |
| 673 | struct smb2_hdr *rsp_hdr; |
| 674 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 675 | rsp_hdr = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 676 | smb2_set_err_rsp(work); |
| 677 | rsp_hdr->Status = status; |
| 678 | |
| 679 | work->multiRsp = 1; |
| 680 | ksmbd_conn_write(work); |
| 681 | rsp_hdr->Status = 0; |
| 682 | work->multiRsp = 0; |
| 683 | } |
| 684 | |
| 685 | static __le32 smb2_get_reparse_tag_special_file(umode_t mode) |
| 686 | { |
| 687 | if (S_ISDIR(mode) || S_ISREG(mode)) |
| 688 | return 0; |
| 689 | |
| 690 | if (S_ISLNK(mode)) |
| 691 | return IO_REPARSE_TAG_LX_SYMLINK_LE; |
| 692 | else if (S_ISFIFO(mode)) |
| 693 | return IO_REPARSE_TAG_LX_FIFO_LE; |
| 694 | else if (S_ISSOCK(mode)) |
| 695 | return IO_REPARSE_TAG_AF_UNIX_LE; |
| 696 | else if (S_ISCHR(mode)) |
| 697 | return IO_REPARSE_TAG_LX_CHR_LE; |
| 698 | else if (S_ISBLK(mode)) |
| 699 | return IO_REPARSE_TAG_LX_BLK_LE; |
| 700 | |
| 701 | return 0; |
| 702 | } |
| 703 | |
| 704 | /** |
| 705 | * smb2_get_dos_mode() - get file mode in dos format from unix mode |
| 706 | * @stat: kstat containing file mode |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 707 | * @attribute: attribute flags |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 708 | * |
| 709 | * Return: converted dos mode |
| 710 | */ |
| 711 | static int smb2_get_dos_mode(struct kstat *stat, int attribute) |
| 712 | { |
| 713 | int attr = 0; |
| 714 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 715 | if (S_ISDIR(stat->mode)) { |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 716 | attr = FILE_ATTRIBUTE_DIRECTORY | |
| 717 | (attribute & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 718 | } else { |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 719 | attr = (attribute & 0x00005137) | FILE_ATTRIBUTE_ARCHIVE; |
| 720 | attr &= ~(FILE_ATTRIBUTE_DIRECTORY); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 721 | if (S_ISREG(stat->mode) && (server_conf.share_fake_fscaps & |
| 722 | FILE_SUPPORTS_SPARSE_FILES)) |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 723 | attr |= FILE_ATTRIBUTE_SPARSE_FILE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 724 | |
| 725 | if (smb2_get_reparse_tag_special_file(stat->mode)) |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 726 | attr |= FILE_ATTRIBUTE_REPARSE_POINT; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | return attr; |
| 730 | } |
| 731 | |
| 732 | static void build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 733 | __le16 hash_id) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 734 | { |
| 735 | pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES; |
| 736 | pneg_ctxt->DataLength = cpu_to_le16(38); |
| 737 | pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1); |
| 738 | pneg_ctxt->Reserved = cpu_to_le32(0); |
| 739 | pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE); |
| 740 | get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE); |
| 741 | pneg_ctxt->HashAlgorithms = hash_id; |
| 742 | } |
| 743 | |
| 744 | static void build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 745 | __le16 cipher_type) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 746 | { |
| 747 | pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES; |
| 748 | pneg_ctxt->DataLength = cpu_to_le16(4); |
| 749 | pneg_ctxt->Reserved = cpu_to_le32(0); |
| 750 | pneg_ctxt->CipherCount = cpu_to_le16(1); |
| 751 | pneg_ctxt->Ciphers[0] = cipher_type; |
| 752 | } |
| 753 | |
Ronnie Sahlberg | d6c9ad23b | 2021-11-03 08:44:38 +0900 | [diff] [blame] | 754 | static void build_compression_ctxt(struct smb2_compression_capabilities_context *pneg_ctxt, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 755 | __le16 comp_algo) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 756 | { |
| 757 | pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES; |
| 758 | pneg_ctxt->DataLength = |
Ronnie Sahlberg | d6c9ad23b | 2021-11-03 08:44:38 +0900 | [diff] [blame] | 759 | cpu_to_le16(sizeof(struct smb2_compression_capabilities_context) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 760 | - sizeof(struct smb2_neg_context)); |
| 761 | pneg_ctxt->Reserved = cpu_to_le32(0); |
| 762 | pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(1); |
Ronnie Sahlberg | d6c9ad23b | 2021-11-03 08:44:38 +0900 | [diff] [blame] | 763 | pneg_ctxt->Flags = cpu_to_le32(0); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 764 | pneg_ctxt->CompressionAlgorithms[0] = comp_algo; |
| 765 | } |
| 766 | |
Namjae Jeon | 378087c | 2021-07-21 10:05:53 +0900 | [diff] [blame] | 767 | static void build_sign_cap_ctxt(struct smb2_signing_capabilities *pneg_ctxt, |
| 768 | __le16 sign_algo) |
| 769 | { |
| 770 | pneg_ctxt->ContextType = SMB2_SIGNING_CAPABILITIES; |
| 771 | pneg_ctxt->DataLength = |
| 772 | cpu_to_le16((sizeof(struct smb2_signing_capabilities) + 2) |
| 773 | - sizeof(struct smb2_neg_context)); |
| 774 | pneg_ctxt->Reserved = cpu_to_le32(0); |
| 775 | pneg_ctxt->SigningAlgorithmCount = cpu_to_le16(1); |
| 776 | pneg_ctxt->SigningAlgorithms[0] = sign_algo; |
| 777 | } |
| 778 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 779 | static void build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 780 | { |
| 781 | pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE; |
| 782 | pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN); |
| 783 | /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */ |
| 784 | pneg_ctxt->Name[0] = 0x93; |
| 785 | pneg_ctxt->Name[1] = 0xAD; |
| 786 | pneg_ctxt->Name[2] = 0x25; |
| 787 | pneg_ctxt->Name[3] = 0x50; |
| 788 | pneg_ctxt->Name[4] = 0x9C; |
| 789 | pneg_ctxt->Name[5] = 0xB4; |
| 790 | pneg_ctxt->Name[6] = 0x11; |
| 791 | pneg_ctxt->Name[7] = 0xE7; |
| 792 | pneg_ctxt->Name[8] = 0xB4; |
| 793 | pneg_ctxt->Name[9] = 0x23; |
| 794 | pneg_ctxt->Name[10] = 0x83; |
| 795 | pneg_ctxt->Name[11] = 0xDE; |
| 796 | pneg_ctxt->Name[12] = 0x96; |
| 797 | pneg_ctxt->Name[13] = 0x8B; |
| 798 | pneg_ctxt->Name[14] = 0xCD; |
| 799 | pneg_ctxt->Name[15] = 0x7C; |
| 800 | } |
| 801 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 802 | static void assemble_neg_contexts(struct ksmbd_conn *conn, |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 803 | struct smb2_negotiate_rsp *rsp, |
| 804 | void *smb2_buf_len) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 805 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 806 | char *pneg_ctxt = (char *)rsp + |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 807 | le32_to_cpu(rsp->NegotiateContextOffset); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 808 | int neg_ctxt_cnt = 1; |
| 809 | int ctxt_size; |
| 810 | |
| 811 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 812 | "assemble SMB2_PREAUTH_INTEGRITY_CAPABILITIES context\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 813 | build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 814 | conn->preauth_info->Preauth_HashId); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 815 | rsp->NegotiateContextCount = cpu_to_le16(neg_ctxt_cnt); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 816 | inc_rfc1001_len(smb2_buf_len, AUTH_GSS_PADDING); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 817 | ctxt_size = sizeof(struct smb2_preauth_neg_context); |
| 818 | /* Round to 8 byte boundary */ |
| 819 | pneg_ctxt += round_up(sizeof(struct smb2_preauth_neg_context), 8); |
| 820 | |
| 821 | if (conn->cipher_type) { |
| 822 | ctxt_size = round_up(ctxt_size, 8); |
| 823 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 824 | "assemble SMB2_ENCRYPTION_CAPABILITIES context\n"); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 825 | build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 826 | conn->cipher_type); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 827 | rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt); |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 828 | ctxt_size += sizeof(struct smb2_encryption_neg_context) + 2; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 829 | /* Round to 8 byte boundary */ |
| 830 | pneg_ctxt += |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 831 | round_up(sizeof(struct smb2_encryption_neg_context) + 2, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 832 | 8); |
| 833 | } |
| 834 | |
| 835 | if (conn->compress_algorithm) { |
| 836 | ctxt_size = round_up(ctxt_size, 8); |
| 837 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 838 | "assemble SMB2_COMPRESSION_CAPABILITIES context\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 839 | /* Temporarily set to SMB3_COMPRESS_NONE */ |
Ronnie Sahlberg | d6c9ad23b | 2021-11-03 08:44:38 +0900 | [diff] [blame] | 840 | build_compression_ctxt((struct smb2_compression_capabilities_context *)pneg_ctxt, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 841 | conn->compress_algorithm); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 842 | rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt); |
Ronnie Sahlberg | d6c9ad23b | 2021-11-03 08:44:38 +0900 | [diff] [blame] | 843 | ctxt_size += sizeof(struct smb2_compression_capabilities_context) + 2; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 844 | /* Round to 8 byte boundary */ |
Ronnie Sahlberg | d6c9ad23b | 2021-11-03 08:44:38 +0900 | [diff] [blame] | 845 | pneg_ctxt += round_up(sizeof(struct smb2_compression_capabilities_context) + 2, |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 846 | 8); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | if (conn->posix_ext_supported) { |
| 850 | ctxt_size = round_up(ctxt_size, 8); |
| 851 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 852 | "assemble SMB2_POSIX_EXTENSIONS_AVAILABLE context\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 853 | build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt); |
| 854 | rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt); |
| 855 | ctxt_size += sizeof(struct smb2_posix_neg_context); |
Namjae Jeon | 378087c | 2021-07-21 10:05:53 +0900 | [diff] [blame] | 856 | /* Round to 8 byte boundary */ |
| 857 | pneg_ctxt += round_up(sizeof(struct smb2_posix_neg_context), 8); |
| 858 | } |
| 859 | |
| 860 | if (conn->signing_negotiated) { |
| 861 | ctxt_size = round_up(ctxt_size, 8); |
| 862 | ksmbd_debug(SMB, |
| 863 | "assemble SMB2_SIGNING_CAPABILITIES context\n"); |
| 864 | build_sign_cap_ctxt((struct smb2_signing_capabilities *)pneg_ctxt, |
| 865 | conn->signing_algorithm); |
| 866 | rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt); |
| 867 | ctxt_size += sizeof(struct smb2_signing_capabilities) + 2; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 868 | } |
| 869 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 870 | inc_rfc1001_len(smb2_buf_len, ctxt_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 871 | } |
| 872 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 873 | static __le32 decode_preauth_ctxt(struct ksmbd_conn *conn, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 874 | struct smb2_preauth_neg_context *pneg_ctxt) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 875 | { |
| 876 | __le32 err = STATUS_NO_PREAUTH_INTEGRITY_HASH_OVERLAP; |
| 877 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 878 | if (pneg_ctxt->HashAlgorithms == SMB2_PREAUTH_INTEGRITY_SHA512) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 879 | conn->preauth_info->Preauth_HashId = |
| 880 | SMB2_PREAUTH_INTEGRITY_SHA512; |
| 881 | err = STATUS_SUCCESS; |
| 882 | } |
| 883 | |
| 884 | return err; |
| 885 | } |
| 886 | |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 887 | static void decode_encrypt_ctxt(struct ksmbd_conn *conn, |
| 888 | struct smb2_encryption_neg_context *pneg_ctxt, |
| 889 | int len_of_ctxts) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 890 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 891 | int cph_cnt = le16_to_cpu(pneg_ctxt->CipherCount); |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 892 | int i, cphs_size = cph_cnt * sizeof(__le16); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 893 | |
| 894 | conn->cipher_type = 0; |
| 895 | |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 896 | if (sizeof(struct smb2_encryption_neg_context) + cphs_size > |
| 897 | len_of_ctxts) { |
| 898 | pr_err("Invalid cipher count(%d)\n", cph_cnt); |
| 899 | return; |
| 900 | } |
| 901 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 902 | if (!(server_conf.flags & KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION)) |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 903 | return; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 904 | |
| 905 | for (i = 0; i < cph_cnt; i++) { |
| 906 | if (pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES128_GCM || |
Namjae Jeon | 5a0ca77 | 2021-05-06 11:43:37 +0900 | [diff] [blame] | 907 | pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES128_CCM || |
| 908 | pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES256_CCM || |
| 909 | pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES256_GCM) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 910 | ksmbd_debug(SMB, "Cipher ID = 0x%x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 911 | pneg_ctxt->Ciphers[i]); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 912 | conn->cipher_type = pneg_ctxt->Ciphers[i]; |
| 913 | break; |
| 914 | } |
| 915 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 916 | } |
| 917 | |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 918 | static void decode_compress_ctxt(struct ksmbd_conn *conn, |
Ronnie Sahlberg | d6c9ad23b | 2021-11-03 08:44:38 +0900 | [diff] [blame] | 919 | struct smb2_compression_capabilities_context *pneg_ctxt) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 920 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 921 | conn->compress_algorithm = SMB3_COMPRESS_NONE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 922 | } |
| 923 | |
Namjae Jeon | 378087c | 2021-07-21 10:05:53 +0900 | [diff] [blame] | 924 | static void decode_sign_cap_ctxt(struct ksmbd_conn *conn, |
| 925 | struct smb2_signing_capabilities *pneg_ctxt, |
| 926 | int len_of_ctxts) |
| 927 | { |
| 928 | int sign_algo_cnt = le16_to_cpu(pneg_ctxt->SigningAlgorithmCount); |
| 929 | int i, sign_alos_size = sign_algo_cnt * sizeof(__le16); |
| 930 | |
| 931 | conn->signing_negotiated = false; |
| 932 | |
| 933 | if (sizeof(struct smb2_signing_capabilities) + sign_alos_size > |
| 934 | len_of_ctxts) { |
| 935 | pr_err("Invalid signing algorithm count(%d)\n", sign_algo_cnt); |
| 936 | return; |
| 937 | } |
| 938 | |
| 939 | for (i = 0; i < sign_algo_cnt; i++) { |
Ronnie Sahlberg | d6c9ad23b | 2021-11-03 08:44:38 +0900 | [diff] [blame] | 940 | if (pneg_ctxt->SigningAlgorithms[i] == SIGNING_ALG_HMAC_SHA256_LE || |
| 941 | pneg_ctxt->SigningAlgorithms[i] == SIGNING_ALG_AES_CMAC_LE) { |
Namjae Jeon | 378087c | 2021-07-21 10:05:53 +0900 | [diff] [blame] | 942 | ksmbd_debug(SMB, "Signing Algorithm ID = 0x%x\n", |
| 943 | pneg_ctxt->SigningAlgorithms[i]); |
| 944 | conn->signing_negotiated = true; |
| 945 | conn->signing_algorithm = |
| 946 | pneg_ctxt->SigningAlgorithms[i]; |
| 947 | break; |
| 948 | } |
| 949 | } |
| 950 | } |
| 951 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 952 | static __le32 deassemble_neg_contexts(struct ksmbd_conn *conn, |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 953 | struct smb2_negotiate_req *req, |
| 954 | int len_of_smb) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 955 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 956 | /* +4 is to account for the RFC1001 len field */ |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 957 | struct smb2_neg_context *pctx = (struct smb2_neg_context *)req; |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 958 | int i = 0, len_of_ctxts; |
| 959 | int offset = le32_to_cpu(req->NegotiateContextOffset); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 960 | int neg_ctxt_cnt = le16_to_cpu(req->NegotiateContextCount); |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 961 | __le32 status = STATUS_INVALID_PARAMETER; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 962 | |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 963 | ksmbd_debug(SMB, "decoding %d negotiate contexts\n", neg_ctxt_cnt); |
| 964 | if (len_of_smb <= offset) { |
| 965 | ksmbd_debug(SMB, "Invalid response: negotiate context offset\n"); |
| 966 | return status; |
| 967 | } |
| 968 | |
| 969 | len_of_ctxts = len_of_smb - offset; |
| 970 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 971 | while (i++ < neg_ctxt_cnt) { |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 972 | int clen; |
| 973 | |
| 974 | /* check that offset is not beyond end of SMB */ |
| 975 | if (len_of_ctxts == 0) |
| 976 | break; |
| 977 | |
| 978 | if (len_of_ctxts < sizeof(struct smb2_neg_context)) |
| 979 | break; |
| 980 | |
| 981 | pctx = (struct smb2_neg_context *)((char *)pctx + offset); |
| 982 | clen = le16_to_cpu(pctx->DataLength); |
| 983 | if (clen + sizeof(struct smb2_neg_context) > len_of_ctxts) |
| 984 | break; |
| 985 | |
| 986 | if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 987 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 988 | "deassemble SMB2_PREAUTH_INTEGRITY_CAPABILITIES context\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 989 | if (conn->preauth_info->Preauth_HashId) |
| 990 | break; |
| 991 | |
| 992 | status = decode_preauth_ctxt(conn, |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 993 | (struct smb2_preauth_neg_context *)pctx); |
| 994 | if (status != STATUS_SUCCESS) |
| 995 | break; |
| 996 | } else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 997 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 998 | "deassemble SMB2_ENCRYPTION_CAPABILITIES context\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 999 | if (conn->cipher_type) |
| 1000 | break; |
| 1001 | |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 1002 | decode_encrypt_ctxt(conn, |
| 1003 | (struct smb2_encryption_neg_context *)pctx, |
| 1004 | len_of_ctxts); |
| 1005 | } else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1006 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1007 | "deassemble SMB2_COMPRESSION_CAPABILITIES context\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1008 | if (conn->compress_algorithm) |
| 1009 | break; |
| 1010 | |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 1011 | decode_compress_ctxt(conn, |
Ronnie Sahlberg | d6c9ad23b | 2021-11-03 08:44:38 +0900 | [diff] [blame] | 1012 | (struct smb2_compression_capabilities_context *)pctx); |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 1013 | } else if (pctx->ContextType == SMB2_NETNAME_NEGOTIATE_CONTEXT_ID) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1014 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1015 | "deassemble SMB2_NETNAME_NEGOTIATE_CONTEXT_ID context\n"); |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 1016 | } else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1017 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1018 | "deassemble SMB2_POSIX_EXTENSIONS_AVAILABLE context\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1019 | conn->posix_ext_supported = true; |
Namjae Jeon | 378087c | 2021-07-21 10:05:53 +0900 | [diff] [blame] | 1020 | } else if (pctx->ContextType == SMB2_SIGNING_CAPABILITIES) { |
| 1021 | ksmbd_debug(SMB, |
| 1022 | "deassemble SMB2_SIGNING_CAPABILITIES context\n"); |
| 1023 | decode_sign_cap_ctxt(conn, |
| 1024 | (struct smb2_signing_capabilities *)pctx, |
| 1025 | len_of_ctxts); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1026 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1027 | |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 1028 | /* offsets must be 8 byte aligned */ |
| 1029 | clen = (clen + 7) & ~0x7; |
| 1030 | offset = clen + sizeof(struct smb2_neg_context); |
| 1031 | len_of_ctxts -= clen + sizeof(struct smb2_neg_context); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1032 | } |
| 1033 | return status; |
| 1034 | } |
| 1035 | |
| 1036 | /** |
| 1037 | * smb2_handle_negotiate() - handler for smb2 negotiate command |
| 1038 | * @work: smb work containing smb request buffer |
| 1039 | * |
| 1040 | * Return: 0 |
| 1041 | */ |
| 1042 | int smb2_handle_negotiate(struct ksmbd_work *work) |
| 1043 | { |
| 1044 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1045 | struct smb2_negotiate_req *req = smb2_get_msg(work->request_buf); |
| 1046 | struct smb2_negotiate_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1047 | int rc = 0; |
Namjae Jeon | 442ff9e | 2021-09-29 15:44:32 +0900 | [diff] [blame] | 1048 | unsigned int smb2_buf_len, smb2_neg_size; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1049 | __le32 status; |
| 1050 | |
| 1051 | ksmbd_debug(SMB, "Received negotiate request\n"); |
| 1052 | conn->need_neg = false; |
| 1053 | if (ksmbd_conn_good(work)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 1054 | pr_err("conn->tcp_status is already in CifsGood State\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1055 | work->send_no_response = 1; |
| 1056 | return rc; |
| 1057 | } |
| 1058 | |
| 1059 | if (req->DialectCount == 0) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 1060 | pr_err("malformed packet\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1061 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 1062 | rc = -EINVAL; |
| 1063 | goto err_out; |
| 1064 | } |
| 1065 | |
Namjae Jeon | 442ff9e | 2021-09-29 15:44:32 +0900 | [diff] [blame] | 1066 | smb2_buf_len = get_rfc1002_len(work->request_buf); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1067 | smb2_neg_size = offsetof(struct smb2_negotiate_req, Dialects); |
Namjae Jeon | 442ff9e | 2021-09-29 15:44:32 +0900 | [diff] [blame] | 1068 | if (smb2_neg_size > smb2_buf_len) { |
| 1069 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 1070 | rc = -EINVAL; |
| 1071 | goto err_out; |
| 1072 | } |
| 1073 | |
| 1074 | if (conn->dialect == SMB311_PROT_ID) { |
| 1075 | unsigned int nego_ctxt_off = le32_to_cpu(req->NegotiateContextOffset); |
| 1076 | |
| 1077 | if (smb2_buf_len < nego_ctxt_off) { |
| 1078 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 1079 | rc = -EINVAL; |
| 1080 | goto err_out; |
| 1081 | } |
| 1082 | |
| 1083 | if (smb2_neg_size > nego_ctxt_off) { |
| 1084 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 1085 | rc = -EINVAL; |
| 1086 | goto err_out; |
| 1087 | } |
| 1088 | |
| 1089 | if (smb2_neg_size + le16_to_cpu(req->DialectCount) * sizeof(__le16) > |
| 1090 | nego_ctxt_off) { |
| 1091 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 1092 | rc = -EINVAL; |
| 1093 | goto err_out; |
| 1094 | } |
| 1095 | } else { |
| 1096 | if (smb2_neg_size + le16_to_cpu(req->DialectCount) * sizeof(__le16) > |
| 1097 | smb2_buf_len) { |
| 1098 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 1099 | rc = -EINVAL; |
| 1100 | goto err_out; |
| 1101 | } |
| 1102 | } |
| 1103 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1104 | conn->cli_cap = le32_to_cpu(req->Capabilities); |
| 1105 | switch (conn->dialect) { |
| 1106 | case SMB311_PROT_ID: |
| 1107 | conn->preauth_info = |
| 1108 | kzalloc(sizeof(struct preauth_integrity_info), |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1109 | GFP_KERNEL); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1110 | if (!conn->preauth_info) { |
| 1111 | rc = -ENOMEM; |
| 1112 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 1113 | goto err_out; |
| 1114 | } |
| 1115 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1116 | status = deassemble_neg_contexts(conn, req, |
| 1117 | get_rfc1002_len(work->request_buf)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1118 | if (status != STATUS_SUCCESS) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 1119 | pr_err("deassemble_neg_contexts error(0x%x)\n", |
| 1120 | status); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1121 | rsp->hdr.Status = status; |
| 1122 | rc = -EINVAL; |
| 1123 | goto err_out; |
| 1124 | } |
| 1125 | |
| 1126 | rc = init_smb3_11_server(conn); |
| 1127 | if (rc < 0) { |
| 1128 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 1129 | goto err_out; |
| 1130 | } |
| 1131 | |
| 1132 | ksmbd_gen_preauth_integrity_hash(conn, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1133 | work->request_buf, |
| 1134 | conn->preauth_info->Preauth_HashValue); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1135 | rsp->NegotiateContextOffset = |
| 1136 | cpu_to_le32(OFFSET_OF_NEG_CONTEXT); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1137 | assemble_neg_contexts(conn, rsp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1138 | break; |
| 1139 | case SMB302_PROT_ID: |
| 1140 | init_smb3_02_server(conn); |
| 1141 | break; |
| 1142 | case SMB30_PROT_ID: |
| 1143 | init_smb3_0_server(conn); |
| 1144 | break; |
| 1145 | case SMB21_PROT_ID: |
| 1146 | init_smb2_1_server(conn); |
| 1147 | break; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1148 | case SMB2X_PROT_ID: |
| 1149 | case BAD_PROT_ID: |
| 1150 | default: |
| 1151 | ksmbd_debug(SMB, "Server dialect :0x%x not supported\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1152 | conn->dialect); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1153 | rsp->hdr.Status = STATUS_NOT_SUPPORTED; |
| 1154 | rc = -EINVAL; |
| 1155 | goto err_out; |
| 1156 | } |
| 1157 | rsp->Capabilities = cpu_to_le32(conn->vals->capabilities); |
| 1158 | |
| 1159 | /* For stats */ |
| 1160 | conn->connection_type = conn->dialect; |
| 1161 | |
| 1162 | rsp->MaxTransactSize = cpu_to_le32(conn->vals->max_trans_size); |
| 1163 | rsp->MaxReadSize = cpu_to_le32(conn->vals->max_read_size); |
| 1164 | rsp->MaxWriteSize = cpu_to_le32(conn->vals->max_write_size); |
| 1165 | |
Namjae Jeon | 51a1387 | 2021-09-29 13:09:24 +0900 | [diff] [blame] | 1166 | memcpy(conn->ClientGUID, req->ClientGUID, |
| 1167 | SMB2_CLIENT_GUID_SIZE); |
| 1168 | conn->cli_sec_mode = le16_to_cpu(req->SecurityMode); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1169 | |
| 1170 | rsp->StructureSize = cpu_to_le16(65); |
| 1171 | rsp->DialectRevision = cpu_to_le16(conn->dialect); |
| 1172 | /* Not setting conn guid rsp->ServerGUID, as it |
| 1173 | * not used by client for identifying server |
| 1174 | */ |
| 1175 | memset(rsp->ServerGUID, 0, SMB2_CLIENT_GUID_SIZE); |
| 1176 | |
| 1177 | rsp->SystemTime = cpu_to_le64(ksmbd_systime()); |
| 1178 | rsp->ServerStartTime = 0; |
| 1179 | ksmbd_debug(SMB, "negotiate context offset %d, count %d\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1180 | le32_to_cpu(rsp->NegotiateContextOffset), |
| 1181 | le16_to_cpu(rsp->NegotiateContextCount)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1182 | |
| 1183 | rsp->SecurityBufferOffset = cpu_to_le16(128); |
| 1184 | rsp->SecurityBufferLength = cpu_to_le16(AUTH_GSS_LENGTH); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1185 | ksmbd_copy_gss_neg_header((char *)(&rsp->hdr) + |
| 1186 | le16_to_cpu(rsp->SecurityBufferOffset)); |
| 1187 | inc_rfc1001_len(work->response_buf, sizeof(struct smb2_negotiate_rsp) - |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1188 | sizeof(struct smb2_hdr) - sizeof(rsp->Buffer) + |
| 1189 | AUTH_GSS_LENGTH); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1190 | rsp->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED_LE; |
| 1191 | conn->use_spnego = true; |
| 1192 | |
| 1193 | if ((server_conf.signing == KSMBD_CONFIG_OPT_AUTO || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1194 | server_conf.signing == KSMBD_CONFIG_OPT_DISABLED) && |
| 1195 | req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED_LE) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1196 | conn->sign = true; |
| 1197 | else if (server_conf.signing == KSMBD_CONFIG_OPT_MANDATORY) { |
| 1198 | server_conf.enforced_signing = true; |
| 1199 | rsp->SecurityMode |= SMB2_NEGOTIATE_SIGNING_REQUIRED_LE; |
| 1200 | conn->sign = true; |
| 1201 | } |
| 1202 | |
| 1203 | conn->srv_sec_mode = le16_to_cpu(rsp->SecurityMode); |
| 1204 | ksmbd_conn_set_need_negotiate(work); |
| 1205 | |
| 1206 | err_out: |
| 1207 | if (rc < 0) |
| 1208 | smb2_set_err_rsp(work); |
| 1209 | |
| 1210 | return rc; |
| 1211 | } |
| 1212 | |
| 1213 | static int alloc_preauth_hash(struct ksmbd_session *sess, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1214 | struct ksmbd_conn *conn) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1215 | { |
| 1216 | if (sess->Preauth_HashValue) |
| 1217 | return 0; |
| 1218 | |
kernel test robot | 86f5297 | 2021-04-02 12:17:24 +0900 | [diff] [blame] | 1219 | sess->Preauth_HashValue = kmemdup(conn->preauth_info->Preauth_HashValue, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1220 | PREAUTH_HASHVALUE_SIZE, GFP_KERNEL); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1221 | if (!sess->Preauth_HashValue) |
| 1222 | return -ENOMEM; |
| 1223 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1224 | return 0; |
| 1225 | } |
| 1226 | |
| 1227 | static int generate_preauth_hash(struct ksmbd_work *work) |
| 1228 | { |
| 1229 | struct ksmbd_conn *conn = work->conn; |
| 1230 | struct ksmbd_session *sess = work->sess; |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1231 | u8 *preauth_hash; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1232 | |
| 1233 | if (conn->dialect != SMB311_PROT_ID) |
| 1234 | return 0; |
| 1235 | |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1236 | if (conn->binding) { |
| 1237 | struct preauth_session *preauth_sess; |
| 1238 | |
| 1239 | preauth_sess = ksmbd_preauth_session_lookup(conn, sess->id); |
| 1240 | if (!preauth_sess) { |
| 1241 | preauth_sess = ksmbd_preauth_session_alloc(conn, sess->id); |
| 1242 | if (!preauth_sess) |
| 1243 | return -ENOMEM; |
| 1244 | } |
| 1245 | |
| 1246 | preauth_hash = preauth_sess->Preauth_HashValue; |
| 1247 | } else { |
| 1248 | if (!sess->Preauth_HashValue) |
| 1249 | if (alloc_preauth_hash(sess, conn)) |
| 1250 | return -ENOMEM; |
| 1251 | preauth_hash = sess->Preauth_HashValue; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1252 | } |
| 1253 | |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1254 | ksmbd_gen_preauth_integrity_hash(conn, work->request_buf, preauth_hash); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1255 | return 0; |
| 1256 | } |
| 1257 | |
Marios Makassikis | 0d994cd | 2021-10-19 17:39:38 +0200 | [diff] [blame] | 1258 | static int decode_negotiation_token(struct ksmbd_conn *conn, |
| 1259 | struct negotiate_message *negblob, |
| 1260 | size_t sz) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1261 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1262 | if (!conn->use_spnego) |
| 1263 | return -EINVAL; |
| 1264 | |
Hyunchul Lee | fad4161 | 2021-04-19 17:26:15 +0900 | [diff] [blame] | 1265 | if (ksmbd_decode_negTokenInit((char *)negblob, sz, conn)) { |
| 1266 | if (ksmbd_decode_negTokenTarg((char *)negblob, sz, conn)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1267 | conn->auth_mechs |= KSMBD_AUTH_NTLMSSP; |
| 1268 | conn->preferred_auth_mech = KSMBD_AUTH_NTLMSSP; |
| 1269 | conn->use_spnego = false; |
| 1270 | } |
| 1271 | } |
| 1272 | return 0; |
| 1273 | } |
| 1274 | |
| 1275 | static int ntlm_negotiate(struct ksmbd_work *work, |
Marios Makassikis | 0d994cd | 2021-10-19 17:39:38 +0200 | [diff] [blame] | 1276 | struct negotiate_message *negblob, |
| 1277 | size_t negblob_len) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1278 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1279 | struct smb2_sess_setup_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1280 | struct challenge_message *chgblob; |
| 1281 | unsigned char *spnego_blob = NULL; |
| 1282 | u16 spnego_blob_len; |
| 1283 | char *neg_blob; |
| 1284 | int sz, rc; |
| 1285 | |
| 1286 | ksmbd_debug(SMB, "negotiate phase\n"); |
Marios Makassikis | 0d994cd | 2021-10-19 17:39:38 +0200 | [diff] [blame] | 1287 | rc = ksmbd_decode_ntlmssp_neg_blob(negblob, negblob_len, work->sess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1288 | if (rc) |
| 1289 | return rc; |
| 1290 | |
| 1291 | sz = le16_to_cpu(rsp->SecurityBufferOffset); |
| 1292 | chgblob = |
| 1293 | (struct challenge_message *)((char *)&rsp->hdr.ProtocolId + sz); |
| 1294 | memset(chgblob, 0, sizeof(struct challenge_message)); |
| 1295 | |
| 1296 | if (!work->conn->use_spnego) { |
| 1297 | sz = ksmbd_build_ntlmssp_challenge_blob(chgblob, work->sess); |
| 1298 | if (sz < 0) |
| 1299 | return -ENOMEM; |
| 1300 | |
| 1301 | rsp->SecurityBufferLength = cpu_to_le16(sz); |
| 1302 | return 0; |
| 1303 | } |
| 1304 | |
| 1305 | sz = sizeof(struct challenge_message); |
| 1306 | sz += (strlen(ksmbd_netbios_name()) * 2 + 1 + 4) * 6; |
| 1307 | |
| 1308 | neg_blob = kzalloc(sz, GFP_KERNEL); |
| 1309 | if (!neg_blob) |
| 1310 | return -ENOMEM; |
| 1311 | |
| 1312 | chgblob = (struct challenge_message *)neg_blob; |
| 1313 | sz = ksmbd_build_ntlmssp_challenge_blob(chgblob, work->sess); |
| 1314 | if (sz < 0) { |
| 1315 | rc = -ENOMEM; |
| 1316 | goto out; |
| 1317 | } |
| 1318 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1319 | rc = build_spnego_ntlmssp_neg_blob(&spnego_blob, &spnego_blob_len, |
| 1320 | neg_blob, sz); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1321 | if (rc) { |
| 1322 | rc = -ENOMEM; |
| 1323 | goto out; |
| 1324 | } |
| 1325 | |
| 1326 | sz = le16_to_cpu(rsp->SecurityBufferOffset); |
| 1327 | memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len); |
| 1328 | rsp->SecurityBufferLength = cpu_to_le16(spnego_blob_len); |
| 1329 | |
| 1330 | out: |
| 1331 | kfree(spnego_blob); |
| 1332 | kfree(neg_blob); |
| 1333 | return rc; |
| 1334 | } |
| 1335 | |
| 1336 | static struct authenticate_message *user_authblob(struct ksmbd_conn *conn, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1337 | struct smb2_sess_setup_req *req) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1338 | { |
| 1339 | int sz; |
| 1340 | |
| 1341 | if (conn->use_spnego && conn->mechToken) |
| 1342 | return (struct authenticate_message *)conn->mechToken; |
| 1343 | |
| 1344 | sz = le16_to_cpu(req->SecurityBufferOffset); |
| 1345 | return (struct authenticate_message *)((char *)&req->hdr.ProtocolId |
| 1346 | + sz); |
| 1347 | } |
| 1348 | |
| 1349 | static struct ksmbd_user *session_user(struct ksmbd_conn *conn, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1350 | struct smb2_sess_setup_req *req) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1351 | { |
| 1352 | struct authenticate_message *authblob; |
| 1353 | struct ksmbd_user *user; |
| 1354 | char *name; |
Marios Makassikis | 0d994cd | 2021-10-19 17:39:38 +0200 | [diff] [blame] | 1355 | unsigned int auth_msg_len, name_off, name_len, secbuf_len; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1356 | |
Marios Makassikis | 0d994cd | 2021-10-19 17:39:38 +0200 | [diff] [blame] | 1357 | secbuf_len = le16_to_cpu(req->SecurityBufferLength); |
| 1358 | if (secbuf_len < sizeof(struct authenticate_message)) { |
| 1359 | ksmbd_debug(SMB, "blob len %d too small\n", secbuf_len); |
| 1360 | return NULL; |
| 1361 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1362 | authblob = user_authblob(conn, req); |
Marios Makassikis | 0d994cd | 2021-10-19 17:39:38 +0200 | [diff] [blame] | 1363 | name_off = le32_to_cpu(authblob->UserName.BufferOffset); |
| 1364 | name_len = le16_to_cpu(authblob->UserName.Length); |
| 1365 | auth_msg_len = le16_to_cpu(req->SecurityBufferOffset) + secbuf_len; |
| 1366 | |
| 1367 | if (auth_msg_len < (u64)name_off + name_len) |
| 1368 | return NULL; |
| 1369 | |
| 1370 | name = smb_strndup_from_utf16((const char *)authblob + name_off, |
| 1371 | name_len, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1372 | true, |
| 1373 | conn->local_nls); |
| 1374 | if (IS_ERR(name)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 1375 | pr_err("cannot allocate memory\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1376 | return NULL; |
| 1377 | } |
| 1378 | |
| 1379 | ksmbd_debug(SMB, "session setup request for user %s\n", name); |
| 1380 | user = ksmbd_login_user(name); |
| 1381 | kfree(name); |
| 1382 | return user; |
| 1383 | } |
| 1384 | |
| 1385 | static int ntlm_authenticate(struct ksmbd_work *work) |
| 1386 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1387 | struct smb2_sess_setup_req *req = smb2_get_msg(work->request_buf); |
| 1388 | struct smb2_sess_setup_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1389 | struct ksmbd_conn *conn = work->conn; |
| 1390 | struct ksmbd_session *sess = work->sess; |
| 1391 | struct channel *chann = NULL; |
| 1392 | struct ksmbd_user *user; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1393 | u64 prev_id; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1394 | int sz, rc; |
| 1395 | |
| 1396 | ksmbd_debug(SMB, "authenticate phase\n"); |
| 1397 | if (conn->use_spnego) { |
| 1398 | unsigned char *spnego_blob; |
| 1399 | u16 spnego_blob_len; |
| 1400 | |
| 1401 | rc = build_spnego_ntlmssp_auth_blob(&spnego_blob, |
| 1402 | &spnego_blob_len, |
| 1403 | 0); |
| 1404 | if (rc) |
| 1405 | return -ENOMEM; |
| 1406 | |
| 1407 | sz = le16_to_cpu(rsp->SecurityBufferOffset); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1408 | memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1409 | rsp->SecurityBufferLength = cpu_to_le16(spnego_blob_len); |
| 1410 | kfree(spnego_blob); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1411 | inc_rfc1001_len(work->response_buf, spnego_blob_len - 1); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1412 | } |
| 1413 | |
| 1414 | user = session_user(conn, req); |
| 1415 | if (!user) { |
| 1416 | ksmbd_debug(SMB, "Unknown user name or an error\n"); |
Namjae Jeon | 58090b1 | 2021-07-16 14:52:09 +0900 | [diff] [blame] | 1417 | return -EPERM; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1418 | } |
| 1419 | |
| 1420 | /* Check for previous session */ |
| 1421 | prev_id = le64_to_cpu(req->PreviousSessionId); |
| 1422 | if (prev_id && prev_id != sess->id) |
| 1423 | destroy_previous_session(user, prev_id); |
| 1424 | |
| 1425 | if (sess->state == SMB2_SESSION_VALID) { |
| 1426 | /* |
| 1427 | * Reuse session if anonymous try to connect |
| 1428 | * on reauthetication. |
| 1429 | */ |
| 1430 | if (ksmbd_anonymous_user(user)) { |
| 1431 | ksmbd_free_user(user); |
| 1432 | return 0; |
| 1433 | } |
| 1434 | ksmbd_free_user(sess->user); |
| 1435 | } |
| 1436 | |
| 1437 | sess->user = user; |
| 1438 | if (user_guest(sess->user)) { |
| 1439 | if (conn->sign) { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1440 | ksmbd_debug(SMB, "Guest login not allowed when signing enabled\n"); |
Namjae Jeon | 58090b1 | 2021-07-16 14:52:09 +0900 | [diff] [blame] | 1441 | return -EPERM; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1442 | } |
| 1443 | |
| 1444 | rsp->SessionFlags = SMB2_SESSION_FLAG_IS_GUEST_LE; |
| 1445 | } else { |
| 1446 | struct authenticate_message *authblob; |
| 1447 | |
| 1448 | authblob = user_authblob(conn, req); |
| 1449 | sz = le16_to_cpu(req->SecurityBufferLength); |
| 1450 | rc = ksmbd_decode_ntlmssp_auth_blob(authblob, sz, sess); |
| 1451 | if (rc) { |
| 1452 | set_user_flag(sess->user, KSMBD_USER_FLAG_BAD_PASSWORD); |
| 1453 | ksmbd_debug(SMB, "authentication failed\n"); |
Namjae Jeon | 58090b1 | 2021-07-16 14:52:09 +0900 | [diff] [blame] | 1454 | return -EPERM; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1455 | } |
| 1456 | |
| 1457 | /* |
| 1458 | * If session state is SMB2_SESSION_VALID, We can assume |
| 1459 | * that it is reauthentication. And the user/password |
| 1460 | * has been verified, so return it here. |
| 1461 | */ |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1462 | if (sess->state == SMB2_SESSION_VALID) { |
| 1463 | if (conn->binding) |
| 1464 | goto binding_session; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1465 | return 0; |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1466 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1467 | |
| 1468 | if ((conn->sign || server_conf.enforced_signing) || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1469 | (req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1470 | sess->sign = true; |
| 1471 | |
| 1472 | if (conn->vals->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION && |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1473 | conn->ops->generate_encryptionkey && |
| 1474 | !(req->Flags & SMB2_SESSION_REQ_FLAG_BINDING)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1475 | rc = conn->ops->generate_encryptionkey(sess); |
| 1476 | if (rc) { |
| 1477 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1478 | "SMB3 encryption key generation failed\n"); |
Namjae Jeon | 58090b1 | 2021-07-16 14:52:09 +0900 | [diff] [blame] | 1479 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1480 | } |
| 1481 | sess->enc = true; |
| 1482 | rsp->SessionFlags = SMB2_SESSION_FLAG_ENCRYPT_DATA_LE; |
| 1483 | /* |
| 1484 | * signing is disable if encryption is enable |
| 1485 | * on this session |
| 1486 | */ |
| 1487 | sess->sign = false; |
| 1488 | } |
| 1489 | } |
| 1490 | |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1491 | binding_session: |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1492 | if (conn->dialect >= SMB30_PROT_ID) { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1493 | chann = lookup_chann_list(sess, conn); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1494 | if (!chann) { |
| 1495 | chann = kmalloc(sizeof(struct channel), GFP_KERNEL); |
| 1496 | if (!chann) |
| 1497 | return -ENOMEM; |
| 1498 | |
| 1499 | chann->conn = conn; |
| 1500 | INIT_LIST_HEAD(&chann->chann_list); |
| 1501 | list_add(&chann->chann_list, &sess->ksmbd_chann_list); |
| 1502 | } |
| 1503 | } |
| 1504 | |
| 1505 | if (conn->ops->generate_signingkey) { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1506 | rc = conn->ops->generate_signingkey(sess, conn); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1507 | if (rc) { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1508 | ksmbd_debug(SMB, "SMB3 signing key generation failed\n"); |
Namjae Jeon | 58090b1 | 2021-07-16 14:52:09 +0900 | [diff] [blame] | 1509 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1510 | } |
| 1511 | } |
| 1512 | |
Namjae Jeon | 51a1387 | 2021-09-29 13:09:24 +0900 | [diff] [blame] | 1513 | if (!ksmbd_conn_lookup_dialect(conn)) { |
| 1514 | pr_err("fail to verify the dialect\n"); |
| 1515 | return -ENOENT; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1516 | } |
| 1517 | return 0; |
| 1518 | } |
| 1519 | |
| 1520 | #ifdef CONFIG_SMB_SERVER_KERBEROS5 |
| 1521 | static int krb5_authenticate(struct ksmbd_work *work) |
| 1522 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1523 | struct smb2_sess_setup_req *req = smb2_get_msg(work->request_buf); |
| 1524 | struct smb2_sess_setup_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1525 | struct ksmbd_conn *conn = work->conn; |
| 1526 | struct ksmbd_session *sess = work->sess; |
| 1527 | char *in_blob, *out_blob; |
| 1528 | struct channel *chann = NULL; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1529 | u64 prev_sess_id; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1530 | int in_len, out_len; |
| 1531 | int retval; |
| 1532 | |
| 1533 | in_blob = (char *)&req->hdr.ProtocolId + |
| 1534 | le16_to_cpu(req->SecurityBufferOffset); |
| 1535 | in_len = le16_to_cpu(req->SecurityBufferLength); |
| 1536 | out_blob = (char *)&rsp->hdr.ProtocolId + |
| 1537 | le16_to_cpu(rsp->SecurityBufferOffset); |
| 1538 | out_len = work->response_sz - |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1539 | (le16_to_cpu(rsp->SecurityBufferOffset) + 4); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1540 | |
| 1541 | /* Check previous session */ |
| 1542 | prev_sess_id = le64_to_cpu(req->PreviousSessionId); |
| 1543 | if (prev_sess_id && prev_sess_id != sess->id) |
| 1544 | destroy_previous_session(sess->user, prev_sess_id); |
| 1545 | |
| 1546 | if (sess->state == SMB2_SESSION_VALID) |
| 1547 | ksmbd_free_user(sess->user); |
| 1548 | |
| 1549 | retval = ksmbd_krb5_authenticate(sess, in_blob, in_len, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1550 | out_blob, &out_len); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1551 | if (retval) { |
| 1552 | ksmbd_debug(SMB, "krb5 authentication failed\n"); |
Namjae Jeon | 58090b1 | 2021-07-16 14:52:09 +0900 | [diff] [blame] | 1553 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1554 | } |
| 1555 | rsp->SecurityBufferLength = cpu_to_le16(out_len); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1556 | inc_rfc1001_len(work->response_buf, out_len - 1); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1557 | |
| 1558 | if ((conn->sign || server_conf.enforced_signing) || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1559 | (req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1560 | sess->sign = true; |
| 1561 | |
| 1562 | if ((conn->vals->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION) && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1563 | conn->ops->generate_encryptionkey) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1564 | retval = conn->ops->generate_encryptionkey(sess); |
| 1565 | if (retval) { |
| 1566 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1567 | "SMB3 encryption key generation failed\n"); |
Namjae Jeon | 58090b1 | 2021-07-16 14:52:09 +0900 | [diff] [blame] | 1568 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1569 | } |
| 1570 | sess->enc = true; |
| 1571 | rsp->SessionFlags = SMB2_SESSION_FLAG_ENCRYPT_DATA_LE; |
| 1572 | sess->sign = false; |
| 1573 | } |
| 1574 | |
| 1575 | if (conn->dialect >= SMB30_PROT_ID) { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1576 | chann = lookup_chann_list(sess, conn); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1577 | if (!chann) { |
| 1578 | chann = kmalloc(sizeof(struct channel), GFP_KERNEL); |
| 1579 | if (!chann) |
| 1580 | return -ENOMEM; |
| 1581 | |
| 1582 | chann->conn = conn; |
| 1583 | INIT_LIST_HEAD(&chann->chann_list); |
| 1584 | list_add(&chann->chann_list, &sess->ksmbd_chann_list); |
| 1585 | } |
| 1586 | } |
| 1587 | |
| 1588 | if (conn->ops->generate_signingkey) { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1589 | retval = conn->ops->generate_signingkey(sess, conn); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1590 | if (retval) { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1591 | ksmbd_debug(SMB, "SMB3 signing key generation failed\n"); |
Namjae Jeon | 58090b1 | 2021-07-16 14:52:09 +0900 | [diff] [blame] | 1592 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1593 | } |
| 1594 | } |
| 1595 | |
Namjae Jeon | 51a1387 | 2021-09-29 13:09:24 +0900 | [diff] [blame] | 1596 | if (!ksmbd_conn_lookup_dialect(conn)) { |
| 1597 | pr_err("fail to verify the dialect\n"); |
| 1598 | return -ENOENT; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1599 | } |
| 1600 | return 0; |
| 1601 | } |
| 1602 | #else |
| 1603 | static int krb5_authenticate(struct ksmbd_work *work) |
| 1604 | { |
| 1605 | return -EOPNOTSUPP; |
| 1606 | } |
| 1607 | #endif |
| 1608 | |
| 1609 | int smb2_sess_setup(struct ksmbd_work *work) |
| 1610 | { |
| 1611 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1612 | struct smb2_sess_setup_req *req = smb2_get_msg(work->request_buf); |
| 1613 | struct smb2_sess_setup_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1614 | struct ksmbd_session *sess; |
| 1615 | struct negotiate_message *negblob; |
Marios Makassikis | 0d994cd | 2021-10-19 17:39:38 +0200 | [diff] [blame] | 1616 | unsigned int negblob_len, negblob_off; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1617 | int rc = 0; |
| 1618 | |
| 1619 | ksmbd_debug(SMB, "Received request for session setup\n"); |
| 1620 | |
| 1621 | rsp->StructureSize = cpu_to_le16(9); |
| 1622 | rsp->SessionFlags = 0; |
| 1623 | rsp->SecurityBufferOffset = cpu_to_le16(72); |
| 1624 | rsp->SecurityBufferLength = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1625 | inc_rfc1001_len(work->response_buf, 9); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1626 | |
| 1627 | if (!req->hdr.SessionId) { |
| 1628 | sess = ksmbd_smb2_session_create(); |
| 1629 | if (!sess) { |
| 1630 | rc = -ENOMEM; |
| 1631 | goto out_err; |
| 1632 | } |
| 1633 | rsp->hdr.SessionId = cpu_to_le64(sess->id); |
| 1634 | ksmbd_session_register(conn, sess); |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1635 | } else if (conn->dialect >= SMB30_PROT_ID && |
| 1636 | (server_conf.flags & KSMBD_GLOBAL_FLAG_SMB3_MULTICHANNEL) && |
| 1637 | req->Flags & SMB2_SESSION_REQ_FLAG_BINDING) { |
| 1638 | u64 sess_id = le64_to_cpu(req->hdr.SessionId); |
| 1639 | |
| 1640 | sess = ksmbd_session_lookup_slowpath(sess_id); |
| 1641 | if (!sess) { |
| 1642 | rc = -ENOENT; |
| 1643 | goto out_err; |
| 1644 | } |
| 1645 | |
| 1646 | if (conn->dialect != sess->conn->dialect) { |
| 1647 | rc = -EINVAL; |
| 1648 | goto out_err; |
| 1649 | } |
| 1650 | |
| 1651 | if (!(req->hdr.Flags & SMB2_FLAGS_SIGNED)) { |
| 1652 | rc = -EINVAL; |
| 1653 | goto out_err; |
| 1654 | } |
| 1655 | |
| 1656 | if (strncmp(conn->ClientGUID, sess->conn->ClientGUID, |
| 1657 | SMB2_CLIENT_GUID_SIZE)) { |
| 1658 | rc = -ENOENT; |
| 1659 | goto out_err; |
| 1660 | } |
| 1661 | |
| 1662 | if (sess->state == SMB2_SESSION_IN_PROGRESS) { |
| 1663 | rc = -EACCES; |
| 1664 | goto out_err; |
| 1665 | } |
| 1666 | |
| 1667 | if (sess->state == SMB2_SESSION_EXPIRED) { |
| 1668 | rc = -EFAULT; |
| 1669 | goto out_err; |
| 1670 | } |
| 1671 | |
| 1672 | if (ksmbd_session_lookup(conn, sess_id)) { |
| 1673 | rc = -EACCES; |
| 1674 | goto out_err; |
| 1675 | } |
| 1676 | |
| 1677 | conn->binding = true; |
| 1678 | } else if ((conn->dialect < SMB30_PROT_ID || |
| 1679 | server_conf.flags & KSMBD_GLOBAL_FLAG_SMB3_MULTICHANNEL) && |
| 1680 | (req->Flags & SMB2_SESSION_REQ_FLAG_BINDING)) { |
Colin Ian King | 4951a84 | 2021-07-06 13:05:01 +0100 | [diff] [blame] | 1681 | sess = NULL; |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1682 | rc = -EACCES; |
| 1683 | goto out_err; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1684 | } else { |
| 1685 | sess = ksmbd_session_lookup(conn, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1686 | le64_to_cpu(req->hdr.SessionId)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1687 | if (!sess) { |
| 1688 | rc = -ENOENT; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1689 | goto out_err; |
| 1690 | } |
| 1691 | } |
| 1692 | work->sess = sess; |
| 1693 | |
| 1694 | if (sess->state == SMB2_SESSION_EXPIRED) |
| 1695 | sess->state = SMB2_SESSION_IN_PROGRESS; |
| 1696 | |
Marios Makassikis | 0d994cd | 2021-10-19 17:39:38 +0200 | [diff] [blame] | 1697 | negblob_off = le16_to_cpu(req->SecurityBufferOffset); |
| 1698 | negblob_len = le16_to_cpu(req->SecurityBufferLength); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1699 | if (negblob_off < offsetof(struct smb2_sess_setup_req, Buffer) || |
Christophe JAILLET | f8fbfd8 | 2021-11-07 16:22:57 +0100 | [diff] [blame] | 1700 | negblob_len < offsetof(struct negotiate_message, NegotiateFlags)) { |
| 1701 | rc = -EINVAL; |
| 1702 | goto out_err; |
| 1703 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1704 | |
Marios Makassikis | 0d994cd | 2021-10-19 17:39:38 +0200 | [diff] [blame] | 1705 | negblob = (struct negotiate_message *)((char *)&req->hdr.ProtocolId + |
| 1706 | negblob_off); |
| 1707 | |
| 1708 | if (decode_negotiation_token(conn, negblob, negblob_len) == 0) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1709 | if (conn->mechToken) |
| 1710 | negblob = (struct negotiate_message *)conn->mechToken; |
| 1711 | } |
| 1712 | |
| 1713 | if (server_conf.auth_mechs & conn->auth_mechs) { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1714 | rc = generate_preauth_hash(work); |
| 1715 | if (rc) |
| 1716 | goto out_err; |
| 1717 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1718 | if (conn->preferred_auth_mech & |
| 1719 | (KSMBD_AUTH_KRB5 | KSMBD_AUTH_MSKRB5)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1720 | rc = krb5_authenticate(work); |
| 1721 | if (rc) { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1722 | rc = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1723 | goto out_err; |
| 1724 | } |
| 1725 | |
| 1726 | ksmbd_conn_set_good(work); |
| 1727 | sess->state = SMB2_SESSION_VALID; |
Muhammad Usama Anjum | 822bc8e | 2021-04-02 09:25:35 +0900 | [diff] [blame] | 1728 | kfree(sess->Preauth_HashValue); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1729 | sess->Preauth_HashValue = NULL; |
| 1730 | } else if (conn->preferred_auth_mech == KSMBD_AUTH_NTLMSSP) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1731 | if (negblob->MessageType == NtLmNegotiate) { |
Marios Makassikis | 0d994cd | 2021-10-19 17:39:38 +0200 | [diff] [blame] | 1732 | rc = ntlm_negotiate(work, negblob, negblob_len); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1733 | if (rc) |
| 1734 | goto out_err; |
| 1735 | rsp->hdr.Status = |
| 1736 | STATUS_MORE_PROCESSING_REQUIRED; |
| 1737 | /* |
| 1738 | * Note: here total size -1 is done as an |
| 1739 | * adjustment for 0 size blob |
| 1740 | */ |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1741 | inc_rfc1001_len(work->response_buf, |
| 1742 | le16_to_cpu(rsp->SecurityBufferLength) - 1); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1743 | |
| 1744 | } else if (negblob->MessageType == NtLmAuthenticate) { |
| 1745 | rc = ntlm_authenticate(work); |
| 1746 | if (rc) |
| 1747 | goto out_err; |
| 1748 | |
| 1749 | ksmbd_conn_set_good(work); |
| 1750 | sess->state = SMB2_SESSION_VALID; |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1751 | if (conn->binding) { |
| 1752 | struct preauth_session *preauth_sess; |
| 1753 | |
| 1754 | preauth_sess = |
| 1755 | ksmbd_preauth_session_lookup(conn, sess->id); |
| 1756 | if (preauth_sess) { |
| 1757 | list_del(&preauth_sess->preauth_entry); |
| 1758 | kfree(preauth_sess); |
| 1759 | } |
| 1760 | } |
Muhammad Usama Anjum | 822bc8e | 2021-04-02 09:25:35 +0900 | [diff] [blame] | 1761 | kfree(sess->Preauth_HashValue); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1762 | sess->Preauth_HashValue = NULL; |
| 1763 | } |
| 1764 | } else { |
| 1765 | /* TODO: need one more negotiation */ |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 1766 | pr_err("Not support the preferred authentication\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1767 | rc = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1768 | } |
| 1769 | } else { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 1770 | pr_err("Not support authentication\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1771 | rc = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1772 | } |
| 1773 | |
| 1774 | out_err: |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1775 | if (rc == -EINVAL) |
| 1776 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 1777 | else if (rc == -ENOENT) |
| 1778 | rsp->hdr.Status = STATUS_USER_SESSION_DELETED; |
| 1779 | else if (rc == -EACCES) |
| 1780 | rsp->hdr.Status = STATUS_REQUEST_NOT_ACCEPTED; |
| 1781 | else if (rc == -EFAULT) |
| 1782 | rsp->hdr.Status = STATUS_NETWORK_SESSION_EXPIRED; |
Namjae Jeon | 58090b1 | 2021-07-16 14:52:09 +0900 | [diff] [blame] | 1783 | else if (rc == -ENOMEM) |
| 1784 | rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES; |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1785 | else if (rc) |
| 1786 | rsp->hdr.Status = STATUS_LOGON_FAILURE; |
| 1787 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1788 | if (conn->use_spnego && conn->mechToken) { |
| 1789 | kfree(conn->mechToken); |
| 1790 | conn->mechToken = NULL; |
| 1791 | } |
| 1792 | |
Namjae Jeon | 621be84 | 2021-10-13 17:28:31 +0900 | [diff] [blame] | 1793 | if (rc < 0) { |
| 1794 | /* |
| 1795 | * SecurityBufferOffset should be set to zero |
| 1796 | * in session setup error response. |
| 1797 | */ |
| 1798 | rsp->SecurityBufferOffset = 0; |
| 1799 | |
| 1800 | if (sess) { |
| 1801 | bool try_delay = false; |
| 1802 | |
| 1803 | /* |
| 1804 | * To avoid dictionary attacks (repeated session setups rapidly sent) to |
| 1805 | * connect to server, ksmbd make a delay of a 5 seconds on session setup |
| 1806 | * failure to make it harder to send enough random connection requests |
| 1807 | * to break into a server. |
| 1808 | */ |
| 1809 | if (sess->user && sess->user->flags & KSMBD_USER_FLAG_DELAY_SESSION) |
| 1810 | try_delay = true; |
| 1811 | |
| 1812 | ksmbd_session_destroy(sess); |
| 1813 | work->sess = NULL; |
| 1814 | if (try_delay) |
| 1815 | ssleep(5); |
| 1816 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1817 | } |
| 1818 | |
| 1819 | return rc; |
| 1820 | } |
| 1821 | |
| 1822 | /** |
| 1823 | * smb2_tree_connect() - handler for smb2 tree connect command |
| 1824 | * @work: smb work containing smb request buffer |
| 1825 | * |
| 1826 | * Return: 0 on success, otherwise error |
| 1827 | */ |
| 1828 | int smb2_tree_connect(struct ksmbd_work *work) |
| 1829 | { |
| 1830 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1831 | struct smb2_tree_connect_req *req = smb2_get_msg(work->request_buf); |
| 1832 | struct smb2_tree_connect_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1833 | struct ksmbd_session *sess = work->sess; |
| 1834 | char *treename = NULL, *name = NULL; |
| 1835 | struct ksmbd_tree_conn_status status; |
| 1836 | struct ksmbd_share_config *share; |
| 1837 | int rc = -EINVAL; |
| 1838 | |
| 1839 | treename = smb_strndup_from_utf16(req->Buffer, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1840 | le16_to_cpu(req->PathLength), true, |
| 1841 | conn->local_nls); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1842 | if (IS_ERR(treename)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 1843 | pr_err("treename is NULL\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1844 | status.ret = KSMBD_TREE_CONN_STATUS_ERROR; |
| 1845 | goto out_err1; |
| 1846 | } |
| 1847 | |
Stephen Rothwell | 36ba386 | 2021-03-17 17:01:15 +0900 | [diff] [blame] | 1848 | name = ksmbd_extract_sharename(treename); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1849 | if (IS_ERR(name)) { |
| 1850 | status.ret = KSMBD_TREE_CONN_STATUS_ERROR; |
| 1851 | goto out_err1; |
| 1852 | } |
| 1853 | |
| 1854 | ksmbd_debug(SMB, "tree connect request for tree %s treename %s\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1855 | name, treename); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1856 | |
| 1857 | status = ksmbd_tree_conn_connect(sess, name); |
| 1858 | if (status.ret == KSMBD_TREE_CONN_STATUS_OK) |
| 1859 | rsp->hdr.Id.SyncId.TreeId = cpu_to_le32(status.tree_conn->id); |
| 1860 | else |
| 1861 | goto out_err1; |
| 1862 | |
| 1863 | share = status.tree_conn->share_conf; |
| 1864 | if (test_share_config_flag(share, KSMBD_SHARE_FLAG_PIPE)) { |
| 1865 | ksmbd_debug(SMB, "IPC share path request\n"); |
| 1866 | rsp->ShareType = SMB2_SHARE_TYPE_PIPE; |
| 1867 | rsp->MaximalAccess = FILE_READ_DATA_LE | FILE_READ_EA_LE | |
| 1868 | FILE_EXECUTE_LE | FILE_READ_ATTRIBUTES_LE | |
| 1869 | FILE_DELETE_LE | FILE_READ_CONTROL_LE | |
| 1870 | FILE_WRITE_DAC_LE | FILE_WRITE_OWNER_LE | |
| 1871 | FILE_SYNCHRONIZE_LE; |
| 1872 | } else { |
| 1873 | rsp->ShareType = SMB2_SHARE_TYPE_DISK; |
| 1874 | rsp->MaximalAccess = FILE_READ_DATA_LE | FILE_READ_EA_LE | |
| 1875 | FILE_EXECUTE_LE | FILE_READ_ATTRIBUTES_LE; |
| 1876 | if (test_tree_conn_flag(status.tree_conn, |
| 1877 | KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
| 1878 | rsp->MaximalAccess |= FILE_WRITE_DATA_LE | |
| 1879 | FILE_APPEND_DATA_LE | FILE_WRITE_EA_LE | |
Wan Jiabing | 3aefd54 | 2021-06-07 12:54:32 +0800 | [diff] [blame] | 1880 | FILE_DELETE_LE | FILE_WRITE_ATTRIBUTES_LE | |
| 1881 | FILE_DELETE_CHILD_LE | FILE_READ_CONTROL_LE | |
| 1882 | FILE_WRITE_DAC_LE | FILE_WRITE_OWNER_LE | |
| 1883 | FILE_SYNCHRONIZE_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1884 | } |
| 1885 | } |
| 1886 | |
| 1887 | status.tree_conn->maximal_access = le32_to_cpu(rsp->MaximalAccess); |
| 1888 | if (conn->posix_ext_supported) |
| 1889 | status.tree_conn->posix_extensions = true; |
| 1890 | |
| 1891 | out_err1: |
| 1892 | rsp->StructureSize = cpu_to_le16(16); |
| 1893 | rsp->Capabilities = 0; |
| 1894 | rsp->Reserved = 0; |
| 1895 | /* default manual caching */ |
| 1896 | rsp->ShareFlags = SMB2_SHAREFLAG_MANUAL_CACHING; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1897 | inc_rfc1001_len(work->response_buf, 16); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1898 | |
| 1899 | if (!IS_ERR(treename)) |
| 1900 | kfree(treename); |
| 1901 | if (!IS_ERR(name)) |
| 1902 | kfree(name); |
| 1903 | |
| 1904 | switch (status.ret) { |
| 1905 | case KSMBD_TREE_CONN_STATUS_OK: |
| 1906 | rsp->hdr.Status = STATUS_SUCCESS; |
| 1907 | rc = 0; |
| 1908 | break; |
| 1909 | case KSMBD_TREE_CONN_STATUS_NO_SHARE: |
| 1910 | rsp->hdr.Status = STATUS_BAD_NETWORK_PATH; |
| 1911 | break; |
| 1912 | case -ENOMEM: |
| 1913 | case KSMBD_TREE_CONN_STATUS_NOMEM: |
| 1914 | rsp->hdr.Status = STATUS_NO_MEMORY; |
| 1915 | break; |
| 1916 | case KSMBD_TREE_CONN_STATUS_ERROR: |
| 1917 | case KSMBD_TREE_CONN_STATUS_TOO_MANY_CONNS: |
| 1918 | case KSMBD_TREE_CONN_STATUS_TOO_MANY_SESSIONS: |
| 1919 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 1920 | break; |
| 1921 | case -EINVAL: |
| 1922 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 1923 | break; |
| 1924 | default: |
| 1925 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 1926 | } |
| 1927 | |
| 1928 | return rc; |
| 1929 | } |
| 1930 | |
| 1931 | /** |
| 1932 | * smb2_create_open_flags() - convert smb open flags to unix open flags |
| 1933 | * @file_present: is file already present |
| 1934 | * @access: file access flags |
| 1935 | * @disposition: file disposition flags |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 1936 | * @may_flags: set with MAY_ flags |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1937 | * |
| 1938 | * Return: file open flags |
| 1939 | */ |
| 1940 | static int smb2_create_open_flags(bool file_present, __le32 access, |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 1941 | __le32 disposition, |
| 1942 | int *may_flags) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1943 | { |
| 1944 | int oflags = O_NONBLOCK | O_LARGEFILE; |
| 1945 | |
| 1946 | if (access & FILE_READ_DESIRED_ACCESS_LE && |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 1947 | access & FILE_WRITE_DESIRE_ACCESS_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1948 | oflags |= O_RDWR; |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 1949 | *may_flags = MAY_OPEN | MAY_READ | MAY_WRITE; |
| 1950 | } else if (access & FILE_WRITE_DESIRE_ACCESS_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1951 | oflags |= O_WRONLY; |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 1952 | *may_flags = MAY_OPEN | MAY_WRITE; |
| 1953 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1954 | oflags |= O_RDONLY; |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 1955 | *may_flags = MAY_OPEN | MAY_READ; |
| 1956 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1957 | |
| 1958 | if (access == FILE_READ_ATTRIBUTES_LE) |
| 1959 | oflags |= O_PATH; |
| 1960 | |
| 1961 | if (file_present) { |
| 1962 | switch (disposition & FILE_CREATE_MASK_LE) { |
| 1963 | case FILE_OPEN_LE: |
| 1964 | case FILE_CREATE_LE: |
| 1965 | break; |
| 1966 | case FILE_SUPERSEDE_LE: |
| 1967 | case FILE_OVERWRITE_LE: |
| 1968 | case FILE_OVERWRITE_IF_LE: |
| 1969 | oflags |= O_TRUNC; |
| 1970 | break; |
| 1971 | default: |
| 1972 | break; |
| 1973 | } |
| 1974 | } else { |
| 1975 | switch (disposition & FILE_CREATE_MASK_LE) { |
| 1976 | case FILE_SUPERSEDE_LE: |
| 1977 | case FILE_CREATE_LE: |
| 1978 | case FILE_OPEN_IF_LE: |
| 1979 | case FILE_OVERWRITE_IF_LE: |
| 1980 | oflags |= O_CREAT; |
| 1981 | break; |
| 1982 | case FILE_OPEN_LE: |
| 1983 | case FILE_OVERWRITE_LE: |
| 1984 | oflags &= ~O_CREAT; |
| 1985 | break; |
| 1986 | default: |
| 1987 | break; |
| 1988 | } |
| 1989 | } |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 1990 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1991 | return oflags; |
| 1992 | } |
| 1993 | |
| 1994 | /** |
| 1995 | * smb2_tree_disconnect() - handler for smb tree connect request |
| 1996 | * @work: smb work containing request buffer |
| 1997 | * |
| 1998 | * Return: 0 |
| 1999 | */ |
| 2000 | int smb2_tree_disconnect(struct ksmbd_work *work) |
| 2001 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 2002 | struct smb2_tree_disconnect_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2003 | struct ksmbd_session *sess = work->sess; |
| 2004 | struct ksmbd_tree_connect *tcon = work->tcon; |
| 2005 | |
| 2006 | rsp->StructureSize = cpu_to_le16(4); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 2007 | inc_rfc1001_len(work->response_buf, 4); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2008 | |
| 2009 | ksmbd_debug(SMB, "request\n"); |
| 2010 | |
| 2011 | if (!tcon) { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 2012 | struct smb2_tree_disconnect_req *req = |
| 2013 | smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2014 | |
| 2015 | ksmbd_debug(SMB, "Invalid tid %d\n", req->hdr.Id.SyncId.TreeId); |
| 2016 | rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED; |
| 2017 | smb2_set_err_rsp(work); |
| 2018 | return 0; |
| 2019 | } |
| 2020 | |
| 2021 | ksmbd_close_tree_conn_fds(work); |
| 2022 | ksmbd_tree_conn_disconnect(sess, tcon); |
| 2023 | return 0; |
| 2024 | } |
| 2025 | |
| 2026 | /** |
| 2027 | * smb2_session_logoff() - handler for session log off request |
| 2028 | * @work: smb work containing request buffer |
| 2029 | * |
| 2030 | * Return: 0 |
| 2031 | */ |
| 2032 | int smb2_session_logoff(struct ksmbd_work *work) |
| 2033 | { |
| 2034 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 2035 | struct smb2_logoff_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2036 | struct ksmbd_session *sess = work->sess; |
| 2037 | |
| 2038 | rsp->StructureSize = cpu_to_le16(4); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 2039 | inc_rfc1001_len(work->response_buf, 4); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2040 | |
| 2041 | ksmbd_debug(SMB, "request\n"); |
| 2042 | |
| 2043 | /* Got a valid session, set connection state */ |
| 2044 | WARN_ON(sess->conn != conn); |
| 2045 | |
| 2046 | /* setting CifsExiting here may race with start_tcp_sess */ |
| 2047 | ksmbd_conn_set_need_reconnect(work); |
| 2048 | ksmbd_close_session_fds(work); |
| 2049 | ksmbd_conn_wait_idle(conn); |
| 2050 | |
| 2051 | if (ksmbd_tree_conn_session_logoff(sess)) { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 2052 | struct smb2_logoff_req *req = smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2053 | |
| 2054 | ksmbd_debug(SMB, "Invalid tid %d\n", req->hdr.Id.SyncId.TreeId); |
| 2055 | rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED; |
| 2056 | smb2_set_err_rsp(work); |
| 2057 | return 0; |
| 2058 | } |
| 2059 | |
| 2060 | ksmbd_destroy_file_table(&sess->file_table); |
| 2061 | sess->state = SMB2_SESSION_EXPIRED; |
| 2062 | |
| 2063 | ksmbd_free_user(sess->user); |
| 2064 | sess->user = NULL; |
| 2065 | |
| 2066 | /* let start_tcp_sess free connection info now */ |
| 2067 | ksmbd_conn_set_need_negotiate(work); |
| 2068 | return 0; |
| 2069 | } |
| 2070 | |
| 2071 | /** |
| 2072 | * create_smb2_pipe() - create IPC pipe |
| 2073 | * @work: smb work containing request buffer |
| 2074 | * |
| 2075 | * Return: 0 on success, otherwise error |
| 2076 | */ |
| 2077 | static noinline int create_smb2_pipe(struct ksmbd_work *work) |
| 2078 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 2079 | struct smb2_create_rsp *rsp = smb2_get_msg(work->response_buf); |
| 2080 | struct smb2_create_req *req = smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2081 | int id; |
| 2082 | int err; |
| 2083 | char *name; |
| 2084 | |
| 2085 | name = smb_strndup_from_utf16(req->Buffer, le16_to_cpu(req->NameLength), |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2086 | 1, work->conn->local_nls); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2087 | if (IS_ERR(name)) { |
| 2088 | rsp->hdr.Status = STATUS_NO_MEMORY; |
| 2089 | err = PTR_ERR(name); |
| 2090 | goto out; |
| 2091 | } |
| 2092 | |
| 2093 | id = ksmbd_session_rpc_open(work->sess, name); |
Marios Makassikis | 79caa96 | 2021-05-06 11:38:35 +0900 | [diff] [blame] | 2094 | if (id < 0) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2095 | pr_err("Unable to open RPC pipe: %d\n", id); |
Marios Makassikis | 79caa96 | 2021-05-06 11:38:35 +0900 | [diff] [blame] | 2096 | err = id; |
| 2097 | goto out; |
| 2098 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2099 | |
Marios Makassikis | 79caa96 | 2021-05-06 11:38:35 +0900 | [diff] [blame] | 2100 | rsp->hdr.Status = STATUS_SUCCESS; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2101 | rsp->StructureSize = cpu_to_le16(89); |
| 2102 | rsp->OplockLevel = SMB2_OPLOCK_LEVEL_NONE; |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 2103 | rsp->Flags = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2104 | rsp->CreateAction = cpu_to_le32(FILE_OPENED); |
| 2105 | |
| 2106 | rsp->CreationTime = cpu_to_le64(0); |
| 2107 | rsp->LastAccessTime = cpu_to_le64(0); |
| 2108 | rsp->ChangeTime = cpu_to_le64(0); |
| 2109 | rsp->AllocationSize = cpu_to_le64(0); |
| 2110 | rsp->EndofFile = cpu_to_le64(0); |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 2111 | rsp->FileAttributes = FILE_ATTRIBUTE_NORMAL_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2112 | rsp->Reserved2 = 0; |
| 2113 | rsp->VolatileFileId = cpu_to_le64(id); |
| 2114 | rsp->PersistentFileId = 0; |
| 2115 | rsp->CreateContextsOffset = 0; |
| 2116 | rsp->CreateContextsLength = 0; |
| 2117 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 2118 | inc_rfc1001_len(work->response_buf, 88); /* StructureSize - 1*/ |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2119 | kfree(name); |
| 2120 | return 0; |
| 2121 | |
| 2122 | out: |
Marios Makassikis | 79caa96 | 2021-05-06 11:38:35 +0900 | [diff] [blame] | 2123 | switch (err) { |
| 2124 | case -EINVAL: |
| 2125 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 2126 | break; |
| 2127 | case -ENOSPC: |
| 2128 | case -ENOMEM: |
| 2129 | rsp->hdr.Status = STATUS_NO_MEMORY; |
| 2130 | break; |
| 2131 | } |
| 2132 | |
| 2133 | if (!IS_ERR(name)) |
| 2134 | kfree(name); |
| 2135 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2136 | smb2_set_err_rsp(work); |
| 2137 | return err; |
| 2138 | } |
| 2139 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2140 | /** |
| 2141 | * smb2_set_ea() - handler for setting extended attributes using set |
| 2142 | * info command |
| 2143 | * @eabuf: set info command buffer |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 2144 | * @buf_len: set info command buffer length |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2145 | * @path: dentry path for get ea |
| 2146 | * |
| 2147 | * Return: 0 on success, otherwise error |
| 2148 | */ |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 2149 | static int smb2_set_ea(struct smb2_ea_info *eabuf, unsigned int buf_len, |
| 2150 | struct path *path) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2151 | { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2152 | struct user_namespace *user_ns = mnt_user_ns(path->mnt); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2153 | char *attr_name = NULL, *value; |
| 2154 | int rc = 0; |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 2155 | unsigned int next = 0; |
| 2156 | |
| 2157 | if (buf_len < sizeof(struct smb2_ea_info) + eabuf->EaNameLength + |
| 2158 | le16_to_cpu(eabuf->EaValueLength)) |
| 2159 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2160 | |
| 2161 | attr_name = kmalloc(XATTR_NAME_MAX + 1, GFP_KERNEL); |
| 2162 | if (!attr_name) |
| 2163 | return -ENOMEM; |
| 2164 | |
| 2165 | do { |
| 2166 | if (!eabuf->EaNameLength) |
| 2167 | goto next; |
| 2168 | |
| 2169 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2170 | "name : <%s>, name_len : %u, value_len : %u, next : %u\n", |
| 2171 | eabuf->name, eabuf->EaNameLength, |
| 2172 | le16_to_cpu(eabuf->EaValueLength), |
| 2173 | le32_to_cpu(eabuf->NextEntryOffset)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2174 | |
| 2175 | if (eabuf->EaNameLength > |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2176 | (XATTR_NAME_MAX - XATTR_USER_PREFIX_LEN)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2177 | rc = -EINVAL; |
| 2178 | break; |
| 2179 | } |
| 2180 | |
| 2181 | memcpy(attr_name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN); |
| 2182 | memcpy(&attr_name[XATTR_USER_PREFIX_LEN], eabuf->name, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2183 | eabuf->EaNameLength); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2184 | attr_name[XATTR_USER_PREFIX_LEN + eabuf->EaNameLength] = '\0'; |
| 2185 | value = (char *)&eabuf->name + eabuf->EaNameLength + 1; |
| 2186 | |
| 2187 | if (!eabuf->EaValueLength) { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2188 | rc = ksmbd_vfs_casexattr_len(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2189 | path->dentry, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2190 | attr_name, |
| 2191 | XATTR_USER_PREFIX_LEN + |
| 2192 | eabuf->EaNameLength); |
| 2193 | |
| 2194 | /* delete the EA only when it exits */ |
| 2195 | if (rc > 0) { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2196 | rc = ksmbd_vfs_remove_xattr(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2197 | path->dentry, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2198 | attr_name); |
| 2199 | |
| 2200 | if (rc < 0) { |
| 2201 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2202 | "remove xattr failed(%d)\n", |
| 2203 | rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2204 | break; |
| 2205 | } |
| 2206 | } |
| 2207 | |
| 2208 | /* if the EA doesn't exist, just do nothing. */ |
| 2209 | rc = 0; |
| 2210 | } else { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2211 | rc = ksmbd_vfs_setxattr(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2212 | path->dentry, attr_name, value, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2213 | le16_to_cpu(eabuf->EaValueLength), 0); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2214 | if (rc < 0) { |
| 2215 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2216 | "ksmbd_vfs_setxattr is failed(%d)\n", |
| 2217 | rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2218 | break; |
| 2219 | } |
| 2220 | } |
| 2221 | |
| 2222 | next: |
| 2223 | next = le32_to_cpu(eabuf->NextEntryOffset); |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 2224 | if (next == 0 || buf_len < next) |
| 2225 | break; |
| 2226 | buf_len -= next; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2227 | eabuf = (struct smb2_ea_info *)((char *)eabuf + next); |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 2228 | if (next < (u32)eabuf->EaNameLength + le16_to_cpu(eabuf->EaValueLength)) |
| 2229 | break; |
| 2230 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2231 | } while (next != 0); |
| 2232 | |
| 2233 | kfree(attr_name); |
| 2234 | return rc; |
| 2235 | } |
| 2236 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2237 | static noinline int smb2_set_stream_name_xattr(struct path *path, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2238 | struct ksmbd_file *fp, |
| 2239 | char *stream_name, int s_type) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2240 | { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2241 | struct user_namespace *user_ns = mnt_user_ns(path->mnt); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2242 | size_t xattr_stream_size; |
| 2243 | char *xattr_stream_name; |
| 2244 | int rc; |
| 2245 | |
| 2246 | rc = ksmbd_vfs_xattr_stream_name(stream_name, |
| 2247 | &xattr_stream_name, |
| 2248 | &xattr_stream_size, |
| 2249 | s_type); |
| 2250 | if (rc) |
| 2251 | return rc; |
| 2252 | |
| 2253 | fp->stream.name = xattr_stream_name; |
| 2254 | fp->stream.size = xattr_stream_size; |
| 2255 | |
| 2256 | /* Check if there is stream prefix in xattr space */ |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2257 | rc = ksmbd_vfs_casexattr_len(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2258 | path->dentry, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2259 | xattr_stream_name, |
| 2260 | xattr_stream_size); |
| 2261 | if (rc >= 0) |
| 2262 | return 0; |
| 2263 | |
| 2264 | if (fp->cdoption == FILE_OPEN_LE) { |
| 2265 | ksmbd_debug(SMB, "XATTR stream name lookup failed: %d\n", rc); |
| 2266 | return -EBADF; |
| 2267 | } |
| 2268 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2269 | rc = ksmbd_vfs_setxattr(user_ns, path->dentry, |
| 2270 | xattr_stream_name, NULL, 0, 0); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2271 | if (rc < 0) |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2272 | pr_err("Failed to store XATTR stream name :%d\n", rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2273 | return 0; |
| 2274 | } |
| 2275 | |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 2276 | static int smb2_remove_smb_xattrs(struct path *path) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2277 | { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2278 | struct user_namespace *user_ns = mnt_user_ns(path->mnt); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2279 | char *name, *xattr_list = NULL; |
| 2280 | ssize_t xattr_list_len; |
| 2281 | int err = 0; |
| 2282 | |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 2283 | xattr_list_len = ksmbd_vfs_listxattr(path->dentry, &xattr_list); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2284 | if (xattr_list_len < 0) { |
| 2285 | goto out; |
| 2286 | } else if (!xattr_list_len) { |
| 2287 | ksmbd_debug(SMB, "empty xattr in the file\n"); |
| 2288 | goto out; |
| 2289 | } |
| 2290 | |
| 2291 | for (name = xattr_list; name - xattr_list < xattr_list_len; |
| 2292 | name += strlen(name) + 1) { |
| 2293 | ksmbd_debug(SMB, "%s, len %zd\n", name, strlen(name)); |
| 2294 | |
| 2295 | if (strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2296 | strncmp(&name[XATTR_USER_PREFIX_LEN], DOS_ATTRIBUTE_PREFIX, |
| 2297 | DOS_ATTRIBUTE_PREFIX_LEN) && |
| 2298 | strncmp(&name[XATTR_USER_PREFIX_LEN], STREAM_PREFIX, STREAM_PREFIX_LEN)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2299 | continue; |
| 2300 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2301 | err = ksmbd_vfs_remove_xattr(user_ns, path->dentry, name); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2302 | if (err) |
| 2303 | ksmbd_debug(SMB, "remove xattr failed : %s\n", name); |
| 2304 | } |
| 2305 | out: |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 2306 | kvfree(xattr_list); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2307 | return err; |
| 2308 | } |
| 2309 | |
| 2310 | static int smb2_create_truncate(struct path *path) |
| 2311 | { |
| 2312 | int rc = vfs_truncate(path, 0); |
| 2313 | |
| 2314 | if (rc) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2315 | pr_err("vfs_truncate failed, rc %d\n", rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2316 | return rc; |
| 2317 | } |
| 2318 | |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 2319 | rc = smb2_remove_smb_xattrs(path); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2320 | if (rc == -EOPNOTSUPP) |
| 2321 | rc = 0; |
| 2322 | if (rc) |
| 2323 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2324 | "ksmbd_truncate_stream_name_xattr failed, rc %d\n", |
| 2325 | rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2326 | return rc; |
| 2327 | } |
| 2328 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2329 | 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] | 2330 | struct ksmbd_file *fp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2331 | { |
| 2332 | struct xattr_dos_attrib da = {0}; |
| 2333 | int rc; |
| 2334 | |
| 2335 | if (!test_share_config_flag(tcon->share_conf, |
| 2336 | KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) |
| 2337 | return; |
| 2338 | |
| 2339 | da.version = 4; |
| 2340 | da.attr = le32_to_cpu(fp->f_ci->m_fattr); |
| 2341 | da.itime = da.create_time = fp->create_time; |
| 2342 | da.flags = XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME | |
| 2343 | XATTR_DOSINFO_ITIME; |
| 2344 | |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2345 | rc = ksmbd_vfs_set_dos_attrib_xattr(mnt_user_ns(path->mnt), |
| 2346 | path->dentry, &da); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2347 | if (rc) |
| 2348 | ksmbd_debug(SMB, "failed to store file attribute into xattr\n"); |
| 2349 | } |
| 2350 | |
| 2351 | static void smb2_update_xattrs(struct ksmbd_tree_connect *tcon, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2352 | struct path *path, struct ksmbd_file *fp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2353 | { |
| 2354 | struct xattr_dos_attrib da; |
| 2355 | int rc; |
| 2356 | |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 2357 | fp->f_ci->m_fattr &= ~(FILE_ATTRIBUTE_HIDDEN_LE | FILE_ATTRIBUTE_SYSTEM_LE); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2358 | |
| 2359 | /* get FileAttributes from XATTR_NAME_DOS_ATTRIBUTE */ |
| 2360 | if (!test_share_config_flag(tcon->share_conf, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2361 | KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2362 | return; |
| 2363 | |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2364 | rc = ksmbd_vfs_get_dos_attrib_xattr(mnt_user_ns(path->mnt), |
| 2365 | path->dentry, &da); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2366 | if (rc > 0) { |
| 2367 | fp->f_ci->m_fattr = cpu_to_le32(da.attr); |
| 2368 | fp->create_time = da.create_time; |
| 2369 | fp->itime = da.itime; |
| 2370 | } |
| 2371 | } |
| 2372 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2373 | 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] | 2374 | int open_flags, umode_t posix_mode, bool is_dir) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2375 | { |
| 2376 | struct ksmbd_tree_connect *tcon = work->tcon; |
| 2377 | struct ksmbd_share_config *share = tcon->share_conf; |
| 2378 | umode_t mode; |
| 2379 | int rc; |
| 2380 | |
| 2381 | if (!(open_flags & O_CREAT)) |
| 2382 | return -EBADF; |
| 2383 | |
| 2384 | ksmbd_debug(SMB, "file does not exist, so creating\n"); |
| 2385 | if (is_dir == true) { |
| 2386 | ksmbd_debug(SMB, "creating directory\n"); |
| 2387 | |
| 2388 | mode = share_config_directory_mode(share, posix_mode); |
| 2389 | rc = ksmbd_vfs_mkdir(work, name, mode); |
| 2390 | if (rc) |
| 2391 | return rc; |
| 2392 | } else { |
| 2393 | ksmbd_debug(SMB, "creating regular file\n"); |
| 2394 | |
| 2395 | mode = share_config_create_mode(share, posix_mode); |
| 2396 | rc = ksmbd_vfs_create(work, name, mode); |
| 2397 | if (rc) |
| 2398 | return rc; |
| 2399 | } |
| 2400 | |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 2401 | rc = ksmbd_vfs_kern_path(work, name, 0, path, 0); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2402 | if (rc) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2403 | pr_err("cannot get linux path (%s), err = %d\n", |
| 2404 | name, rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2405 | return rc; |
| 2406 | } |
| 2407 | return 0; |
| 2408 | } |
| 2409 | |
| 2410 | static int smb2_create_sd_buffer(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2411 | struct smb2_create_req *req, |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 2412 | struct path *path) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2413 | { |
| 2414 | struct create_context *context; |
Hyunchul Lee | 21dd1fd | 2021-07-09 17:06:34 +0900 | [diff] [blame] | 2415 | struct create_sd_buf_req *sd_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2416 | |
| 2417 | if (!req->CreateContextsOffset) |
Hyunchul Lee | 21dd1fd | 2021-07-09 17:06:34 +0900 | [diff] [blame] | 2418 | return -ENOENT; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2419 | |
| 2420 | /* Parse SD BUFFER create contexts */ |
| 2421 | context = smb2_find_context_vals(req, SMB2_CREATE_SD_BUFFER); |
Hyunchul Lee | 21dd1fd | 2021-07-09 17:06:34 +0900 | [diff] [blame] | 2422 | if (!context) |
| 2423 | return -ENOENT; |
| 2424 | else if (IS_ERR(context)) |
| 2425 | return PTR_ERR(context); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2426 | |
Hyunchul Lee | 21dd1fd | 2021-07-09 17:06:34 +0900 | [diff] [blame] | 2427 | ksmbd_debug(SMB, |
| 2428 | "Set ACLs using SMB2_CREATE_SD_BUFFER context\n"); |
| 2429 | sd_buf = (struct create_sd_buf_req *)context; |
Hyunchul Lee | 8f77150 | 2021-09-24 22:22:22 +0900 | [diff] [blame] | 2430 | if (le16_to_cpu(context->DataOffset) + |
| 2431 | le32_to_cpu(context->DataLength) < |
| 2432 | sizeof(struct create_sd_buf_req)) |
| 2433 | return -EINVAL; |
Hyunchul Lee | 21dd1fd | 2021-07-09 17:06:34 +0900 | [diff] [blame] | 2434 | return set_info_sec(work->conn, work->tcon, path, &sd_buf->ntsd, |
| 2435 | le32_to_cpu(sd_buf->ccontext.DataLength), true); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2436 | } |
| 2437 | |
Christian Brauner | 43205ca | 2021-08-23 17:13:50 +0200 | [diff] [blame] | 2438 | static void ksmbd_acls_fattr(struct smb_fattr *fattr, |
| 2439 | struct user_namespace *mnt_userns, |
| 2440 | struct inode *inode) |
Namjae Jeon | 3d47e54 | 2021-04-20 14:25:35 +0900 | [diff] [blame] | 2441 | { |
Christian Brauner | 43205ca | 2021-08-23 17:13:50 +0200 | [diff] [blame] | 2442 | fattr->cf_uid = i_uid_into_mnt(mnt_userns, inode); |
| 2443 | fattr->cf_gid = i_gid_into_mnt(mnt_userns, inode); |
Namjae Jeon | 3d47e54 | 2021-04-20 14:25:35 +0900 | [diff] [blame] | 2444 | fattr->cf_mode = inode->i_mode; |
Namjae Jeon | 777cad1 | 2021-08-13 08:15:33 +0900 | [diff] [blame] | 2445 | fattr->cf_acls = NULL; |
Namjae Jeon | 3d47e54 | 2021-04-20 14:25:35 +0900 | [diff] [blame] | 2446 | fattr->cf_dacls = NULL; |
| 2447 | |
Namjae Jeon | 777cad1 | 2021-08-13 08:15:33 +0900 | [diff] [blame] | 2448 | if (IS_ENABLED(CONFIG_FS_POSIX_ACL)) { |
| 2449 | fattr->cf_acls = get_acl(inode, ACL_TYPE_ACCESS); |
| 2450 | if (S_ISDIR(inode->i_mode)) |
| 2451 | fattr->cf_dacls = get_acl(inode, ACL_TYPE_DEFAULT); |
| 2452 | } |
Namjae Jeon | 3d47e54 | 2021-04-20 14:25:35 +0900 | [diff] [blame] | 2453 | } |
| 2454 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2455 | /** |
| 2456 | * smb2_open() - handler for smb file open request |
| 2457 | * @work: smb work containing request buffer |
| 2458 | * |
| 2459 | * Return: 0 on success, otherwise error |
| 2460 | */ |
| 2461 | int smb2_open(struct ksmbd_work *work) |
| 2462 | { |
| 2463 | struct ksmbd_conn *conn = work->conn; |
| 2464 | struct ksmbd_session *sess = work->sess; |
| 2465 | struct ksmbd_tree_connect *tcon = work->tcon; |
| 2466 | struct smb2_create_req *req; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 2467 | struct smb2_create_rsp *rsp; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2468 | struct path path; |
| 2469 | struct ksmbd_share_config *share = tcon->share_conf; |
| 2470 | struct ksmbd_file *fp = NULL; |
| 2471 | struct file *filp = NULL; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2472 | struct user_namespace *user_ns = NULL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2473 | struct kstat stat; |
| 2474 | struct create_context *context; |
| 2475 | struct lease_ctx_info *lc = NULL; |
| 2476 | struct create_ea_buf_req *ea_buf = NULL; |
| 2477 | struct oplock_info *opinfo; |
| 2478 | __le32 *next_ptr = NULL; |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 2479 | 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] | 2480 | int rc = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2481 | int contxt_cnt = 0, query_disk_id = 0; |
| 2482 | int maximal_access_ctxt = 0, posix_ctxt = 0; |
| 2483 | int s_type = 0; |
| 2484 | int next_off = 0; |
| 2485 | char *name = NULL; |
| 2486 | char *stream_name = NULL; |
| 2487 | bool file_present = false, created = false, already_permitted = false; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2488 | int share_ret, need_truncate = 0; |
| 2489 | u64 time; |
| 2490 | umode_t posix_mode = 0; |
| 2491 | __le32 daccess, maximal_access = 0; |
| 2492 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2493 | WORK_BUFFERS(work, req, rsp); |
| 2494 | |
| 2495 | if (req->hdr.NextCommand && !work->next_smb2_rcv_hdr_off && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2496 | (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2497 | ksmbd_debug(SMB, "invalid flag in chained command\n"); |
| 2498 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 2499 | smb2_set_err_rsp(work); |
| 2500 | return -EINVAL; |
| 2501 | } |
| 2502 | |
| 2503 | if (test_share_config_flag(share, KSMBD_SHARE_FLAG_PIPE)) { |
| 2504 | ksmbd_debug(SMB, "IPC pipe create request\n"); |
| 2505 | return create_smb2_pipe(work); |
| 2506 | } |
| 2507 | |
| 2508 | if (req->NameLength) { |
| 2509 | if ((req->CreateOptions & FILE_DIRECTORY_FILE_LE) && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2510 | *(char *)req->Buffer == '\\') { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2511 | pr_err("not allow directory name included leading slash\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2512 | rc = -EINVAL; |
| 2513 | goto err_out1; |
| 2514 | } |
| 2515 | |
| 2516 | name = smb2_get_name(share, |
| 2517 | req->Buffer, |
| 2518 | le16_to_cpu(req->NameLength), |
| 2519 | work->conn->local_nls); |
| 2520 | if (IS_ERR(name)) { |
| 2521 | rc = PTR_ERR(name); |
| 2522 | if (rc != -ENOMEM) |
| 2523 | rc = -ENOENT; |
Dan Carpenter | 8b99f35 | 2021-08-02 08:14:03 +0900 | [diff] [blame] | 2524 | name = NULL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2525 | goto err_out1; |
| 2526 | } |
| 2527 | |
| 2528 | ksmbd_debug(SMB, "converted name = %s\n", name); |
| 2529 | if (strchr(name, ':')) { |
| 2530 | if (!test_share_config_flag(work->tcon->share_conf, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2531 | KSMBD_SHARE_FLAG_STREAMS)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2532 | rc = -EBADF; |
| 2533 | goto err_out1; |
| 2534 | } |
| 2535 | rc = parse_stream_name(name, &stream_name, &s_type); |
| 2536 | if (rc < 0) |
| 2537 | goto err_out1; |
| 2538 | } |
| 2539 | |
| 2540 | rc = ksmbd_validate_filename(name); |
| 2541 | if (rc < 0) |
| 2542 | goto err_out1; |
| 2543 | |
| 2544 | if (ksmbd_share_veto_filename(share, name)) { |
| 2545 | rc = -ENOENT; |
| 2546 | ksmbd_debug(SMB, "Reject open(), vetoed file: %s\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2547 | name); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2548 | goto err_out1; |
| 2549 | } |
| 2550 | } else { |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 2551 | name = kstrdup("", GFP_KERNEL); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2552 | if (!name) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2553 | rc = -ENOMEM; |
| 2554 | goto err_out1; |
| 2555 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2556 | } |
| 2557 | |
| 2558 | req_op_level = req->RequestedOplockLevel; |
Namjae Jeon | 73f9dad | 2021-04-16 14:12:06 +0900 | [diff] [blame] | 2559 | if (req_op_level == SMB2_OPLOCK_LEVEL_LEASE) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2560 | lc = parse_lease_state(req); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2561 | |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 2562 | if (le32_to_cpu(req->ImpersonationLevel) > le32_to_cpu(IL_DELEGATE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2563 | pr_err("Invalid impersonationlevel : 0x%x\n", |
| 2564 | le32_to_cpu(req->ImpersonationLevel)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2565 | rc = -EIO; |
| 2566 | rsp->hdr.Status = STATUS_BAD_IMPERSONATION_LEVEL; |
| 2567 | goto err_out1; |
| 2568 | } |
| 2569 | |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 2570 | if (req->CreateOptions && !(req->CreateOptions & CREATE_OPTIONS_MASK_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2571 | pr_err("Invalid create options : 0x%x\n", |
| 2572 | le32_to_cpu(req->CreateOptions)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2573 | rc = -EINVAL; |
| 2574 | goto err_out1; |
| 2575 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2576 | if (req->CreateOptions & FILE_SEQUENTIAL_ONLY_LE && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2577 | req->CreateOptions & FILE_RANDOM_ACCESS_LE) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2578 | req->CreateOptions = ~(FILE_SEQUENTIAL_ONLY_LE); |
| 2579 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2580 | if (req->CreateOptions & |
| 2581 | (FILE_OPEN_BY_FILE_ID_LE | CREATE_TREE_CONNECTION | |
| 2582 | FILE_RESERVE_OPFILTER_LE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2583 | rc = -EOPNOTSUPP; |
| 2584 | goto err_out1; |
| 2585 | } |
| 2586 | |
| 2587 | if (req->CreateOptions & FILE_DIRECTORY_FILE_LE) { |
| 2588 | if (req->CreateOptions & FILE_NON_DIRECTORY_FILE_LE) { |
| 2589 | rc = -EINVAL; |
| 2590 | goto err_out1; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2591 | } else if (req->CreateOptions & FILE_NO_COMPRESSION_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2592 | req->CreateOptions = ~(FILE_NO_COMPRESSION_LE); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2593 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2594 | } |
| 2595 | } |
| 2596 | |
| 2597 | if (le32_to_cpu(req->CreateDisposition) > |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2598 | le32_to_cpu(FILE_OVERWRITE_IF_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2599 | pr_err("Invalid create disposition : 0x%x\n", |
| 2600 | le32_to_cpu(req->CreateDisposition)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2601 | rc = -EINVAL; |
| 2602 | goto err_out1; |
| 2603 | } |
| 2604 | |
| 2605 | if (!(req->DesiredAccess & DESIRED_ACCESS_MASK)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2606 | pr_err("Invalid desired access : 0x%x\n", |
| 2607 | le32_to_cpu(req->DesiredAccess)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2608 | rc = -EACCES; |
| 2609 | goto err_out1; |
| 2610 | } |
| 2611 | |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 2612 | if (req->FileAttributes && !(req->FileAttributes & FILE_ATTRIBUTE_MASK_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2613 | pr_err("Invalid file attribute : 0x%x\n", |
| 2614 | le32_to_cpu(req->FileAttributes)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2615 | rc = -EINVAL; |
| 2616 | goto err_out1; |
| 2617 | } |
| 2618 | |
| 2619 | if (req->CreateContextsOffset) { |
| 2620 | /* Parse non-durable handle create contexts */ |
| 2621 | context = smb2_find_context_vals(req, SMB2_CREATE_EA_BUFFER); |
Namjae Jeon | f19b396 | 2021-07-13 09:59:34 +0900 | [diff] [blame] | 2622 | if (IS_ERR(context)) { |
| 2623 | rc = PTR_ERR(context); |
| 2624 | goto err_out1; |
| 2625 | } else if (context) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2626 | ea_buf = (struct create_ea_buf_req *)context; |
Hyunchul Lee | 8f77150 | 2021-09-24 22:22:22 +0900 | [diff] [blame] | 2627 | if (le16_to_cpu(context->DataOffset) + |
| 2628 | le32_to_cpu(context->DataLength) < |
| 2629 | sizeof(struct create_ea_buf_req)) { |
| 2630 | rc = -EINVAL; |
| 2631 | goto err_out1; |
| 2632 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2633 | if (req->CreateOptions & FILE_NO_EA_KNOWLEDGE_LE) { |
| 2634 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 2635 | rc = -EACCES; |
| 2636 | goto err_out1; |
| 2637 | } |
| 2638 | } |
| 2639 | |
| 2640 | context = smb2_find_context_vals(req, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2641 | SMB2_CREATE_QUERY_MAXIMAL_ACCESS_REQUEST); |
Namjae Jeon | f19b396 | 2021-07-13 09:59:34 +0900 | [diff] [blame] | 2642 | if (IS_ERR(context)) { |
| 2643 | rc = PTR_ERR(context); |
| 2644 | goto err_out1; |
| 2645 | } else if (context) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2646 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2647 | "get query maximal access context\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2648 | maximal_access_ctxt = 1; |
| 2649 | } |
| 2650 | |
| 2651 | context = smb2_find_context_vals(req, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2652 | SMB2_CREATE_TIMEWARP_REQUEST); |
Namjae Jeon | f19b396 | 2021-07-13 09:59:34 +0900 | [diff] [blame] | 2653 | if (IS_ERR(context)) { |
| 2654 | rc = PTR_ERR(context); |
| 2655 | goto err_out1; |
| 2656 | } else if (context) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2657 | ksmbd_debug(SMB, "get timewarp context\n"); |
| 2658 | rc = -EBADF; |
| 2659 | goto err_out1; |
| 2660 | } |
| 2661 | |
| 2662 | if (tcon->posix_extensions) { |
| 2663 | context = smb2_find_context_vals(req, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2664 | SMB2_CREATE_TAG_POSIX); |
Namjae Jeon | f19b396 | 2021-07-13 09:59:34 +0900 | [diff] [blame] | 2665 | if (IS_ERR(context)) { |
| 2666 | rc = PTR_ERR(context); |
| 2667 | goto err_out1; |
| 2668 | } else if (context) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2669 | struct create_posix *posix = |
| 2670 | (struct create_posix *)context; |
Hyunchul Lee | 8f77150 | 2021-09-24 22:22:22 +0900 | [diff] [blame] | 2671 | if (le16_to_cpu(context->DataOffset) + |
| 2672 | le32_to_cpu(context->DataLength) < |
| 2673 | sizeof(struct create_posix)) { |
| 2674 | rc = -EINVAL; |
| 2675 | goto err_out1; |
| 2676 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2677 | ksmbd_debug(SMB, "get posix context\n"); |
| 2678 | |
| 2679 | posix_mode = le32_to_cpu(posix->Mode); |
| 2680 | posix_ctxt = 1; |
| 2681 | } |
| 2682 | } |
| 2683 | } |
| 2684 | |
| 2685 | if (ksmbd_override_fsids(work)) { |
| 2686 | rc = -ENOMEM; |
| 2687 | goto err_out1; |
| 2688 | } |
| 2689 | |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 2690 | rc = ksmbd_vfs_kern_path(work, name, LOOKUP_NO_SYMLINKS, &path, 1); |
Namjae Jeon | 4ea4779 | 2021-09-21 14:19:33 +0900 | [diff] [blame] | 2691 | if (!rc) { |
| 2692 | if (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2693 | /* |
| 2694 | * If file exists with under flags, return access |
| 2695 | * denied error. |
| 2696 | */ |
| 2697 | if (req->CreateDisposition == FILE_OVERWRITE_IF_LE || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2698 | req->CreateDisposition == FILE_OPEN_IF_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2699 | rc = -EACCES; |
| 2700 | path_put(&path); |
| 2701 | goto err_out; |
| 2702 | } |
| 2703 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2704 | if (!test_tree_conn_flag(tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2705 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2706 | "User does not have write permission\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2707 | rc = -EACCES; |
| 2708 | path_put(&path); |
| 2709 | goto err_out; |
| 2710 | } |
Namjae Jeon | 4ea4779 | 2021-09-21 14:19:33 +0900 | [diff] [blame] | 2711 | } else if (d_is_symlink(path.dentry)) { |
| 2712 | rc = -EACCES; |
| 2713 | path_put(&path); |
| 2714 | goto err_out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2715 | } |
| 2716 | } |
| 2717 | |
| 2718 | if (rc) { |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 2719 | if (rc != -ENOENT) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2720 | goto err_out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2721 | 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] | 2722 | name, rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2723 | rc = 0; |
| 2724 | } else { |
| 2725 | file_present = true; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2726 | user_ns = mnt_user_ns(path.mnt); |
| 2727 | generic_fillattr(user_ns, d_inode(path.dentry), &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2728 | } |
| 2729 | if (stream_name) { |
| 2730 | if (req->CreateOptions & FILE_DIRECTORY_FILE_LE) { |
| 2731 | if (s_type == DATA_STREAM) { |
| 2732 | rc = -EIO; |
| 2733 | rsp->hdr.Status = STATUS_NOT_A_DIRECTORY; |
| 2734 | } |
| 2735 | } else { |
| 2736 | if (S_ISDIR(stat.mode) && s_type == DATA_STREAM) { |
| 2737 | rc = -EIO; |
| 2738 | rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY; |
| 2739 | } |
| 2740 | } |
| 2741 | |
| 2742 | if (req->CreateOptions & FILE_DIRECTORY_FILE_LE && |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 2743 | req->FileAttributes & FILE_ATTRIBUTE_NORMAL_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2744 | rsp->hdr.Status = STATUS_NOT_A_DIRECTORY; |
| 2745 | rc = -EIO; |
| 2746 | } |
| 2747 | |
| 2748 | if (rc < 0) |
| 2749 | goto err_out; |
| 2750 | } |
| 2751 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2752 | if (file_present && req->CreateOptions & FILE_NON_DIRECTORY_FILE_LE && |
| 2753 | S_ISDIR(stat.mode) && !(req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2754 | ksmbd_debug(SMB, "open() argument is a directory: %s, %x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2755 | name, req->CreateOptions); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2756 | rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY; |
| 2757 | rc = -EIO; |
| 2758 | goto err_out; |
| 2759 | } |
| 2760 | |
| 2761 | if (file_present && (req->CreateOptions & FILE_DIRECTORY_FILE_LE) && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2762 | !(req->CreateDisposition == FILE_CREATE_LE) && |
| 2763 | !S_ISDIR(stat.mode)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2764 | rsp->hdr.Status = STATUS_NOT_A_DIRECTORY; |
| 2765 | rc = -EIO; |
| 2766 | goto err_out; |
| 2767 | } |
| 2768 | |
| 2769 | if (!stream_name && file_present && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2770 | req->CreateDisposition == FILE_CREATE_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2771 | rc = -EEXIST; |
| 2772 | goto err_out; |
| 2773 | } |
| 2774 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2775 | daccess = smb_map_generic_desired_access(req->DesiredAccess); |
| 2776 | |
| 2777 | if (file_present && !(req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) { |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 2778 | rc = smb_check_perm_dacl(conn, &path, &daccess, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2779 | sess->user->uid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2780 | if (rc) |
| 2781 | goto err_out; |
| 2782 | } |
| 2783 | |
| 2784 | if (daccess & FILE_MAXIMAL_ACCESS_LE) { |
| 2785 | if (!file_present) { |
| 2786 | daccess = cpu_to_le32(GENERIC_ALL_FLAGS); |
| 2787 | } else { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2788 | rc = ksmbd_vfs_query_maximal_access(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2789 | path.dentry, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2790 | &daccess); |
| 2791 | if (rc) |
| 2792 | goto err_out; |
| 2793 | already_permitted = true; |
| 2794 | } |
| 2795 | maximal_access = daccess; |
| 2796 | } |
| 2797 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2798 | open_flags = smb2_create_open_flags(file_present, daccess, |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 2799 | req->CreateDisposition, |
| 2800 | &may_flags); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2801 | |
| 2802 | if (!test_tree_conn_flag(tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
| 2803 | if (open_flags & O_CREAT) { |
| 2804 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2805 | "User does not have write permission\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2806 | rc = -EACCES; |
| 2807 | goto err_out; |
| 2808 | } |
| 2809 | } |
| 2810 | |
| 2811 | /*create file if not present */ |
| 2812 | if (!file_present) { |
| 2813 | rc = smb2_creat(work, &path, name, open_flags, posix_mode, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2814 | req->CreateOptions & FILE_DIRECTORY_FILE_LE); |
Marios Makassikis | d337a44 | 2021-07-27 09:24:51 +0900 | [diff] [blame] | 2815 | if (rc) { |
| 2816 | if (rc == -ENOENT) { |
| 2817 | rc = -EIO; |
| 2818 | rsp->hdr.Status = STATUS_OBJECT_PATH_NOT_FOUND; |
| 2819 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2820 | goto err_out; |
Marios Makassikis | d337a44 | 2021-07-27 09:24:51 +0900 | [diff] [blame] | 2821 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2822 | |
| 2823 | created = true; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2824 | user_ns = mnt_user_ns(path.mnt); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2825 | if (ea_buf) { |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 2826 | if (le32_to_cpu(ea_buf->ccontext.DataLength) < |
| 2827 | sizeof(struct smb2_ea_info)) { |
| 2828 | rc = -EINVAL; |
| 2829 | goto err_out; |
| 2830 | } |
| 2831 | |
| 2832 | rc = smb2_set_ea(&ea_buf->ea, |
| 2833 | le32_to_cpu(ea_buf->ccontext.DataLength), |
| 2834 | &path); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2835 | if (rc == -EOPNOTSUPP) |
| 2836 | rc = 0; |
| 2837 | else if (rc) |
| 2838 | goto err_out; |
| 2839 | } |
| 2840 | } else if (!already_permitted) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2841 | /* FILE_READ_ATTRIBUTE is allowed without inode_permission, |
| 2842 | * because execute(search) permission on a parent directory, |
| 2843 | * is already granted. |
| 2844 | */ |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2845 | if (daccess & ~(FILE_READ_ATTRIBUTES_LE | FILE_READ_CONTROL_LE)) { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2846 | rc = inode_permission(user_ns, |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 2847 | d_inode(path.dentry), |
| 2848 | may_flags); |
Namjae Jeon | ff1d572 | 2021-04-13 13:18:10 +0900 | [diff] [blame] | 2849 | if (rc) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2850 | goto err_out; |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 2851 | |
| 2852 | if ((daccess & FILE_DELETE_LE) || |
| 2853 | (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2854 | rc = ksmbd_vfs_may_delete(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2855 | path.dentry); |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 2856 | if (rc) |
| 2857 | goto err_out; |
| 2858 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2859 | } |
| 2860 | } |
| 2861 | |
| 2862 | rc = ksmbd_query_inode_status(d_inode(path.dentry->d_parent)); |
| 2863 | if (rc == KSMBD_INODE_STATUS_PENDING_DELETE) { |
| 2864 | rc = -EBUSY; |
| 2865 | goto err_out; |
| 2866 | } |
| 2867 | |
| 2868 | rc = 0; |
| 2869 | filp = dentry_open(&path, open_flags, current_cred()); |
| 2870 | if (IS_ERR(filp)) { |
| 2871 | rc = PTR_ERR(filp); |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2872 | pr_err("dentry open for dir failed, rc %d\n", rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2873 | goto err_out; |
| 2874 | } |
| 2875 | |
| 2876 | if (file_present) { |
| 2877 | if (!(open_flags & O_TRUNC)) |
| 2878 | file_info = FILE_OPENED; |
| 2879 | else |
| 2880 | file_info = FILE_OVERWRITTEN; |
| 2881 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2882 | if ((req->CreateDisposition & FILE_CREATE_MASK_LE) == |
| 2883 | FILE_SUPERSEDE_LE) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2884 | file_info = FILE_SUPERSEDED; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2885 | } else if (open_flags & O_CREAT) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2886 | file_info = FILE_CREATED; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2887 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2888 | |
| 2889 | ksmbd_vfs_set_fadvise(filp, req->CreateOptions); |
| 2890 | |
| 2891 | /* Obtain Volatile-ID */ |
| 2892 | fp = ksmbd_open_fd(work, filp); |
| 2893 | if (IS_ERR(fp)) { |
| 2894 | fput(filp); |
| 2895 | rc = PTR_ERR(fp); |
| 2896 | fp = NULL; |
| 2897 | goto err_out; |
| 2898 | } |
| 2899 | |
| 2900 | /* Get Persistent-ID */ |
| 2901 | ksmbd_open_durable_fd(fp); |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 2902 | if (!has_file_id(fp->persistent_id)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2903 | rc = -ENOMEM; |
| 2904 | goto err_out; |
| 2905 | } |
| 2906 | |
| 2907 | fp->filename = name; |
| 2908 | fp->cdoption = req->CreateDisposition; |
| 2909 | fp->daccess = daccess; |
| 2910 | fp->saccess = req->ShareAccess; |
| 2911 | fp->coption = req->CreateOptions; |
| 2912 | |
| 2913 | /* Set default windows and posix acls if creating new file */ |
| 2914 | if (created) { |
| 2915 | int posix_acl_rc; |
Namjae Jeon | fba08fa | 2021-04-15 10:29:39 +0900 | [diff] [blame] | 2916 | struct inode *inode = d_inode(path.dentry); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2917 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2918 | posix_acl_rc = ksmbd_vfs_inherit_posix_acl(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2919 | inode, |
| 2920 | d_inode(path.dentry->d_parent)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2921 | if (posix_acl_rc) |
| 2922 | ksmbd_debug(SMB, "inherit posix acl failed : %d\n", posix_acl_rc); |
| 2923 | |
| 2924 | if (test_share_config_flag(work->tcon->share_conf, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2925 | KSMBD_SHARE_FLAG_ACL_XATTR)) { |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 2926 | rc = smb_inherit_dacl(conn, &path, sess->user->uid, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2927 | sess->user->gid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2928 | } |
| 2929 | |
| 2930 | if (rc) { |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 2931 | rc = smb2_create_sd_buffer(work, req, &path); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2932 | if (rc) { |
| 2933 | if (posix_acl_rc) |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2934 | ksmbd_vfs_set_init_posix_acl(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2935 | inode); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2936 | |
| 2937 | if (test_share_config_flag(work->tcon->share_conf, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2938 | KSMBD_SHARE_FLAG_ACL_XATTR)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2939 | struct smb_fattr fattr; |
| 2940 | struct smb_ntsd *pntsd; |
Namjae Jeon | 3d47e54 | 2021-04-20 14:25:35 +0900 | [diff] [blame] | 2941 | int pntsd_size, ace_num = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2942 | |
Christian Brauner | 43205ca | 2021-08-23 17:13:50 +0200 | [diff] [blame] | 2943 | ksmbd_acls_fattr(&fattr, user_ns, inode); |
Marios Makassikis | e6b1059 | 2021-04-15 10:24:56 +0900 | [diff] [blame] | 2944 | if (fattr.cf_acls) |
| 2945 | ace_num = fattr.cf_acls->a_count; |
Namjae Jeon | 3d47e54 | 2021-04-20 14:25:35 +0900 | [diff] [blame] | 2946 | if (fattr.cf_dacls) |
| 2947 | ace_num += fattr.cf_dacls->a_count; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2948 | |
| 2949 | pntsd = kmalloc(sizeof(struct smb_ntsd) + |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2950 | sizeof(struct smb_sid) * 3 + |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2951 | sizeof(struct smb_acl) + |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2952 | sizeof(struct smb_ace) * ace_num * 2, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2953 | GFP_KERNEL); |
| 2954 | if (!pntsd) |
| 2955 | goto err_out; |
| 2956 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2957 | rc = build_sec_desc(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2958 | pntsd, NULL, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2959 | OWNER_SECINFO | |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2960 | GROUP_SECINFO | |
| 2961 | DACL_SECINFO, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2962 | &pntsd_size, &fattr); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2963 | posix_acl_release(fattr.cf_acls); |
| 2964 | posix_acl_release(fattr.cf_dacls); |
Namjae Jeon | f2e78af | 2021-12-01 10:12:39 +0900 | [diff] [blame^] | 2965 | if (rc) { |
| 2966 | kfree(pntsd); |
| 2967 | goto err_out; |
| 2968 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2969 | |
| 2970 | rc = ksmbd_vfs_set_sd_xattr(conn, |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2971 | user_ns, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2972 | path.dentry, |
| 2973 | pntsd, |
| 2974 | pntsd_size); |
Namjae Jeon | 3d47e54 | 2021-04-20 14:25:35 +0900 | [diff] [blame] | 2975 | kfree(pntsd); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2976 | if (rc) |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2977 | pr_err("failed to store ntacl in xattr : %d\n", |
| 2978 | rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2979 | } |
| 2980 | } |
| 2981 | } |
| 2982 | rc = 0; |
| 2983 | } |
| 2984 | |
| 2985 | if (stream_name) { |
| 2986 | rc = smb2_set_stream_name_xattr(&path, |
| 2987 | fp, |
| 2988 | stream_name, |
| 2989 | s_type); |
| 2990 | if (rc) |
| 2991 | goto err_out; |
| 2992 | file_info = FILE_CREATED; |
| 2993 | } |
| 2994 | |
| 2995 | fp->attrib_only = !(req->DesiredAccess & ~(FILE_READ_ATTRIBUTES_LE | |
| 2996 | FILE_WRITE_ATTRIBUTES_LE | FILE_SYNCHRONIZE_LE)); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2997 | if (!S_ISDIR(file_inode(filp)->i_mode) && open_flags & O_TRUNC && |
| 2998 | !fp->attrib_only && !stream_name) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2999 | smb_break_all_oplock(work, fp); |
| 3000 | need_truncate = 1; |
| 3001 | } |
| 3002 | |
| 3003 | /* fp should be searchable through ksmbd_inode.m_fp_list |
| 3004 | * after daccess, saccess, attrib_only, and stream are |
| 3005 | * initialized. |
| 3006 | */ |
| 3007 | write_lock(&fp->f_ci->m_lock); |
| 3008 | list_add(&fp->node, &fp->f_ci->m_fp_list); |
| 3009 | write_unlock(&fp->f_ci->m_lock); |
| 3010 | |
| 3011 | rc = ksmbd_vfs_getattr(&path, &stat); |
| 3012 | if (rc) { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 3013 | generic_fillattr(user_ns, d_inode(path.dentry), &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3014 | rc = 0; |
| 3015 | } |
| 3016 | |
| 3017 | /* Check delete pending among previous fp before oplock break */ |
| 3018 | if (ksmbd_inode_pending_delete(fp)) { |
| 3019 | rc = -EBUSY; |
| 3020 | goto err_out; |
| 3021 | } |
| 3022 | |
| 3023 | share_ret = ksmbd_smb_check_shared_mode(fp->filp, fp); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3024 | if (!test_share_config_flag(work->tcon->share_conf, KSMBD_SHARE_FLAG_OPLOCKS) || |
| 3025 | (req_op_level == SMB2_OPLOCK_LEVEL_LEASE && |
| 3026 | !(conn->vals->capabilities & SMB2_GLOBAL_CAP_LEASING))) { |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 3027 | if (share_ret < 0 && !S_ISDIR(file_inode(fp->filp)->i_mode)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3028 | rc = share_ret; |
| 3029 | goto err_out; |
| 3030 | } |
| 3031 | } else { |
| 3032 | if (req_op_level == SMB2_OPLOCK_LEVEL_LEASE) { |
| 3033 | req_op_level = smb2_map_lease_to_oplock(lc->req_state); |
| 3034 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3035 | "lease req for(%s) req oplock state 0x%x, lease state 0x%x\n", |
| 3036 | name, req_op_level, lc->req_state); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3037 | rc = find_same_lease_key(sess, fp->f_ci, lc); |
| 3038 | if (rc) |
| 3039 | goto err_out; |
| 3040 | } else if (open_flags == O_RDONLY && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3041 | (req_op_level == SMB2_OPLOCK_LEVEL_BATCH || |
| 3042 | req_op_level == SMB2_OPLOCK_LEVEL_EXCLUSIVE)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3043 | req_op_level = SMB2_OPLOCK_LEVEL_II; |
| 3044 | |
| 3045 | rc = smb_grant_oplock(work, req_op_level, |
| 3046 | fp->persistent_id, fp, |
| 3047 | le32_to_cpu(req->hdr.Id.SyncId.TreeId), |
| 3048 | lc, share_ret); |
| 3049 | if (rc < 0) |
| 3050 | goto err_out; |
| 3051 | } |
| 3052 | |
| 3053 | if (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE) |
| 3054 | ksmbd_fd_set_delete_on_close(fp, file_info); |
| 3055 | |
| 3056 | if (need_truncate) { |
| 3057 | rc = smb2_create_truncate(&path); |
| 3058 | if (rc) |
| 3059 | goto err_out; |
| 3060 | } |
| 3061 | |
| 3062 | if (req->CreateContextsOffset) { |
| 3063 | struct create_alloc_size_req *az_req; |
| 3064 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3065 | az_req = (struct create_alloc_size_req *)smb2_find_context_vals(req, |
| 3066 | SMB2_CREATE_ALLOCATION_SIZE); |
Namjae Jeon | f19b396 | 2021-07-13 09:59:34 +0900 | [diff] [blame] | 3067 | if (IS_ERR(az_req)) { |
| 3068 | rc = PTR_ERR(az_req); |
| 3069 | goto err_out; |
| 3070 | } else if (az_req) { |
Hyunchul Lee | 8f77150 | 2021-09-24 22:22:22 +0900 | [diff] [blame] | 3071 | loff_t alloc_size; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3072 | int err; |
| 3073 | |
Hyunchul Lee | 8f77150 | 2021-09-24 22:22:22 +0900 | [diff] [blame] | 3074 | if (le16_to_cpu(az_req->ccontext.DataOffset) + |
| 3075 | le32_to_cpu(az_req->ccontext.DataLength) < |
| 3076 | sizeof(struct create_alloc_size_req)) { |
| 3077 | rc = -EINVAL; |
| 3078 | goto err_out; |
| 3079 | } |
| 3080 | alloc_size = le64_to_cpu(az_req->AllocationSize); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3081 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3082 | "request smb2 create allocate size : %llu\n", |
| 3083 | alloc_size); |
Namjae Jeon | e8c0619 | 2021-06-22 11:06:11 +0900 | [diff] [blame] | 3084 | smb_break_all_levII_oplock(work, fp, 1); |
| 3085 | err = vfs_fallocate(fp->filp, FALLOC_FL_KEEP_SIZE, 0, |
| 3086 | alloc_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3087 | if (err < 0) |
| 3088 | ksmbd_debug(SMB, |
Namjae Jeon | e8c0619 | 2021-06-22 11:06:11 +0900 | [diff] [blame] | 3089 | "vfs_fallocate is failed : %d\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3090 | err); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3091 | } |
| 3092 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3093 | context = smb2_find_context_vals(req, SMB2_CREATE_QUERY_ON_DISK_ID); |
Namjae Jeon | f19b396 | 2021-07-13 09:59:34 +0900 | [diff] [blame] | 3094 | if (IS_ERR(context)) { |
| 3095 | rc = PTR_ERR(context); |
| 3096 | goto err_out; |
| 3097 | } else if (context) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3098 | ksmbd_debug(SMB, "get query on disk id context\n"); |
| 3099 | query_disk_id = 1; |
| 3100 | } |
| 3101 | } |
| 3102 | |
| 3103 | if (stat.result_mask & STATX_BTIME) |
| 3104 | fp->create_time = ksmbd_UnixTimeToNT(stat.btime); |
| 3105 | else |
| 3106 | fp->create_time = ksmbd_UnixTimeToNT(stat.ctime); |
| 3107 | if (req->FileAttributes || fp->f_ci->m_fattr == 0) |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3108 | fp->f_ci->m_fattr = |
| 3109 | 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] | 3110 | |
| 3111 | if (!created) |
| 3112 | smb2_update_xattrs(tcon, &path, fp); |
| 3113 | else |
| 3114 | smb2_new_xattrs(tcon, &path, fp); |
| 3115 | |
| 3116 | memcpy(fp->client_guid, conn->ClientGUID, SMB2_CLIENT_GUID_SIZE); |
| 3117 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 3118 | generic_fillattr(user_ns, file_inode(fp->filp), |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 3119 | &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3120 | |
| 3121 | rsp->StructureSize = cpu_to_le16(89); |
| 3122 | rcu_read_lock(); |
| 3123 | opinfo = rcu_dereference(fp->f_opinfo); |
| 3124 | rsp->OplockLevel = opinfo != NULL ? opinfo->level : 0; |
| 3125 | rcu_read_unlock(); |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 3126 | rsp->Flags = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3127 | rsp->CreateAction = cpu_to_le32(file_info); |
| 3128 | rsp->CreationTime = cpu_to_le64(fp->create_time); |
| 3129 | time = ksmbd_UnixTimeToNT(stat.atime); |
| 3130 | rsp->LastAccessTime = cpu_to_le64(time); |
| 3131 | time = ksmbd_UnixTimeToNT(stat.mtime); |
| 3132 | rsp->LastWriteTime = cpu_to_le64(time); |
| 3133 | time = ksmbd_UnixTimeToNT(stat.ctime); |
| 3134 | rsp->ChangeTime = cpu_to_le64(time); |
| 3135 | rsp->AllocationSize = S_ISDIR(stat.mode) ? 0 : |
| 3136 | cpu_to_le64(stat.blocks << 9); |
| 3137 | rsp->EndofFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size); |
| 3138 | rsp->FileAttributes = fp->f_ci->m_fattr; |
| 3139 | |
| 3140 | rsp->Reserved2 = 0; |
| 3141 | |
| 3142 | rsp->PersistentFileId = cpu_to_le64(fp->persistent_id); |
| 3143 | rsp->VolatileFileId = cpu_to_le64(fp->volatile_id); |
| 3144 | |
| 3145 | rsp->CreateContextsOffset = 0; |
| 3146 | rsp->CreateContextsLength = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 3147 | inc_rfc1001_len(work->response_buf, 88); /* StructureSize - 1*/ |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3148 | |
| 3149 | /* If lease is request send lease context response */ |
| 3150 | if (opinfo && opinfo->is_lease) { |
| 3151 | struct create_context *lease_ccontext; |
| 3152 | |
| 3153 | ksmbd_debug(SMB, "lease granted on(%s) lease state 0x%x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3154 | name, opinfo->o_lease->state); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3155 | rsp->OplockLevel = SMB2_OPLOCK_LEVEL_LEASE; |
| 3156 | |
| 3157 | lease_ccontext = (struct create_context *)rsp->Buffer; |
| 3158 | contxt_cnt++; |
| 3159 | create_lease_buf(rsp->Buffer, opinfo->o_lease); |
| 3160 | le32_add_cpu(&rsp->CreateContextsLength, |
| 3161 | conn->vals->create_lease_size); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 3162 | inc_rfc1001_len(work->response_buf, |
| 3163 | conn->vals->create_lease_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3164 | next_ptr = &lease_ccontext->Next; |
| 3165 | next_off = conn->vals->create_lease_size; |
| 3166 | } |
| 3167 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3168 | if (maximal_access_ctxt) { |
| 3169 | struct create_context *mxac_ccontext; |
| 3170 | |
| 3171 | if (maximal_access == 0) |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 3172 | ksmbd_vfs_query_maximal_access(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 3173 | path.dentry, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3174 | &maximal_access); |
| 3175 | mxac_ccontext = (struct create_context *)(rsp->Buffer + |
| 3176 | le32_to_cpu(rsp->CreateContextsLength)); |
| 3177 | contxt_cnt++; |
| 3178 | create_mxac_rsp_buf(rsp->Buffer + |
| 3179 | le32_to_cpu(rsp->CreateContextsLength), |
| 3180 | le32_to_cpu(maximal_access)); |
| 3181 | le32_add_cpu(&rsp->CreateContextsLength, |
| 3182 | conn->vals->create_mxac_size); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 3183 | inc_rfc1001_len(work->response_buf, |
| 3184 | conn->vals->create_mxac_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3185 | if (next_ptr) |
| 3186 | *next_ptr = cpu_to_le32(next_off); |
| 3187 | next_ptr = &mxac_ccontext->Next; |
| 3188 | next_off = conn->vals->create_mxac_size; |
| 3189 | } |
| 3190 | |
| 3191 | if (query_disk_id) { |
| 3192 | struct create_context *disk_id_ccontext; |
| 3193 | |
| 3194 | disk_id_ccontext = (struct create_context *)(rsp->Buffer + |
| 3195 | le32_to_cpu(rsp->CreateContextsLength)); |
| 3196 | contxt_cnt++; |
| 3197 | create_disk_id_rsp_buf(rsp->Buffer + |
| 3198 | le32_to_cpu(rsp->CreateContextsLength), |
| 3199 | stat.ino, tcon->id); |
| 3200 | le32_add_cpu(&rsp->CreateContextsLength, |
| 3201 | conn->vals->create_disk_id_size); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 3202 | inc_rfc1001_len(work->response_buf, |
| 3203 | conn->vals->create_disk_id_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3204 | if (next_ptr) |
| 3205 | *next_ptr = cpu_to_le32(next_off); |
| 3206 | next_ptr = &disk_id_ccontext->Next; |
| 3207 | next_off = conn->vals->create_disk_id_size; |
| 3208 | } |
| 3209 | |
| 3210 | if (posix_ctxt) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3211 | contxt_cnt++; |
| 3212 | create_posix_rsp_buf(rsp->Buffer + |
| 3213 | le32_to_cpu(rsp->CreateContextsLength), |
| 3214 | fp); |
| 3215 | le32_add_cpu(&rsp->CreateContextsLength, |
| 3216 | conn->vals->create_posix_size); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 3217 | inc_rfc1001_len(work->response_buf, |
| 3218 | conn->vals->create_posix_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3219 | if (next_ptr) |
| 3220 | *next_ptr = cpu_to_le32(next_off); |
| 3221 | } |
| 3222 | |
| 3223 | if (contxt_cnt > 0) { |
| 3224 | rsp->CreateContextsOffset = |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 3225 | cpu_to_le32(offsetof(struct smb2_create_rsp, Buffer)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3226 | } |
| 3227 | |
| 3228 | err_out: |
| 3229 | if (file_present || created) |
| 3230 | path_put(&path); |
| 3231 | ksmbd_revert_fsids(work); |
| 3232 | err_out1: |
| 3233 | if (rc) { |
| 3234 | if (rc == -EINVAL) |
| 3235 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 3236 | else if (rc == -EOPNOTSUPP) |
| 3237 | rsp->hdr.Status = STATUS_NOT_SUPPORTED; |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 3238 | else if (rc == -EACCES || rc == -ESTALE || rc == -EXDEV) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3239 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 3240 | else if (rc == -ENOENT) |
| 3241 | rsp->hdr.Status = STATUS_OBJECT_NAME_INVALID; |
| 3242 | else if (rc == -EPERM) |
| 3243 | rsp->hdr.Status = STATUS_SHARING_VIOLATION; |
| 3244 | else if (rc == -EBUSY) |
| 3245 | rsp->hdr.Status = STATUS_DELETE_PENDING; |
| 3246 | else if (rc == -EBADF) |
| 3247 | rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND; |
| 3248 | else if (rc == -ENOEXEC) |
| 3249 | rsp->hdr.Status = STATUS_DUPLICATE_OBJECTID; |
| 3250 | else if (rc == -ENXIO) |
| 3251 | rsp->hdr.Status = STATUS_NO_SUCH_DEVICE; |
| 3252 | else if (rc == -EEXIST) |
| 3253 | rsp->hdr.Status = STATUS_OBJECT_NAME_COLLISION; |
| 3254 | else if (rc == -EMFILE) |
| 3255 | rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES; |
| 3256 | if (!rsp->hdr.Status) |
| 3257 | rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR; |
| 3258 | |
| 3259 | if (!fp || !fp->filename) |
| 3260 | kfree(name); |
| 3261 | if (fp) |
| 3262 | ksmbd_fd_put(work, fp); |
| 3263 | smb2_set_err_rsp(work); |
| 3264 | ksmbd_debug(SMB, "Error response: %x\n", rsp->hdr.Status); |
| 3265 | } |
| 3266 | |
| 3267 | kfree(lc); |
| 3268 | |
| 3269 | return 0; |
| 3270 | } |
| 3271 | |
| 3272 | static int readdir_info_level_struct_sz(int info_level) |
| 3273 | { |
| 3274 | switch (info_level) { |
| 3275 | case FILE_FULL_DIRECTORY_INFORMATION: |
| 3276 | return sizeof(struct file_full_directory_info); |
| 3277 | case FILE_BOTH_DIRECTORY_INFORMATION: |
| 3278 | return sizeof(struct file_both_directory_info); |
| 3279 | case FILE_DIRECTORY_INFORMATION: |
| 3280 | return sizeof(struct file_directory_info); |
| 3281 | case FILE_NAMES_INFORMATION: |
| 3282 | return sizeof(struct file_names_info); |
| 3283 | case FILEID_FULL_DIRECTORY_INFORMATION: |
| 3284 | return sizeof(struct file_id_full_dir_info); |
| 3285 | case FILEID_BOTH_DIRECTORY_INFORMATION: |
| 3286 | return sizeof(struct file_id_both_directory_info); |
| 3287 | case SMB_FIND_FILE_POSIX_INFO: |
| 3288 | return sizeof(struct smb2_posix_info); |
| 3289 | default: |
| 3290 | return -EOPNOTSUPP; |
| 3291 | } |
| 3292 | } |
| 3293 | |
| 3294 | static int dentry_name(struct ksmbd_dir_info *d_info, int info_level) |
| 3295 | { |
| 3296 | switch (info_level) { |
| 3297 | case FILE_FULL_DIRECTORY_INFORMATION: |
| 3298 | { |
| 3299 | struct file_full_directory_info *ffdinfo; |
| 3300 | |
| 3301 | ffdinfo = (struct file_full_directory_info *)d_info->rptr; |
| 3302 | d_info->rptr += le32_to_cpu(ffdinfo->NextEntryOffset); |
| 3303 | d_info->name = ffdinfo->FileName; |
| 3304 | d_info->name_len = le32_to_cpu(ffdinfo->FileNameLength); |
| 3305 | return 0; |
| 3306 | } |
| 3307 | case FILE_BOTH_DIRECTORY_INFORMATION: |
| 3308 | { |
| 3309 | struct file_both_directory_info *fbdinfo; |
| 3310 | |
| 3311 | fbdinfo = (struct file_both_directory_info *)d_info->rptr; |
| 3312 | d_info->rptr += le32_to_cpu(fbdinfo->NextEntryOffset); |
| 3313 | d_info->name = fbdinfo->FileName; |
| 3314 | d_info->name_len = le32_to_cpu(fbdinfo->FileNameLength); |
| 3315 | return 0; |
| 3316 | } |
| 3317 | case FILE_DIRECTORY_INFORMATION: |
| 3318 | { |
| 3319 | struct file_directory_info *fdinfo; |
| 3320 | |
| 3321 | fdinfo = (struct file_directory_info *)d_info->rptr; |
| 3322 | d_info->rptr += le32_to_cpu(fdinfo->NextEntryOffset); |
| 3323 | d_info->name = fdinfo->FileName; |
| 3324 | d_info->name_len = le32_to_cpu(fdinfo->FileNameLength); |
| 3325 | return 0; |
| 3326 | } |
| 3327 | case FILE_NAMES_INFORMATION: |
| 3328 | { |
| 3329 | struct file_names_info *fninfo; |
| 3330 | |
| 3331 | fninfo = (struct file_names_info *)d_info->rptr; |
| 3332 | d_info->rptr += le32_to_cpu(fninfo->NextEntryOffset); |
| 3333 | d_info->name = fninfo->FileName; |
| 3334 | d_info->name_len = le32_to_cpu(fninfo->FileNameLength); |
| 3335 | return 0; |
| 3336 | } |
| 3337 | case FILEID_FULL_DIRECTORY_INFORMATION: |
| 3338 | { |
| 3339 | struct file_id_full_dir_info *dinfo; |
| 3340 | |
| 3341 | dinfo = (struct file_id_full_dir_info *)d_info->rptr; |
| 3342 | d_info->rptr += le32_to_cpu(dinfo->NextEntryOffset); |
| 3343 | d_info->name = dinfo->FileName; |
| 3344 | d_info->name_len = le32_to_cpu(dinfo->FileNameLength); |
| 3345 | return 0; |
| 3346 | } |
| 3347 | case FILEID_BOTH_DIRECTORY_INFORMATION: |
| 3348 | { |
| 3349 | struct file_id_both_directory_info *fibdinfo; |
| 3350 | |
| 3351 | fibdinfo = (struct file_id_both_directory_info *)d_info->rptr; |
| 3352 | d_info->rptr += le32_to_cpu(fibdinfo->NextEntryOffset); |
| 3353 | d_info->name = fibdinfo->FileName; |
| 3354 | d_info->name_len = le32_to_cpu(fibdinfo->FileNameLength); |
| 3355 | return 0; |
| 3356 | } |
| 3357 | case SMB_FIND_FILE_POSIX_INFO: |
| 3358 | { |
| 3359 | struct smb2_posix_info *posix_info; |
| 3360 | |
| 3361 | posix_info = (struct smb2_posix_info *)d_info->rptr; |
| 3362 | d_info->rptr += le32_to_cpu(posix_info->NextEntryOffset); |
| 3363 | d_info->name = posix_info->name; |
| 3364 | d_info->name_len = le32_to_cpu(posix_info->name_len); |
| 3365 | return 0; |
| 3366 | } |
| 3367 | default: |
| 3368 | return -EINVAL; |
| 3369 | } |
| 3370 | } |
| 3371 | |
| 3372 | /** |
| 3373 | * smb2_populate_readdir_entry() - encode directory entry in smb2 response |
| 3374 | * buffer |
| 3375 | * @conn: connection instance |
| 3376 | * @info_level: smb information level |
| 3377 | * @d_info: structure included variables for query dir |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 3378 | * @user_ns: user namespace |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3379 | * @ksmbd_kstat: ksmbd wrapper of dirent stat information |
| 3380 | * |
| 3381 | * if directory has many entries, find first can't read it fully. |
| 3382 | * find next might be called multiple times to read remaining dir entries |
| 3383 | * |
| 3384 | * Return: 0 on success, otherwise error |
| 3385 | */ |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3386 | 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] | 3387 | struct ksmbd_dir_info *d_info, |
| 3388 | struct ksmbd_kstat *ksmbd_kstat) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3389 | { |
| 3390 | int next_entry_offset = 0; |
| 3391 | char *conv_name; |
| 3392 | int conv_len; |
| 3393 | void *kstat; |
Namjae Jeon | dac0ec6 | 2021-07-07 14:57:24 +0900 | [diff] [blame] | 3394 | int struct_sz, rc = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3395 | |
| 3396 | conv_name = ksmbd_convert_dir_info_name(d_info, |
| 3397 | conn->local_nls, |
| 3398 | &conv_len); |
| 3399 | if (!conv_name) |
| 3400 | return -ENOMEM; |
| 3401 | |
| 3402 | /* Somehow the name has only terminating NULL bytes */ |
| 3403 | if (conv_len < 0) { |
Namjae Jeon | dac0ec6 | 2021-07-07 14:57:24 +0900 | [diff] [blame] | 3404 | rc = -EINVAL; |
| 3405 | goto free_conv_name; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3406 | } |
| 3407 | |
| 3408 | struct_sz = readdir_info_level_struct_sz(info_level); |
| 3409 | next_entry_offset = ALIGN(struct_sz - 1 + conv_len, |
| 3410 | KSMBD_DIR_INFO_ALIGNMENT); |
| 3411 | |
| 3412 | if (next_entry_offset > d_info->out_buf_len) { |
| 3413 | d_info->out_buf_len = 0; |
Namjae Jeon | dac0ec6 | 2021-07-07 14:57:24 +0900 | [diff] [blame] | 3414 | rc = -ENOSPC; |
| 3415 | goto free_conv_name; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3416 | } |
| 3417 | |
| 3418 | kstat = d_info->wptr; |
| 3419 | if (info_level != FILE_NAMES_INFORMATION) |
| 3420 | kstat = ksmbd_vfs_init_kstat(&d_info->wptr, ksmbd_kstat); |
| 3421 | |
| 3422 | switch (info_level) { |
| 3423 | case FILE_FULL_DIRECTORY_INFORMATION: |
| 3424 | { |
| 3425 | struct file_full_directory_info *ffdinfo; |
| 3426 | |
| 3427 | ffdinfo = (struct file_full_directory_info *)kstat; |
| 3428 | ffdinfo->FileNameLength = cpu_to_le32(conv_len); |
| 3429 | ffdinfo->EaSize = |
| 3430 | smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode); |
| 3431 | if (ffdinfo->EaSize) |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 3432 | ffdinfo->ExtFileAttributes = FILE_ATTRIBUTE_REPARSE_POINT_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3433 | if (d_info->hide_dot_file && d_info->name[0] == '.') |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 3434 | ffdinfo->ExtFileAttributes |= FILE_ATTRIBUTE_HIDDEN_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3435 | memcpy(ffdinfo->FileName, conv_name, conv_len); |
| 3436 | ffdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3437 | break; |
| 3438 | } |
| 3439 | case FILE_BOTH_DIRECTORY_INFORMATION: |
| 3440 | { |
| 3441 | struct file_both_directory_info *fbdinfo; |
| 3442 | |
| 3443 | fbdinfo = (struct file_both_directory_info *)kstat; |
| 3444 | fbdinfo->FileNameLength = cpu_to_le32(conv_len); |
| 3445 | fbdinfo->EaSize = |
| 3446 | smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode); |
| 3447 | if (fbdinfo->EaSize) |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 3448 | fbdinfo->ExtFileAttributes = FILE_ATTRIBUTE_REPARSE_POINT_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3449 | fbdinfo->ShortNameLength = 0; |
| 3450 | fbdinfo->Reserved = 0; |
| 3451 | if (d_info->hide_dot_file && d_info->name[0] == '.') |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 3452 | fbdinfo->ExtFileAttributes |= FILE_ATTRIBUTE_HIDDEN_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3453 | memcpy(fbdinfo->FileName, conv_name, conv_len); |
| 3454 | fbdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3455 | break; |
| 3456 | } |
| 3457 | case FILE_DIRECTORY_INFORMATION: |
| 3458 | { |
| 3459 | struct file_directory_info *fdinfo; |
| 3460 | |
| 3461 | fdinfo = (struct file_directory_info *)kstat; |
| 3462 | fdinfo->FileNameLength = cpu_to_le32(conv_len); |
| 3463 | if (d_info->hide_dot_file && d_info->name[0] == '.') |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 3464 | fdinfo->ExtFileAttributes |= FILE_ATTRIBUTE_HIDDEN_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3465 | memcpy(fdinfo->FileName, conv_name, conv_len); |
| 3466 | fdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3467 | break; |
| 3468 | } |
| 3469 | case FILE_NAMES_INFORMATION: |
| 3470 | { |
| 3471 | struct file_names_info *fninfo; |
| 3472 | |
| 3473 | fninfo = (struct file_names_info *)kstat; |
| 3474 | fninfo->FileNameLength = cpu_to_le32(conv_len); |
| 3475 | memcpy(fninfo->FileName, conv_name, conv_len); |
| 3476 | fninfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3477 | break; |
| 3478 | } |
| 3479 | case FILEID_FULL_DIRECTORY_INFORMATION: |
| 3480 | { |
| 3481 | struct file_id_full_dir_info *dinfo; |
| 3482 | |
| 3483 | dinfo = (struct file_id_full_dir_info *)kstat; |
| 3484 | dinfo->FileNameLength = cpu_to_le32(conv_len); |
| 3485 | dinfo->EaSize = |
| 3486 | smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode); |
| 3487 | if (dinfo->EaSize) |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 3488 | dinfo->ExtFileAttributes = FILE_ATTRIBUTE_REPARSE_POINT_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3489 | dinfo->Reserved = 0; |
| 3490 | dinfo->UniqueId = cpu_to_le64(ksmbd_kstat->kstat->ino); |
| 3491 | if (d_info->hide_dot_file && d_info->name[0] == '.') |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 3492 | dinfo->ExtFileAttributes |= FILE_ATTRIBUTE_HIDDEN_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3493 | memcpy(dinfo->FileName, conv_name, conv_len); |
| 3494 | dinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3495 | break; |
| 3496 | } |
| 3497 | case FILEID_BOTH_DIRECTORY_INFORMATION: |
| 3498 | { |
| 3499 | struct file_id_both_directory_info *fibdinfo; |
| 3500 | |
| 3501 | fibdinfo = (struct file_id_both_directory_info *)kstat; |
| 3502 | fibdinfo->FileNameLength = cpu_to_le32(conv_len); |
| 3503 | fibdinfo->EaSize = |
| 3504 | smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode); |
| 3505 | if (fibdinfo->EaSize) |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 3506 | fibdinfo->ExtFileAttributes = FILE_ATTRIBUTE_REPARSE_POINT_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3507 | fibdinfo->UniqueId = cpu_to_le64(ksmbd_kstat->kstat->ino); |
| 3508 | fibdinfo->ShortNameLength = 0; |
| 3509 | fibdinfo->Reserved = 0; |
| 3510 | fibdinfo->Reserved2 = cpu_to_le16(0); |
| 3511 | if (d_info->hide_dot_file && d_info->name[0] == '.') |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 3512 | fibdinfo->ExtFileAttributes |= FILE_ATTRIBUTE_HIDDEN_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3513 | memcpy(fibdinfo->FileName, conv_name, conv_len); |
| 3514 | fibdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3515 | break; |
| 3516 | } |
| 3517 | case SMB_FIND_FILE_POSIX_INFO: |
| 3518 | { |
| 3519 | struct smb2_posix_info *posix_info; |
| 3520 | u64 time; |
| 3521 | |
| 3522 | posix_info = (struct smb2_posix_info *)kstat; |
| 3523 | posix_info->Ignored = 0; |
| 3524 | posix_info->CreationTime = cpu_to_le64(ksmbd_kstat->create_time); |
| 3525 | time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->ctime); |
| 3526 | posix_info->ChangeTime = cpu_to_le64(time); |
| 3527 | time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->atime); |
| 3528 | posix_info->LastAccessTime = cpu_to_le64(time); |
| 3529 | time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->mtime); |
| 3530 | posix_info->LastWriteTime = cpu_to_le64(time); |
| 3531 | posix_info->EndOfFile = cpu_to_le64(ksmbd_kstat->kstat->size); |
| 3532 | posix_info->AllocationSize = cpu_to_le64(ksmbd_kstat->kstat->blocks << 9); |
| 3533 | posix_info->DeviceId = cpu_to_le32(ksmbd_kstat->kstat->rdev); |
| 3534 | posix_info->HardLinks = cpu_to_le32(ksmbd_kstat->kstat->nlink); |
| 3535 | posix_info->Mode = cpu_to_le32(ksmbd_kstat->kstat->mode); |
| 3536 | posix_info->Inode = cpu_to_le64(ksmbd_kstat->kstat->ino); |
| 3537 | posix_info->DosAttributes = |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 3538 | S_ISDIR(ksmbd_kstat->kstat->mode) ? |
| 3539 | FILE_ATTRIBUTE_DIRECTORY_LE : FILE_ATTRIBUTE_ARCHIVE_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3540 | if (d_info->hide_dot_file && d_info->name[0] == '.') |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 3541 | posix_info->DosAttributes |= FILE_ATTRIBUTE_HIDDEN_LE; |
Christian Brauner | 475d6f9 | 2021-08-23 17:13:48 +0200 | [diff] [blame] | 3542 | 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] | 3543 | SIDNFS_USER, (struct smb_sid *)&posix_info->SidBuffer[0]); |
Christian Brauner | 475d6f9 | 2021-08-23 17:13:48 +0200 | [diff] [blame] | 3544 | 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] | 3545 | SIDNFS_GROUP, (struct smb_sid *)&posix_info->SidBuffer[20]); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3546 | memcpy(posix_info->name, conv_name, conv_len); |
| 3547 | posix_info->name_len = cpu_to_le32(conv_len); |
| 3548 | posix_info->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3549 | break; |
| 3550 | } |
| 3551 | |
| 3552 | } /* switch (info_level) */ |
| 3553 | |
| 3554 | d_info->last_entry_offset = d_info->data_count; |
| 3555 | d_info->data_count += next_entry_offset; |
Marios Makassikis | e7735c8 | 2021-05-06 11:40:02 +0900 | [diff] [blame] | 3556 | d_info->out_buf_len -= next_entry_offset; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3557 | d_info->wptr += next_entry_offset; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3558 | |
| 3559 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3560 | "info_level : %d, buf_len :%d, next_offset : %d, data_count : %d\n", |
| 3561 | info_level, d_info->out_buf_len, |
| 3562 | next_entry_offset, d_info->data_count); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3563 | |
Namjae Jeon | dac0ec6 | 2021-07-07 14:57:24 +0900 | [diff] [blame] | 3564 | free_conv_name: |
| 3565 | kfree(conv_name); |
| 3566 | return rc; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3567 | } |
| 3568 | |
| 3569 | struct smb2_query_dir_private { |
| 3570 | struct ksmbd_work *work; |
| 3571 | char *search_pattern; |
| 3572 | struct ksmbd_file *dir_fp; |
| 3573 | |
| 3574 | struct ksmbd_dir_info *d_info; |
| 3575 | int info_level; |
| 3576 | }; |
| 3577 | |
| 3578 | static void lock_dir(struct ksmbd_file *dir_fp) |
| 3579 | { |
| 3580 | struct dentry *dir = dir_fp->filp->f_path.dentry; |
| 3581 | |
| 3582 | inode_lock_nested(d_inode(dir), I_MUTEX_PARENT); |
| 3583 | } |
| 3584 | |
| 3585 | static void unlock_dir(struct ksmbd_file *dir_fp) |
| 3586 | { |
| 3587 | struct dentry *dir = dir_fp->filp->f_path.dentry; |
| 3588 | |
| 3589 | inode_unlock(d_inode(dir)); |
| 3590 | } |
| 3591 | |
| 3592 | static int process_query_dir_entries(struct smb2_query_dir_private *priv) |
| 3593 | { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 3594 | 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] | 3595 | struct kstat kstat; |
| 3596 | struct ksmbd_kstat ksmbd_kstat; |
| 3597 | int rc; |
| 3598 | int i; |
| 3599 | |
| 3600 | for (i = 0; i < priv->d_info->num_entry; i++) { |
| 3601 | struct dentry *dent; |
| 3602 | |
| 3603 | if (dentry_name(priv->d_info, priv->info_level)) |
| 3604 | return -EINVAL; |
| 3605 | |
| 3606 | lock_dir(priv->dir_fp); |
Christian Brauner | da1e7ad | 2021-08-23 17:13:47 +0200 | [diff] [blame] | 3607 | dent = lookup_one(user_ns, priv->d_info->name, |
| 3608 | priv->dir_fp->filp->f_path.dentry, |
| 3609 | priv->d_info->name_len); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3610 | unlock_dir(priv->dir_fp); |
| 3611 | |
| 3612 | if (IS_ERR(dent)) { |
| 3613 | ksmbd_debug(SMB, "Cannot lookup `%s' [%ld]\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3614 | priv->d_info->name, |
| 3615 | PTR_ERR(dent)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3616 | continue; |
| 3617 | } |
| 3618 | if (unlikely(d_is_negative(dent))) { |
| 3619 | dput(dent); |
| 3620 | ksmbd_debug(SMB, "Negative dentry `%s'\n", |
| 3621 | priv->d_info->name); |
| 3622 | continue; |
| 3623 | } |
| 3624 | |
| 3625 | ksmbd_kstat.kstat = &kstat; |
| 3626 | if (priv->info_level != FILE_NAMES_INFORMATION) |
| 3627 | ksmbd_vfs_fill_dentry_attrs(priv->work, |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 3628 | user_ns, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3629 | dent, |
| 3630 | &ksmbd_kstat); |
| 3631 | |
| 3632 | rc = smb2_populate_readdir_entry(priv->work->conn, |
| 3633 | priv->info_level, |
| 3634 | priv->d_info, |
| 3635 | &ksmbd_kstat); |
| 3636 | dput(dent); |
| 3637 | if (rc) |
| 3638 | return rc; |
| 3639 | } |
| 3640 | return 0; |
| 3641 | } |
| 3642 | |
| 3643 | static int reserve_populate_dentry(struct ksmbd_dir_info *d_info, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3644 | int info_level) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3645 | { |
| 3646 | int struct_sz; |
| 3647 | int conv_len; |
| 3648 | int next_entry_offset; |
| 3649 | |
| 3650 | struct_sz = readdir_info_level_struct_sz(info_level); |
| 3651 | if (struct_sz == -EOPNOTSUPP) |
| 3652 | return -EOPNOTSUPP; |
| 3653 | |
| 3654 | conv_len = (d_info->name_len + 1) * 2; |
| 3655 | next_entry_offset = ALIGN(struct_sz - 1 + conv_len, |
| 3656 | KSMBD_DIR_INFO_ALIGNMENT); |
| 3657 | |
| 3658 | if (next_entry_offset > d_info->out_buf_len) { |
| 3659 | d_info->out_buf_len = 0; |
| 3660 | return -ENOSPC; |
| 3661 | } |
| 3662 | |
| 3663 | switch (info_level) { |
| 3664 | case FILE_FULL_DIRECTORY_INFORMATION: |
| 3665 | { |
| 3666 | struct file_full_directory_info *ffdinfo; |
| 3667 | |
| 3668 | ffdinfo = (struct file_full_directory_info *)d_info->wptr; |
| 3669 | memcpy(ffdinfo->FileName, d_info->name, d_info->name_len); |
| 3670 | ffdinfo->FileName[d_info->name_len] = 0x00; |
| 3671 | ffdinfo->FileNameLength = cpu_to_le32(d_info->name_len); |
| 3672 | ffdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3673 | break; |
| 3674 | } |
| 3675 | case FILE_BOTH_DIRECTORY_INFORMATION: |
| 3676 | { |
| 3677 | struct file_both_directory_info *fbdinfo; |
| 3678 | |
| 3679 | fbdinfo = (struct file_both_directory_info *)d_info->wptr; |
| 3680 | memcpy(fbdinfo->FileName, d_info->name, d_info->name_len); |
| 3681 | fbdinfo->FileName[d_info->name_len] = 0x00; |
| 3682 | fbdinfo->FileNameLength = cpu_to_le32(d_info->name_len); |
| 3683 | fbdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3684 | break; |
| 3685 | } |
| 3686 | case FILE_DIRECTORY_INFORMATION: |
| 3687 | { |
| 3688 | struct file_directory_info *fdinfo; |
| 3689 | |
| 3690 | fdinfo = (struct file_directory_info *)d_info->wptr; |
| 3691 | memcpy(fdinfo->FileName, d_info->name, d_info->name_len); |
| 3692 | fdinfo->FileName[d_info->name_len] = 0x00; |
| 3693 | fdinfo->FileNameLength = cpu_to_le32(d_info->name_len); |
| 3694 | fdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3695 | break; |
| 3696 | } |
| 3697 | case FILE_NAMES_INFORMATION: |
| 3698 | { |
| 3699 | struct file_names_info *fninfo; |
| 3700 | |
| 3701 | fninfo = (struct file_names_info *)d_info->wptr; |
| 3702 | memcpy(fninfo->FileName, d_info->name, d_info->name_len); |
| 3703 | fninfo->FileName[d_info->name_len] = 0x00; |
| 3704 | fninfo->FileNameLength = cpu_to_le32(d_info->name_len); |
| 3705 | fninfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3706 | break; |
| 3707 | } |
| 3708 | case FILEID_FULL_DIRECTORY_INFORMATION: |
| 3709 | { |
| 3710 | struct file_id_full_dir_info *dinfo; |
| 3711 | |
| 3712 | dinfo = (struct file_id_full_dir_info *)d_info->wptr; |
| 3713 | memcpy(dinfo->FileName, d_info->name, d_info->name_len); |
| 3714 | dinfo->FileName[d_info->name_len] = 0x00; |
| 3715 | dinfo->FileNameLength = cpu_to_le32(d_info->name_len); |
| 3716 | dinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3717 | break; |
| 3718 | } |
| 3719 | case FILEID_BOTH_DIRECTORY_INFORMATION: |
| 3720 | { |
| 3721 | struct file_id_both_directory_info *fibdinfo; |
| 3722 | |
| 3723 | fibdinfo = (struct file_id_both_directory_info *)d_info->wptr; |
| 3724 | memcpy(fibdinfo->FileName, d_info->name, d_info->name_len); |
| 3725 | fibdinfo->FileName[d_info->name_len] = 0x00; |
| 3726 | fibdinfo->FileNameLength = cpu_to_le32(d_info->name_len); |
| 3727 | fibdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3728 | break; |
| 3729 | } |
| 3730 | case SMB_FIND_FILE_POSIX_INFO: |
| 3731 | { |
| 3732 | struct smb2_posix_info *posix_info; |
| 3733 | |
| 3734 | posix_info = (struct smb2_posix_info *)d_info->wptr; |
| 3735 | memcpy(posix_info->name, d_info->name, d_info->name_len); |
| 3736 | posix_info->name[d_info->name_len] = 0x00; |
| 3737 | posix_info->name_len = cpu_to_le32(d_info->name_len); |
| 3738 | posix_info->NextEntryOffset = |
| 3739 | cpu_to_le32(next_entry_offset); |
| 3740 | break; |
| 3741 | } |
| 3742 | } /* switch (info_level) */ |
| 3743 | |
| 3744 | d_info->num_entry++; |
| 3745 | d_info->out_buf_len -= next_entry_offset; |
| 3746 | d_info->wptr += next_entry_offset; |
| 3747 | return 0; |
| 3748 | } |
| 3749 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3750 | 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] | 3751 | loff_t offset, u64 ino, unsigned int d_type) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3752 | { |
| 3753 | struct ksmbd_readdir_data *buf; |
| 3754 | struct smb2_query_dir_private *priv; |
| 3755 | struct ksmbd_dir_info *d_info; |
| 3756 | int rc; |
| 3757 | |
| 3758 | buf = container_of(ctx, struct ksmbd_readdir_data, ctx); |
| 3759 | priv = buf->private; |
| 3760 | d_info = priv->d_info; |
| 3761 | |
| 3762 | /* dot and dotdot entries are already reserved */ |
| 3763 | if (!strcmp(".", name) || !strcmp("..", name)) |
| 3764 | return 0; |
| 3765 | if (ksmbd_share_veto_filename(priv->work->tcon->share_conf, name)) |
| 3766 | return 0; |
Namjae Jeon | b24c933 | 2021-03-21 17:32:19 +0900 | [diff] [blame] | 3767 | if (!match_pattern(name, namlen, priv->search_pattern)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3768 | return 0; |
| 3769 | |
| 3770 | d_info->name = name; |
| 3771 | d_info->name_len = namlen; |
| 3772 | rc = reserve_populate_dentry(d_info, priv->info_level); |
| 3773 | if (rc) |
| 3774 | return rc; |
| 3775 | if (d_info->flags & SMB2_RETURN_SINGLE_ENTRY) { |
| 3776 | d_info->out_buf_len = 0; |
| 3777 | return 0; |
| 3778 | } |
| 3779 | return 0; |
| 3780 | } |
| 3781 | |
| 3782 | static void restart_ctx(struct dir_context *ctx) |
| 3783 | { |
| 3784 | ctx->pos = 0; |
| 3785 | } |
| 3786 | |
| 3787 | static int verify_info_level(int info_level) |
| 3788 | { |
| 3789 | switch (info_level) { |
| 3790 | case FILE_FULL_DIRECTORY_INFORMATION: |
| 3791 | case FILE_BOTH_DIRECTORY_INFORMATION: |
| 3792 | case FILE_DIRECTORY_INFORMATION: |
| 3793 | case FILE_NAMES_INFORMATION: |
| 3794 | case FILEID_FULL_DIRECTORY_INFORMATION: |
| 3795 | case FILEID_BOTH_DIRECTORY_INFORMATION: |
| 3796 | case SMB_FIND_FILE_POSIX_INFO: |
| 3797 | break; |
| 3798 | default: |
| 3799 | return -EOPNOTSUPP; |
| 3800 | } |
| 3801 | |
| 3802 | return 0; |
| 3803 | } |
| 3804 | |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 3805 | static int smb2_calc_max_out_buf_len(struct ksmbd_work *work, |
| 3806 | unsigned short hdr2_len, |
| 3807 | unsigned int out_buf_len) |
| 3808 | { |
| 3809 | int free_len; |
| 3810 | |
| 3811 | if (out_buf_len > work->conn->vals->max_trans_size) |
| 3812 | return -EINVAL; |
| 3813 | |
| 3814 | free_len = (int)(work->response_sz - |
| 3815 | (get_rfc1002_len(work->response_buf) + 4)) - |
| 3816 | hdr2_len; |
| 3817 | if (free_len < 0) |
| 3818 | return -EINVAL; |
| 3819 | |
| 3820 | return min_t(int, out_buf_len, free_len); |
| 3821 | } |
| 3822 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3823 | int smb2_query_dir(struct ksmbd_work *work) |
| 3824 | { |
| 3825 | struct ksmbd_conn *conn = work->conn; |
| 3826 | struct smb2_query_directory_req *req; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 3827 | struct smb2_query_directory_rsp *rsp; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3828 | struct ksmbd_share_config *share = work->tcon->share_conf; |
| 3829 | struct ksmbd_file *dir_fp = NULL; |
| 3830 | struct ksmbd_dir_info d_info; |
| 3831 | int rc = 0; |
| 3832 | char *srch_ptr = NULL; |
| 3833 | unsigned char srch_flag; |
| 3834 | int buffer_sz; |
| 3835 | struct smb2_query_dir_private query_dir_private = {NULL, }; |
| 3836 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3837 | WORK_BUFFERS(work, req, rsp); |
| 3838 | |
| 3839 | if (ksmbd_override_fsids(work)) { |
| 3840 | rsp->hdr.Status = STATUS_NO_MEMORY; |
| 3841 | smb2_set_err_rsp(work); |
| 3842 | return -ENOMEM; |
| 3843 | } |
| 3844 | |
| 3845 | rc = verify_info_level(req->FileInformationClass); |
| 3846 | if (rc) { |
| 3847 | rc = -EFAULT; |
| 3848 | goto err_out2; |
| 3849 | } |
| 3850 | |
| 3851 | dir_fp = ksmbd_lookup_fd_slow(work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3852 | le64_to_cpu(req->VolatileFileId), |
| 3853 | le64_to_cpu(req->PersistentFileId)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3854 | if (!dir_fp) { |
| 3855 | rc = -EBADF; |
| 3856 | goto err_out2; |
| 3857 | } |
| 3858 | |
| 3859 | if (!(dir_fp->daccess & FILE_LIST_DIRECTORY_LE) || |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 3860 | inode_permission(file_mnt_user_ns(dir_fp->filp), |
| 3861 | file_inode(dir_fp->filp), |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3862 | MAY_READ | MAY_EXEC)) { |
Namjae Jeon | 493fa2f | 2021-06-29 09:22:16 +0900 | [diff] [blame] | 3863 | pr_err("no right to enumerate directory (%pd)\n", |
| 3864 | dir_fp->filp->f_path.dentry); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3865 | rc = -EACCES; |
| 3866 | goto err_out2; |
| 3867 | } |
| 3868 | |
| 3869 | if (!S_ISDIR(file_inode(dir_fp->filp)->i_mode)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 3870 | pr_err("can't do query dir for a file\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3871 | rc = -EINVAL; |
| 3872 | goto err_out2; |
| 3873 | } |
| 3874 | |
| 3875 | srch_flag = req->Flags; |
| 3876 | srch_ptr = smb_strndup_from_utf16(req->Buffer, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3877 | le16_to_cpu(req->FileNameLength), 1, |
| 3878 | conn->local_nls); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3879 | if (IS_ERR(srch_ptr)) { |
| 3880 | ksmbd_debug(SMB, "Search Pattern not found\n"); |
| 3881 | rc = -EINVAL; |
| 3882 | goto err_out2; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3883 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3884 | ksmbd_debug(SMB, "Search pattern is %s\n", srch_ptr); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3885 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3886 | |
| 3887 | ksmbd_debug(SMB, "Directory name is %s\n", dir_fp->filename); |
| 3888 | |
| 3889 | if (srch_flag & SMB2_REOPEN || srch_flag & SMB2_RESTART_SCANS) { |
| 3890 | ksmbd_debug(SMB, "Restart directory scan\n"); |
| 3891 | generic_file_llseek(dir_fp->filp, 0, SEEK_SET); |
| 3892 | restart_ctx(&dir_fp->readdir_data.ctx); |
| 3893 | } |
| 3894 | |
| 3895 | memset(&d_info, 0, sizeof(struct ksmbd_dir_info)); |
| 3896 | d_info.wptr = (char *)rsp->Buffer; |
| 3897 | d_info.rptr = (char *)rsp->Buffer; |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 3898 | d_info.out_buf_len = |
| 3899 | smb2_calc_max_out_buf_len(work, 8, |
| 3900 | le32_to_cpu(req->OutputBufferLength)); |
| 3901 | if (d_info.out_buf_len < 0) { |
| 3902 | rc = -EINVAL; |
| 3903 | goto err_out; |
| 3904 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3905 | d_info.flags = srch_flag; |
| 3906 | |
| 3907 | /* |
| 3908 | * reserve dot and dotdot entries in head of buffer |
| 3909 | * in first response |
| 3910 | */ |
| 3911 | rc = ksmbd_populate_dot_dotdot_entries(work, req->FileInformationClass, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3912 | dir_fp, &d_info, srch_ptr, |
| 3913 | smb2_populate_readdir_entry); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3914 | if (rc == -ENOSPC) |
| 3915 | rc = 0; |
| 3916 | else if (rc) |
| 3917 | goto err_out; |
| 3918 | |
| 3919 | if (test_share_config_flag(share, KSMBD_SHARE_FLAG_HIDE_DOT_FILES)) |
| 3920 | d_info.hide_dot_file = true; |
| 3921 | |
| 3922 | buffer_sz = d_info.out_buf_len; |
| 3923 | d_info.rptr = d_info.wptr; |
| 3924 | query_dir_private.work = work; |
| 3925 | query_dir_private.search_pattern = srch_ptr; |
| 3926 | query_dir_private.dir_fp = dir_fp; |
| 3927 | query_dir_private.d_info = &d_info; |
| 3928 | query_dir_private.info_level = req->FileInformationClass; |
| 3929 | dir_fp->readdir_data.private = &query_dir_private; |
| 3930 | set_ctx_actor(&dir_fp->readdir_data.ctx, __query_dir); |
| 3931 | |
Namjae Jeon | e8c0619 | 2021-06-22 11:06:11 +0900 | [diff] [blame] | 3932 | rc = iterate_dir(dir_fp->filp, &dir_fp->readdir_data.ctx); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3933 | if (rc == 0) |
| 3934 | restart_ctx(&dir_fp->readdir_data.ctx); |
| 3935 | if (rc == -ENOSPC) |
| 3936 | rc = 0; |
| 3937 | if (rc) |
| 3938 | goto err_out; |
| 3939 | |
| 3940 | d_info.wptr = d_info.rptr; |
| 3941 | d_info.out_buf_len = buffer_sz; |
| 3942 | rc = process_query_dir_entries(&query_dir_private); |
| 3943 | if (rc) |
| 3944 | goto err_out; |
| 3945 | |
| 3946 | if (!d_info.data_count && d_info.out_buf_len >= 0) { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3947 | if (srch_flag & SMB2_RETURN_SINGLE_ENTRY && !is_asterisk(srch_ptr)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3948 | rsp->hdr.Status = STATUS_NO_SUCH_FILE; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3949 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3950 | dir_fp->dot_dotdot[0] = dir_fp->dot_dotdot[1] = 0; |
| 3951 | rsp->hdr.Status = STATUS_NO_MORE_FILES; |
| 3952 | } |
| 3953 | rsp->StructureSize = cpu_to_le16(9); |
| 3954 | rsp->OutputBufferOffset = cpu_to_le16(0); |
| 3955 | rsp->OutputBufferLength = cpu_to_le32(0); |
| 3956 | rsp->Buffer[0] = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 3957 | inc_rfc1001_len(work->response_buf, 9); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3958 | } else { |
| 3959 | ((struct file_directory_info *) |
| 3960 | ((char *)rsp->Buffer + d_info.last_entry_offset)) |
| 3961 | ->NextEntryOffset = 0; |
| 3962 | |
| 3963 | rsp->StructureSize = cpu_to_le16(9); |
| 3964 | rsp->OutputBufferOffset = cpu_to_le16(72); |
| 3965 | rsp->OutputBufferLength = cpu_to_le32(d_info.data_count); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 3966 | inc_rfc1001_len(work->response_buf, 8 + d_info.data_count); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3967 | } |
| 3968 | |
| 3969 | kfree(srch_ptr); |
| 3970 | ksmbd_fd_put(work, dir_fp); |
| 3971 | ksmbd_revert_fsids(work); |
| 3972 | return 0; |
| 3973 | |
| 3974 | err_out: |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 3975 | pr_err("error while processing smb2 query dir rc = %d\n", rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3976 | kfree(srch_ptr); |
| 3977 | |
| 3978 | err_out2: |
| 3979 | if (rc == -EINVAL) |
| 3980 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 3981 | else if (rc == -EACCES) |
| 3982 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 3983 | else if (rc == -ENOENT) |
| 3984 | rsp->hdr.Status = STATUS_NO_SUCH_FILE; |
| 3985 | else if (rc == -EBADF) |
| 3986 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 3987 | else if (rc == -ENOMEM) |
| 3988 | rsp->hdr.Status = STATUS_NO_MEMORY; |
| 3989 | else if (rc == -EFAULT) |
| 3990 | rsp->hdr.Status = STATUS_INVALID_INFO_CLASS; |
| 3991 | if (!rsp->hdr.Status) |
| 3992 | rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR; |
| 3993 | |
| 3994 | smb2_set_err_rsp(work); |
| 3995 | ksmbd_fd_put(work, dir_fp); |
| 3996 | ksmbd_revert_fsids(work); |
| 3997 | return 0; |
| 3998 | } |
| 3999 | |
| 4000 | /** |
| 4001 | * buffer_check_err() - helper function to check buffer errors |
| 4002 | * @reqOutputBufferLength: max buffer length expected in command response |
| 4003 | * @rsp: query info response buffer contains output buffer length |
| 4004 | * @infoclass_size: query info class response buffer size |
| 4005 | * |
| 4006 | * Return: 0 on success, otherwise error |
| 4007 | */ |
| 4008 | static int buffer_check_err(int reqOutputBufferLength, |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4009 | struct smb2_query_info_rsp *rsp, |
| 4010 | void *rsp_org, int infoclass_size) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4011 | { |
| 4012 | if (reqOutputBufferLength < le32_to_cpu(rsp->OutputBufferLength)) { |
| 4013 | if (reqOutputBufferLength < infoclass_size) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4014 | pr_err("Invalid Buffer Size Requested\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4015 | rsp->hdr.Status = STATUS_INFO_LENGTH_MISMATCH; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4016 | *(__be32 *)rsp_org = cpu_to_be32(sizeof(struct smb2_hdr)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4017 | return -EINVAL; |
| 4018 | } |
| 4019 | |
| 4020 | ksmbd_debug(SMB, "Buffer Overflow\n"); |
| 4021 | rsp->hdr.Status = STATUS_BUFFER_OVERFLOW; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4022 | *(__be32 *)rsp_org = cpu_to_be32(sizeof(struct smb2_hdr) + |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4023 | reqOutputBufferLength); |
| 4024 | rsp->OutputBufferLength = cpu_to_le32(reqOutputBufferLength); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4025 | } |
| 4026 | return 0; |
| 4027 | } |
| 4028 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4029 | static void get_standard_info_pipe(struct smb2_query_info_rsp *rsp, |
| 4030 | void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4031 | { |
| 4032 | struct smb2_file_standard_info *sinfo; |
| 4033 | |
| 4034 | sinfo = (struct smb2_file_standard_info *)rsp->Buffer; |
| 4035 | |
| 4036 | sinfo->AllocationSize = cpu_to_le64(4096); |
| 4037 | sinfo->EndOfFile = cpu_to_le64(0); |
| 4038 | sinfo->NumberOfLinks = cpu_to_le32(1); |
| 4039 | sinfo->DeletePending = 1; |
| 4040 | sinfo->Directory = 0; |
| 4041 | rsp->OutputBufferLength = |
| 4042 | cpu_to_le32(sizeof(struct smb2_file_standard_info)); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4043 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_standard_info)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4044 | } |
| 4045 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4046 | static void get_internal_info_pipe(struct smb2_query_info_rsp *rsp, u64 num, |
| 4047 | void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4048 | { |
| 4049 | struct smb2_file_internal_info *file_info; |
| 4050 | |
| 4051 | file_info = (struct smb2_file_internal_info *)rsp->Buffer; |
| 4052 | |
| 4053 | /* any unique number */ |
| 4054 | file_info->IndexNumber = cpu_to_le64(num | (1ULL << 63)); |
| 4055 | rsp->OutputBufferLength = |
| 4056 | cpu_to_le32(sizeof(struct smb2_file_internal_info)); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4057 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_internal_info)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4058 | } |
| 4059 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4060 | static int smb2_get_info_file_pipe(struct ksmbd_session *sess, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4061 | struct smb2_query_info_req *req, |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4062 | struct smb2_query_info_rsp *rsp, |
| 4063 | void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4064 | { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4065 | u64 id; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4066 | int rc; |
| 4067 | |
| 4068 | /* |
| 4069 | * Windows can sometime send query file info request on |
| 4070 | * pipe without opening it, checking error condition here |
| 4071 | */ |
| 4072 | id = le64_to_cpu(req->VolatileFileId); |
| 4073 | if (!ksmbd_session_rpc_method(sess, id)) |
| 4074 | return -ENOENT; |
| 4075 | |
| 4076 | ksmbd_debug(SMB, "FileInfoClass %u, FileId 0x%llx\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4077 | req->FileInfoClass, le64_to_cpu(req->VolatileFileId)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4078 | |
| 4079 | switch (req->FileInfoClass) { |
| 4080 | case FILE_STANDARD_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4081 | get_standard_info_pipe(rsp, rsp_org); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4082 | rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength), |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4083 | rsp, rsp_org, |
| 4084 | FILE_STANDARD_INFORMATION_SIZE); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4085 | break; |
| 4086 | case FILE_INTERNAL_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4087 | get_internal_info_pipe(rsp, id, rsp_org); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4088 | rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength), |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4089 | rsp, rsp_org, |
| 4090 | FILE_INTERNAL_INFORMATION_SIZE); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4091 | break; |
| 4092 | default: |
| 4093 | ksmbd_debug(SMB, "smb2_info_file_pipe for %u not supported\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4094 | req->FileInfoClass); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4095 | rc = -EOPNOTSUPP; |
| 4096 | } |
| 4097 | return rc; |
| 4098 | } |
| 4099 | |
| 4100 | /** |
| 4101 | * smb2_get_ea() - handler for smb2 get extended attribute command |
| 4102 | * @work: smb work containing query info command buffer |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 4103 | * @fp: ksmbd_file pointer |
| 4104 | * @req: get extended attribute request |
| 4105 | * @rsp: response buffer pointer |
| 4106 | * @rsp_org: base response buffer pointer in case of chained response |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4107 | * |
| 4108 | * Return: 0 on success, otherwise error |
| 4109 | */ |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4110 | 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] | 4111 | struct smb2_query_info_req *req, |
| 4112 | struct smb2_query_info_rsp *rsp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4113 | { |
| 4114 | struct smb2_ea_info *eainfo, *prev_eainfo; |
| 4115 | char *name, *ptr, *xattr_list = NULL, *buf; |
| 4116 | int rc, name_len, value_len, xattr_list_len, idx; |
| 4117 | ssize_t buf_free_len, alignment_bytes, next_offset, rsp_data_cnt = 0; |
| 4118 | struct smb2_ea_info_req *ea_req = NULL; |
| 4119 | struct path *path; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 4120 | struct user_namespace *user_ns = file_mnt_user_ns(fp->filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4121 | |
| 4122 | if (!(fp->daccess & FILE_READ_EA_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4123 | pr_err("Not permitted to read ext attr : 0x%x\n", |
| 4124 | fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4125 | return -EACCES; |
| 4126 | } |
| 4127 | |
| 4128 | path = &fp->filp->f_path; |
| 4129 | /* single EA entry is requested with given user.* name */ |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4130 | if (req->InputBufferLength) { |
Namjae Jeon | 6d56262 | 2021-09-18 18:45:12 +0900 | [diff] [blame] | 4131 | if (le32_to_cpu(req->InputBufferLength) < |
| 4132 | sizeof(struct smb2_ea_info_req)) |
| 4133 | return -EINVAL; |
| 4134 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4135 | ea_req = (struct smb2_ea_info_req *)req->Buffer; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4136 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4137 | /* need to send all EAs, if no specific EA is requested*/ |
| 4138 | if (le32_to_cpu(req->Flags) & SL_RETURN_SINGLE_ENTRY) |
| 4139 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4140 | "All EAs are requested but need to send single EA entry in rsp flags 0x%x\n", |
| 4141 | le32_to_cpu(req->Flags)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4142 | } |
| 4143 | |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 4144 | buf_free_len = |
| 4145 | smb2_calc_max_out_buf_len(work, 8, |
| 4146 | le32_to_cpu(req->OutputBufferLength)); |
| 4147 | if (buf_free_len < 0) |
| 4148 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4149 | |
| 4150 | rc = ksmbd_vfs_listxattr(path->dentry, &xattr_list); |
| 4151 | if (rc < 0) { |
| 4152 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 4153 | goto out; |
| 4154 | } else if (!rc) { /* there is no EA in the file */ |
| 4155 | ksmbd_debug(SMB, "no ea data in the file\n"); |
| 4156 | goto done; |
| 4157 | } |
| 4158 | xattr_list_len = rc; |
| 4159 | |
| 4160 | ptr = (char *)rsp->Buffer; |
| 4161 | eainfo = (struct smb2_ea_info *)ptr; |
| 4162 | prev_eainfo = eainfo; |
| 4163 | idx = 0; |
| 4164 | |
| 4165 | while (idx < xattr_list_len) { |
| 4166 | name = xattr_list + idx; |
| 4167 | name_len = strlen(name); |
| 4168 | |
| 4169 | ksmbd_debug(SMB, "%s, len %d\n", name, name_len); |
| 4170 | idx += name_len + 1; |
| 4171 | |
| 4172 | /* |
| 4173 | * CIFS does not support EA other than user.* namespace, |
| 4174 | * still keep the framework generic, to list other attrs |
| 4175 | * in future. |
| 4176 | */ |
| 4177 | if (strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) |
| 4178 | continue; |
| 4179 | |
| 4180 | if (!strncmp(&name[XATTR_USER_PREFIX_LEN], STREAM_PREFIX, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4181 | STREAM_PREFIX_LEN)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4182 | continue; |
| 4183 | |
| 4184 | if (req->InputBufferLength && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4185 | strncmp(&name[XATTR_USER_PREFIX_LEN], ea_req->name, |
| 4186 | ea_req->EaNameLength)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4187 | continue; |
| 4188 | |
| 4189 | if (!strncmp(&name[XATTR_USER_PREFIX_LEN], |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4190 | DOS_ATTRIBUTE_PREFIX, DOS_ATTRIBUTE_PREFIX_LEN)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4191 | continue; |
| 4192 | |
| 4193 | if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) |
| 4194 | name_len -= XATTR_USER_PREFIX_LEN; |
| 4195 | |
| 4196 | ptr = (char *)(&eainfo->name + name_len + 1); |
| 4197 | buf_free_len -= (offsetof(struct smb2_ea_info, name) + |
| 4198 | name_len + 1); |
| 4199 | /* bailout if xattr can't fit in buf_free_len */ |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 4200 | value_len = ksmbd_vfs_getxattr(user_ns, path->dentry, |
| 4201 | name, &buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4202 | if (value_len <= 0) { |
| 4203 | rc = -ENOENT; |
| 4204 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 4205 | goto out; |
| 4206 | } |
| 4207 | |
| 4208 | buf_free_len -= value_len; |
| 4209 | if (buf_free_len < 0) { |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 4210 | kfree(buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4211 | break; |
| 4212 | } |
| 4213 | |
| 4214 | memcpy(ptr, buf, value_len); |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 4215 | kfree(buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4216 | |
| 4217 | ptr += value_len; |
| 4218 | eainfo->Flags = 0; |
| 4219 | eainfo->EaNameLength = name_len; |
| 4220 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4221 | if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4222 | memcpy(eainfo->name, &name[XATTR_USER_PREFIX_LEN], |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4223 | name_len); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4224 | else |
| 4225 | memcpy(eainfo->name, name, name_len); |
| 4226 | |
| 4227 | eainfo->name[name_len] = '\0'; |
| 4228 | eainfo->EaValueLength = cpu_to_le16(value_len); |
| 4229 | next_offset = offsetof(struct smb2_ea_info, name) + |
| 4230 | name_len + 1 + value_len; |
| 4231 | |
| 4232 | /* align next xattr entry at 4 byte bundary */ |
| 4233 | alignment_bytes = ((next_offset + 3) & ~3) - next_offset; |
| 4234 | if (alignment_bytes) { |
| 4235 | memset(ptr, '\0', alignment_bytes); |
| 4236 | ptr += alignment_bytes; |
| 4237 | next_offset += alignment_bytes; |
| 4238 | buf_free_len -= alignment_bytes; |
| 4239 | } |
| 4240 | eainfo->NextEntryOffset = cpu_to_le32(next_offset); |
| 4241 | prev_eainfo = eainfo; |
| 4242 | eainfo = (struct smb2_ea_info *)ptr; |
| 4243 | rsp_data_cnt += next_offset; |
| 4244 | |
| 4245 | if (req->InputBufferLength) { |
| 4246 | ksmbd_debug(SMB, "single entry requested\n"); |
| 4247 | break; |
| 4248 | } |
| 4249 | } |
| 4250 | |
| 4251 | /* no more ea entries */ |
| 4252 | prev_eainfo->NextEntryOffset = 0; |
| 4253 | done: |
| 4254 | rc = 0; |
| 4255 | if (rsp_data_cnt == 0) |
| 4256 | rsp->hdr.Status = STATUS_NO_EAS_ON_FILE; |
| 4257 | rsp->OutputBufferLength = cpu_to_le32(rsp_data_cnt); |
| 4258 | inc_rfc1001_len(rsp_org, rsp_data_cnt); |
| 4259 | out: |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 4260 | kvfree(xattr_list); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4261 | return rc; |
| 4262 | } |
| 4263 | |
| 4264 | static void get_file_access_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4265 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4266 | { |
| 4267 | struct smb2_file_access_info *file_info; |
| 4268 | |
| 4269 | file_info = (struct smb2_file_access_info *)rsp->Buffer; |
| 4270 | file_info->AccessFlags = fp->daccess; |
| 4271 | rsp->OutputBufferLength = |
| 4272 | cpu_to_le32(sizeof(struct smb2_file_access_info)); |
| 4273 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_access_info)); |
| 4274 | } |
| 4275 | |
| 4276 | static int get_file_basic_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4277 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4278 | { |
Namjae Jeon | 88d3005 | 2021-09-29 15:37:18 +0900 | [diff] [blame] | 4279 | struct smb2_file_basic_info *basic_info; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4280 | struct kstat stat; |
| 4281 | u64 time; |
| 4282 | |
| 4283 | if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4284 | pr_err("no right to read the attributes : 0x%x\n", |
| 4285 | fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4286 | return -EACCES; |
| 4287 | } |
| 4288 | |
Namjae Jeon | 88d3005 | 2021-09-29 15:37:18 +0900 | [diff] [blame] | 4289 | basic_info = (struct smb2_file_basic_info *)rsp->Buffer; |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 4290 | generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp), |
| 4291 | &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4292 | basic_info->CreationTime = cpu_to_le64(fp->create_time); |
| 4293 | time = ksmbd_UnixTimeToNT(stat.atime); |
| 4294 | basic_info->LastAccessTime = cpu_to_le64(time); |
| 4295 | time = ksmbd_UnixTimeToNT(stat.mtime); |
| 4296 | basic_info->LastWriteTime = cpu_to_le64(time); |
| 4297 | time = ksmbd_UnixTimeToNT(stat.ctime); |
| 4298 | basic_info->ChangeTime = cpu_to_le64(time); |
| 4299 | basic_info->Attributes = fp->f_ci->m_fattr; |
| 4300 | basic_info->Pad1 = 0; |
| 4301 | rsp->OutputBufferLength = |
Namjae Jeon | 88d3005 | 2021-09-29 15:37:18 +0900 | [diff] [blame] | 4302 | cpu_to_le32(sizeof(struct smb2_file_basic_info)); |
| 4303 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_basic_info)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4304 | return 0; |
| 4305 | } |
| 4306 | |
| 4307 | static unsigned long long get_allocation_size(struct inode *inode, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4308 | struct kstat *stat) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4309 | { |
| 4310 | unsigned long long alloc_size = 0; |
| 4311 | |
| 4312 | if (!S_ISDIR(stat->mode)) { |
| 4313 | if ((inode->i_blocks << 9) <= stat->size) |
| 4314 | alloc_size = stat->size; |
| 4315 | else |
| 4316 | alloc_size = inode->i_blocks << 9; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4317 | } |
| 4318 | |
| 4319 | return alloc_size; |
| 4320 | } |
| 4321 | |
| 4322 | static void get_file_standard_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4323 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4324 | { |
| 4325 | struct smb2_file_standard_info *sinfo; |
| 4326 | unsigned int delete_pending; |
| 4327 | struct inode *inode; |
| 4328 | struct kstat stat; |
| 4329 | |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 4330 | inode = file_inode(fp->filp); |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 4331 | generic_fillattr(file_mnt_user_ns(fp->filp), inode, &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4332 | |
| 4333 | sinfo = (struct smb2_file_standard_info *)rsp->Buffer; |
| 4334 | delete_pending = ksmbd_inode_pending_delete(fp); |
| 4335 | |
| 4336 | sinfo->AllocationSize = cpu_to_le64(get_allocation_size(inode, &stat)); |
| 4337 | sinfo->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size); |
| 4338 | sinfo->NumberOfLinks = cpu_to_le32(get_nlink(&stat) - delete_pending); |
| 4339 | sinfo->DeletePending = delete_pending; |
| 4340 | sinfo->Directory = S_ISDIR(stat.mode) ? 1 : 0; |
| 4341 | rsp->OutputBufferLength = |
| 4342 | cpu_to_le32(sizeof(struct smb2_file_standard_info)); |
| 4343 | inc_rfc1001_len(rsp_org, |
| 4344 | sizeof(struct smb2_file_standard_info)); |
| 4345 | } |
| 4346 | |
| 4347 | static void get_file_alignment_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4348 | void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4349 | { |
| 4350 | struct smb2_file_alignment_info *file_info; |
| 4351 | |
| 4352 | file_info = (struct smb2_file_alignment_info *)rsp->Buffer; |
| 4353 | file_info->AlignmentRequirement = 0; |
| 4354 | rsp->OutputBufferLength = |
| 4355 | cpu_to_le32(sizeof(struct smb2_file_alignment_info)); |
| 4356 | inc_rfc1001_len(rsp_org, |
| 4357 | sizeof(struct smb2_file_alignment_info)); |
| 4358 | } |
| 4359 | |
| 4360 | static int get_file_all_info(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4361 | struct smb2_query_info_rsp *rsp, |
| 4362 | struct ksmbd_file *fp, |
| 4363 | void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4364 | { |
| 4365 | struct ksmbd_conn *conn = work->conn; |
| 4366 | struct smb2_file_all_info *file_info; |
| 4367 | unsigned int delete_pending; |
| 4368 | struct inode *inode; |
| 4369 | struct kstat stat; |
| 4370 | int conv_len; |
| 4371 | char *filename; |
| 4372 | u64 time; |
| 4373 | |
| 4374 | if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) { |
| 4375 | ksmbd_debug(SMB, "no right to read the attributes : 0x%x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4376 | fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4377 | return -EACCES; |
| 4378 | } |
| 4379 | |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 4380 | filename = convert_to_nt_pathname(fp->filename); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4381 | if (!filename) |
| 4382 | return -ENOMEM; |
| 4383 | |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 4384 | inode = file_inode(fp->filp); |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 4385 | generic_fillattr(file_mnt_user_ns(fp->filp), inode, &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4386 | |
| 4387 | ksmbd_debug(SMB, "filename = %s\n", filename); |
| 4388 | delete_pending = ksmbd_inode_pending_delete(fp); |
| 4389 | file_info = (struct smb2_file_all_info *)rsp->Buffer; |
| 4390 | |
| 4391 | file_info->CreationTime = cpu_to_le64(fp->create_time); |
| 4392 | time = ksmbd_UnixTimeToNT(stat.atime); |
| 4393 | file_info->LastAccessTime = cpu_to_le64(time); |
| 4394 | time = ksmbd_UnixTimeToNT(stat.mtime); |
| 4395 | file_info->LastWriteTime = cpu_to_le64(time); |
| 4396 | time = ksmbd_UnixTimeToNT(stat.ctime); |
| 4397 | file_info->ChangeTime = cpu_to_le64(time); |
| 4398 | file_info->Attributes = fp->f_ci->m_fattr; |
| 4399 | file_info->Pad1 = 0; |
| 4400 | file_info->AllocationSize = |
| 4401 | cpu_to_le64(get_allocation_size(inode, &stat)); |
| 4402 | file_info->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size); |
| 4403 | file_info->NumberOfLinks = |
| 4404 | cpu_to_le32(get_nlink(&stat) - delete_pending); |
| 4405 | file_info->DeletePending = delete_pending; |
| 4406 | file_info->Directory = S_ISDIR(stat.mode) ? 1 : 0; |
| 4407 | file_info->Pad2 = 0; |
| 4408 | file_info->IndexNumber = cpu_to_le64(stat.ino); |
| 4409 | file_info->EASize = 0; |
| 4410 | file_info->AccessFlags = fp->daccess; |
| 4411 | file_info->CurrentByteOffset = cpu_to_le64(fp->filp->f_pos); |
| 4412 | file_info->Mode = fp->coption; |
| 4413 | file_info->AlignmentRequirement = 0; |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4414 | conv_len = smbConvertToUTF16((__le16 *)file_info->FileName, filename, |
| 4415 | PATH_MAX, conn->local_nls, 0); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4416 | conv_len *= 2; |
| 4417 | file_info->FileNameLength = cpu_to_le32(conv_len); |
| 4418 | rsp->OutputBufferLength = |
| 4419 | cpu_to_le32(sizeof(struct smb2_file_all_info) + conv_len - 1); |
| 4420 | kfree(filename); |
| 4421 | inc_rfc1001_len(rsp_org, le32_to_cpu(rsp->OutputBufferLength)); |
| 4422 | return 0; |
| 4423 | } |
| 4424 | |
| 4425 | static void get_file_alternate_info(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4426 | struct smb2_query_info_rsp *rsp, |
| 4427 | struct ksmbd_file *fp, |
| 4428 | void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4429 | { |
| 4430 | struct ksmbd_conn *conn = work->conn; |
| 4431 | struct smb2_file_alt_name_info *file_info; |
Namjae Jeon | 493fa2f | 2021-06-29 09:22:16 +0900 | [diff] [blame] | 4432 | struct dentry *dentry = fp->filp->f_path.dentry; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4433 | int conv_len; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4434 | |
Namjae Jeon | 493fa2f | 2021-06-29 09:22:16 +0900 | [diff] [blame] | 4435 | spin_lock(&dentry->d_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4436 | file_info = (struct smb2_file_alt_name_info *)rsp->Buffer; |
| 4437 | conv_len = ksmbd_extract_shortname(conn, |
Namjae Jeon | 493fa2f | 2021-06-29 09:22:16 +0900 | [diff] [blame] | 4438 | dentry->d_name.name, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4439 | file_info->FileName); |
Namjae Jeon | 493fa2f | 2021-06-29 09:22:16 +0900 | [diff] [blame] | 4440 | spin_unlock(&dentry->d_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4441 | file_info->FileNameLength = cpu_to_le32(conv_len); |
| 4442 | rsp->OutputBufferLength = |
| 4443 | cpu_to_le32(sizeof(struct smb2_file_alt_name_info) + conv_len); |
| 4444 | inc_rfc1001_len(rsp_org, le32_to_cpu(rsp->OutputBufferLength)); |
| 4445 | } |
| 4446 | |
| 4447 | static void get_file_stream_info(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4448 | struct smb2_query_info_rsp *rsp, |
| 4449 | struct ksmbd_file *fp, |
| 4450 | void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4451 | { |
| 4452 | struct ksmbd_conn *conn = work->conn; |
| 4453 | struct smb2_file_stream_info *file_info; |
| 4454 | char *stream_name, *xattr_list = NULL, *stream_buf; |
| 4455 | struct kstat stat; |
| 4456 | struct path *path = &fp->filp->f_path; |
| 4457 | ssize_t xattr_list_len; |
| 4458 | int nbytes = 0, streamlen, stream_name_len, next, idx = 0; |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 4459 | int buf_free_len; |
| 4460 | struct smb2_query_info_req *req = ksmbd_req_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4461 | |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 4462 | generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp), |
| 4463 | &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4464 | file_info = (struct smb2_file_stream_info *)rsp->Buffer; |
| 4465 | |
Namjae Jeon | 1ec72153 | 2021-11-21 11:32:39 +0900 | [diff] [blame] | 4466 | buf_free_len = |
| 4467 | smb2_calc_max_out_buf_len(work, 8, |
| 4468 | le32_to_cpu(req->OutputBufferLength)); |
| 4469 | if (buf_free_len < 0) |
| 4470 | goto out; |
| 4471 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4472 | xattr_list_len = ksmbd_vfs_listxattr(path->dentry, &xattr_list); |
| 4473 | if (xattr_list_len < 0) { |
| 4474 | goto out; |
| 4475 | } else if (!xattr_list_len) { |
| 4476 | ksmbd_debug(SMB, "empty xattr in the file\n"); |
| 4477 | goto out; |
| 4478 | } |
| 4479 | |
| 4480 | while (idx < xattr_list_len) { |
| 4481 | stream_name = xattr_list + idx; |
| 4482 | streamlen = strlen(stream_name); |
| 4483 | idx += streamlen + 1; |
| 4484 | |
| 4485 | ksmbd_debug(SMB, "%s, len %d\n", stream_name, streamlen); |
| 4486 | |
| 4487 | if (strncmp(&stream_name[XATTR_USER_PREFIX_LEN], |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4488 | STREAM_PREFIX, STREAM_PREFIX_LEN)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4489 | continue; |
| 4490 | |
| 4491 | stream_name_len = streamlen - (XATTR_USER_PREFIX_LEN + |
| 4492 | STREAM_PREFIX_LEN); |
| 4493 | streamlen = stream_name_len; |
| 4494 | |
| 4495 | /* plus : size */ |
| 4496 | streamlen += 1; |
| 4497 | stream_buf = kmalloc(streamlen + 1, GFP_KERNEL); |
| 4498 | if (!stream_buf) |
| 4499 | break; |
| 4500 | |
| 4501 | streamlen = snprintf(stream_buf, streamlen + 1, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4502 | ":%s", &stream_name[XATTR_NAME_STREAM_LEN]); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4503 | |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 4504 | next = sizeof(struct smb2_file_stream_info) + streamlen * 2; |
Namjae Jeon | 178ca6f | 2021-11-24 10:23:02 +0900 | [diff] [blame] | 4505 | if (next > buf_free_len) { |
| 4506 | kfree(stream_buf); |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 4507 | break; |
Namjae Jeon | 178ca6f | 2021-11-24 10:23:02 +0900 | [diff] [blame] | 4508 | } |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 4509 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4510 | file_info = (struct smb2_file_stream_info *)&rsp->Buffer[nbytes]; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4511 | streamlen = smbConvertToUTF16((__le16 *)file_info->StreamName, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4512 | stream_buf, streamlen, |
| 4513 | conn->local_nls, 0); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4514 | streamlen *= 2; |
| 4515 | kfree(stream_buf); |
| 4516 | file_info->StreamNameLength = cpu_to_le32(streamlen); |
| 4517 | file_info->StreamSize = cpu_to_le64(stream_name_len); |
| 4518 | file_info->StreamAllocationSize = cpu_to_le64(stream_name_len); |
| 4519 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4520 | nbytes += next; |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 4521 | buf_free_len -= next; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4522 | file_info->NextEntryOffset = cpu_to_le32(next); |
| 4523 | } |
| 4524 | |
Namjae Jeon | 1ec72153 | 2021-11-21 11:32:39 +0900 | [diff] [blame] | 4525 | out: |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 4526 | if (!S_ISDIR(stat.mode) && |
| 4527 | buf_free_len >= sizeof(struct smb2_file_stream_info) + 7 * 2) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4528 | file_info = (struct smb2_file_stream_info *) |
| 4529 | &rsp->Buffer[nbytes]; |
| 4530 | streamlen = smbConvertToUTF16((__le16 *)file_info->StreamName, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4531 | "::$DATA", 7, conn->local_nls, 0); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4532 | streamlen *= 2; |
| 4533 | file_info->StreamNameLength = cpu_to_le32(streamlen); |
Namjae Jeon | 1ec72153 | 2021-11-21 11:32:39 +0900 | [diff] [blame] | 4534 | file_info->StreamSize = cpu_to_le64(stat.size); |
| 4535 | file_info->StreamAllocationSize = cpu_to_le64(stat.blocks << 9); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4536 | nbytes += sizeof(struct smb2_file_stream_info) + streamlen; |
| 4537 | } |
| 4538 | |
| 4539 | /* last entry offset should be 0 */ |
| 4540 | file_info->NextEntryOffset = 0; |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 4541 | kvfree(xattr_list); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4542 | |
| 4543 | rsp->OutputBufferLength = cpu_to_le32(nbytes); |
| 4544 | inc_rfc1001_len(rsp_org, nbytes); |
| 4545 | } |
| 4546 | |
| 4547 | static void get_file_internal_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4548 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4549 | { |
| 4550 | struct smb2_file_internal_info *file_info; |
| 4551 | struct kstat stat; |
| 4552 | |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 4553 | generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp), |
| 4554 | &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4555 | file_info = (struct smb2_file_internal_info *)rsp->Buffer; |
| 4556 | file_info->IndexNumber = cpu_to_le64(stat.ino); |
| 4557 | rsp->OutputBufferLength = |
| 4558 | cpu_to_le32(sizeof(struct smb2_file_internal_info)); |
| 4559 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_internal_info)); |
| 4560 | } |
| 4561 | |
| 4562 | 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] | 4563 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4564 | { |
| 4565 | struct smb2_file_ntwrk_info *file_info; |
| 4566 | struct inode *inode; |
| 4567 | struct kstat stat; |
| 4568 | u64 time; |
| 4569 | |
| 4570 | if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4571 | pr_err("no right to read the attributes : 0x%x\n", |
| 4572 | fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4573 | return -EACCES; |
| 4574 | } |
| 4575 | |
| 4576 | file_info = (struct smb2_file_ntwrk_info *)rsp->Buffer; |
| 4577 | |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 4578 | inode = file_inode(fp->filp); |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 4579 | generic_fillattr(file_mnt_user_ns(fp->filp), inode, &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4580 | |
| 4581 | file_info->CreationTime = cpu_to_le64(fp->create_time); |
| 4582 | time = ksmbd_UnixTimeToNT(stat.atime); |
| 4583 | file_info->LastAccessTime = cpu_to_le64(time); |
| 4584 | time = ksmbd_UnixTimeToNT(stat.mtime); |
| 4585 | file_info->LastWriteTime = cpu_to_le64(time); |
| 4586 | time = ksmbd_UnixTimeToNT(stat.ctime); |
| 4587 | file_info->ChangeTime = cpu_to_le64(time); |
| 4588 | file_info->Attributes = fp->f_ci->m_fattr; |
| 4589 | file_info->AllocationSize = |
| 4590 | cpu_to_le64(get_allocation_size(inode, &stat)); |
| 4591 | file_info->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size); |
| 4592 | file_info->Reserved = cpu_to_le32(0); |
| 4593 | rsp->OutputBufferLength = |
| 4594 | cpu_to_le32(sizeof(struct smb2_file_ntwrk_info)); |
| 4595 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_ntwrk_info)); |
| 4596 | return 0; |
| 4597 | } |
| 4598 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4599 | 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] | 4600 | { |
| 4601 | struct smb2_file_ea_info *file_info; |
| 4602 | |
| 4603 | file_info = (struct smb2_file_ea_info *)rsp->Buffer; |
| 4604 | file_info->EASize = 0; |
| 4605 | rsp->OutputBufferLength = |
| 4606 | cpu_to_le32(sizeof(struct smb2_file_ea_info)); |
| 4607 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_ea_info)); |
| 4608 | } |
| 4609 | |
| 4610 | static void get_file_position_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4611 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4612 | { |
| 4613 | struct smb2_file_pos_info *file_info; |
| 4614 | |
| 4615 | file_info = (struct smb2_file_pos_info *)rsp->Buffer; |
| 4616 | file_info->CurrentByteOffset = cpu_to_le64(fp->filp->f_pos); |
| 4617 | rsp->OutputBufferLength = |
| 4618 | cpu_to_le32(sizeof(struct smb2_file_pos_info)); |
| 4619 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_pos_info)); |
| 4620 | } |
| 4621 | |
| 4622 | static void get_file_mode_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4623 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4624 | { |
| 4625 | struct smb2_file_mode_info *file_info; |
| 4626 | |
| 4627 | file_info = (struct smb2_file_mode_info *)rsp->Buffer; |
| 4628 | file_info->Mode = fp->coption & FILE_MODE_INFO_MASK; |
| 4629 | rsp->OutputBufferLength = |
| 4630 | cpu_to_le32(sizeof(struct smb2_file_mode_info)); |
| 4631 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_mode_info)); |
| 4632 | } |
| 4633 | |
| 4634 | static void get_file_compression_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4635 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4636 | { |
| 4637 | struct smb2_file_comp_info *file_info; |
| 4638 | struct kstat stat; |
| 4639 | |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 4640 | generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp), |
| 4641 | &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4642 | |
| 4643 | file_info = (struct smb2_file_comp_info *)rsp->Buffer; |
| 4644 | file_info->CompressedFileSize = cpu_to_le64(stat.blocks << 9); |
| 4645 | file_info->CompressionFormat = COMPRESSION_FORMAT_NONE; |
| 4646 | file_info->CompressionUnitShift = 0; |
| 4647 | file_info->ChunkShift = 0; |
| 4648 | file_info->ClusterShift = 0; |
| 4649 | memset(&file_info->Reserved[0], 0, 3); |
| 4650 | |
| 4651 | rsp->OutputBufferLength = |
| 4652 | cpu_to_le32(sizeof(struct smb2_file_comp_info)); |
| 4653 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_comp_info)); |
| 4654 | } |
| 4655 | |
| 4656 | 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] | 4657 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4658 | { |
| 4659 | struct smb2_file_attr_tag_info *file_info; |
| 4660 | |
| 4661 | if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4662 | pr_err("no right to read the attributes : 0x%x\n", |
| 4663 | fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4664 | return -EACCES; |
| 4665 | } |
| 4666 | |
| 4667 | file_info = (struct smb2_file_attr_tag_info *)rsp->Buffer; |
| 4668 | file_info->FileAttributes = fp->f_ci->m_fattr; |
| 4669 | file_info->ReparseTag = 0; |
| 4670 | rsp->OutputBufferLength = |
| 4671 | cpu_to_le32(sizeof(struct smb2_file_attr_tag_info)); |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4672 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_attr_tag_info)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4673 | return 0; |
| 4674 | } |
| 4675 | |
| 4676 | static int find_file_posix_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4677 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4678 | { |
| 4679 | struct smb311_posix_qinfo *file_info; |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 4680 | struct inode *inode = file_inode(fp->filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4681 | u64 time; |
| 4682 | |
| 4683 | file_info = (struct smb311_posix_qinfo *)rsp->Buffer; |
| 4684 | file_info->CreationTime = cpu_to_le64(fp->create_time); |
| 4685 | time = ksmbd_UnixTimeToNT(inode->i_atime); |
| 4686 | file_info->LastAccessTime = cpu_to_le64(time); |
| 4687 | time = ksmbd_UnixTimeToNT(inode->i_mtime); |
| 4688 | file_info->LastWriteTime = cpu_to_le64(time); |
| 4689 | time = ksmbd_UnixTimeToNT(inode->i_ctime); |
| 4690 | file_info->ChangeTime = cpu_to_le64(time); |
| 4691 | file_info->DosAttributes = fp->f_ci->m_fattr; |
| 4692 | file_info->Inode = cpu_to_le64(inode->i_ino); |
| 4693 | file_info->EndOfFile = cpu_to_le64(inode->i_size); |
| 4694 | file_info->AllocationSize = cpu_to_le64(inode->i_blocks << 9); |
| 4695 | file_info->HardLinks = cpu_to_le32(inode->i_nlink); |
| 4696 | file_info->Mode = cpu_to_le32(inode->i_mode); |
| 4697 | file_info->DeviceId = cpu_to_le32(inode->i_rdev); |
| 4698 | rsp->OutputBufferLength = |
| 4699 | cpu_to_le32(sizeof(struct smb311_posix_qinfo)); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4700 | inc_rfc1001_len(rsp_org, sizeof(struct smb311_posix_qinfo)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4701 | return 0; |
| 4702 | } |
| 4703 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4704 | static int smb2_get_info_file(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4705 | struct smb2_query_info_req *req, |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4706 | struct smb2_query_info_rsp *rsp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4707 | { |
| 4708 | struct ksmbd_file *fp; |
| 4709 | int fileinfoclass = 0; |
| 4710 | int rc = 0; |
| 4711 | int file_infoclass_size; |
| 4712 | unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID; |
| 4713 | |
| 4714 | if (test_share_config_flag(work->tcon->share_conf, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4715 | KSMBD_SHARE_FLAG_PIPE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4716 | /* smb2 info file called for pipe */ |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4717 | return smb2_get_info_file_pipe(work->sess, req, rsp, |
| 4718 | work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4719 | } |
| 4720 | |
| 4721 | if (work->next_smb2_rcv_hdr_off) { |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 4722 | if (!has_file_id(le64_to_cpu(req->VolatileFileId))) { |
| 4723 | ksmbd_debug(SMB, "Compound request set FID = %llu\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4724 | work->compound_fid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4725 | id = work->compound_fid; |
| 4726 | pid = work->compound_pfid; |
| 4727 | } |
| 4728 | } |
| 4729 | |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 4730 | if (!has_file_id(id)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4731 | id = le64_to_cpu(req->VolatileFileId); |
| 4732 | pid = le64_to_cpu(req->PersistentFileId); |
| 4733 | } |
| 4734 | |
| 4735 | fp = ksmbd_lookup_fd_slow(work, id, pid); |
| 4736 | if (!fp) |
| 4737 | return -ENOENT; |
| 4738 | |
| 4739 | fileinfoclass = req->FileInfoClass; |
| 4740 | |
| 4741 | switch (fileinfoclass) { |
| 4742 | case FILE_ACCESS_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4743 | get_file_access_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4744 | file_infoclass_size = FILE_ACCESS_INFORMATION_SIZE; |
| 4745 | break; |
| 4746 | |
| 4747 | case FILE_BASIC_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4748 | rc = get_file_basic_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4749 | file_infoclass_size = FILE_BASIC_INFORMATION_SIZE; |
| 4750 | break; |
| 4751 | |
| 4752 | case FILE_STANDARD_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4753 | get_file_standard_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4754 | file_infoclass_size = FILE_STANDARD_INFORMATION_SIZE; |
| 4755 | break; |
| 4756 | |
| 4757 | case FILE_ALIGNMENT_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4758 | get_file_alignment_info(rsp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4759 | file_infoclass_size = FILE_ALIGNMENT_INFORMATION_SIZE; |
| 4760 | break; |
| 4761 | |
| 4762 | case FILE_ALL_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4763 | rc = get_file_all_info(work, rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4764 | file_infoclass_size = FILE_ALL_INFORMATION_SIZE; |
| 4765 | break; |
| 4766 | |
| 4767 | case FILE_ALTERNATE_NAME_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4768 | get_file_alternate_info(work, rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4769 | file_infoclass_size = FILE_ALTERNATE_NAME_INFORMATION_SIZE; |
| 4770 | break; |
| 4771 | |
| 4772 | case FILE_STREAM_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4773 | get_file_stream_info(work, rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4774 | file_infoclass_size = FILE_STREAM_INFORMATION_SIZE; |
| 4775 | break; |
| 4776 | |
| 4777 | case FILE_INTERNAL_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4778 | get_file_internal_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4779 | file_infoclass_size = FILE_INTERNAL_INFORMATION_SIZE; |
| 4780 | break; |
| 4781 | |
| 4782 | case FILE_NETWORK_OPEN_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4783 | rc = get_file_network_open_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4784 | file_infoclass_size = FILE_NETWORK_OPEN_INFORMATION_SIZE; |
| 4785 | break; |
| 4786 | |
| 4787 | case FILE_EA_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4788 | get_file_ea_info(rsp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4789 | file_infoclass_size = FILE_EA_INFORMATION_SIZE; |
| 4790 | break; |
| 4791 | |
| 4792 | case FILE_FULL_EA_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4793 | rc = smb2_get_ea(work, fp, req, rsp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4794 | file_infoclass_size = FILE_FULL_EA_INFORMATION_SIZE; |
| 4795 | break; |
| 4796 | |
| 4797 | case FILE_POSITION_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4798 | get_file_position_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4799 | file_infoclass_size = FILE_POSITION_INFORMATION_SIZE; |
| 4800 | break; |
| 4801 | |
| 4802 | case FILE_MODE_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4803 | get_file_mode_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4804 | file_infoclass_size = FILE_MODE_INFORMATION_SIZE; |
| 4805 | break; |
| 4806 | |
| 4807 | case FILE_COMPRESSION_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4808 | get_file_compression_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4809 | file_infoclass_size = FILE_COMPRESSION_INFORMATION_SIZE; |
| 4810 | break; |
| 4811 | |
| 4812 | case FILE_ATTRIBUTE_TAG_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4813 | rc = get_file_attribute_tag_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4814 | file_infoclass_size = FILE_ATTRIBUTE_TAG_INFORMATION_SIZE; |
| 4815 | break; |
| 4816 | case SMB_FIND_FILE_POSIX_INFO: |
| 4817 | if (!work->tcon->posix_extensions) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4818 | 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] | 4819 | rc = -EOPNOTSUPP; |
| 4820 | } else { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4821 | rc = find_file_posix_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4822 | file_infoclass_size = sizeof(struct smb311_posix_qinfo); |
| 4823 | } |
| 4824 | break; |
| 4825 | default: |
| 4826 | ksmbd_debug(SMB, "fileinfoclass %d not supported yet\n", |
| 4827 | fileinfoclass); |
| 4828 | rc = -EOPNOTSUPP; |
| 4829 | } |
| 4830 | if (!rc) |
| 4831 | rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength), |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4832 | rsp, work->response_buf, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4833 | file_infoclass_size); |
| 4834 | ksmbd_fd_put(work, fp); |
| 4835 | return rc; |
| 4836 | } |
| 4837 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4838 | static int smb2_get_info_filesystem(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4839 | struct smb2_query_info_req *req, |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4840 | struct smb2_query_info_rsp *rsp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4841 | { |
| 4842 | struct ksmbd_session *sess = work->sess; |
| 4843 | struct ksmbd_conn *conn = sess->conn; |
| 4844 | struct ksmbd_share_config *share = work->tcon->share_conf; |
| 4845 | int fsinfoclass = 0; |
| 4846 | struct kstatfs stfs; |
| 4847 | struct path path; |
| 4848 | int rc = 0, len; |
| 4849 | int fs_infoclass_size = 0; |
| 4850 | |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 4851 | rc = kern_path(share->path, LOOKUP_NO_SYMLINKS, &path); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4852 | if (rc) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4853 | pr_err("cannot create vfs path\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4854 | return -EIO; |
| 4855 | } |
| 4856 | |
| 4857 | rc = vfs_statfs(&path, &stfs); |
| 4858 | if (rc) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4859 | pr_err("cannot do stat of path %s\n", share->path); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4860 | path_put(&path); |
| 4861 | return -EIO; |
| 4862 | } |
| 4863 | |
| 4864 | fsinfoclass = req->FileInfoClass; |
| 4865 | |
| 4866 | switch (fsinfoclass) { |
| 4867 | case FS_DEVICE_INFORMATION: |
| 4868 | { |
| 4869 | struct filesystem_device_info *info; |
| 4870 | |
| 4871 | info = (struct filesystem_device_info *)rsp->Buffer; |
| 4872 | |
| 4873 | info->DeviceType = cpu_to_le32(stfs.f_type); |
| 4874 | info->DeviceCharacteristics = cpu_to_le32(0x00000020); |
| 4875 | rsp->OutputBufferLength = cpu_to_le32(8); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4876 | inc_rfc1001_len(work->response_buf, 8); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4877 | fs_infoclass_size = FS_DEVICE_INFORMATION_SIZE; |
| 4878 | break; |
| 4879 | } |
| 4880 | case FS_ATTRIBUTE_INFORMATION: |
| 4881 | { |
| 4882 | struct filesystem_attribute_info *info; |
| 4883 | size_t sz; |
| 4884 | |
| 4885 | info = (struct filesystem_attribute_info *)rsp->Buffer; |
| 4886 | info->Attributes = cpu_to_le32(FILE_SUPPORTS_OBJECT_IDS | |
| 4887 | FILE_PERSISTENT_ACLS | |
| 4888 | FILE_UNICODE_ON_DISK | |
| 4889 | FILE_CASE_PRESERVED_NAMES | |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 4890 | FILE_CASE_SENSITIVE_SEARCH | |
| 4891 | FILE_SUPPORTS_BLOCK_REFCOUNTING); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4892 | |
| 4893 | info->Attributes |= cpu_to_le32(server_conf.share_fake_fscaps); |
| 4894 | |
| 4895 | info->MaxPathNameComponentLength = cpu_to_le32(stfs.f_namelen); |
| 4896 | len = smbConvertToUTF16((__le16 *)info->FileSystemName, |
| 4897 | "NTFS", PATH_MAX, conn->local_nls, 0); |
| 4898 | len = len * 2; |
| 4899 | info->FileSystemNameLen = cpu_to_le32(len); |
| 4900 | sz = sizeof(struct filesystem_attribute_info) - 2 + len; |
| 4901 | rsp->OutputBufferLength = cpu_to_le32(sz); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4902 | inc_rfc1001_len(work->response_buf, sz); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4903 | fs_infoclass_size = FS_ATTRIBUTE_INFORMATION_SIZE; |
| 4904 | break; |
| 4905 | } |
| 4906 | case FS_VOLUME_INFORMATION: |
| 4907 | { |
| 4908 | struct filesystem_vol_info *info; |
| 4909 | size_t sz; |
Namjae Jeon | 5d2f0b1 | 2021-10-31 09:53:50 +0900 | [diff] [blame] | 4910 | unsigned int serial_crc = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4911 | |
| 4912 | info = (struct filesystem_vol_info *)(rsp->Buffer); |
| 4913 | info->VolumeCreationTime = 0; |
Namjae Jeon | 5d2f0b1 | 2021-10-31 09:53:50 +0900 | [diff] [blame] | 4914 | serial_crc = crc32_le(serial_crc, share->name, |
| 4915 | strlen(share->name)); |
| 4916 | serial_crc = crc32_le(serial_crc, share->path, |
| 4917 | strlen(share->path)); |
| 4918 | serial_crc = crc32_le(serial_crc, ksmbd_netbios_name(), |
| 4919 | strlen(ksmbd_netbios_name())); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4920 | /* Taking dummy value of serial number*/ |
Namjae Jeon | 5d2f0b1 | 2021-10-31 09:53:50 +0900 | [diff] [blame] | 4921 | info->SerialNumber = cpu_to_le32(serial_crc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4922 | len = smbConvertToUTF16((__le16 *)info->VolumeLabel, |
| 4923 | share->name, PATH_MAX, |
| 4924 | conn->local_nls, 0); |
| 4925 | len = len * 2; |
| 4926 | info->VolumeLabelSize = cpu_to_le32(len); |
| 4927 | info->Reserved = 0; |
| 4928 | sz = sizeof(struct filesystem_vol_info) - 2 + len; |
| 4929 | rsp->OutputBufferLength = cpu_to_le32(sz); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4930 | inc_rfc1001_len(work->response_buf, sz); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4931 | fs_infoclass_size = FS_VOLUME_INFORMATION_SIZE; |
| 4932 | break; |
| 4933 | } |
| 4934 | case FS_SIZE_INFORMATION: |
| 4935 | { |
| 4936 | struct filesystem_info *info; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4937 | |
| 4938 | info = (struct filesystem_info *)(rsp->Buffer); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4939 | info->TotalAllocationUnits = cpu_to_le64(stfs.f_blocks); |
| 4940 | info->FreeAllocationUnits = cpu_to_le64(stfs.f_bfree); |
Namjae Jeon | ee81cae | 2021-06-26 22:32:34 +0900 | [diff] [blame] | 4941 | info->SectorsPerAllocationUnit = cpu_to_le32(1); |
| 4942 | info->BytesPerSector = cpu_to_le32(stfs.f_bsize); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4943 | rsp->OutputBufferLength = cpu_to_le32(24); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4944 | inc_rfc1001_len(work->response_buf, 24); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4945 | fs_infoclass_size = FS_SIZE_INFORMATION_SIZE; |
| 4946 | break; |
| 4947 | } |
| 4948 | case FS_FULL_SIZE_INFORMATION: |
| 4949 | { |
| 4950 | struct smb2_fs_full_size_info *info; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4951 | |
| 4952 | info = (struct smb2_fs_full_size_info *)(rsp->Buffer); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4953 | info->TotalAllocationUnits = cpu_to_le64(stfs.f_blocks); |
| 4954 | info->CallerAvailableAllocationUnits = |
| 4955 | cpu_to_le64(stfs.f_bavail); |
| 4956 | info->ActualAvailableAllocationUnits = |
| 4957 | cpu_to_le64(stfs.f_bfree); |
Namjae Jeon | ee81cae | 2021-06-26 22:32:34 +0900 | [diff] [blame] | 4958 | info->SectorsPerAllocationUnit = cpu_to_le32(1); |
| 4959 | info->BytesPerSector = cpu_to_le32(stfs.f_bsize); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4960 | rsp->OutputBufferLength = cpu_to_le32(32); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4961 | inc_rfc1001_len(work->response_buf, 32); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4962 | fs_infoclass_size = FS_FULL_SIZE_INFORMATION_SIZE; |
| 4963 | break; |
| 4964 | } |
| 4965 | case FS_OBJECT_ID_INFORMATION: |
| 4966 | { |
| 4967 | struct object_id_info *info; |
| 4968 | |
| 4969 | info = (struct object_id_info *)(rsp->Buffer); |
| 4970 | |
| 4971 | if (!user_guest(sess->user)) |
| 4972 | memcpy(info->objid, user_passkey(sess->user), 16); |
| 4973 | else |
| 4974 | memset(info->objid, 0, 16); |
| 4975 | |
| 4976 | info->extended_info.magic = cpu_to_le32(EXTENDED_INFO_MAGIC); |
| 4977 | info->extended_info.version = cpu_to_le32(1); |
| 4978 | info->extended_info.release = cpu_to_le32(1); |
| 4979 | info->extended_info.rel_date = 0; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4980 | 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] | 4981 | rsp->OutputBufferLength = cpu_to_le32(64); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4982 | inc_rfc1001_len(work->response_buf, 64); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4983 | fs_infoclass_size = FS_OBJECT_ID_INFORMATION_SIZE; |
| 4984 | break; |
| 4985 | } |
| 4986 | case FS_SECTOR_SIZE_INFORMATION: |
| 4987 | { |
| 4988 | struct smb3_fs_ss_info *info; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4989 | |
| 4990 | info = (struct smb3_fs_ss_info *)(rsp->Buffer); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4991 | |
Namjae Jeon | 131bac1 | 2021-06-22 16:20:47 +0900 | [diff] [blame] | 4992 | info->LogicalBytesPerSector = cpu_to_le32(stfs.f_bsize); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4993 | info->PhysicalBytesPerSectorForAtomicity = |
Namjae Jeon | 131bac1 | 2021-06-22 16:20:47 +0900 | [diff] [blame] | 4994 | cpu_to_le32(stfs.f_bsize); |
| 4995 | info->PhysicalBytesPerSectorForPerf = cpu_to_le32(stfs.f_bsize); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4996 | info->FSEffPhysicalBytesPerSectorForAtomicity = |
Namjae Jeon | 131bac1 | 2021-06-22 16:20:47 +0900 | [diff] [blame] | 4997 | cpu_to_le32(stfs.f_bsize); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4998 | info->Flags = cpu_to_le32(SSINFO_FLAGS_ALIGNED_DEVICE | |
| 4999 | SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE); |
| 5000 | info->ByteOffsetForSectorAlignment = 0; |
| 5001 | info->ByteOffsetForPartitionAlignment = 0; |
| 5002 | rsp->OutputBufferLength = cpu_to_le32(28); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5003 | inc_rfc1001_len(work->response_buf, 28); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5004 | fs_infoclass_size = FS_SECTOR_SIZE_INFORMATION_SIZE; |
| 5005 | break; |
| 5006 | } |
| 5007 | case FS_CONTROL_INFORMATION: |
| 5008 | { |
| 5009 | /* |
| 5010 | * TODO : The current implementation is based on |
| 5011 | * test result with win7(NTFS) server. It's need to |
| 5012 | * modify this to get valid Quota values |
| 5013 | * from Linux kernel |
| 5014 | */ |
| 5015 | struct smb2_fs_control_info *info; |
| 5016 | |
| 5017 | info = (struct smb2_fs_control_info *)(rsp->Buffer); |
| 5018 | info->FreeSpaceStartFiltering = 0; |
| 5019 | info->FreeSpaceThreshold = 0; |
| 5020 | info->FreeSpaceStopFiltering = 0; |
| 5021 | info->DefaultQuotaThreshold = cpu_to_le64(SMB2_NO_FID); |
| 5022 | info->DefaultQuotaLimit = cpu_to_le64(SMB2_NO_FID); |
| 5023 | info->Padding = 0; |
| 5024 | rsp->OutputBufferLength = cpu_to_le32(48); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5025 | inc_rfc1001_len(work->response_buf, 48); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5026 | fs_infoclass_size = FS_CONTROL_INFORMATION_SIZE; |
| 5027 | break; |
| 5028 | } |
| 5029 | case FS_POSIX_INFORMATION: |
| 5030 | { |
| 5031 | struct filesystem_posix_info *info; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5032 | |
| 5033 | if (!work->tcon->posix_extensions) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5034 | 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] | 5035 | rc = -EOPNOTSUPP; |
| 5036 | } else { |
| 5037 | info = (struct filesystem_posix_info *)(rsp->Buffer); |
Namjae Jeon | ee81cae | 2021-06-26 22:32:34 +0900 | [diff] [blame] | 5038 | info->OptimalTransferSize = cpu_to_le32(stfs.f_bsize); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5039 | info->BlockSize = cpu_to_le32(stfs.f_bsize); |
| 5040 | info->TotalBlocks = cpu_to_le64(stfs.f_blocks); |
| 5041 | info->BlocksAvail = cpu_to_le64(stfs.f_bfree); |
| 5042 | info->UserBlocksAvail = cpu_to_le64(stfs.f_bavail); |
| 5043 | info->TotalFileNodes = cpu_to_le64(stfs.f_files); |
| 5044 | info->FreeFileNodes = cpu_to_le64(stfs.f_ffree); |
| 5045 | rsp->OutputBufferLength = cpu_to_le32(56); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5046 | inc_rfc1001_len(work->response_buf, 56); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5047 | fs_infoclass_size = FS_POSIX_INFORMATION_SIZE; |
| 5048 | } |
| 5049 | break; |
| 5050 | } |
| 5051 | default: |
| 5052 | path_put(&path); |
| 5053 | return -EOPNOTSUPP; |
| 5054 | } |
| 5055 | rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength), |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5056 | rsp, work->response_buf, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5057 | fs_infoclass_size); |
| 5058 | path_put(&path); |
| 5059 | return rc; |
| 5060 | } |
| 5061 | |
| 5062 | static int smb2_get_info_sec(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5063 | struct smb2_query_info_req *req, |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5064 | struct smb2_query_info_rsp *rsp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5065 | { |
| 5066 | struct ksmbd_file *fp; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 5067 | struct user_namespace *user_ns; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5068 | struct smb_ntsd *pntsd = (struct smb_ntsd *)rsp->Buffer, *ppntsd = NULL; |
| 5069 | struct smb_fattr fattr = {{0}}; |
| 5070 | struct inode *inode; |
| 5071 | __u32 secdesclen; |
| 5072 | unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID; |
| 5073 | int addition_info = le32_to_cpu(req->AdditionalInformation); |
| 5074 | int rc; |
| 5075 | |
Namjae Jeon | e294f78 | 2021-06-28 15:26:37 +0900 | [diff] [blame] | 5076 | if (addition_info & ~(OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO | |
| 5077 | PROTECTED_DACL_SECINFO | |
| 5078 | UNPROTECTED_DACL_SECINFO)) { |
Namjae Jeon | 8e537d1 | 2021-11-21 07:48:45 +0900 | [diff] [blame] | 5079 | ksmbd_debug(SMB, "Unsupported addition info: 0x%x)\n", |
Namjae Jeon | e294f78 | 2021-06-28 15:26:37 +0900 | [diff] [blame] | 5080 | addition_info); |
Sebastian Gottschall | ced2b26 | 2021-04-27 15:33:54 +0900 | [diff] [blame] | 5081 | |
| 5082 | pntsd->revision = cpu_to_le16(1); |
| 5083 | pntsd->type = cpu_to_le16(SELF_RELATIVE | DACL_PROTECTED); |
| 5084 | pntsd->osidoffset = 0; |
| 5085 | pntsd->gsidoffset = 0; |
| 5086 | pntsd->sacloffset = 0; |
| 5087 | pntsd->dacloffset = 0; |
| 5088 | |
| 5089 | secdesclen = sizeof(struct smb_ntsd); |
| 5090 | rsp->OutputBufferLength = cpu_to_le32(secdesclen); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5091 | inc_rfc1001_len(work->response_buf, secdesclen); |
Sebastian Gottschall | ced2b26 | 2021-04-27 15:33:54 +0900 | [diff] [blame] | 5092 | |
| 5093 | return 0; |
| 5094 | } |
| 5095 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5096 | if (work->next_smb2_rcv_hdr_off) { |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 5097 | if (!has_file_id(le64_to_cpu(req->VolatileFileId))) { |
| 5098 | ksmbd_debug(SMB, "Compound request set FID = %llu\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5099 | work->compound_fid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5100 | id = work->compound_fid; |
| 5101 | pid = work->compound_pfid; |
| 5102 | } |
| 5103 | } |
| 5104 | |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 5105 | if (!has_file_id(id)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5106 | id = le64_to_cpu(req->VolatileFileId); |
| 5107 | pid = le64_to_cpu(req->PersistentFileId); |
| 5108 | } |
| 5109 | |
| 5110 | fp = ksmbd_lookup_fd_slow(work, id, pid); |
| 5111 | if (!fp) |
| 5112 | return -ENOENT; |
| 5113 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 5114 | user_ns = file_mnt_user_ns(fp->filp); |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 5115 | inode = file_inode(fp->filp); |
Christian Brauner | 43205ca | 2021-08-23 17:13:50 +0200 | [diff] [blame] | 5116 | ksmbd_acls_fattr(&fattr, user_ns, inode); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5117 | |
| 5118 | if (test_share_config_flag(work->tcon->share_conf, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5119 | KSMBD_SHARE_FLAG_ACL_XATTR)) |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 5120 | ksmbd_vfs_get_sd_xattr(work->conn, user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 5121 | fp->filp->f_path.dentry, &ppntsd); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5122 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 5123 | rc = build_sec_desc(user_ns, pntsd, ppntsd, addition_info, |
| 5124 | &secdesclen, &fattr); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5125 | posix_acl_release(fattr.cf_acls); |
| 5126 | posix_acl_release(fattr.cf_dacls); |
| 5127 | kfree(ppntsd); |
| 5128 | ksmbd_fd_put(work, fp); |
| 5129 | if (rc) |
| 5130 | return rc; |
| 5131 | |
| 5132 | rsp->OutputBufferLength = cpu_to_le32(secdesclen); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5133 | inc_rfc1001_len(work->response_buf, secdesclen); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5134 | return 0; |
| 5135 | } |
| 5136 | |
| 5137 | /** |
| 5138 | * smb2_query_info() - handler for smb2 query info command |
| 5139 | * @work: smb work containing query info request buffer |
| 5140 | * |
| 5141 | * Return: 0 on success, otherwise error |
| 5142 | */ |
| 5143 | int smb2_query_info(struct ksmbd_work *work) |
| 5144 | { |
| 5145 | struct smb2_query_info_req *req; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5146 | struct smb2_query_info_rsp *rsp; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5147 | int rc = 0; |
| 5148 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5149 | WORK_BUFFERS(work, req, rsp); |
| 5150 | |
| 5151 | ksmbd_debug(SMB, "GOT query info request\n"); |
| 5152 | |
| 5153 | switch (req->InfoType) { |
| 5154 | case SMB2_O_INFO_FILE: |
| 5155 | ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILE\n"); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5156 | rc = smb2_get_info_file(work, req, rsp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5157 | break; |
| 5158 | case SMB2_O_INFO_FILESYSTEM: |
| 5159 | ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILESYSTEM\n"); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5160 | rc = smb2_get_info_filesystem(work, req, rsp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5161 | break; |
| 5162 | case SMB2_O_INFO_SECURITY: |
| 5163 | ksmbd_debug(SMB, "GOT SMB2_O_INFO_SECURITY\n"); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5164 | rc = smb2_get_info_sec(work, req, rsp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5165 | break; |
| 5166 | default: |
| 5167 | ksmbd_debug(SMB, "InfoType %d not supported yet\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5168 | req->InfoType); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5169 | rc = -EOPNOTSUPP; |
| 5170 | } |
| 5171 | |
| 5172 | if (rc < 0) { |
| 5173 | if (rc == -EACCES) |
| 5174 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 5175 | else if (rc == -ENOENT) |
| 5176 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 5177 | else if (rc == -EIO) |
| 5178 | rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR; |
| 5179 | else if (rc == -EOPNOTSUPP || rsp->hdr.Status == 0) |
| 5180 | rsp->hdr.Status = STATUS_INVALID_INFO_CLASS; |
| 5181 | smb2_set_err_rsp(work); |
| 5182 | |
| 5183 | ksmbd_debug(SMB, "error while processing smb2 query rc = %d\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5184 | rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5185 | return rc; |
| 5186 | } |
| 5187 | rsp->StructureSize = cpu_to_le16(9); |
| 5188 | rsp->OutputBufferOffset = cpu_to_le16(72); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5189 | inc_rfc1001_len(work->response_buf, 8); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5190 | return 0; |
| 5191 | } |
| 5192 | |
| 5193 | /** |
| 5194 | * smb2_close_pipe() - handler for closing IPC pipe |
| 5195 | * @work: smb work containing close request buffer |
| 5196 | * |
| 5197 | * Return: 0 |
| 5198 | */ |
| 5199 | static noinline int smb2_close_pipe(struct ksmbd_work *work) |
| 5200 | { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5201 | u64 id; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5202 | struct smb2_close_req *req = smb2_get_msg(work->request_buf); |
| 5203 | struct smb2_close_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5204 | |
| 5205 | id = le64_to_cpu(req->VolatileFileId); |
| 5206 | ksmbd_session_rpc_close(work->sess, id); |
| 5207 | |
| 5208 | rsp->StructureSize = cpu_to_le16(60); |
| 5209 | rsp->Flags = 0; |
| 5210 | rsp->Reserved = 0; |
| 5211 | rsp->CreationTime = 0; |
| 5212 | rsp->LastAccessTime = 0; |
| 5213 | rsp->LastWriteTime = 0; |
| 5214 | rsp->ChangeTime = 0; |
| 5215 | rsp->AllocationSize = 0; |
| 5216 | rsp->EndOfFile = 0; |
| 5217 | rsp->Attributes = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5218 | inc_rfc1001_len(work->response_buf, 60); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5219 | return 0; |
| 5220 | } |
| 5221 | |
| 5222 | /** |
| 5223 | * smb2_close() - handler for smb2 close file command |
| 5224 | * @work: smb work containing close request buffer |
| 5225 | * |
| 5226 | * Return: 0 |
| 5227 | */ |
| 5228 | int smb2_close(struct ksmbd_work *work) |
| 5229 | { |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 5230 | u64 volatile_id = KSMBD_NO_FID; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5231 | u64 sess_id; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5232 | struct smb2_close_req *req; |
| 5233 | struct smb2_close_rsp *rsp; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5234 | struct ksmbd_conn *conn = work->conn; |
| 5235 | struct ksmbd_file *fp; |
| 5236 | struct inode *inode; |
| 5237 | u64 time; |
| 5238 | int err = 0; |
| 5239 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5240 | WORK_BUFFERS(work, req, rsp); |
| 5241 | |
| 5242 | if (test_share_config_flag(work->tcon->share_conf, |
| 5243 | KSMBD_SHARE_FLAG_PIPE)) { |
| 5244 | ksmbd_debug(SMB, "IPC pipe close request\n"); |
| 5245 | return smb2_close_pipe(work); |
| 5246 | } |
| 5247 | |
| 5248 | sess_id = le64_to_cpu(req->hdr.SessionId); |
| 5249 | if (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS) |
| 5250 | sess_id = work->compound_sid; |
| 5251 | |
| 5252 | work->compound_sid = 0; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5253 | if (check_session_id(conn, sess_id)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5254 | work->compound_sid = sess_id; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5255 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5256 | rsp->hdr.Status = STATUS_USER_SESSION_DELETED; |
| 5257 | if (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS) |
| 5258 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 5259 | err = -EBADF; |
| 5260 | goto out; |
| 5261 | } |
| 5262 | |
| 5263 | if (work->next_smb2_rcv_hdr_off && |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 5264 | !has_file_id(le64_to_cpu(req->VolatileFileId))) { |
| 5265 | if (!has_file_id(work->compound_fid)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5266 | /* file already closed, return FILE_CLOSED */ |
| 5267 | ksmbd_debug(SMB, "file already closed\n"); |
| 5268 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 5269 | err = -EBADF; |
| 5270 | goto out; |
| 5271 | } else { |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 5272 | ksmbd_debug(SMB, |
| 5273 | "Compound request set FID = %llu:%llu\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5274 | work->compound_fid, |
| 5275 | work->compound_pfid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5276 | volatile_id = work->compound_fid; |
| 5277 | |
| 5278 | /* file closed, stored id is not valid anymore */ |
| 5279 | work->compound_fid = KSMBD_NO_FID; |
| 5280 | work->compound_pfid = KSMBD_NO_FID; |
| 5281 | } |
| 5282 | } else { |
| 5283 | volatile_id = le64_to_cpu(req->VolatileFileId); |
| 5284 | } |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 5285 | ksmbd_debug(SMB, "volatile_id = %llu\n", volatile_id); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5286 | |
| 5287 | rsp->StructureSize = cpu_to_le16(60); |
| 5288 | rsp->Reserved = 0; |
| 5289 | |
| 5290 | if (req->Flags == SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB) { |
| 5291 | fp = ksmbd_lookup_fd_fast(work, volatile_id); |
| 5292 | if (!fp) { |
| 5293 | err = -ENOENT; |
| 5294 | goto out; |
| 5295 | } |
| 5296 | |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 5297 | inode = file_inode(fp->filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5298 | rsp->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB; |
| 5299 | rsp->AllocationSize = S_ISDIR(inode->i_mode) ? 0 : |
| 5300 | cpu_to_le64(inode->i_blocks << 9); |
| 5301 | rsp->EndOfFile = cpu_to_le64(inode->i_size); |
| 5302 | rsp->Attributes = fp->f_ci->m_fattr; |
| 5303 | rsp->CreationTime = cpu_to_le64(fp->create_time); |
| 5304 | time = ksmbd_UnixTimeToNT(inode->i_atime); |
| 5305 | rsp->LastAccessTime = cpu_to_le64(time); |
| 5306 | time = ksmbd_UnixTimeToNT(inode->i_mtime); |
| 5307 | rsp->LastWriteTime = cpu_to_le64(time); |
| 5308 | time = ksmbd_UnixTimeToNT(inode->i_ctime); |
| 5309 | rsp->ChangeTime = cpu_to_le64(time); |
| 5310 | ksmbd_fd_put(work, fp); |
| 5311 | } else { |
| 5312 | rsp->Flags = 0; |
| 5313 | rsp->AllocationSize = 0; |
| 5314 | rsp->EndOfFile = 0; |
| 5315 | rsp->Attributes = 0; |
| 5316 | rsp->CreationTime = 0; |
| 5317 | rsp->LastAccessTime = 0; |
| 5318 | rsp->LastWriteTime = 0; |
| 5319 | rsp->ChangeTime = 0; |
| 5320 | } |
| 5321 | |
| 5322 | err = ksmbd_close_fd(work, volatile_id); |
| 5323 | out: |
| 5324 | if (err) { |
| 5325 | if (rsp->hdr.Status == 0) |
| 5326 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 5327 | smb2_set_err_rsp(work); |
| 5328 | } else { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5329 | inc_rfc1001_len(work->response_buf, 60); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5330 | } |
| 5331 | |
| 5332 | return 0; |
| 5333 | } |
| 5334 | |
| 5335 | /** |
| 5336 | * smb2_echo() - handler for smb2 echo(ping) command |
| 5337 | * @work: smb work containing echo request buffer |
| 5338 | * |
| 5339 | * Return: 0 |
| 5340 | */ |
| 5341 | int smb2_echo(struct ksmbd_work *work) |
| 5342 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5343 | struct smb2_echo_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5344 | |
| 5345 | rsp->StructureSize = cpu_to_le16(4); |
| 5346 | rsp->Reserved = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5347 | inc_rfc1001_len(work->response_buf, 4); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5348 | return 0; |
| 5349 | } |
| 5350 | |
Christian Brauner | da1e7ad | 2021-08-23 17:13:47 +0200 | [diff] [blame] | 5351 | static int smb2_rename(struct ksmbd_work *work, |
| 5352 | struct ksmbd_file *fp, |
| 5353 | struct user_namespace *user_ns, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5354 | struct smb2_file_rename_info *file_info, |
| 5355 | struct nls_table *local_nls) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5356 | { |
| 5357 | struct ksmbd_share_config *share = fp->tcon->share_conf; |
| 5358 | char *new_name = NULL, *abs_oldname = NULL, *old_name = NULL; |
| 5359 | char *pathname = NULL; |
| 5360 | struct path path; |
| 5361 | bool file_present = true; |
| 5362 | int rc; |
| 5363 | |
| 5364 | ksmbd_debug(SMB, "setting FILE_RENAME_INFO\n"); |
| 5365 | pathname = kmalloc(PATH_MAX, GFP_KERNEL); |
| 5366 | if (!pathname) |
| 5367 | return -ENOMEM; |
| 5368 | |
| 5369 | abs_oldname = d_path(&fp->filp->f_path, pathname, PATH_MAX); |
| 5370 | if (IS_ERR(abs_oldname)) { |
| 5371 | rc = -EINVAL; |
| 5372 | goto out; |
| 5373 | } |
| 5374 | old_name = strrchr(abs_oldname, '/'); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5375 | if (old_name && old_name[1] != '\0') { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5376 | old_name++; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5377 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5378 | ksmbd_debug(SMB, "can't get last component in path %s\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5379 | abs_oldname); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5380 | rc = -ENOENT; |
| 5381 | goto out; |
| 5382 | } |
| 5383 | |
| 5384 | new_name = smb2_get_name(share, |
| 5385 | file_info->FileName, |
| 5386 | le32_to_cpu(file_info->FileNameLength), |
| 5387 | local_nls); |
| 5388 | if (IS_ERR(new_name)) { |
| 5389 | rc = PTR_ERR(new_name); |
| 5390 | goto out; |
| 5391 | } |
| 5392 | |
| 5393 | if (strchr(new_name, ':')) { |
| 5394 | int s_type; |
| 5395 | char *xattr_stream_name, *stream_name = NULL; |
| 5396 | size_t xattr_stream_size; |
| 5397 | int len; |
| 5398 | |
| 5399 | rc = parse_stream_name(new_name, &stream_name, &s_type); |
| 5400 | if (rc < 0) |
| 5401 | goto out; |
| 5402 | |
| 5403 | len = strlen(new_name); |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 5404 | if (len > 0 && new_name[len - 1] != '/') { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5405 | pr_err("not allow base filename in rename\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5406 | rc = -ESHARE; |
| 5407 | goto out; |
| 5408 | } |
| 5409 | |
| 5410 | rc = ksmbd_vfs_xattr_stream_name(stream_name, |
| 5411 | &xattr_stream_name, |
| 5412 | &xattr_stream_size, |
| 5413 | s_type); |
| 5414 | if (rc) |
| 5415 | goto out; |
| 5416 | |
Christian Brauner | da1e7ad | 2021-08-23 17:13:47 +0200 | [diff] [blame] | 5417 | rc = ksmbd_vfs_setxattr(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 5418 | fp->filp->f_path.dentry, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5419 | xattr_stream_name, |
| 5420 | NULL, 0, 0); |
| 5421 | if (rc < 0) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5422 | pr_err("failed to store stream name in xattr: %d\n", |
| 5423 | rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5424 | rc = -EINVAL; |
| 5425 | goto out; |
| 5426 | } |
| 5427 | |
| 5428 | goto out; |
| 5429 | } |
| 5430 | |
| 5431 | ksmbd_debug(SMB, "new name %s\n", new_name); |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 5432 | rc = ksmbd_vfs_kern_path(work, new_name, LOOKUP_NO_SYMLINKS, &path, 1); |
| 5433 | if (rc) { |
| 5434 | if (rc != -ENOENT) |
| 5435 | goto out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5436 | file_present = false; |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 5437 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5438 | path_put(&path); |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 5439 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5440 | |
| 5441 | if (ksmbd_share_veto_filename(share, new_name)) { |
| 5442 | rc = -ENOENT; |
| 5443 | ksmbd_debug(SMB, "Can't rename vetoed file: %s\n", new_name); |
| 5444 | goto out; |
| 5445 | } |
| 5446 | |
| 5447 | if (file_info->ReplaceIfExists) { |
| 5448 | if (file_present) { |
| 5449 | rc = ksmbd_vfs_remove_file(work, new_name); |
| 5450 | if (rc) { |
| 5451 | if (rc != -ENOTEMPTY) |
| 5452 | rc = -EINVAL; |
| 5453 | ksmbd_debug(SMB, "cannot delete %s, rc %d\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5454 | new_name, rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5455 | goto out; |
| 5456 | } |
| 5457 | } |
| 5458 | } else { |
| 5459 | if (file_present && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5460 | strncmp(old_name, path.dentry->d_name.name, strlen(old_name))) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5461 | rc = -EEXIST; |
| 5462 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5463 | "cannot rename already existing file\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5464 | goto out; |
| 5465 | } |
| 5466 | } |
| 5467 | |
| 5468 | rc = ksmbd_vfs_fp_rename(work, fp, new_name); |
| 5469 | out: |
| 5470 | kfree(pathname); |
| 5471 | if (!IS_ERR(new_name)) |
Marios Makassikis | 915f570 | 2021-04-13 13:25:57 +0900 | [diff] [blame] | 5472 | kfree(new_name); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5473 | return rc; |
| 5474 | } |
| 5475 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5476 | static int smb2_create_link(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5477 | struct ksmbd_share_config *share, |
| 5478 | struct smb2_file_link_info *file_info, |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5479 | unsigned int buf_len, struct file *filp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5480 | struct nls_table *local_nls) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5481 | { |
| 5482 | char *link_name = NULL, *target_name = NULL, *pathname = NULL; |
| 5483 | struct path path; |
| 5484 | bool file_present = true; |
| 5485 | int rc; |
| 5486 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5487 | if (buf_len < (u64)sizeof(struct smb2_file_link_info) + |
| 5488 | le32_to_cpu(file_info->FileNameLength)) |
| 5489 | return -EINVAL; |
| 5490 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5491 | ksmbd_debug(SMB, "setting FILE_LINK_INFORMATION\n"); |
| 5492 | pathname = kmalloc(PATH_MAX, GFP_KERNEL); |
| 5493 | if (!pathname) |
| 5494 | return -ENOMEM; |
| 5495 | |
| 5496 | link_name = smb2_get_name(share, |
| 5497 | file_info->FileName, |
| 5498 | le32_to_cpu(file_info->FileNameLength), |
| 5499 | local_nls); |
| 5500 | if (IS_ERR(link_name) || S_ISDIR(file_inode(filp)->i_mode)) { |
| 5501 | rc = -EINVAL; |
| 5502 | goto out; |
| 5503 | } |
| 5504 | |
| 5505 | ksmbd_debug(SMB, "link name is %s\n", link_name); |
| 5506 | target_name = d_path(&filp->f_path, pathname, PATH_MAX); |
| 5507 | if (IS_ERR(target_name)) { |
| 5508 | rc = -EINVAL; |
| 5509 | goto out; |
| 5510 | } |
| 5511 | |
| 5512 | ksmbd_debug(SMB, "target name is %s\n", target_name); |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 5513 | rc = ksmbd_vfs_kern_path(work, link_name, LOOKUP_NO_SYMLINKS, &path, 0); |
| 5514 | if (rc) { |
| 5515 | if (rc != -ENOENT) |
| 5516 | goto out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5517 | file_present = false; |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 5518 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5519 | path_put(&path); |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 5520 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5521 | |
| 5522 | if (file_info->ReplaceIfExists) { |
| 5523 | if (file_present) { |
| 5524 | rc = ksmbd_vfs_remove_file(work, link_name); |
| 5525 | if (rc) { |
| 5526 | rc = -EINVAL; |
| 5527 | ksmbd_debug(SMB, "cannot delete %s\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5528 | link_name); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5529 | goto out; |
| 5530 | } |
| 5531 | } |
| 5532 | } else { |
| 5533 | if (file_present) { |
| 5534 | rc = -EEXIST; |
| 5535 | ksmbd_debug(SMB, "link already exists\n"); |
| 5536 | goto out; |
| 5537 | } |
| 5538 | } |
| 5539 | |
| 5540 | rc = ksmbd_vfs_link(work, target_name, link_name); |
| 5541 | if (rc) |
| 5542 | rc = -EINVAL; |
| 5543 | out: |
| 5544 | if (!IS_ERR(link_name)) |
Marios Makassikis | 915f570 | 2021-04-13 13:25:57 +0900 | [diff] [blame] | 5545 | kfree(link_name); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5546 | kfree(pathname); |
| 5547 | return rc; |
| 5548 | } |
| 5549 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5550 | static int set_file_basic_info(struct ksmbd_file *fp, |
| 5551 | struct smb2_file_basic_info *file_info, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5552 | struct ksmbd_share_config *share) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5553 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5554 | struct iattr attrs; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5555 | struct file *filp; |
| 5556 | struct inode *inode; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 5557 | struct user_namespace *user_ns; |
Namjae Jeon | 4ffd526 | 2021-09-07 08:15:21 +0900 | [diff] [blame] | 5558 | int rc = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5559 | |
Marios Makassikis | 7adfd4f | 2021-04-27 15:30:22 +0900 | [diff] [blame] | 5560 | if (!(fp->daccess & FILE_WRITE_ATTRIBUTES_LE)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5561 | return -EACCES; |
| 5562 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5563 | attrs.ia_valid = 0; |
| 5564 | filp = fp->filp; |
| 5565 | inode = file_inode(filp); |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 5566 | user_ns = file_mnt_user_ns(filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5567 | |
| 5568 | if (file_info->CreationTime) |
| 5569 | fp->create_time = le64_to_cpu(file_info->CreationTime); |
| 5570 | |
| 5571 | if (file_info->LastAccessTime) { |
| 5572 | attrs.ia_atime = ksmbd_NTtimeToUnix(file_info->LastAccessTime); |
| 5573 | attrs.ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET); |
| 5574 | } |
| 5575 | |
Namjae Jeon | 64e7875 | 2021-10-03 13:19:00 +0900 | [diff] [blame] | 5576 | attrs.ia_valid |= ATTR_CTIME; |
| 5577 | if (file_info->ChangeTime) |
Christian Brauner | db7fb6f | 2021-08-26 10:07:05 +0900 | [diff] [blame] | 5578 | attrs.ia_ctime = ksmbd_NTtimeToUnix(file_info->ChangeTime); |
Namjae Jeon | 64e7875 | 2021-10-03 13:19:00 +0900 | [diff] [blame] | 5579 | else |
| 5580 | attrs.ia_ctime = inode->i_ctime; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5581 | |
| 5582 | if (file_info->LastWriteTime) { |
| 5583 | attrs.ia_mtime = ksmbd_NTtimeToUnix(file_info->LastWriteTime); |
| 5584 | attrs.ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET); |
| 5585 | } |
| 5586 | |
| 5587 | if (file_info->Attributes) { |
| 5588 | if (!S_ISDIR(inode->i_mode) && |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 5589 | file_info->Attributes & FILE_ATTRIBUTE_DIRECTORY_LE) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5590 | pr_err("can't change a file to a directory\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5591 | return -EINVAL; |
| 5592 | } |
| 5593 | |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 5594 | if (!(S_ISDIR(inode->i_mode) && file_info->Attributes == FILE_ATTRIBUTE_NORMAL_LE)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5595 | fp->f_ci->m_fattr = file_info->Attributes | |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 5596 | (fp->f_ci->m_fattr & FILE_ATTRIBUTE_DIRECTORY_LE); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5597 | } |
| 5598 | |
| 5599 | if (test_share_config_flag(share, KSMBD_SHARE_FLAG_STORE_DOS_ATTRS) && |
| 5600 | (file_info->CreationTime || file_info->Attributes)) { |
| 5601 | struct xattr_dos_attrib da = {0}; |
| 5602 | |
| 5603 | da.version = 4; |
| 5604 | da.itime = fp->itime; |
| 5605 | da.create_time = fp->create_time; |
| 5606 | da.attr = le32_to_cpu(fp->f_ci->m_fattr); |
| 5607 | da.flags = XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME | |
| 5608 | XATTR_DOSINFO_ITIME; |
| 5609 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 5610 | rc = ksmbd_vfs_set_dos_attrib_xattr(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 5611 | filp->f_path.dentry, &da); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5612 | if (rc) |
| 5613 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5614 | "failed to restore file attribute in EA\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5615 | rc = 0; |
| 5616 | } |
| 5617 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5618 | if (attrs.ia_valid) { |
| 5619 | struct dentry *dentry = filp->f_path.dentry; |
| 5620 | struct inode *inode = d_inode(dentry); |
| 5621 | |
| 5622 | if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) |
| 5623 | return -EACCES; |
| 5624 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5625 | inode_lock(inode); |
Namjae Jeon | 64e7875 | 2021-10-03 13:19:00 +0900 | [diff] [blame] | 5626 | inode->i_ctime = attrs.ia_ctime; |
| 5627 | attrs.ia_valid &= ~ATTR_CTIME; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 5628 | rc = notify_change(user_ns, dentry, &attrs, NULL); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5629 | inode_unlock(inode); |
| 5630 | } |
Christian Brauner | eb5784f | 2021-08-23 17:13:55 +0200 | [diff] [blame] | 5631 | return rc; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5632 | } |
| 5633 | |
| 5634 | static int set_file_allocation_info(struct ksmbd_work *work, |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5635 | struct ksmbd_file *fp, |
| 5636 | struct smb2_file_alloc_info *file_alloc_info) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5637 | { |
| 5638 | /* |
| 5639 | * TODO : It's working fine only when store dos attributes |
| 5640 | * is not yes. need to implement a logic which works |
| 5641 | * properly with any smb.conf option |
| 5642 | */ |
| 5643 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5644 | loff_t alloc_blks; |
| 5645 | struct inode *inode; |
| 5646 | int rc; |
| 5647 | |
Marios Makassikis | a299669 | 2021-04-27 15:29:01 +0900 | [diff] [blame] | 5648 | if (!(fp->daccess & FILE_WRITE_DATA_LE)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5649 | return -EACCES; |
| 5650 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5651 | alloc_blks = (le64_to_cpu(file_alloc_info->AllocationSize) + 511) >> 9; |
| 5652 | inode = file_inode(fp->filp); |
| 5653 | |
| 5654 | if (alloc_blks > inode->i_blocks) { |
Namjae Jeon | e8c0619 | 2021-06-22 11:06:11 +0900 | [diff] [blame] | 5655 | smb_break_all_levII_oplock(work, fp, 1); |
| 5656 | rc = vfs_fallocate(fp->filp, FALLOC_FL_KEEP_SIZE, 0, |
| 5657 | alloc_blks * 512); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5658 | if (rc && rc != -EOPNOTSUPP) { |
Namjae Jeon | e8c0619 | 2021-06-22 11:06:11 +0900 | [diff] [blame] | 5659 | pr_err("vfs_fallocate is failed : %d\n", rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5660 | return rc; |
| 5661 | } |
| 5662 | } else if (alloc_blks < inode->i_blocks) { |
| 5663 | loff_t size; |
| 5664 | |
| 5665 | /* |
| 5666 | * Allocation size could be smaller than original one |
| 5667 | * which means allocated blocks in file should be |
| 5668 | * deallocated. use truncate to cut out it, but inode |
| 5669 | * size is also updated with truncate offset. |
| 5670 | * inode size is retained by backup inode size. |
| 5671 | */ |
| 5672 | size = i_size_read(inode); |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 5673 | rc = ksmbd_vfs_truncate(work, fp, alloc_blks * 512); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5674 | if (rc) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5675 | pr_err("truncate failed! filename : %s, err %d\n", |
| 5676 | fp->filename, rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5677 | return rc; |
| 5678 | } |
| 5679 | if (size < alloc_blks * 512) |
| 5680 | i_size_write(inode, size); |
| 5681 | } |
| 5682 | return 0; |
| 5683 | } |
| 5684 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5685 | static int set_end_of_file_info(struct ksmbd_work *work, struct ksmbd_file *fp, |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5686 | struct smb2_file_eof_info *file_eof_info) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5687 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5688 | loff_t newsize; |
| 5689 | struct inode *inode; |
| 5690 | int rc; |
| 5691 | |
Marios Makassikis | a299669 | 2021-04-27 15:29:01 +0900 | [diff] [blame] | 5692 | if (!(fp->daccess & FILE_WRITE_DATA_LE)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5693 | return -EACCES; |
| 5694 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5695 | newsize = le64_to_cpu(file_eof_info->EndOfFile); |
| 5696 | inode = file_inode(fp->filp); |
| 5697 | |
| 5698 | /* |
| 5699 | * If FILE_END_OF_FILE_INFORMATION of set_info_file is called |
| 5700 | * on FAT32 shared device, truncate execution time is too long |
| 5701 | * and network error could cause from windows client. because |
| 5702 | * truncate of some filesystem like FAT32 fill zero data in |
| 5703 | * truncated range. |
| 5704 | */ |
| 5705 | if (inode->i_sb->s_magic != MSDOS_SUPER_MAGIC) { |
| 5706 | ksmbd_debug(SMB, "filename : %s truncated to newsize %lld\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5707 | fp->filename, newsize); |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 5708 | rc = ksmbd_vfs_truncate(work, fp, newsize); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5709 | if (rc) { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5710 | ksmbd_debug(SMB, "truncate failed! filename : %s err %d\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5711 | fp->filename, rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5712 | if (rc != -EAGAIN) |
| 5713 | rc = -EBADF; |
| 5714 | return rc; |
| 5715 | } |
| 5716 | } |
| 5717 | return 0; |
| 5718 | } |
| 5719 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5720 | static int set_rename_info(struct ksmbd_work *work, struct ksmbd_file *fp, |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5721 | struct smb2_file_rename_info *rename_info, |
| 5722 | unsigned int buf_len) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5723 | { |
Christian Brauner | da1e7ad | 2021-08-23 17:13:47 +0200 | [diff] [blame] | 5724 | struct user_namespace *user_ns; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5725 | struct ksmbd_file *parent_fp; |
Namjae Jeon | 12202c0 | 2021-06-29 09:23:56 +0900 | [diff] [blame] | 5726 | struct dentry *parent; |
| 5727 | struct dentry *dentry = fp->filp->f_path.dentry; |
| 5728 | int ret; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5729 | |
| 5730 | if (!(fp->daccess & FILE_DELETE_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5731 | pr_err("no right to delete : 0x%x\n", fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5732 | return -EACCES; |
| 5733 | } |
| 5734 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5735 | if (buf_len < (u64)sizeof(struct smb2_file_rename_info) + |
| 5736 | le32_to_cpu(rename_info->FileNameLength)) |
| 5737 | return -EINVAL; |
| 5738 | |
Christian Brauner | da1e7ad | 2021-08-23 17:13:47 +0200 | [diff] [blame] | 5739 | user_ns = file_mnt_user_ns(fp->filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5740 | if (ksmbd_stream_fd(fp)) |
| 5741 | goto next; |
| 5742 | |
Namjae Jeon | 12202c0 | 2021-06-29 09:23:56 +0900 | [diff] [blame] | 5743 | parent = dget_parent(dentry); |
Christian Brauner | da1e7ad | 2021-08-23 17:13:47 +0200 | [diff] [blame] | 5744 | ret = ksmbd_vfs_lock_parent(user_ns, parent, dentry); |
Namjae Jeon | 12202c0 | 2021-06-29 09:23:56 +0900 | [diff] [blame] | 5745 | if (ret) { |
| 5746 | dput(parent); |
| 5747 | return ret; |
| 5748 | } |
| 5749 | |
| 5750 | parent_fp = ksmbd_lookup_fd_inode(d_inode(parent)); |
| 5751 | inode_unlock(d_inode(parent)); |
| 5752 | dput(parent); |
| 5753 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5754 | if (parent_fp) { |
| 5755 | if (parent_fp->daccess & FILE_DELETE_LE) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5756 | pr_err("parent dir is opened with delete access\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5757 | return -ESHARE; |
| 5758 | } |
| 5759 | } |
| 5760 | next: |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5761 | return smb2_rename(work, fp, user_ns, rename_info, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5762 | work->sess->conn->local_nls); |
| 5763 | } |
| 5764 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5765 | static int set_file_disposition_info(struct ksmbd_file *fp, |
| 5766 | struct smb2_file_disposition_info *file_info) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5767 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5768 | struct inode *inode; |
| 5769 | |
| 5770 | if (!(fp->daccess & FILE_DELETE_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5771 | pr_err("no right to delete : 0x%x\n", fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5772 | return -EACCES; |
| 5773 | } |
| 5774 | |
| 5775 | inode = file_inode(fp->filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5776 | if (file_info->DeletePending) { |
| 5777 | if (S_ISDIR(inode->i_mode) && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5778 | ksmbd_vfs_empty_dir(fp) == -ENOTEMPTY) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5779 | return -EBUSY; |
| 5780 | ksmbd_set_inode_pending_delete(fp); |
| 5781 | } else { |
| 5782 | ksmbd_clear_inode_pending_delete(fp); |
| 5783 | } |
| 5784 | return 0; |
| 5785 | } |
| 5786 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5787 | static int set_file_position_info(struct ksmbd_file *fp, |
| 5788 | struct smb2_file_pos_info *file_info) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5789 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5790 | loff_t current_byte_offset; |
Namjae Jeon | ee81cae | 2021-06-26 22:32:34 +0900 | [diff] [blame] | 5791 | unsigned long sector_size; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5792 | struct inode *inode; |
| 5793 | |
| 5794 | inode = file_inode(fp->filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5795 | current_byte_offset = le64_to_cpu(file_info->CurrentByteOffset); |
Namjae Jeon | ee81cae | 2021-06-26 22:32:34 +0900 | [diff] [blame] | 5796 | sector_size = inode->i_sb->s_blocksize; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5797 | |
| 5798 | if (current_byte_offset < 0 || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5799 | (fp->coption == FILE_NO_INTERMEDIATE_BUFFERING_LE && |
| 5800 | current_byte_offset & (sector_size - 1))) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5801 | pr_err("CurrentByteOffset is not valid : %llu\n", |
| 5802 | current_byte_offset); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5803 | return -EINVAL; |
| 5804 | } |
| 5805 | |
| 5806 | fp->filp->f_pos = current_byte_offset; |
| 5807 | return 0; |
| 5808 | } |
| 5809 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5810 | static int set_file_mode_info(struct ksmbd_file *fp, |
| 5811 | struct smb2_file_mode_info *file_info) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5812 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5813 | __le32 mode; |
| 5814 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5815 | mode = file_info->Mode; |
| 5816 | |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 5817 | if ((mode & ~FILE_MODE_INFO_MASK)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5818 | 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] | 5819 | return -EINVAL; |
| 5820 | } |
| 5821 | |
| 5822 | /* |
| 5823 | * TODO : need to implement consideration for |
| 5824 | * FILE_SYNCHRONOUS_IO_ALERT and FILE_SYNCHRONOUS_IO_NONALERT |
| 5825 | */ |
| 5826 | ksmbd_vfs_set_fadvise(fp->filp, mode); |
| 5827 | fp->coption = mode; |
| 5828 | return 0; |
| 5829 | } |
| 5830 | |
| 5831 | /** |
| 5832 | * smb2_set_info_file() - handler for smb2 set info command |
| 5833 | * @work: smb work containing set info command buffer |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 5834 | * @fp: ksmbd_file pointer |
| 5835 | * @info_class: smb2 set info class |
| 5836 | * @share: ksmbd_share_config pointer |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5837 | * |
| 5838 | * Return: 0 on success, otherwise error |
| 5839 | * TODO: need to implement an error handling for STATUS_INFO_LENGTH_MISMATCH |
| 5840 | */ |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5841 | static int smb2_set_info_file(struct ksmbd_work *work, struct ksmbd_file *fp, |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5842 | struct smb2_set_info_req *req, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5843 | struct ksmbd_share_config *share) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5844 | { |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5845 | unsigned int buf_len = le32_to_cpu(req->BufferLength); |
| 5846 | |
| 5847 | switch (req->FileInfoClass) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5848 | case FILE_BASIC_INFORMATION: |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5849 | { |
| 5850 | if (buf_len < sizeof(struct smb2_file_basic_info)) |
| 5851 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5852 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5853 | return set_file_basic_info(fp, (struct smb2_file_basic_info *)req->Buffer, share); |
| 5854 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5855 | case FILE_ALLOCATION_INFORMATION: |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5856 | { |
| 5857 | if (buf_len < sizeof(struct smb2_file_alloc_info)) |
| 5858 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5859 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5860 | return set_file_allocation_info(work, fp, |
| 5861 | (struct smb2_file_alloc_info *)req->Buffer); |
| 5862 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5863 | case FILE_END_OF_FILE_INFORMATION: |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5864 | { |
| 5865 | if (buf_len < sizeof(struct smb2_file_eof_info)) |
| 5866 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5867 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5868 | return set_end_of_file_info(work, fp, |
| 5869 | (struct smb2_file_eof_info *)req->Buffer); |
| 5870 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5871 | case FILE_RENAME_INFORMATION: |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5872 | { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5873 | if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5874 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5875 | "User does not have write permission\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5876 | return -EACCES; |
| 5877 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5878 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5879 | if (buf_len < sizeof(struct smb2_file_rename_info)) |
| 5880 | return -EINVAL; |
| 5881 | |
| 5882 | return set_rename_info(work, fp, |
| 5883 | (struct smb2_file_rename_info *)req->Buffer, |
| 5884 | buf_len); |
| 5885 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5886 | case FILE_LINK_INFORMATION: |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5887 | { |
| 5888 | if (buf_len < sizeof(struct smb2_file_link_info)) |
| 5889 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5890 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5891 | return smb2_create_link(work, work->tcon->share_conf, |
| 5892 | (struct smb2_file_link_info *)req->Buffer, |
| 5893 | buf_len, fp->filp, |
| 5894 | work->sess->conn->local_nls); |
| 5895 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5896 | case FILE_DISPOSITION_INFORMATION: |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5897 | { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5898 | if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5899 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5900 | "User does not have write permission\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5901 | return -EACCES; |
| 5902 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5903 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5904 | if (buf_len < sizeof(struct smb2_file_disposition_info)) |
| 5905 | return -EINVAL; |
| 5906 | |
| 5907 | return set_file_disposition_info(fp, |
| 5908 | (struct smb2_file_disposition_info *)req->Buffer); |
| 5909 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5910 | case FILE_FULL_EA_INFORMATION: |
| 5911 | { |
| 5912 | if (!(fp->daccess & FILE_WRITE_EA_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5913 | pr_err("Not permitted to write ext attr: 0x%x\n", |
| 5914 | fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5915 | return -EACCES; |
| 5916 | } |
| 5917 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5918 | if (buf_len < sizeof(struct smb2_ea_info)) |
| 5919 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5920 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5921 | return smb2_set_ea((struct smb2_ea_info *)req->Buffer, |
| 5922 | buf_len, &fp->filp->f_path); |
| 5923 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5924 | case FILE_POSITION_INFORMATION: |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5925 | { |
| 5926 | if (buf_len < sizeof(struct smb2_file_pos_info)) |
| 5927 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5928 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5929 | return set_file_position_info(fp, (struct smb2_file_pos_info *)req->Buffer); |
| 5930 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5931 | case FILE_MODE_INFORMATION: |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5932 | { |
| 5933 | if (buf_len < sizeof(struct smb2_file_mode_info)) |
| 5934 | return -EINVAL; |
| 5935 | |
| 5936 | return set_file_mode_info(fp, (struct smb2_file_mode_info *)req->Buffer); |
| 5937 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5938 | } |
| 5939 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5940 | pr_err("Unimplemented Fileinfoclass :%d\n", req->FileInfoClass); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5941 | return -EOPNOTSUPP; |
| 5942 | } |
| 5943 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5944 | 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] | 5945 | char *buffer, int buf_len) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5946 | { |
| 5947 | struct smb_ntsd *pntsd = (struct smb_ntsd *)buffer; |
| 5948 | |
| 5949 | fp->saccess |= FILE_SHARE_DELETE_LE; |
| 5950 | |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 5951 | 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] | 5952 | buf_len, false); |
| 5953 | } |
| 5954 | |
| 5955 | /** |
| 5956 | * smb2_set_info() - handler for smb2 set info command handler |
| 5957 | * @work: smb work containing set info request buffer |
| 5958 | * |
| 5959 | * Return: 0 on success, otherwise error |
| 5960 | */ |
| 5961 | int smb2_set_info(struct ksmbd_work *work) |
| 5962 | { |
| 5963 | struct smb2_set_info_req *req; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5964 | struct smb2_set_info_rsp *rsp; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5965 | struct ksmbd_file *fp; |
| 5966 | int rc = 0; |
| 5967 | unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID; |
| 5968 | |
| 5969 | ksmbd_debug(SMB, "Received set info request\n"); |
| 5970 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5971 | if (work->next_smb2_rcv_hdr_off) { |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 5972 | req = ksmbd_req_buf_next(work); |
| 5973 | rsp = ksmbd_resp_buf_next(work); |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 5974 | if (!has_file_id(le64_to_cpu(req->VolatileFileId))) { |
| 5975 | ksmbd_debug(SMB, "Compound request set FID = %llu\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5976 | work->compound_fid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5977 | id = work->compound_fid; |
| 5978 | pid = work->compound_pfid; |
| 5979 | } |
| 5980 | } else { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5981 | req = smb2_get_msg(work->request_buf); |
| 5982 | rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5983 | } |
| 5984 | |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 5985 | if (!has_file_id(id)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5986 | id = le64_to_cpu(req->VolatileFileId); |
| 5987 | pid = le64_to_cpu(req->PersistentFileId); |
| 5988 | } |
| 5989 | |
| 5990 | fp = ksmbd_lookup_fd_slow(work, id, pid); |
| 5991 | if (!fp) { |
| 5992 | ksmbd_debug(SMB, "Invalid id for close: %u\n", id); |
| 5993 | rc = -ENOENT; |
| 5994 | goto err_out; |
| 5995 | } |
| 5996 | |
| 5997 | switch (req->InfoType) { |
| 5998 | case SMB2_O_INFO_FILE: |
| 5999 | ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILE\n"); |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 6000 | rc = smb2_set_info_file(work, fp, req, work->tcon->share_conf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6001 | break; |
| 6002 | case SMB2_O_INFO_SECURITY: |
| 6003 | ksmbd_debug(SMB, "GOT SMB2_O_INFO_SECURITY\n"); |
Namjae Jeon | e70e392 | 2021-08-21 23:26:01 +0900 | [diff] [blame] | 6004 | if (ksmbd_override_fsids(work)) { |
| 6005 | rc = -ENOMEM; |
| 6006 | goto err_out; |
| 6007 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6008 | rc = smb2_set_info_sec(fp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6009 | le32_to_cpu(req->AdditionalInformation), |
| 6010 | req->Buffer, |
| 6011 | le32_to_cpu(req->BufferLength)); |
Namjae Jeon | e70e392 | 2021-08-21 23:26:01 +0900 | [diff] [blame] | 6012 | ksmbd_revert_fsids(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6013 | break; |
| 6014 | default: |
| 6015 | rc = -EOPNOTSUPP; |
| 6016 | } |
| 6017 | |
| 6018 | if (rc < 0) |
| 6019 | goto err_out; |
| 6020 | |
| 6021 | rsp->StructureSize = cpu_to_le16(2); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6022 | inc_rfc1001_len(work->response_buf, 2); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6023 | ksmbd_fd_put(work, fp); |
| 6024 | return 0; |
| 6025 | |
| 6026 | err_out: |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 6027 | if (rc == -EACCES || rc == -EPERM || rc == -EXDEV) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6028 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 6029 | else if (rc == -EINVAL) |
| 6030 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 6031 | else if (rc == -ESHARE) |
| 6032 | rsp->hdr.Status = STATUS_SHARING_VIOLATION; |
| 6033 | else if (rc == -ENOENT) |
| 6034 | rsp->hdr.Status = STATUS_OBJECT_NAME_INVALID; |
| 6035 | else if (rc == -EBUSY || rc == -ENOTEMPTY) |
| 6036 | rsp->hdr.Status = STATUS_DIRECTORY_NOT_EMPTY; |
| 6037 | else if (rc == -EAGAIN) |
| 6038 | rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT; |
Namjae Jeon | ff1d572 | 2021-04-13 13:18:10 +0900 | [diff] [blame] | 6039 | else if (rc == -EBADF || rc == -ESTALE) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6040 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 6041 | else if (rc == -EEXIST) |
| 6042 | rsp->hdr.Status = STATUS_OBJECT_NAME_COLLISION; |
| 6043 | else if (rsp->hdr.Status == 0 || rc == -EOPNOTSUPP) |
| 6044 | rsp->hdr.Status = STATUS_INVALID_INFO_CLASS; |
| 6045 | smb2_set_err_rsp(work); |
| 6046 | ksmbd_fd_put(work, fp); |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6047 | ksmbd_debug(SMB, "error while processing smb2 query rc = %d\n", rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6048 | return rc; |
| 6049 | } |
| 6050 | |
| 6051 | /** |
| 6052 | * smb2_read_pipe() - handler for smb2 read from IPC pipe |
| 6053 | * @work: smb work containing read IPC pipe command buffer |
| 6054 | * |
| 6055 | * Return: 0 on success, otherwise error |
| 6056 | */ |
| 6057 | static noinline int smb2_read_pipe(struct ksmbd_work *work) |
| 6058 | { |
| 6059 | int nbytes = 0, err; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6060 | u64 id; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6061 | struct ksmbd_rpc_command *rpc_resp; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6062 | struct smb2_read_req *req = smb2_get_msg(work->request_buf); |
| 6063 | struct smb2_read_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6064 | |
| 6065 | id = le64_to_cpu(req->VolatileFileId); |
| 6066 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6067 | inc_rfc1001_len(work->response_buf, 16); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6068 | rpc_resp = ksmbd_rpc_read(work->sess, id); |
| 6069 | if (rpc_resp) { |
| 6070 | if (rpc_resp->flags != KSMBD_RPC_OK) { |
| 6071 | err = -EINVAL; |
| 6072 | goto out; |
| 6073 | } |
| 6074 | |
| 6075 | work->aux_payload_buf = |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6076 | kvmalloc(rpc_resp->payload_sz, GFP_KERNEL | __GFP_ZERO); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6077 | if (!work->aux_payload_buf) { |
| 6078 | err = -ENOMEM; |
| 6079 | goto out; |
| 6080 | } |
| 6081 | |
| 6082 | memcpy(work->aux_payload_buf, rpc_resp->payload, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6083 | rpc_resp->payload_sz); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6084 | |
| 6085 | nbytes = rpc_resp->payload_sz; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6086 | work->resp_hdr_sz = get_rfc1002_len(work->response_buf) + 4; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6087 | work->aux_payload_sz = nbytes; |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6088 | kvfree(rpc_resp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6089 | } |
| 6090 | |
| 6091 | rsp->StructureSize = cpu_to_le16(17); |
| 6092 | rsp->DataOffset = 80; |
| 6093 | rsp->Reserved = 0; |
| 6094 | rsp->DataLength = cpu_to_le32(nbytes); |
| 6095 | rsp->DataRemaining = 0; |
Ronnie Sahlberg | 699230f | 2021-09-09 12:26:12 +0900 | [diff] [blame] | 6096 | rsp->Flags = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6097 | inc_rfc1001_len(work->response_buf, nbytes); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6098 | return 0; |
| 6099 | |
| 6100 | out: |
| 6101 | rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR; |
| 6102 | smb2_set_err_rsp(work); |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6103 | kvfree(rpc_resp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6104 | return err; |
| 6105 | } |
| 6106 | |
| 6107 | static ssize_t smb2_read_rdma_channel(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6108 | struct smb2_read_req *req, void *data_buf, |
| 6109 | size_t length) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6110 | { |
| 6111 | struct smb2_buffer_desc_v1 *desc = |
| 6112 | (struct smb2_buffer_desc_v1 *)&req->Buffer[0]; |
| 6113 | int err; |
| 6114 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6115 | if (work->conn->dialect == SMB30_PROT_ID && |
| 6116 | req->Channel != SMB2_CHANNEL_RDMA_V1) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6117 | return -EINVAL; |
| 6118 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6119 | if (req->ReadChannelInfoOffset == 0 || |
| 6120 | le16_to_cpu(req->ReadChannelInfoLength) < sizeof(*desc)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6121 | return -EINVAL; |
| 6122 | |
| 6123 | work->need_invalidate_rkey = |
| 6124 | (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE); |
| 6125 | work->remote_key = le32_to_cpu(desc->token); |
| 6126 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6127 | err = ksmbd_conn_rdma_write(work->conn, data_buf, length, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6128 | le32_to_cpu(desc->token), |
| 6129 | le64_to_cpu(desc->offset), |
| 6130 | le32_to_cpu(desc->length)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6131 | if (err) |
| 6132 | return err; |
| 6133 | |
| 6134 | return length; |
| 6135 | } |
| 6136 | |
| 6137 | /** |
| 6138 | * smb2_read() - handler for smb2 read from file |
| 6139 | * @work: smb work containing read command buffer |
| 6140 | * |
| 6141 | * Return: 0 on success, otherwise error |
| 6142 | */ |
| 6143 | int smb2_read(struct ksmbd_work *work) |
| 6144 | { |
| 6145 | struct ksmbd_conn *conn = work->conn; |
| 6146 | struct smb2_read_req *req; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6147 | struct smb2_read_rsp *rsp; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6148 | struct ksmbd_file *fp; |
| 6149 | loff_t offset; |
| 6150 | size_t length, mincount; |
| 6151 | ssize_t nbytes = 0, remain_bytes = 0; |
| 6152 | int err = 0; |
| 6153 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6154 | WORK_BUFFERS(work, req, rsp); |
| 6155 | |
| 6156 | if (test_share_config_flag(work->tcon->share_conf, |
| 6157 | KSMBD_SHARE_FLAG_PIPE)) { |
| 6158 | ksmbd_debug(SMB, "IPC pipe read request\n"); |
| 6159 | return smb2_read_pipe(work); |
| 6160 | } |
| 6161 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6162 | fp = ksmbd_lookup_fd_slow(work, le64_to_cpu(req->VolatileFileId), |
| 6163 | le64_to_cpu(req->PersistentFileId)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6164 | if (!fp) { |
Marios Makassikis | a4382db | 2021-05-06 11:34:52 +0900 | [diff] [blame] | 6165 | err = -ENOENT; |
| 6166 | goto out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6167 | } |
| 6168 | |
| 6169 | if (!(fp->daccess & (FILE_READ_DATA_LE | FILE_READ_ATTRIBUTES_LE))) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 6170 | pr_err("Not permitted to read : 0x%x\n", fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6171 | err = -EACCES; |
| 6172 | goto out; |
| 6173 | } |
| 6174 | |
| 6175 | offset = le64_to_cpu(req->Offset); |
| 6176 | length = le32_to_cpu(req->Length); |
| 6177 | mincount = le32_to_cpu(req->MinimumCount); |
| 6178 | |
| 6179 | if (length > conn->vals->max_read_size) { |
| 6180 | ksmbd_debug(SMB, "limiting read size to max size(%u)\n", |
| 6181 | conn->vals->max_read_size); |
| 6182 | err = -EINVAL; |
| 6183 | goto out; |
| 6184 | } |
| 6185 | |
Namjae Jeon | 493fa2f | 2021-06-29 09:22:16 +0900 | [diff] [blame] | 6186 | ksmbd_debug(SMB, "filename %pd, offset %lld, len %zu\n", |
| 6187 | fp->filp->f_path.dentry, offset, length); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6188 | |
Namjae Jeon | c30f4eb | 2021-06-18 10:17:37 +0900 | [diff] [blame] | 6189 | work->aux_payload_buf = kvmalloc(length, GFP_KERNEL | __GFP_ZERO); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6190 | if (!work->aux_payload_buf) { |
Dan Carpenter | c1ea111 | 2021-03-22 17:50:11 +0300 | [diff] [blame] | 6191 | err = -ENOMEM; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6192 | goto out; |
| 6193 | } |
| 6194 | |
| 6195 | nbytes = ksmbd_vfs_read(work, fp, length, &offset); |
| 6196 | if (nbytes < 0) { |
| 6197 | err = nbytes; |
| 6198 | goto out; |
| 6199 | } |
| 6200 | |
| 6201 | if ((nbytes == 0 && length != 0) || nbytes < mincount) { |
Namjae Jeon | c30f4eb | 2021-06-18 10:17:37 +0900 | [diff] [blame] | 6202 | kvfree(work->aux_payload_buf); |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 6203 | work->aux_payload_buf = NULL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6204 | rsp->hdr.Status = STATUS_END_OF_FILE; |
| 6205 | smb2_set_err_rsp(work); |
| 6206 | ksmbd_fd_put(work, fp); |
| 6207 | return 0; |
| 6208 | } |
| 6209 | |
| 6210 | ksmbd_debug(SMB, "nbytes %zu, offset %lld mincount %zu\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6211 | nbytes, offset, mincount); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6212 | |
| 6213 | if (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6214 | req->Channel == SMB2_CHANNEL_RDMA_V1) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6215 | /* write data to the client using rdma channel */ |
| 6216 | remain_bytes = smb2_read_rdma_channel(work, req, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6217 | work->aux_payload_buf, |
| 6218 | nbytes); |
Namjae Jeon | c30f4eb | 2021-06-18 10:17:37 +0900 | [diff] [blame] | 6219 | kvfree(work->aux_payload_buf); |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 6220 | work->aux_payload_buf = NULL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6221 | |
| 6222 | nbytes = 0; |
| 6223 | if (remain_bytes < 0) { |
| 6224 | err = (int)remain_bytes; |
| 6225 | goto out; |
| 6226 | } |
| 6227 | } |
| 6228 | |
| 6229 | rsp->StructureSize = cpu_to_le16(17); |
| 6230 | rsp->DataOffset = 80; |
| 6231 | rsp->Reserved = 0; |
| 6232 | rsp->DataLength = cpu_to_le32(nbytes); |
| 6233 | rsp->DataRemaining = cpu_to_le32(remain_bytes); |
Ronnie Sahlberg | 699230f | 2021-09-09 12:26:12 +0900 | [diff] [blame] | 6234 | rsp->Flags = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6235 | inc_rfc1001_len(work->response_buf, 16); |
| 6236 | work->resp_hdr_sz = get_rfc1002_len(work->response_buf) + 4; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6237 | work->aux_payload_sz = nbytes; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6238 | inc_rfc1001_len(work->response_buf, nbytes); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6239 | ksmbd_fd_put(work, fp); |
| 6240 | return 0; |
| 6241 | |
| 6242 | out: |
| 6243 | if (err) { |
| 6244 | if (err == -EISDIR) |
| 6245 | rsp->hdr.Status = STATUS_INVALID_DEVICE_REQUEST; |
| 6246 | else if (err == -EAGAIN) |
| 6247 | rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT; |
| 6248 | else if (err == -ENOENT) |
| 6249 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 6250 | else if (err == -EACCES) |
| 6251 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 6252 | else if (err == -ESHARE) |
| 6253 | rsp->hdr.Status = STATUS_SHARING_VIOLATION; |
| 6254 | else if (err == -EINVAL) |
| 6255 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 6256 | else |
| 6257 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 6258 | |
| 6259 | smb2_set_err_rsp(work); |
| 6260 | } |
| 6261 | ksmbd_fd_put(work, fp); |
| 6262 | return err; |
| 6263 | } |
| 6264 | |
| 6265 | /** |
| 6266 | * smb2_write_pipe() - handler for smb2 write on IPC pipe |
| 6267 | * @work: smb work containing write IPC pipe command buffer |
| 6268 | * |
| 6269 | * Return: 0 on success, otherwise error |
| 6270 | */ |
| 6271 | static noinline int smb2_write_pipe(struct ksmbd_work *work) |
| 6272 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6273 | struct smb2_write_req *req = smb2_get_msg(work->request_buf); |
| 6274 | struct smb2_write_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6275 | struct ksmbd_rpc_command *rpc_resp; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6276 | u64 id = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6277 | int err = 0, ret = 0; |
| 6278 | char *data_buf; |
| 6279 | size_t length; |
| 6280 | |
| 6281 | length = le32_to_cpu(req->Length); |
| 6282 | id = le64_to_cpu(req->VolatileFileId); |
| 6283 | |
| 6284 | if (le16_to_cpu(req->DataOffset) == |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6285 | offsetof(struct smb2_write_req, Buffer)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6286 | data_buf = (char *)&req->Buffer[0]; |
| 6287 | } else { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6288 | if ((u64)le16_to_cpu(req->DataOffset) + length > |
| 6289 | get_rfc1002_len(work->request_buf)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 6290 | pr_err("invalid write data offset %u, smb_len %u\n", |
| 6291 | le16_to_cpu(req->DataOffset), |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6292 | get_rfc1002_len(work->request_buf)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6293 | err = -EINVAL; |
| 6294 | goto out; |
| 6295 | } |
| 6296 | |
| 6297 | data_buf = (char *)(((char *)&req->hdr.ProtocolId) + |
| 6298 | le16_to_cpu(req->DataOffset)); |
| 6299 | } |
| 6300 | |
| 6301 | rpc_resp = ksmbd_rpc_write(work->sess, id, data_buf, length); |
| 6302 | if (rpc_resp) { |
| 6303 | if (rpc_resp->flags == KSMBD_RPC_ENOTIMPLEMENTED) { |
| 6304 | rsp->hdr.Status = STATUS_NOT_SUPPORTED; |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6305 | kvfree(rpc_resp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6306 | smb2_set_err_rsp(work); |
| 6307 | return -EOPNOTSUPP; |
| 6308 | } |
| 6309 | if (rpc_resp->flags != KSMBD_RPC_OK) { |
| 6310 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 6311 | smb2_set_err_rsp(work); |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6312 | kvfree(rpc_resp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6313 | return ret; |
| 6314 | } |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6315 | kvfree(rpc_resp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6316 | } |
| 6317 | |
| 6318 | rsp->StructureSize = cpu_to_le16(17); |
| 6319 | rsp->DataOffset = 0; |
| 6320 | rsp->Reserved = 0; |
| 6321 | rsp->DataLength = cpu_to_le32(length); |
| 6322 | rsp->DataRemaining = 0; |
| 6323 | rsp->Reserved2 = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6324 | inc_rfc1001_len(work->response_buf, 16); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6325 | return 0; |
| 6326 | out: |
| 6327 | if (err) { |
| 6328 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 6329 | smb2_set_err_rsp(work); |
| 6330 | } |
| 6331 | |
| 6332 | return err; |
| 6333 | } |
| 6334 | |
| 6335 | static ssize_t smb2_write_rdma_channel(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6336 | struct smb2_write_req *req, |
| 6337 | struct ksmbd_file *fp, |
| 6338 | loff_t offset, size_t length, bool sync) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6339 | { |
| 6340 | struct smb2_buffer_desc_v1 *desc; |
| 6341 | char *data_buf; |
| 6342 | int ret; |
| 6343 | ssize_t nbytes; |
| 6344 | |
| 6345 | desc = (struct smb2_buffer_desc_v1 *)&req->Buffer[0]; |
| 6346 | |
| 6347 | if (work->conn->dialect == SMB30_PROT_ID && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6348 | req->Channel != SMB2_CHANNEL_RDMA_V1) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6349 | return -EINVAL; |
| 6350 | |
| 6351 | if (req->Length != 0 || req->DataOffset != 0) |
| 6352 | return -EINVAL; |
| 6353 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6354 | if (req->WriteChannelInfoOffset == 0 || |
| 6355 | le16_to_cpu(req->WriteChannelInfoLength) < sizeof(*desc)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6356 | return -EINVAL; |
| 6357 | |
| 6358 | work->need_invalidate_rkey = |
| 6359 | (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE); |
| 6360 | work->remote_key = le32_to_cpu(desc->token); |
| 6361 | |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6362 | data_buf = kvmalloc(length, GFP_KERNEL | __GFP_ZERO); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6363 | if (!data_buf) |
| 6364 | return -ENOMEM; |
| 6365 | |
| 6366 | ret = ksmbd_conn_rdma_read(work->conn, data_buf, length, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6367 | le32_to_cpu(desc->token), |
| 6368 | le64_to_cpu(desc->offset), |
| 6369 | le32_to_cpu(desc->length)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6370 | if (ret < 0) { |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6371 | kvfree(data_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6372 | return ret; |
| 6373 | } |
| 6374 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6375 | ret = ksmbd_vfs_write(work, fp, data_buf, length, &offset, sync, &nbytes); |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6376 | kvfree(data_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6377 | if (ret < 0) |
| 6378 | return ret; |
| 6379 | |
| 6380 | return nbytes; |
| 6381 | } |
| 6382 | |
| 6383 | /** |
| 6384 | * smb2_write() - handler for smb2 write from file |
| 6385 | * @work: smb work containing write command buffer |
| 6386 | * |
| 6387 | * Return: 0 on success, otherwise error |
| 6388 | */ |
| 6389 | int smb2_write(struct ksmbd_work *work) |
| 6390 | { |
| 6391 | struct smb2_write_req *req; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6392 | struct smb2_write_rsp *rsp; |
Namjae Jeon | bcd62a3 | 2021-05-10 09:08:19 +0900 | [diff] [blame] | 6393 | struct ksmbd_file *fp = NULL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6394 | loff_t offset; |
| 6395 | size_t length; |
| 6396 | ssize_t nbytes; |
| 6397 | char *data_buf; |
| 6398 | bool writethrough = false; |
| 6399 | int err = 0; |
| 6400 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6401 | WORK_BUFFERS(work, req, rsp); |
| 6402 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6403 | 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] | 6404 | ksmbd_debug(SMB, "IPC pipe write request\n"); |
| 6405 | return smb2_write_pipe(work); |
| 6406 | } |
| 6407 | |
| 6408 | if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
| 6409 | ksmbd_debug(SMB, "User does not have write permission\n"); |
| 6410 | err = -EACCES; |
| 6411 | goto out; |
| 6412 | } |
| 6413 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6414 | fp = ksmbd_lookup_fd_slow(work, le64_to_cpu(req->VolatileFileId), |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6415 | le64_to_cpu(req->PersistentFileId)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6416 | if (!fp) { |
Marios Makassikis | a4382db | 2021-05-06 11:34:52 +0900 | [diff] [blame] | 6417 | err = -ENOENT; |
| 6418 | goto out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6419 | } |
| 6420 | |
| 6421 | if (!(fp->daccess & (FILE_WRITE_DATA_LE | FILE_READ_ATTRIBUTES_LE))) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 6422 | pr_err("Not permitted to write : 0x%x\n", fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6423 | err = -EACCES; |
| 6424 | goto out; |
| 6425 | } |
| 6426 | |
| 6427 | offset = le64_to_cpu(req->Offset); |
| 6428 | length = le32_to_cpu(req->Length); |
| 6429 | |
| 6430 | if (length > work->conn->vals->max_write_size) { |
| 6431 | ksmbd_debug(SMB, "limiting write size to max size(%u)\n", |
| 6432 | work->conn->vals->max_write_size); |
| 6433 | err = -EINVAL; |
| 6434 | goto out; |
| 6435 | } |
| 6436 | |
| 6437 | if (le32_to_cpu(req->Flags) & SMB2_WRITEFLAG_WRITE_THROUGH) |
| 6438 | writethrough = true; |
| 6439 | |
| 6440 | if (req->Channel != SMB2_CHANNEL_RDMA_V1 && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6441 | req->Channel != SMB2_CHANNEL_RDMA_V1_INVALIDATE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6442 | if (le16_to_cpu(req->DataOffset) == |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6443 | offsetof(struct smb2_write_req, Buffer)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6444 | data_buf = (char *)&req->Buffer[0]; |
| 6445 | } else { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6446 | if ((u64)le16_to_cpu(req->DataOffset) + length > |
| 6447 | get_rfc1002_len(work->request_buf)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 6448 | pr_err("invalid write data offset %u, smb_len %u\n", |
| 6449 | le16_to_cpu(req->DataOffset), |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6450 | get_rfc1002_len(work->request_buf)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6451 | err = -EINVAL; |
| 6452 | goto out; |
| 6453 | } |
| 6454 | |
| 6455 | data_buf = (char *)(((char *)&req->hdr.ProtocolId) + |
| 6456 | le16_to_cpu(req->DataOffset)); |
| 6457 | } |
| 6458 | |
| 6459 | ksmbd_debug(SMB, "flags %u\n", le32_to_cpu(req->Flags)); |
| 6460 | if (le32_to_cpu(req->Flags) & SMB2_WRITEFLAG_WRITE_THROUGH) |
| 6461 | writethrough = true; |
| 6462 | |
Namjae Jeon | 493fa2f | 2021-06-29 09:22:16 +0900 | [diff] [blame] | 6463 | ksmbd_debug(SMB, "filename %pd, offset %lld, len %zu\n", |
| 6464 | fp->filp->f_path.dentry, offset, length); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6465 | err = ksmbd_vfs_write(work, fp, data_buf, length, &offset, |
| 6466 | writethrough, &nbytes); |
| 6467 | if (err < 0) |
| 6468 | goto out; |
| 6469 | } else { |
| 6470 | /* read data from the client using rdma channel, and |
| 6471 | * write the data. |
| 6472 | */ |
| 6473 | nbytes = smb2_write_rdma_channel(work, req, fp, offset, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6474 | le32_to_cpu(req->RemainingBytes), |
| 6475 | writethrough); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6476 | if (nbytes < 0) { |
| 6477 | err = (int)nbytes; |
| 6478 | goto out; |
| 6479 | } |
| 6480 | } |
| 6481 | |
| 6482 | rsp->StructureSize = cpu_to_le16(17); |
| 6483 | rsp->DataOffset = 0; |
| 6484 | rsp->Reserved = 0; |
| 6485 | rsp->DataLength = cpu_to_le32(nbytes); |
| 6486 | rsp->DataRemaining = 0; |
| 6487 | rsp->Reserved2 = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6488 | inc_rfc1001_len(work->response_buf, 16); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6489 | ksmbd_fd_put(work, fp); |
| 6490 | return 0; |
| 6491 | |
| 6492 | out: |
| 6493 | if (err == -EAGAIN) |
| 6494 | rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT; |
| 6495 | else if (err == -ENOSPC || err == -EFBIG) |
| 6496 | rsp->hdr.Status = STATUS_DISK_FULL; |
| 6497 | else if (err == -ENOENT) |
| 6498 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 6499 | else if (err == -EACCES) |
| 6500 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 6501 | else if (err == -ESHARE) |
| 6502 | rsp->hdr.Status = STATUS_SHARING_VIOLATION; |
| 6503 | else if (err == -EINVAL) |
| 6504 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 6505 | else |
| 6506 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 6507 | |
| 6508 | smb2_set_err_rsp(work); |
| 6509 | ksmbd_fd_put(work, fp); |
| 6510 | return err; |
| 6511 | } |
| 6512 | |
| 6513 | /** |
| 6514 | * smb2_flush() - handler for smb2 flush file - fsync |
| 6515 | * @work: smb work containing flush command buffer |
| 6516 | * |
| 6517 | * Return: 0 on success, otherwise error |
| 6518 | */ |
| 6519 | int smb2_flush(struct ksmbd_work *work) |
| 6520 | { |
| 6521 | struct smb2_flush_req *req; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6522 | struct smb2_flush_rsp *rsp; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6523 | int err; |
| 6524 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6525 | WORK_BUFFERS(work, req, rsp); |
| 6526 | |
| 6527 | ksmbd_debug(SMB, "SMB2_FLUSH called for fid %llu\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6528 | le64_to_cpu(req->VolatileFileId)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6529 | |
| 6530 | err = ksmbd_vfs_fsync(work, |
| 6531 | le64_to_cpu(req->VolatileFileId), |
| 6532 | le64_to_cpu(req->PersistentFileId)); |
| 6533 | if (err) |
| 6534 | goto out; |
| 6535 | |
| 6536 | rsp->StructureSize = cpu_to_le16(4); |
| 6537 | rsp->Reserved = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6538 | inc_rfc1001_len(work->response_buf, 4); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6539 | return 0; |
| 6540 | |
| 6541 | out: |
| 6542 | if (err) { |
| 6543 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 6544 | smb2_set_err_rsp(work); |
| 6545 | } |
| 6546 | |
| 6547 | return err; |
| 6548 | } |
| 6549 | |
| 6550 | /** |
| 6551 | * smb2_cancel() - handler for smb2 cancel command |
| 6552 | * @work: smb work containing cancel command buffer |
| 6553 | * |
| 6554 | * Return: 0 on success, otherwise error |
| 6555 | */ |
| 6556 | int smb2_cancel(struct ksmbd_work *work) |
| 6557 | { |
| 6558 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6559 | struct smb2_hdr *hdr = smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6560 | struct smb2_hdr *chdr; |
| 6561 | struct ksmbd_work *cancel_work = NULL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6562 | int canceled = 0; |
| 6563 | struct list_head *command_list; |
| 6564 | |
| 6565 | 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] | 6566 | hdr->MessageId, hdr->Flags); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6567 | |
| 6568 | if (hdr->Flags & SMB2_FLAGS_ASYNC_COMMAND) { |
| 6569 | command_list = &conn->async_requests; |
| 6570 | |
| 6571 | spin_lock(&conn->request_lock); |
Namjae Jeon | 6f3d5ee | 2021-06-18 10:28:52 +0900 | [diff] [blame] | 6572 | list_for_each_entry(cancel_work, command_list, |
| 6573 | async_request_entry) { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6574 | chdr = smb2_get_msg(cancel_work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6575 | |
| 6576 | if (cancel_work->async_id != |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6577 | le64_to_cpu(hdr->Id.AsyncId)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6578 | continue; |
| 6579 | |
| 6580 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6581 | "smb2 with AsyncId %llu cancelled command = 0x%x\n", |
| 6582 | le64_to_cpu(hdr->Id.AsyncId), |
| 6583 | le16_to_cpu(chdr->Command)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6584 | canceled = 1; |
| 6585 | break; |
| 6586 | } |
| 6587 | spin_unlock(&conn->request_lock); |
| 6588 | } else { |
| 6589 | command_list = &conn->requests; |
| 6590 | |
| 6591 | spin_lock(&conn->request_lock); |
Namjae Jeon | 6f3d5ee | 2021-06-18 10:28:52 +0900 | [diff] [blame] | 6592 | list_for_each_entry(cancel_work, command_list, request_entry) { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6593 | chdr = smb2_get_msg(cancel_work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6594 | |
| 6595 | if (chdr->MessageId != hdr->MessageId || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6596 | cancel_work == work) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6597 | continue; |
| 6598 | |
| 6599 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6600 | "smb2 with mid %llu cancelled command = 0x%x\n", |
| 6601 | le64_to_cpu(hdr->MessageId), |
| 6602 | le16_to_cpu(chdr->Command)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6603 | canceled = 1; |
| 6604 | break; |
| 6605 | } |
| 6606 | spin_unlock(&conn->request_lock); |
| 6607 | } |
| 6608 | |
| 6609 | if (canceled) { |
| 6610 | cancel_work->state = KSMBD_WORK_CANCELLED; |
| 6611 | if (cancel_work->cancel_fn) |
| 6612 | cancel_work->cancel_fn(cancel_work->cancel_argv); |
| 6613 | } |
| 6614 | |
| 6615 | /* For SMB2_CANCEL command itself send no response*/ |
| 6616 | work->send_no_response = 1; |
| 6617 | return 0; |
| 6618 | } |
| 6619 | |
| 6620 | struct file_lock *smb_flock_init(struct file *f) |
| 6621 | { |
| 6622 | struct file_lock *fl; |
| 6623 | |
| 6624 | fl = locks_alloc_lock(); |
| 6625 | if (!fl) |
| 6626 | goto out; |
| 6627 | |
| 6628 | locks_init_lock(fl); |
| 6629 | |
| 6630 | fl->fl_owner = f; |
| 6631 | fl->fl_pid = current->tgid; |
| 6632 | fl->fl_file = f; |
| 6633 | fl->fl_flags = FL_POSIX; |
| 6634 | fl->fl_ops = NULL; |
| 6635 | fl->fl_lmops = NULL; |
| 6636 | |
| 6637 | out: |
| 6638 | return fl; |
| 6639 | } |
| 6640 | |
| 6641 | static int smb2_set_flock_flags(struct file_lock *flock, int flags) |
| 6642 | { |
| 6643 | int cmd = -EINVAL; |
| 6644 | |
| 6645 | /* Checking for wrong flag combination during lock request*/ |
| 6646 | switch (flags) { |
| 6647 | case SMB2_LOCKFLAG_SHARED: |
| 6648 | ksmbd_debug(SMB, "received shared request\n"); |
| 6649 | cmd = F_SETLKW; |
| 6650 | flock->fl_type = F_RDLCK; |
| 6651 | flock->fl_flags |= FL_SLEEP; |
| 6652 | break; |
| 6653 | case SMB2_LOCKFLAG_EXCLUSIVE: |
| 6654 | ksmbd_debug(SMB, "received exclusive request\n"); |
| 6655 | cmd = F_SETLKW; |
| 6656 | flock->fl_type = F_WRLCK; |
| 6657 | flock->fl_flags |= FL_SLEEP; |
| 6658 | break; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6659 | case SMB2_LOCKFLAG_SHARED | SMB2_LOCKFLAG_FAIL_IMMEDIATELY: |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6660 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6661 | "received shared & fail immediately request\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6662 | cmd = F_SETLK; |
| 6663 | flock->fl_type = F_RDLCK; |
| 6664 | break; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6665 | case SMB2_LOCKFLAG_EXCLUSIVE | SMB2_LOCKFLAG_FAIL_IMMEDIATELY: |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6666 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6667 | "received exclusive & fail immediately request\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6668 | cmd = F_SETLK; |
| 6669 | flock->fl_type = F_WRLCK; |
| 6670 | break; |
| 6671 | case SMB2_LOCKFLAG_UNLOCK: |
| 6672 | ksmbd_debug(SMB, "received unlock request\n"); |
| 6673 | flock->fl_type = F_UNLCK; |
| 6674 | cmd = 0; |
| 6675 | break; |
| 6676 | } |
| 6677 | |
| 6678 | return cmd; |
| 6679 | } |
| 6680 | |
| 6681 | static struct ksmbd_lock *smb2_lock_init(struct file_lock *flock, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6682 | unsigned int cmd, int flags, |
| 6683 | struct list_head *lock_list) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6684 | { |
| 6685 | struct ksmbd_lock *lock; |
| 6686 | |
| 6687 | lock = kzalloc(sizeof(struct ksmbd_lock), GFP_KERNEL); |
| 6688 | if (!lock) |
| 6689 | return NULL; |
| 6690 | |
| 6691 | lock->cmd = cmd; |
| 6692 | lock->fl = flock; |
| 6693 | lock->start = flock->fl_start; |
| 6694 | lock->end = flock->fl_end; |
| 6695 | lock->flags = flags; |
| 6696 | if (lock->start == lock->end) |
| 6697 | lock->zero_len = 1; |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6698 | INIT_LIST_HEAD(&lock->clist); |
| 6699 | INIT_LIST_HEAD(&lock->flist); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6700 | INIT_LIST_HEAD(&lock->llist); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6701 | list_add_tail(&lock->llist, lock_list); |
| 6702 | |
| 6703 | return lock; |
| 6704 | } |
| 6705 | |
| 6706 | static void smb2_remove_blocked_lock(void **argv) |
| 6707 | { |
| 6708 | struct file_lock *flock = (struct file_lock *)argv[0]; |
| 6709 | |
| 6710 | ksmbd_vfs_posix_lock_unblock(flock); |
| 6711 | wake_up(&flock->fl_wait); |
| 6712 | } |
| 6713 | |
| 6714 | static inline bool lock_defer_pending(struct file_lock *fl) |
| 6715 | { |
| 6716 | /* check pending lock waiters */ |
| 6717 | return waitqueue_active(&fl->fl_wait); |
| 6718 | } |
| 6719 | |
| 6720 | /** |
| 6721 | * smb2_lock() - handler for smb2 file lock command |
| 6722 | * @work: smb work containing lock command buffer |
| 6723 | * |
| 6724 | * Return: 0 on success, otherwise error |
| 6725 | */ |
| 6726 | int smb2_lock(struct ksmbd_work *work) |
| 6727 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6728 | struct smb2_lock_req *req = smb2_get_msg(work->request_buf); |
| 6729 | struct smb2_lock_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6730 | struct smb2_lock_element *lock_ele; |
| 6731 | struct ksmbd_file *fp = NULL; |
| 6732 | struct file_lock *flock = NULL; |
| 6733 | struct file *filp = NULL; |
| 6734 | int lock_count; |
| 6735 | int flags = 0; |
| 6736 | int cmd = 0; |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6737 | int err = -EIO, i, rc = 0; |
Namjae Jeon | 50bf80a | 2021-05-14 12:20:07 +0900 | [diff] [blame] | 6738 | u64 lock_start, lock_length; |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6739 | struct ksmbd_lock *smb_lock = NULL, *cmp_lock, *tmp, *tmp2; |
| 6740 | struct ksmbd_conn *conn; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6741 | int nolock = 0; |
| 6742 | LIST_HEAD(lock_list); |
| 6743 | LIST_HEAD(rollback_list); |
| 6744 | int prior_lock = 0; |
| 6745 | |
| 6746 | ksmbd_debug(SMB, "Received lock request\n"); |
| 6747 | fp = ksmbd_lookup_fd_slow(work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6748 | le64_to_cpu(req->VolatileFileId), |
| 6749 | le64_to_cpu(req->PersistentFileId)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6750 | if (!fp) { |
| 6751 | ksmbd_debug(SMB, "Invalid file id for lock : %llu\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6752 | le64_to_cpu(req->VolatileFileId)); |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6753 | err = -ENOENT; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6754 | goto out2; |
| 6755 | } |
| 6756 | |
| 6757 | filp = fp->filp; |
| 6758 | lock_count = le16_to_cpu(req->LockCount); |
| 6759 | lock_ele = req->locks; |
| 6760 | |
| 6761 | ksmbd_debug(SMB, "lock count is %d\n", lock_count); |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6762 | if (!lock_count) { |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6763 | err = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6764 | goto out2; |
| 6765 | } |
| 6766 | |
| 6767 | for (i = 0; i < lock_count; i++) { |
| 6768 | flags = le32_to_cpu(lock_ele[i].Flags); |
| 6769 | |
| 6770 | flock = smb_flock_init(filp); |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6771 | if (!flock) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6772 | goto out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6773 | |
| 6774 | cmd = smb2_set_flock_flags(flock, flags); |
| 6775 | |
Namjae Jeon | 50bf80a | 2021-05-14 12:20:07 +0900 | [diff] [blame] | 6776 | lock_start = le64_to_cpu(lock_ele[i].Offset); |
| 6777 | lock_length = le64_to_cpu(lock_ele[i].Length); |
| 6778 | if (lock_start > U64_MAX - lock_length) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 6779 | pr_err("Invalid lock range requested\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6780 | rsp->hdr.Status = STATUS_INVALID_LOCK_RANGE; |
| 6781 | goto out; |
| 6782 | } |
| 6783 | |
Namjae Jeon | 50bf80a | 2021-05-14 12:20:07 +0900 | [diff] [blame] | 6784 | if (lock_start > OFFSET_MAX) |
| 6785 | flock->fl_start = OFFSET_MAX; |
| 6786 | else |
| 6787 | flock->fl_start = lock_start; |
| 6788 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6789 | lock_length = le64_to_cpu(lock_ele[i].Length); |
Namjae Jeon | 50bf80a | 2021-05-14 12:20:07 +0900 | [diff] [blame] | 6790 | if (lock_length > OFFSET_MAX - flock->fl_start) |
| 6791 | lock_length = OFFSET_MAX - flock->fl_start; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6792 | |
| 6793 | flock->fl_end = flock->fl_start + lock_length; |
| 6794 | |
| 6795 | if (flock->fl_end < flock->fl_start) { |
| 6796 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6797 | "the end offset(%llx) is smaller than the start offset(%llx)\n", |
| 6798 | flock->fl_end, flock->fl_start); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6799 | rsp->hdr.Status = STATUS_INVALID_LOCK_RANGE; |
| 6800 | goto out; |
| 6801 | } |
| 6802 | |
| 6803 | /* Check conflict locks in one request */ |
| 6804 | list_for_each_entry(cmp_lock, &lock_list, llist) { |
| 6805 | if (cmp_lock->fl->fl_start <= flock->fl_start && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6806 | cmp_lock->fl->fl_end >= flock->fl_end) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6807 | if (cmp_lock->fl->fl_type != F_UNLCK && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6808 | flock->fl_type != F_UNLCK) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 6809 | pr_err("conflict two locks in one request\n"); |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6810 | err = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6811 | goto out; |
| 6812 | } |
| 6813 | } |
| 6814 | } |
| 6815 | |
| 6816 | smb_lock = smb2_lock_init(flock, cmd, flags, &lock_list); |
| 6817 | if (!smb_lock) { |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6818 | err = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6819 | goto out; |
| 6820 | } |
| 6821 | } |
| 6822 | |
| 6823 | list_for_each_entry_safe(smb_lock, tmp, &lock_list, llist) { |
| 6824 | if (smb_lock->cmd < 0) { |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6825 | err = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6826 | goto out; |
| 6827 | } |
| 6828 | |
| 6829 | if (!(smb_lock->flags & SMB2_LOCKFLAG_MASK)) { |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6830 | err = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6831 | goto out; |
| 6832 | } |
| 6833 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6834 | if ((prior_lock & (SMB2_LOCKFLAG_EXCLUSIVE | SMB2_LOCKFLAG_SHARED) && |
| 6835 | smb_lock->flags & SMB2_LOCKFLAG_UNLOCK) || |
| 6836 | (prior_lock == SMB2_LOCKFLAG_UNLOCK && |
| 6837 | !(smb_lock->flags & SMB2_LOCKFLAG_UNLOCK))) { |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6838 | err = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6839 | goto out; |
| 6840 | } |
| 6841 | |
| 6842 | prior_lock = smb_lock->flags; |
| 6843 | |
| 6844 | if (!(smb_lock->flags & SMB2_LOCKFLAG_UNLOCK) && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6845 | !(smb_lock->flags & SMB2_LOCKFLAG_FAIL_IMMEDIATELY)) |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6846 | goto no_check_cl; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6847 | |
| 6848 | nolock = 1; |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6849 | /* check locks in connection list */ |
| 6850 | read_lock(&conn_list_lock); |
| 6851 | list_for_each_entry(conn, &conn_list, conns_list) { |
| 6852 | spin_lock(&conn->llist_lock); |
| 6853 | list_for_each_entry_safe(cmp_lock, tmp2, &conn->lock_list, clist) { |
| 6854 | if (file_inode(cmp_lock->fl->fl_file) != |
| 6855 | file_inode(smb_lock->fl->fl_file)) |
| 6856 | continue; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6857 | |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6858 | if (smb_lock->fl->fl_type == F_UNLCK) { |
| 6859 | if (cmp_lock->fl->fl_file == smb_lock->fl->fl_file && |
| 6860 | cmp_lock->start == smb_lock->start && |
| 6861 | cmp_lock->end == smb_lock->end && |
| 6862 | !lock_defer_pending(cmp_lock->fl)) { |
| 6863 | nolock = 0; |
| 6864 | list_del(&cmp_lock->flist); |
| 6865 | list_del(&cmp_lock->clist); |
| 6866 | spin_unlock(&conn->llist_lock); |
| 6867 | read_unlock(&conn_list_lock); |
| 6868 | |
| 6869 | locks_free_lock(cmp_lock->fl); |
| 6870 | kfree(cmp_lock); |
| 6871 | goto out_check_cl; |
| 6872 | } |
| 6873 | continue; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6874 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6875 | |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6876 | if (cmp_lock->fl->fl_file == smb_lock->fl->fl_file) { |
| 6877 | if (smb_lock->flags & SMB2_LOCKFLAG_SHARED) |
| 6878 | continue; |
| 6879 | } else { |
| 6880 | if (cmp_lock->flags & SMB2_LOCKFLAG_SHARED) |
| 6881 | continue; |
| 6882 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6883 | |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6884 | /* check zero byte lock range */ |
| 6885 | if (cmp_lock->zero_len && !smb_lock->zero_len && |
| 6886 | cmp_lock->start > smb_lock->start && |
| 6887 | cmp_lock->start < smb_lock->end) { |
| 6888 | spin_unlock(&conn->llist_lock); |
| 6889 | read_unlock(&conn_list_lock); |
| 6890 | pr_err("previous lock conflict with zero byte lock range\n"); |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6891 | goto out; |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6892 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6893 | |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6894 | if (smb_lock->zero_len && !cmp_lock->zero_len && |
| 6895 | smb_lock->start > cmp_lock->start && |
| 6896 | smb_lock->start < cmp_lock->end) { |
| 6897 | spin_unlock(&conn->llist_lock); |
| 6898 | read_unlock(&conn_list_lock); |
| 6899 | pr_err("current lock conflict with zero byte lock range\n"); |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6900 | goto out; |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6901 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6902 | |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6903 | if (((cmp_lock->start <= smb_lock->start && |
| 6904 | cmp_lock->end > smb_lock->start) || |
| 6905 | (cmp_lock->start < smb_lock->end && |
| 6906 | cmp_lock->end >= smb_lock->end)) && |
| 6907 | !cmp_lock->zero_len && !smb_lock->zero_len) { |
| 6908 | spin_unlock(&conn->llist_lock); |
| 6909 | read_unlock(&conn_list_lock); |
| 6910 | pr_err("Not allow lock operation on exclusive lock range\n"); |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6911 | goto out; |
| 6912 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6913 | } |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6914 | spin_unlock(&conn->llist_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6915 | } |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6916 | read_unlock(&conn_list_lock); |
| 6917 | out_check_cl: |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6918 | if (smb_lock->fl->fl_type == F_UNLCK && nolock) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 6919 | pr_err("Try to unlock nolocked range\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6920 | rsp->hdr.Status = STATUS_RANGE_NOT_LOCKED; |
| 6921 | goto out; |
| 6922 | } |
| 6923 | |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6924 | no_check_cl: |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6925 | if (smb_lock->zero_len) { |
| 6926 | err = 0; |
| 6927 | goto skip; |
| 6928 | } |
| 6929 | |
| 6930 | flock = smb_lock->fl; |
| 6931 | list_del(&smb_lock->llist); |
| 6932 | retry: |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6933 | rc = vfs_lock_file(filp, smb_lock->cmd, flock, NULL); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6934 | skip: |
| 6935 | if (flags & SMB2_LOCKFLAG_UNLOCK) { |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6936 | if (!rc) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6937 | ksmbd_debug(SMB, "File unlocked\n"); |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6938 | } else if (rc == -ENOENT) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6939 | rsp->hdr.Status = STATUS_NOT_LOCKED; |
| 6940 | goto out; |
| 6941 | } |
| 6942 | locks_free_lock(flock); |
| 6943 | kfree(smb_lock); |
| 6944 | } else { |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6945 | if (rc == FILE_LOCK_DEFERRED) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6946 | void **argv; |
| 6947 | |
| 6948 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6949 | "would have to wait for getting lock\n"); |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6950 | spin_lock(&work->conn->llist_lock); |
| 6951 | list_add_tail(&smb_lock->clist, |
| 6952 | &work->conn->lock_list); |
| 6953 | spin_unlock(&work->conn->llist_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6954 | list_add(&smb_lock->llist, &rollback_list); |
| 6955 | |
| 6956 | argv = kmalloc(sizeof(void *), GFP_KERNEL); |
| 6957 | if (!argv) { |
| 6958 | err = -ENOMEM; |
| 6959 | goto out; |
| 6960 | } |
| 6961 | argv[0] = flock; |
| 6962 | |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6963 | rc = setup_async_work(work, |
| 6964 | smb2_remove_blocked_lock, |
| 6965 | argv); |
| 6966 | if (rc) { |
| 6967 | err = -ENOMEM; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6968 | goto out; |
| 6969 | } |
| 6970 | spin_lock(&fp->f_lock); |
| 6971 | list_add(&work->fp_entry, &fp->blocked_works); |
| 6972 | spin_unlock(&fp->f_lock); |
| 6973 | |
| 6974 | smb2_send_interim_resp(work, STATUS_PENDING); |
| 6975 | |
Hyunchul Lee | 45a64e8 | 2021-07-10 09:34:20 +0900 | [diff] [blame] | 6976 | ksmbd_vfs_posix_lock_wait(flock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6977 | |
Hyunchul Lee | d4075ab | 2021-06-25 07:02:10 +0900 | [diff] [blame] | 6978 | if (work->state != KSMBD_WORK_ACTIVE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6979 | list_del(&smb_lock->llist); |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6980 | spin_lock(&work->conn->llist_lock); |
| 6981 | list_del(&smb_lock->clist); |
| 6982 | spin_unlock(&work->conn->llist_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6983 | locks_free_lock(flock); |
| 6984 | |
Hyunchul Lee | d4075ab | 2021-06-25 07:02:10 +0900 | [diff] [blame] | 6985 | if (work->state == KSMBD_WORK_CANCELLED) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6986 | spin_lock(&fp->f_lock); |
| 6987 | list_del(&work->fp_entry); |
| 6988 | spin_unlock(&fp->f_lock); |
| 6989 | rsp->hdr.Status = |
| 6990 | STATUS_CANCELLED; |
| 6991 | kfree(smb_lock); |
| 6992 | smb2_send_interim_resp(work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6993 | STATUS_CANCELLED); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6994 | work->send_no_response = 1; |
| 6995 | goto out; |
| 6996 | } |
| 6997 | init_smb2_rsp_hdr(work); |
| 6998 | smb2_set_err_rsp(work); |
| 6999 | rsp->hdr.Status = |
| 7000 | STATUS_RANGE_NOT_LOCKED; |
| 7001 | kfree(smb_lock); |
| 7002 | goto out2; |
| 7003 | } |
| 7004 | |
| 7005 | list_del(&smb_lock->llist); |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 7006 | spin_lock(&work->conn->llist_lock); |
| 7007 | list_del(&smb_lock->clist); |
| 7008 | spin_unlock(&work->conn->llist_lock); |
| 7009 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7010 | spin_lock(&fp->f_lock); |
| 7011 | list_del(&work->fp_entry); |
| 7012 | spin_unlock(&fp->f_lock); |
| 7013 | goto retry; |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 7014 | } else if (!rc) { |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 7015 | spin_lock(&work->conn->llist_lock); |
| 7016 | list_add_tail(&smb_lock->clist, |
| 7017 | &work->conn->lock_list); |
| 7018 | list_add_tail(&smb_lock->flist, |
| 7019 | &fp->lock_list); |
| 7020 | spin_unlock(&work->conn->llist_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7021 | list_add(&smb_lock->llist, &rollback_list); |
| 7022 | ksmbd_debug(SMB, "successful in taking lock\n"); |
| 7023 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7024 | goto out; |
| 7025 | } |
| 7026 | } |
| 7027 | } |
| 7028 | |
| 7029 | if (atomic_read(&fp->f_ci->op_count) > 1) |
| 7030 | smb_break_all_oplock(work, fp); |
| 7031 | |
| 7032 | rsp->StructureSize = cpu_to_le16(4); |
| 7033 | ksmbd_debug(SMB, "successful in taking lock\n"); |
| 7034 | rsp->hdr.Status = STATUS_SUCCESS; |
| 7035 | rsp->Reserved = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 7036 | inc_rfc1001_len(work->response_buf, 4); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7037 | ksmbd_fd_put(work, fp); |
Namjae Jeon | 96ad4ec | 2021-07-13 17:17:28 +0900 | [diff] [blame] | 7038 | return 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7039 | |
| 7040 | out: |
| 7041 | list_for_each_entry_safe(smb_lock, tmp, &lock_list, llist) { |
| 7042 | locks_free_lock(smb_lock->fl); |
| 7043 | list_del(&smb_lock->llist); |
| 7044 | kfree(smb_lock); |
| 7045 | } |
| 7046 | |
| 7047 | list_for_each_entry_safe(smb_lock, tmp, &rollback_list, llist) { |
| 7048 | struct file_lock *rlock = NULL; |
| 7049 | |
| 7050 | rlock = smb_flock_init(filp); |
| 7051 | rlock->fl_type = F_UNLCK; |
| 7052 | rlock->fl_start = smb_lock->start; |
| 7053 | rlock->fl_end = smb_lock->end; |
| 7054 | |
Namjae Jeon | 96ad4ec | 2021-07-13 17:17:28 +0900 | [diff] [blame] | 7055 | rc = vfs_lock_file(filp, 0, rlock, NULL); |
| 7056 | if (rc) |
| 7057 | pr_err("rollback unlock fail : %d\n", rc); |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 7058 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7059 | list_del(&smb_lock->llist); |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 7060 | spin_lock(&work->conn->llist_lock); |
| 7061 | if (!list_empty(&smb_lock->flist)) |
| 7062 | list_del(&smb_lock->flist); |
| 7063 | list_del(&smb_lock->clist); |
| 7064 | spin_unlock(&work->conn->llist_lock); |
| 7065 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7066 | locks_free_lock(smb_lock->fl); |
| 7067 | locks_free_lock(rlock); |
| 7068 | kfree(smb_lock); |
| 7069 | } |
| 7070 | out2: |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 7071 | ksmbd_debug(SMB, "failed in taking lock(flags : %x), err : %d\n", flags, err); |
| 7072 | |
| 7073 | if (!rsp->hdr.Status) { |
| 7074 | if (err == -EINVAL) |
| 7075 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 7076 | else if (err == -ENOMEM) |
| 7077 | rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES; |
| 7078 | else if (err == -ENOENT) |
| 7079 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 7080 | else |
| 7081 | rsp->hdr.Status = STATUS_LOCK_NOT_GRANTED; |
| 7082 | } |
| 7083 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7084 | smb2_set_err_rsp(work); |
| 7085 | ksmbd_fd_put(work, fp); |
Namjae Jeon | 96ad4ec | 2021-07-13 17:17:28 +0900 | [diff] [blame] | 7086 | return err; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7087 | } |
| 7088 | |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7089 | static int fsctl_copychunk(struct ksmbd_work *work, |
| 7090 | struct copychunk_ioctl_req *ci_req, |
| 7091 | unsigned int cnt_code, |
| 7092 | unsigned int input_count, |
| 7093 | unsigned long long volatile_id, |
| 7094 | unsigned long long persistent_id, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7095 | struct smb2_ioctl_rsp *rsp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7096 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7097 | struct copychunk_ioctl_rsp *ci_rsp; |
| 7098 | struct ksmbd_file *src_fp = NULL, *dst_fp = NULL; |
| 7099 | struct srv_copychunk *chunks; |
| 7100 | unsigned int i, chunk_count, chunk_count_written = 0; |
| 7101 | unsigned int chunk_size_written = 0; |
| 7102 | loff_t total_size_written = 0; |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7103 | int ret = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7104 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7105 | ci_rsp = (struct copychunk_ioctl_rsp *)&rsp->Buffer[0]; |
| 7106 | |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7107 | rsp->VolatileFileId = cpu_to_le64(volatile_id); |
| 7108 | rsp->PersistentFileId = cpu_to_le64(persistent_id); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7109 | ci_rsp->ChunksWritten = |
| 7110 | cpu_to_le32(ksmbd_server_side_copy_max_chunk_count()); |
| 7111 | ci_rsp->ChunkBytesWritten = |
| 7112 | cpu_to_le32(ksmbd_server_side_copy_max_chunk_size()); |
| 7113 | ci_rsp->TotalBytesWritten = |
| 7114 | cpu_to_le32(ksmbd_server_side_copy_max_total_size()); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7115 | |
| 7116 | chunks = (struct srv_copychunk *)&ci_req->Chunks[0]; |
| 7117 | chunk_count = le32_to_cpu(ci_req->ChunkCount); |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7118 | if (chunk_count == 0) |
| 7119 | goto out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7120 | total_size_written = 0; |
| 7121 | |
| 7122 | /* verify the SRV_COPYCHUNK_COPY packet */ |
| 7123 | if (chunk_count > ksmbd_server_side_copy_max_chunk_count() || |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7124 | input_count < offsetof(struct copychunk_ioctl_req, Chunks) + |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7125 | chunk_count * sizeof(struct srv_copychunk)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7126 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 7127 | return -EINVAL; |
| 7128 | } |
| 7129 | |
| 7130 | for (i = 0; i < chunk_count; i++) { |
| 7131 | if (le32_to_cpu(chunks[i].Length) == 0 || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7132 | 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] | 7133 | break; |
| 7134 | total_size_written += le32_to_cpu(chunks[i].Length); |
| 7135 | } |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7136 | |
| 7137 | if (i < chunk_count || |
| 7138 | total_size_written > ksmbd_server_side_copy_max_total_size()) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7139 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 7140 | return -EINVAL; |
| 7141 | } |
| 7142 | |
| 7143 | src_fp = ksmbd_lookup_foreign_fd(work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7144 | le64_to_cpu(ci_req->ResumeKey[0])); |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7145 | dst_fp = ksmbd_lookup_fd_slow(work, volatile_id, persistent_id); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7146 | ret = -EINVAL; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7147 | if (!src_fp || |
| 7148 | src_fp->persistent_id != le64_to_cpu(ci_req->ResumeKey[1])) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7149 | rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND; |
| 7150 | goto out; |
| 7151 | } |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7152 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7153 | if (!dst_fp) { |
| 7154 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 7155 | goto out; |
| 7156 | } |
| 7157 | |
| 7158 | /* |
| 7159 | * FILE_READ_DATA should only be included in |
| 7160 | * the FSCTL_COPYCHUNK case |
| 7161 | */ |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7162 | if (cnt_code == FSCTL_COPYCHUNK && |
| 7163 | !(dst_fp->daccess & (FILE_READ_DATA_LE | FILE_GENERIC_READ_LE))) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7164 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 7165 | goto out; |
| 7166 | } |
| 7167 | |
| 7168 | ret = ksmbd_vfs_copy_file_ranges(work, src_fp, dst_fp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7169 | chunks, chunk_count, |
| 7170 | &chunk_count_written, |
| 7171 | &chunk_size_written, |
| 7172 | &total_size_written); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7173 | if (ret < 0) { |
| 7174 | if (ret == -EACCES) |
| 7175 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 7176 | if (ret == -EAGAIN) |
| 7177 | rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT; |
| 7178 | else if (ret == -EBADF) |
| 7179 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 7180 | else if (ret == -EFBIG || ret == -ENOSPC) |
| 7181 | rsp->hdr.Status = STATUS_DISK_FULL; |
| 7182 | else if (ret == -EINVAL) |
| 7183 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 7184 | else if (ret == -EISDIR) |
| 7185 | rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY; |
| 7186 | else if (ret == -E2BIG) |
| 7187 | rsp->hdr.Status = STATUS_INVALID_VIEW_SIZE; |
| 7188 | else |
| 7189 | rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR; |
| 7190 | } |
| 7191 | |
| 7192 | ci_rsp->ChunksWritten = cpu_to_le32(chunk_count_written); |
| 7193 | ci_rsp->ChunkBytesWritten = cpu_to_le32(chunk_size_written); |
| 7194 | ci_rsp->TotalBytesWritten = cpu_to_le32(total_size_written); |
| 7195 | out: |
| 7196 | ksmbd_fd_put(work, src_fp); |
| 7197 | ksmbd_fd_put(work, dst_fp); |
| 7198 | return ret; |
| 7199 | } |
| 7200 | |
| 7201 | static __be32 idev_ipv4_address(struct in_device *idev) |
| 7202 | { |
| 7203 | __be32 addr = 0; |
| 7204 | |
| 7205 | struct in_ifaddr *ifa; |
| 7206 | |
| 7207 | rcu_read_lock(); |
| 7208 | in_dev_for_each_ifa_rcu(ifa, idev) { |
| 7209 | if (ifa->ifa_flags & IFA_F_SECONDARY) |
| 7210 | continue; |
| 7211 | |
| 7212 | addr = ifa->ifa_address; |
| 7213 | break; |
| 7214 | } |
| 7215 | rcu_read_unlock(); |
| 7216 | return addr; |
| 7217 | } |
| 7218 | |
| 7219 | static int fsctl_query_iface_info_ioctl(struct ksmbd_conn *conn, |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7220 | struct smb2_ioctl_rsp *rsp, |
| 7221 | unsigned int out_buf_len) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7222 | { |
| 7223 | struct network_interface_info_ioctl_rsp *nii_rsp = NULL; |
| 7224 | int nbytes = 0; |
| 7225 | struct net_device *netdev; |
| 7226 | struct sockaddr_storage_rsp *sockaddr_storage; |
| 7227 | unsigned int flags; |
| 7228 | unsigned long long speed; |
Namjae Jeon | f1abdb7 | 2021-07-27 13:25:13 +0900 | [diff] [blame] | 7229 | struct sockaddr_in6 *csin6 = (struct sockaddr_in6 *)&conn->peer_addr; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7230 | |
| 7231 | rtnl_lock(); |
| 7232 | for_each_netdev(&init_net, netdev) { |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7233 | if (out_buf_len < |
| 7234 | nbytes + sizeof(struct network_interface_info_ioctl_rsp)) { |
| 7235 | rtnl_unlock(); |
| 7236 | return -ENOSPC; |
| 7237 | } |
| 7238 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7239 | if (netdev->type == ARPHRD_LOOPBACK) |
| 7240 | continue; |
| 7241 | |
| 7242 | flags = dev_get_flags(netdev); |
| 7243 | if (!(flags & IFF_RUNNING)) |
| 7244 | continue; |
| 7245 | |
| 7246 | nii_rsp = (struct network_interface_info_ioctl_rsp *) |
| 7247 | &rsp->Buffer[nbytes]; |
| 7248 | nii_rsp->IfIndex = cpu_to_le32(netdev->ifindex); |
| 7249 | |
Hyunchul Lee | 03d8d4f | 2021-07-13 16:09:34 +0900 | [diff] [blame] | 7250 | nii_rsp->Capability = 0; |
Hyunchul Lee | 03d8d4f | 2021-07-13 16:09:34 +0900 | [diff] [blame] | 7251 | if (ksmbd_rdma_capable_netdev(netdev)) |
| 7252 | nii_rsp->Capability |= cpu_to_le32(RDMA_CAPABLE); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7253 | |
| 7254 | nii_rsp->Next = cpu_to_le32(152); |
| 7255 | nii_rsp->Reserved = 0; |
| 7256 | |
| 7257 | if (netdev->ethtool_ops->get_link_ksettings) { |
| 7258 | struct ethtool_link_ksettings cmd; |
| 7259 | |
| 7260 | netdev->ethtool_ops->get_link_ksettings(netdev, &cmd); |
| 7261 | speed = cmd.base.speed; |
| 7262 | } else { |
Per Forlin | d475866 | 2021-08-30 13:23:04 +0900 | [diff] [blame] | 7263 | ksmbd_debug(SMB, "%s %s\n", netdev->name, |
| 7264 | "speed is unknown, defaulting to 1Gb/sec"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7265 | speed = SPEED_1000; |
| 7266 | } |
| 7267 | |
| 7268 | speed *= 1000000; |
| 7269 | nii_rsp->LinkSpeed = cpu_to_le64(speed); |
| 7270 | |
| 7271 | sockaddr_storage = (struct sockaddr_storage_rsp *) |
| 7272 | nii_rsp->SockAddr_Storage; |
| 7273 | memset(sockaddr_storage, 0, 128); |
| 7274 | |
Namjae Jeon | f1abdb7 | 2021-07-27 13:25:13 +0900 | [diff] [blame] | 7275 | if (conn->peer_addr.ss_family == PF_INET || |
| 7276 | ipv6_addr_v4mapped(&csin6->sin6_addr)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7277 | struct in_device *idev; |
| 7278 | |
| 7279 | sockaddr_storage->Family = cpu_to_le16(INTERNETWORK); |
| 7280 | sockaddr_storage->addr4.Port = 0; |
| 7281 | |
| 7282 | idev = __in_dev_get_rtnl(netdev); |
| 7283 | if (!idev) |
| 7284 | continue; |
| 7285 | sockaddr_storage->addr4.IPv4address = |
| 7286 | idev_ipv4_address(idev); |
| 7287 | } else { |
| 7288 | struct inet6_dev *idev6; |
| 7289 | struct inet6_ifaddr *ifa; |
| 7290 | __u8 *ipv6_addr = sockaddr_storage->addr6.IPv6address; |
| 7291 | |
| 7292 | sockaddr_storage->Family = cpu_to_le16(INTERNETWORKV6); |
| 7293 | sockaddr_storage->addr6.Port = 0; |
| 7294 | sockaddr_storage->addr6.FlowInfo = 0; |
| 7295 | |
| 7296 | idev6 = __in6_dev_get(netdev); |
| 7297 | if (!idev6) |
| 7298 | continue; |
| 7299 | |
| 7300 | list_for_each_entry(ifa, &idev6->addr_list, if_list) { |
| 7301 | if (ifa->flags & (IFA_F_TENTATIVE | |
| 7302 | IFA_F_DEPRECATED)) |
| 7303 | continue; |
| 7304 | memcpy(ipv6_addr, ifa->addr.s6_addr, 16); |
| 7305 | break; |
| 7306 | } |
| 7307 | sockaddr_storage->addr6.ScopeId = 0; |
| 7308 | } |
| 7309 | |
| 7310 | nbytes += sizeof(struct network_interface_info_ioctl_rsp); |
| 7311 | } |
| 7312 | rtnl_unlock(); |
| 7313 | |
| 7314 | /* zero if this is last one */ |
| 7315 | if (nii_rsp) |
| 7316 | nii_rsp->Next = 0; |
| 7317 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7318 | rsp->PersistentFileId = cpu_to_le64(SMB2_NO_FID); |
| 7319 | rsp->VolatileFileId = cpu_to_le64(SMB2_NO_FID); |
| 7320 | return nbytes; |
| 7321 | } |
| 7322 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7323 | static int fsctl_validate_negotiate_info(struct ksmbd_conn *conn, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7324 | struct validate_negotiate_info_req *neg_req, |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7325 | struct validate_negotiate_info_rsp *neg_rsp, |
| 7326 | unsigned int in_buf_len) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7327 | { |
| 7328 | int ret = 0; |
| 7329 | int dialect; |
| 7330 | |
Marios Makassikis | 78f1688 | 2021-10-28 21:01:27 +0200 | [diff] [blame] | 7331 | if (in_buf_len < offsetof(struct validate_negotiate_info_req, Dialects) + |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7332 | le16_to_cpu(neg_req->DialectCount) * sizeof(__le16)) |
| 7333 | return -EINVAL; |
| 7334 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7335 | dialect = ksmbd_lookup_dialect_by_id(neg_req->Dialects, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7336 | neg_req->DialectCount); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7337 | if (dialect == BAD_PROT_ID || dialect != conn->dialect) { |
| 7338 | ret = -EINVAL; |
| 7339 | goto err_out; |
| 7340 | } |
| 7341 | |
| 7342 | if (strncmp(neg_req->Guid, conn->ClientGUID, SMB2_CLIENT_GUID_SIZE)) { |
| 7343 | ret = -EINVAL; |
| 7344 | goto err_out; |
| 7345 | } |
| 7346 | |
| 7347 | if (le16_to_cpu(neg_req->SecurityMode) != conn->cli_sec_mode) { |
| 7348 | ret = -EINVAL; |
| 7349 | goto err_out; |
| 7350 | } |
| 7351 | |
| 7352 | if (le32_to_cpu(neg_req->Capabilities) != conn->cli_cap) { |
| 7353 | ret = -EINVAL; |
| 7354 | goto err_out; |
| 7355 | } |
| 7356 | |
| 7357 | neg_rsp->Capabilities = cpu_to_le32(conn->vals->capabilities); |
| 7358 | memset(neg_rsp->Guid, 0, SMB2_CLIENT_GUID_SIZE); |
| 7359 | neg_rsp->SecurityMode = cpu_to_le16(conn->srv_sec_mode); |
| 7360 | neg_rsp->Dialect = cpu_to_le16(conn->dialect); |
| 7361 | err_out: |
| 7362 | return ret; |
| 7363 | } |
| 7364 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7365 | static int fsctl_query_allocated_ranges(struct ksmbd_work *work, u64 id, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7366 | struct file_allocated_range_buffer *qar_req, |
| 7367 | struct file_allocated_range_buffer *qar_rsp, |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7368 | unsigned int in_count, unsigned int *out_count) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7369 | { |
| 7370 | struct ksmbd_file *fp; |
| 7371 | loff_t start, length; |
| 7372 | int ret = 0; |
| 7373 | |
| 7374 | *out_count = 0; |
| 7375 | if (in_count == 0) |
| 7376 | return -EINVAL; |
| 7377 | |
| 7378 | fp = ksmbd_lookup_fd_fast(work, id); |
| 7379 | if (!fp) |
| 7380 | return -ENOENT; |
| 7381 | |
| 7382 | start = le64_to_cpu(qar_req->file_offset); |
| 7383 | length = le64_to_cpu(qar_req->length); |
| 7384 | |
| 7385 | ret = ksmbd_vfs_fqar_lseek(fp, start, length, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7386 | qar_rsp, in_count, out_count); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7387 | if (ret && ret != -E2BIG) |
| 7388 | *out_count = 0; |
| 7389 | |
| 7390 | ksmbd_fd_put(work, fp); |
| 7391 | return ret; |
| 7392 | } |
| 7393 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7394 | static int fsctl_pipe_transceive(struct ksmbd_work *work, u64 id, |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7395 | unsigned int out_buf_len, |
| 7396 | struct smb2_ioctl_req *req, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7397 | struct smb2_ioctl_rsp *rsp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7398 | { |
| 7399 | struct ksmbd_rpc_command *rpc_resp; |
| 7400 | char *data_buf = (char *)&req->Buffer[0]; |
| 7401 | int nbytes = 0; |
| 7402 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7403 | rpc_resp = ksmbd_rpc_ioctl(work->sess, id, data_buf, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7404 | le32_to_cpu(req->InputCount)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7405 | if (rpc_resp) { |
| 7406 | if (rpc_resp->flags == KSMBD_RPC_SOME_NOT_MAPPED) { |
| 7407 | /* |
| 7408 | * set STATUS_SOME_NOT_MAPPED response |
| 7409 | * for unknown domain sid. |
| 7410 | */ |
| 7411 | rsp->hdr.Status = STATUS_SOME_NOT_MAPPED; |
| 7412 | } else if (rpc_resp->flags == KSMBD_RPC_ENOTIMPLEMENTED) { |
| 7413 | rsp->hdr.Status = STATUS_NOT_SUPPORTED; |
| 7414 | goto out; |
| 7415 | } else if (rpc_resp->flags != KSMBD_RPC_OK) { |
| 7416 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 7417 | goto out; |
| 7418 | } |
| 7419 | |
| 7420 | nbytes = rpc_resp->payload_sz; |
| 7421 | if (rpc_resp->payload_sz > out_buf_len) { |
| 7422 | rsp->hdr.Status = STATUS_BUFFER_OVERFLOW; |
| 7423 | nbytes = out_buf_len; |
| 7424 | } |
| 7425 | |
| 7426 | if (!rpc_resp->payload_sz) { |
| 7427 | rsp->hdr.Status = |
| 7428 | STATUS_UNEXPECTED_IO_ERROR; |
| 7429 | goto out; |
| 7430 | } |
| 7431 | |
| 7432 | memcpy((char *)rsp->Buffer, rpc_resp->payload, nbytes); |
| 7433 | } |
| 7434 | out: |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 7435 | kvfree(rpc_resp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7436 | return nbytes; |
| 7437 | } |
| 7438 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7439 | static inline int fsctl_set_sparse(struct ksmbd_work *work, u64 id, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7440 | struct file_sparse *sparse) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7441 | { |
| 7442 | struct ksmbd_file *fp; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 7443 | struct user_namespace *user_ns; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7444 | int ret = 0; |
| 7445 | __le32 old_fattr; |
| 7446 | |
| 7447 | fp = ksmbd_lookup_fd_fast(work, id); |
| 7448 | if (!fp) |
| 7449 | return -ENOENT; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 7450 | user_ns = file_mnt_user_ns(fp->filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7451 | |
| 7452 | old_fattr = fp->f_ci->m_fattr; |
| 7453 | if (sparse->SetSparse) |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 7454 | fp->f_ci->m_fattr |= FILE_ATTRIBUTE_SPARSE_FILE_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7455 | else |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 7456 | fp->f_ci->m_fattr &= ~FILE_ATTRIBUTE_SPARSE_FILE_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7457 | |
| 7458 | if (fp->f_ci->m_fattr != old_fattr && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7459 | test_share_config_flag(work->tcon->share_conf, |
| 7460 | KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7461 | struct xattr_dos_attrib da; |
| 7462 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 7463 | ret = ksmbd_vfs_get_dos_attrib_xattr(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 7464 | fp->filp->f_path.dentry, &da); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7465 | if (ret <= 0) |
| 7466 | goto out; |
| 7467 | |
| 7468 | da.attr = le32_to_cpu(fp->f_ci->m_fattr); |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 7469 | ret = ksmbd_vfs_set_dos_attrib_xattr(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 7470 | fp->filp->f_path.dentry, &da); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7471 | if (ret) |
| 7472 | fp->f_ci->m_fattr = old_fattr; |
| 7473 | } |
| 7474 | |
| 7475 | out: |
| 7476 | ksmbd_fd_put(work, fp); |
| 7477 | return ret; |
| 7478 | } |
| 7479 | |
| 7480 | static int fsctl_request_resume_key(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7481 | struct smb2_ioctl_req *req, |
| 7482 | struct resume_key_ioctl_rsp *key_rsp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7483 | { |
| 7484 | struct ksmbd_file *fp; |
| 7485 | |
| 7486 | fp = ksmbd_lookup_fd_slow(work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7487 | le64_to_cpu(req->VolatileFileId), |
| 7488 | le64_to_cpu(req->PersistentFileId)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7489 | if (!fp) |
| 7490 | return -ENOENT; |
| 7491 | |
| 7492 | memset(key_rsp, 0, sizeof(*key_rsp)); |
| 7493 | key_rsp->ResumeKey[0] = req->VolatileFileId; |
| 7494 | key_rsp->ResumeKey[1] = req->PersistentFileId; |
| 7495 | ksmbd_fd_put(work, fp); |
| 7496 | |
| 7497 | return 0; |
| 7498 | } |
| 7499 | |
| 7500 | /** |
| 7501 | * smb2_ioctl() - handler for smb2 ioctl command |
| 7502 | * @work: smb work containing ioctl command buffer |
| 7503 | * |
| 7504 | * Return: 0 on success, otherwise error |
| 7505 | */ |
| 7506 | int smb2_ioctl(struct ksmbd_work *work) |
| 7507 | { |
| 7508 | struct smb2_ioctl_req *req; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 7509 | struct smb2_ioctl_rsp *rsp; |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7510 | unsigned int cnt_code, nbytes = 0, out_buf_len, in_buf_len; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7511 | u64 id = KSMBD_NO_FID; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7512 | struct ksmbd_conn *conn = work->conn; |
| 7513 | int ret = 0; |
| 7514 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7515 | if (work->next_smb2_rcv_hdr_off) { |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 7516 | req = ksmbd_req_buf_next(work); |
| 7517 | rsp = ksmbd_resp_buf_next(work); |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 7518 | if (!has_file_id(le64_to_cpu(req->VolatileFileId))) { |
| 7519 | ksmbd_debug(SMB, "Compound request set FID = %llu\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7520 | work->compound_fid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7521 | id = work->compound_fid; |
| 7522 | } |
| 7523 | } else { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 7524 | req = smb2_get_msg(work->request_buf); |
| 7525 | rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7526 | } |
| 7527 | |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 7528 | if (!has_file_id(id)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7529 | id = le64_to_cpu(req->VolatileFileId); |
| 7530 | |
| 7531 | if (req->Flags != cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL)) { |
| 7532 | rsp->hdr.Status = STATUS_NOT_SUPPORTED; |
| 7533 | goto out; |
| 7534 | } |
| 7535 | |
| 7536 | cnt_code = le32_to_cpu(req->CntCode); |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 7537 | ret = smb2_calc_max_out_buf_len(work, 48, |
| 7538 | le32_to_cpu(req->MaxOutputResponse)); |
| 7539 | if (ret < 0) { |
| 7540 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 7541 | goto out; |
| 7542 | } |
| 7543 | out_buf_len = (unsigned int)ret; |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7544 | in_buf_len = le32_to_cpu(req->InputCount); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7545 | |
| 7546 | switch (cnt_code) { |
| 7547 | case FSCTL_DFS_GET_REFERRALS: |
| 7548 | case FSCTL_DFS_GET_REFERRALS_EX: |
| 7549 | /* Not support DFS yet */ |
| 7550 | rsp->hdr.Status = STATUS_FS_DRIVER_REQUIRED; |
| 7551 | goto out; |
| 7552 | case FSCTL_CREATE_OR_GET_OBJECT_ID: |
| 7553 | { |
| 7554 | struct file_object_buf_type1_ioctl_rsp *obj_buf; |
| 7555 | |
| 7556 | nbytes = sizeof(struct file_object_buf_type1_ioctl_rsp); |
| 7557 | obj_buf = (struct file_object_buf_type1_ioctl_rsp *) |
| 7558 | &rsp->Buffer[0]; |
| 7559 | |
| 7560 | /* |
| 7561 | * TODO: This is dummy implementation to pass smbtorture |
| 7562 | * Need to check correct response later |
| 7563 | */ |
| 7564 | memset(obj_buf->ObjectId, 0x0, 16); |
| 7565 | memset(obj_buf->BirthVolumeId, 0x0, 16); |
| 7566 | memset(obj_buf->BirthObjectId, 0x0, 16); |
| 7567 | memset(obj_buf->DomainId, 0x0, 16); |
| 7568 | |
| 7569 | break; |
| 7570 | } |
| 7571 | case FSCTL_PIPE_TRANSCEIVE: |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7572 | out_buf_len = min_t(u32, KSMBD_IPC_MAX_PAYLOAD, out_buf_len); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7573 | nbytes = fsctl_pipe_transceive(work, id, out_buf_len, req, rsp); |
| 7574 | break; |
| 7575 | case FSCTL_VALIDATE_NEGOTIATE_INFO: |
| 7576 | if (conn->dialect < SMB30_PROT_ID) { |
| 7577 | ret = -EOPNOTSUPP; |
| 7578 | goto out; |
| 7579 | } |
| 7580 | |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7581 | if (in_buf_len < sizeof(struct validate_negotiate_info_req)) |
| 7582 | return -EINVAL; |
| 7583 | |
| 7584 | if (out_buf_len < sizeof(struct validate_negotiate_info_rsp)) |
| 7585 | return -EINVAL; |
| 7586 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7587 | ret = fsctl_validate_negotiate_info(conn, |
| 7588 | (struct validate_negotiate_info_req *)&req->Buffer[0], |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7589 | (struct validate_negotiate_info_rsp *)&rsp->Buffer[0], |
| 7590 | in_buf_len); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7591 | if (ret < 0) |
| 7592 | goto out; |
| 7593 | |
| 7594 | nbytes = sizeof(struct validate_negotiate_info_rsp); |
| 7595 | rsp->PersistentFileId = cpu_to_le64(SMB2_NO_FID); |
| 7596 | rsp->VolatileFileId = cpu_to_le64(SMB2_NO_FID); |
| 7597 | break; |
| 7598 | case FSCTL_QUERY_NETWORK_INTERFACE_INFO: |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7599 | ret = fsctl_query_iface_info_ioctl(conn, rsp, out_buf_len); |
| 7600 | if (ret < 0) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7601 | goto out; |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7602 | nbytes = ret; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7603 | break; |
| 7604 | case FSCTL_REQUEST_RESUME_KEY: |
| 7605 | if (out_buf_len < sizeof(struct resume_key_ioctl_rsp)) { |
| 7606 | ret = -EINVAL; |
| 7607 | goto out; |
| 7608 | } |
| 7609 | |
| 7610 | ret = fsctl_request_resume_key(work, req, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7611 | (struct resume_key_ioctl_rsp *)&rsp->Buffer[0]); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7612 | if (ret < 0) |
| 7613 | goto out; |
| 7614 | rsp->PersistentFileId = req->PersistentFileId; |
| 7615 | rsp->VolatileFileId = req->VolatileFileId; |
| 7616 | nbytes = sizeof(struct resume_key_ioctl_rsp); |
| 7617 | break; |
| 7618 | case FSCTL_COPYCHUNK: |
| 7619 | case FSCTL_COPYCHUNK_WRITE: |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7620 | if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7621 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7622 | "User does not have write permission\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7623 | ret = -EACCES; |
| 7624 | goto out; |
| 7625 | } |
| 7626 | |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7627 | if (in_buf_len < sizeof(struct copychunk_ioctl_req)) { |
| 7628 | ret = -EINVAL; |
| 7629 | goto out; |
| 7630 | } |
| 7631 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7632 | if (out_buf_len < sizeof(struct copychunk_ioctl_rsp)) { |
| 7633 | ret = -EINVAL; |
| 7634 | goto out; |
| 7635 | } |
| 7636 | |
| 7637 | nbytes = sizeof(struct copychunk_ioctl_rsp); |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7638 | rsp->VolatileFileId = req->VolatileFileId; |
| 7639 | rsp->PersistentFileId = req->PersistentFileId; |
| 7640 | fsctl_copychunk(work, |
| 7641 | (struct copychunk_ioctl_req *)&req->Buffer[0], |
| 7642 | le32_to_cpu(req->CntCode), |
| 7643 | le32_to_cpu(req->InputCount), |
| 7644 | le64_to_cpu(req->VolatileFileId), |
| 7645 | le64_to_cpu(req->PersistentFileId), |
| 7646 | rsp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7647 | break; |
| 7648 | case FSCTL_SET_SPARSE: |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7649 | if (in_buf_len < sizeof(struct file_sparse)) { |
| 7650 | ret = -EINVAL; |
| 7651 | goto out; |
| 7652 | } |
| 7653 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7654 | ret = fsctl_set_sparse(work, id, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7655 | (struct file_sparse *)&req->Buffer[0]); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7656 | if (ret < 0) |
| 7657 | goto out; |
| 7658 | break; |
| 7659 | case FSCTL_SET_ZERO_DATA: |
| 7660 | { |
| 7661 | struct file_zero_data_information *zero_data; |
| 7662 | struct ksmbd_file *fp; |
| 7663 | loff_t off, len; |
| 7664 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7665 | if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7666 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7667 | "User does not have write permission\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7668 | ret = -EACCES; |
| 7669 | goto out; |
| 7670 | } |
| 7671 | |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7672 | if (in_buf_len < sizeof(struct file_zero_data_information)) { |
| 7673 | ret = -EINVAL; |
| 7674 | goto out; |
| 7675 | } |
| 7676 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7677 | zero_data = |
| 7678 | (struct file_zero_data_information *)&req->Buffer[0]; |
| 7679 | |
| 7680 | fp = ksmbd_lookup_fd_fast(work, id); |
| 7681 | if (!fp) { |
| 7682 | ret = -ENOENT; |
| 7683 | goto out; |
| 7684 | } |
| 7685 | |
| 7686 | off = le64_to_cpu(zero_data->FileOffset); |
| 7687 | len = le64_to_cpu(zero_data->BeyondFinalZero) - off; |
| 7688 | |
| 7689 | ret = ksmbd_vfs_zero_data(work, fp, off, len); |
| 7690 | ksmbd_fd_put(work, fp); |
| 7691 | if (ret < 0) |
| 7692 | goto out; |
| 7693 | break; |
| 7694 | } |
| 7695 | case FSCTL_QUERY_ALLOCATED_RANGES: |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7696 | if (in_buf_len < sizeof(struct file_allocated_range_buffer)) { |
| 7697 | ret = -EINVAL; |
| 7698 | goto out; |
| 7699 | } |
| 7700 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7701 | ret = fsctl_query_allocated_ranges(work, id, |
| 7702 | (struct file_allocated_range_buffer *)&req->Buffer[0], |
| 7703 | (struct file_allocated_range_buffer *)&rsp->Buffer[0], |
| 7704 | out_buf_len / |
| 7705 | sizeof(struct file_allocated_range_buffer), &nbytes); |
| 7706 | if (ret == -E2BIG) { |
| 7707 | rsp->hdr.Status = STATUS_BUFFER_OVERFLOW; |
| 7708 | } else if (ret < 0) { |
| 7709 | nbytes = 0; |
| 7710 | goto out; |
| 7711 | } |
| 7712 | |
| 7713 | nbytes *= sizeof(struct file_allocated_range_buffer); |
| 7714 | break; |
| 7715 | case FSCTL_GET_REPARSE_POINT: |
| 7716 | { |
| 7717 | struct reparse_data_buffer *reparse_ptr; |
| 7718 | struct ksmbd_file *fp; |
| 7719 | |
| 7720 | reparse_ptr = (struct reparse_data_buffer *)&rsp->Buffer[0]; |
| 7721 | fp = ksmbd_lookup_fd_fast(work, id); |
| 7722 | if (!fp) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 7723 | pr_err("not found fp!!\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7724 | ret = -ENOENT; |
| 7725 | goto out; |
| 7726 | } |
| 7727 | |
| 7728 | reparse_ptr->ReparseTag = |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 7729 | smb2_get_reparse_tag_special_file(file_inode(fp->filp)->i_mode); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7730 | reparse_ptr->ReparseDataLength = 0; |
| 7731 | ksmbd_fd_put(work, fp); |
| 7732 | nbytes = sizeof(struct reparse_data_buffer); |
| 7733 | break; |
| 7734 | } |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 7735 | case FSCTL_DUPLICATE_EXTENTS_TO_FILE: |
| 7736 | { |
| 7737 | struct ksmbd_file *fp_in, *fp_out = NULL; |
| 7738 | struct duplicate_extents_to_file *dup_ext; |
| 7739 | loff_t src_off, dst_off, length, cloned; |
| 7740 | |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7741 | if (in_buf_len < sizeof(struct duplicate_extents_to_file)) { |
| 7742 | ret = -EINVAL; |
| 7743 | goto out; |
| 7744 | } |
| 7745 | |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 7746 | dup_ext = (struct duplicate_extents_to_file *)&req->Buffer[0]; |
| 7747 | |
| 7748 | fp_in = ksmbd_lookup_fd_slow(work, dup_ext->VolatileFileHandle, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7749 | dup_ext->PersistentFileHandle); |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 7750 | if (!fp_in) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 7751 | pr_err("not found file handle in duplicate extent to file\n"); |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 7752 | ret = -ENOENT; |
| 7753 | goto out; |
| 7754 | } |
| 7755 | |
| 7756 | fp_out = ksmbd_lookup_fd_fast(work, id); |
| 7757 | if (!fp_out) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 7758 | pr_err("not found fp\n"); |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 7759 | ret = -ENOENT; |
| 7760 | goto dup_ext_out; |
| 7761 | } |
| 7762 | |
| 7763 | src_off = le64_to_cpu(dup_ext->SourceFileOffset); |
| 7764 | dst_off = le64_to_cpu(dup_ext->TargetFileOffset); |
| 7765 | length = le64_to_cpu(dup_ext->ByteCount); |
| 7766 | 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] | 7767 | dst_off, length, 0); |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 7768 | if (cloned == -EXDEV || cloned == -EOPNOTSUPP) { |
| 7769 | ret = -EOPNOTSUPP; |
| 7770 | goto dup_ext_out; |
| 7771 | } else if (cloned != length) { |
Namjae Jeon | f852477 | 2021-06-18 10:28:00 +0900 | [diff] [blame] | 7772 | cloned = vfs_copy_file_range(fp_in->filp, src_off, |
| 7773 | fp_out->filp, dst_off, length, 0); |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 7774 | if (cloned != length) { |
| 7775 | if (cloned < 0) |
| 7776 | ret = cloned; |
| 7777 | else |
| 7778 | ret = -EINVAL; |
| 7779 | } |
| 7780 | } |
| 7781 | |
| 7782 | dup_ext_out: |
| 7783 | ksmbd_fd_put(work, fp_in); |
| 7784 | ksmbd_fd_put(work, fp_out); |
| 7785 | if (ret < 0) |
| 7786 | goto out; |
| 7787 | break; |
| 7788 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7789 | default: |
| 7790 | ksmbd_debug(SMB, "not implemented yet ioctl command 0x%x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7791 | cnt_code); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7792 | ret = -EOPNOTSUPP; |
| 7793 | goto out; |
| 7794 | } |
| 7795 | |
| 7796 | rsp->CntCode = cpu_to_le32(cnt_code); |
| 7797 | rsp->InputCount = cpu_to_le32(0); |
| 7798 | rsp->InputOffset = cpu_to_le32(112); |
| 7799 | rsp->OutputOffset = cpu_to_le32(112); |
| 7800 | rsp->OutputCount = cpu_to_le32(nbytes); |
| 7801 | rsp->StructureSize = cpu_to_le16(49); |
| 7802 | rsp->Reserved = cpu_to_le16(0); |
| 7803 | rsp->Flags = cpu_to_le32(0); |
| 7804 | rsp->Reserved2 = cpu_to_le32(0); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 7805 | inc_rfc1001_len(work->response_buf, 48 + nbytes); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7806 | |
| 7807 | return 0; |
| 7808 | |
| 7809 | out: |
| 7810 | if (ret == -EACCES) |
| 7811 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 7812 | else if (ret == -ENOENT) |
| 7813 | rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND; |
| 7814 | else if (ret == -EOPNOTSUPP) |
| 7815 | rsp->hdr.Status = STATUS_NOT_SUPPORTED; |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7816 | else if (ret == -ENOSPC) |
| 7817 | rsp->hdr.Status = STATUS_BUFFER_TOO_SMALL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7818 | else if (ret < 0 || rsp->hdr.Status == 0) |
| 7819 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 7820 | smb2_set_err_rsp(work); |
| 7821 | return 0; |
| 7822 | } |
| 7823 | |
| 7824 | /** |
| 7825 | * smb20_oplock_break_ack() - handler for smb2.0 oplock break command |
| 7826 | * @work: smb work containing oplock break command buffer |
| 7827 | * |
| 7828 | * Return: 0 |
| 7829 | */ |
| 7830 | static void smb20_oplock_break_ack(struct ksmbd_work *work) |
| 7831 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 7832 | struct smb2_oplock_break *req = smb2_get_msg(work->request_buf); |
| 7833 | struct smb2_oplock_break *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7834 | struct ksmbd_file *fp; |
| 7835 | struct oplock_info *opinfo = NULL; |
| 7836 | __le32 err = 0; |
| 7837 | int ret = 0; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7838 | u64 volatile_id, persistent_id; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7839 | char req_oplevel = 0, rsp_oplevel = 0; |
| 7840 | unsigned int oplock_change_type; |
| 7841 | |
| 7842 | volatile_id = le64_to_cpu(req->VolatileFid); |
| 7843 | persistent_id = le64_to_cpu(req->PersistentFid); |
| 7844 | req_oplevel = req->OplockLevel; |
| 7845 | ksmbd_debug(OPLOCK, "v_id %llu, p_id %llu request oplock level %d\n", |
| 7846 | volatile_id, persistent_id, req_oplevel); |
| 7847 | |
| 7848 | fp = ksmbd_lookup_fd_slow(work, volatile_id, persistent_id); |
| 7849 | if (!fp) { |
| 7850 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 7851 | smb2_set_err_rsp(work); |
| 7852 | return; |
| 7853 | } |
| 7854 | |
| 7855 | opinfo = opinfo_get(fp); |
| 7856 | if (!opinfo) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 7857 | pr_err("unexpected null oplock_info\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7858 | rsp->hdr.Status = STATUS_INVALID_OPLOCK_PROTOCOL; |
| 7859 | smb2_set_err_rsp(work); |
| 7860 | ksmbd_fd_put(work, fp); |
| 7861 | return; |
| 7862 | } |
| 7863 | |
| 7864 | if (opinfo->level == SMB2_OPLOCK_LEVEL_NONE) { |
| 7865 | rsp->hdr.Status = STATUS_INVALID_OPLOCK_PROTOCOL; |
| 7866 | goto err_out; |
| 7867 | } |
| 7868 | |
| 7869 | if (opinfo->op_state == OPLOCK_STATE_NONE) { |
| 7870 | ksmbd_debug(SMB, "unexpected oplock state 0x%x\n", opinfo->op_state); |
| 7871 | rsp->hdr.Status = STATUS_UNSUCCESSFUL; |
| 7872 | goto err_out; |
| 7873 | } |
| 7874 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7875 | if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE || |
| 7876 | opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) && |
| 7877 | (req_oplevel != SMB2_OPLOCK_LEVEL_II && |
| 7878 | req_oplevel != SMB2_OPLOCK_LEVEL_NONE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7879 | err = STATUS_INVALID_OPLOCK_PROTOCOL; |
| 7880 | oplock_change_type = OPLOCK_WRITE_TO_NONE; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7881 | } else if (opinfo->level == SMB2_OPLOCK_LEVEL_II && |
| 7882 | req_oplevel != SMB2_OPLOCK_LEVEL_NONE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7883 | err = STATUS_INVALID_OPLOCK_PROTOCOL; |
| 7884 | oplock_change_type = OPLOCK_READ_TO_NONE; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7885 | } else if (req_oplevel == SMB2_OPLOCK_LEVEL_II || |
| 7886 | req_oplevel == SMB2_OPLOCK_LEVEL_NONE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7887 | err = STATUS_INVALID_DEVICE_STATE; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7888 | if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE || |
| 7889 | opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) && |
| 7890 | req_oplevel == SMB2_OPLOCK_LEVEL_II) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7891 | oplock_change_type = OPLOCK_WRITE_TO_READ; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7892 | } else if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE || |
| 7893 | opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) && |
| 7894 | req_oplevel == SMB2_OPLOCK_LEVEL_NONE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7895 | oplock_change_type = OPLOCK_WRITE_TO_NONE; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7896 | } else if (opinfo->level == SMB2_OPLOCK_LEVEL_II && |
| 7897 | req_oplevel == SMB2_OPLOCK_LEVEL_NONE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7898 | oplock_change_type = OPLOCK_READ_TO_NONE; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7899 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7900 | oplock_change_type = 0; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7901 | } |
| 7902 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7903 | oplock_change_type = 0; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7904 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7905 | |
| 7906 | switch (oplock_change_type) { |
| 7907 | case OPLOCK_WRITE_TO_READ: |
| 7908 | ret = opinfo_write_to_read(opinfo); |
| 7909 | rsp_oplevel = SMB2_OPLOCK_LEVEL_II; |
| 7910 | break; |
| 7911 | case OPLOCK_WRITE_TO_NONE: |
| 7912 | ret = opinfo_write_to_none(opinfo); |
| 7913 | rsp_oplevel = SMB2_OPLOCK_LEVEL_NONE; |
| 7914 | break; |
| 7915 | case OPLOCK_READ_TO_NONE: |
| 7916 | ret = opinfo_read_to_none(opinfo); |
| 7917 | rsp_oplevel = SMB2_OPLOCK_LEVEL_NONE; |
| 7918 | break; |
| 7919 | default: |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 7920 | pr_err("unknown oplock change 0x%x -> 0x%x\n", |
| 7921 | opinfo->level, rsp_oplevel); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7922 | } |
| 7923 | |
| 7924 | if (ret < 0) { |
| 7925 | rsp->hdr.Status = err; |
| 7926 | goto err_out; |
| 7927 | } |
| 7928 | |
| 7929 | opinfo_put(opinfo); |
| 7930 | ksmbd_fd_put(work, fp); |
| 7931 | opinfo->op_state = OPLOCK_STATE_NONE; |
| 7932 | wake_up_interruptible_all(&opinfo->oplock_q); |
| 7933 | |
| 7934 | rsp->StructureSize = cpu_to_le16(24); |
| 7935 | rsp->OplockLevel = rsp_oplevel; |
| 7936 | rsp->Reserved = 0; |
| 7937 | rsp->Reserved2 = 0; |
| 7938 | rsp->VolatileFid = cpu_to_le64(volatile_id); |
| 7939 | rsp->PersistentFid = cpu_to_le64(persistent_id); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 7940 | inc_rfc1001_len(work->response_buf, 24); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7941 | return; |
| 7942 | |
| 7943 | err_out: |
| 7944 | opinfo->op_state = OPLOCK_STATE_NONE; |
| 7945 | wake_up_interruptible_all(&opinfo->oplock_q); |
| 7946 | |
| 7947 | opinfo_put(opinfo); |
| 7948 | ksmbd_fd_put(work, fp); |
| 7949 | smb2_set_err_rsp(work); |
| 7950 | } |
| 7951 | |
| 7952 | static int check_lease_state(struct lease *lease, __le32 req_state) |
| 7953 | { |
| 7954 | if ((lease->new_state == |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7955 | (SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_HANDLE_CACHING_LE)) && |
| 7956 | !(req_state & SMB2_LEASE_WRITE_CACHING_LE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7957 | lease->new_state = req_state; |
| 7958 | return 0; |
| 7959 | } |
| 7960 | |
| 7961 | if (lease->new_state == req_state) |
| 7962 | return 0; |
| 7963 | |
| 7964 | return 1; |
| 7965 | } |
| 7966 | |
| 7967 | /** |
| 7968 | * smb21_lease_break_ack() - handler for smb2.1 lease break command |
| 7969 | * @work: smb work containing lease break command buffer |
| 7970 | * |
| 7971 | * Return: 0 |
| 7972 | */ |
| 7973 | static void smb21_lease_break_ack(struct ksmbd_work *work) |
| 7974 | { |
| 7975 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 7976 | struct smb2_lease_ack *req = smb2_get_msg(work->request_buf); |
| 7977 | struct smb2_lease_ack *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7978 | struct oplock_info *opinfo; |
| 7979 | __le32 err = 0; |
| 7980 | int ret = 0; |
| 7981 | unsigned int lease_change_type; |
| 7982 | __le32 lease_state; |
| 7983 | struct lease *lease; |
| 7984 | |
| 7985 | ksmbd_debug(OPLOCK, "smb21 lease break, lease state(0x%x)\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7986 | le32_to_cpu(req->LeaseState)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7987 | opinfo = lookup_lease_in_table(conn, req->LeaseKey); |
| 7988 | if (!opinfo) { |
| 7989 | ksmbd_debug(OPLOCK, "file not opened\n"); |
| 7990 | smb2_set_err_rsp(work); |
| 7991 | rsp->hdr.Status = STATUS_UNSUCCESSFUL; |
| 7992 | return; |
| 7993 | } |
| 7994 | lease = opinfo->o_lease; |
| 7995 | |
| 7996 | if (opinfo->op_state == OPLOCK_STATE_NONE) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 7997 | pr_err("unexpected lease break state 0x%x\n", |
| 7998 | opinfo->op_state); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7999 | rsp->hdr.Status = STATUS_UNSUCCESSFUL; |
| 8000 | goto err_out; |
| 8001 | } |
| 8002 | |
| 8003 | if (check_lease_state(lease, req->LeaseState)) { |
| 8004 | rsp->hdr.Status = STATUS_REQUEST_NOT_ACCEPTED; |
| 8005 | ksmbd_debug(OPLOCK, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 8006 | "req lease state: 0x%x, expected state: 0x%x\n", |
| 8007 | req->LeaseState, lease->new_state); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8008 | goto err_out; |
| 8009 | } |
| 8010 | |
| 8011 | if (!atomic_read(&opinfo->breaking_cnt)) { |
| 8012 | rsp->hdr.Status = STATUS_UNSUCCESSFUL; |
| 8013 | goto err_out; |
| 8014 | } |
| 8015 | |
| 8016 | /* check for bad lease state */ |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 8017 | if (req->LeaseState & |
| 8018 | (~(SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_HANDLE_CACHING_LE))) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8019 | err = STATUS_INVALID_OPLOCK_PROTOCOL; |
| 8020 | if (lease->state & SMB2_LEASE_WRITE_CACHING_LE) |
| 8021 | lease_change_type = OPLOCK_WRITE_TO_NONE; |
| 8022 | else |
| 8023 | lease_change_type = OPLOCK_READ_TO_NONE; |
| 8024 | ksmbd_debug(OPLOCK, "handle bad lease state 0x%x -> 0x%x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 8025 | le32_to_cpu(lease->state), |
| 8026 | le32_to_cpu(req->LeaseState)); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 8027 | } else if (lease->state == SMB2_LEASE_READ_CACHING_LE && |
| 8028 | req->LeaseState != SMB2_LEASE_NONE_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8029 | err = STATUS_INVALID_OPLOCK_PROTOCOL; |
| 8030 | lease_change_type = OPLOCK_READ_TO_NONE; |
| 8031 | ksmbd_debug(OPLOCK, "handle bad lease state 0x%x -> 0x%x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 8032 | le32_to_cpu(lease->state), |
| 8033 | le32_to_cpu(req->LeaseState)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8034 | } else { |
| 8035 | /* valid lease state changes */ |
| 8036 | err = STATUS_INVALID_DEVICE_STATE; |
| 8037 | if (req->LeaseState == SMB2_LEASE_NONE_LE) { |
| 8038 | if (lease->state & SMB2_LEASE_WRITE_CACHING_LE) |
| 8039 | lease_change_type = OPLOCK_WRITE_TO_NONE; |
| 8040 | else |
| 8041 | lease_change_type = OPLOCK_READ_TO_NONE; |
| 8042 | } else if (req->LeaseState & SMB2_LEASE_READ_CACHING_LE) { |
| 8043 | if (lease->state & SMB2_LEASE_WRITE_CACHING_LE) |
| 8044 | lease_change_type = OPLOCK_WRITE_TO_READ; |
| 8045 | else |
| 8046 | lease_change_type = OPLOCK_READ_HANDLE_TO_READ; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 8047 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8048 | lease_change_type = 0; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 8049 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8050 | } |
| 8051 | |
| 8052 | switch (lease_change_type) { |
| 8053 | case OPLOCK_WRITE_TO_READ: |
| 8054 | ret = opinfo_write_to_read(opinfo); |
| 8055 | break; |
| 8056 | case OPLOCK_READ_HANDLE_TO_READ: |
| 8057 | ret = opinfo_read_handle_to_read(opinfo); |
| 8058 | break; |
| 8059 | case OPLOCK_WRITE_TO_NONE: |
| 8060 | ret = opinfo_write_to_none(opinfo); |
| 8061 | break; |
| 8062 | case OPLOCK_READ_TO_NONE: |
| 8063 | ret = opinfo_read_to_none(opinfo); |
| 8064 | break; |
| 8065 | default: |
| 8066 | ksmbd_debug(OPLOCK, "unknown lease change 0x%x -> 0x%x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 8067 | le32_to_cpu(lease->state), |
| 8068 | le32_to_cpu(req->LeaseState)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8069 | } |
| 8070 | |
| 8071 | lease_state = lease->state; |
| 8072 | opinfo->op_state = OPLOCK_STATE_NONE; |
| 8073 | wake_up_interruptible_all(&opinfo->oplock_q); |
| 8074 | atomic_dec(&opinfo->breaking_cnt); |
| 8075 | wake_up_interruptible_all(&opinfo->oplock_brk); |
| 8076 | opinfo_put(opinfo); |
| 8077 | |
| 8078 | if (ret < 0) { |
| 8079 | rsp->hdr.Status = err; |
| 8080 | goto err_out; |
| 8081 | } |
| 8082 | |
| 8083 | rsp->StructureSize = cpu_to_le16(36); |
| 8084 | rsp->Reserved = 0; |
| 8085 | rsp->Flags = 0; |
| 8086 | memcpy(rsp->LeaseKey, req->LeaseKey, 16); |
| 8087 | rsp->LeaseState = lease_state; |
| 8088 | rsp->LeaseDuration = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8089 | inc_rfc1001_len(work->response_buf, 36); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8090 | return; |
| 8091 | |
| 8092 | err_out: |
| 8093 | opinfo->op_state = OPLOCK_STATE_NONE; |
| 8094 | wake_up_interruptible_all(&opinfo->oplock_q); |
| 8095 | atomic_dec(&opinfo->breaking_cnt); |
| 8096 | wake_up_interruptible_all(&opinfo->oplock_brk); |
| 8097 | |
| 8098 | opinfo_put(opinfo); |
| 8099 | smb2_set_err_rsp(work); |
| 8100 | } |
| 8101 | |
| 8102 | /** |
| 8103 | * smb2_oplock_break() - dispatcher for smb2.0 and 2.1 oplock/lease break |
| 8104 | * @work: smb work containing oplock/lease break command buffer |
| 8105 | * |
| 8106 | * Return: 0 |
| 8107 | */ |
| 8108 | int smb2_oplock_break(struct ksmbd_work *work) |
| 8109 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8110 | struct smb2_oplock_break *req = smb2_get_msg(work->request_buf); |
| 8111 | struct smb2_oplock_break *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8112 | |
| 8113 | switch (le16_to_cpu(req->StructureSize)) { |
| 8114 | case OP_BREAK_STRUCT_SIZE_20: |
| 8115 | smb20_oplock_break_ack(work); |
| 8116 | break; |
| 8117 | case OP_BREAK_STRUCT_SIZE_21: |
| 8118 | smb21_lease_break_ack(work); |
| 8119 | break; |
| 8120 | default: |
| 8121 | ksmbd_debug(OPLOCK, "invalid break cmd %d\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 8122 | le16_to_cpu(req->StructureSize)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8123 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 8124 | smb2_set_err_rsp(work); |
| 8125 | } |
| 8126 | |
| 8127 | return 0; |
| 8128 | } |
| 8129 | |
| 8130 | /** |
| 8131 | * smb2_notify() - handler for smb2 notify request |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 8132 | * @work: smb work containing notify command buffer |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8133 | * |
| 8134 | * Return: 0 |
| 8135 | */ |
| 8136 | int smb2_notify(struct ksmbd_work *work) |
| 8137 | { |
Ronnie Sahlberg | 699230f | 2021-09-09 12:26:12 +0900 | [diff] [blame] | 8138 | struct smb2_change_notify_req *req; |
| 8139 | struct smb2_change_notify_rsp *rsp; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8140 | |
| 8141 | WORK_BUFFERS(work, req, rsp); |
| 8142 | |
| 8143 | if (work->next_smb2_rcv_hdr_off && req->hdr.NextCommand) { |
| 8144 | rsp->hdr.Status = STATUS_INTERNAL_ERROR; |
| 8145 | smb2_set_err_rsp(work); |
| 8146 | return 0; |
| 8147 | } |
| 8148 | |
| 8149 | smb2_set_err_rsp(work); |
| 8150 | rsp->hdr.Status = STATUS_NOT_IMPLEMENTED; |
| 8151 | return 0; |
| 8152 | } |
| 8153 | |
| 8154 | /** |
| 8155 | * smb2_is_sign_req() - handler for checking packet signing status |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 8156 | * @work: smb work containing notify command buffer |
| 8157 | * @command: SMB2 command id |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8158 | * |
| 8159 | * Return: true if packed is signed, false otherwise |
| 8160 | */ |
| 8161 | bool smb2_is_sign_req(struct ksmbd_work *work, unsigned int command) |
| 8162 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8163 | struct smb2_hdr *rcv_hdr2 = smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8164 | |
| 8165 | if ((rcv_hdr2->Flags & SMB2_FLAGS_SIGNED) && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 8166 | command != SMB2_NEGOTIATE_HE && |
| 8167 | command != SMB2_SESSION_SETUP_HE && |
| 8168 | command != SMB2_OPLOCK_BREAK_HE) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8169 | return true; |
| 8170 | |
kernel test robot | 5616015 | 2021-05-12 09:24:37 +0900 | [diff] [blame] | 8171 | return false; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8172 | } |
| 8173 | |
| 8174 | /** |
| 8175 | * smb2_check_sign_req() - handler for req packet sign processing |
| 8176 | * @work: smb work containing notify command buffer |
| 8177 | * |
| 8178 | * Return: 1 on success, 0 otherwise |
| 8179 | */ |
| 8180 | int smb2_check_sign_req(struct ksmbd_work *work) |
| 8181 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8182 | struct smb2_hdr *hdr; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8183 | char signature_req[SMB2_SIGNATURE_SIZE]; |
| 8184 | char signature[SMB2_HMACSHA256_SIZE]; |
| 8185 | struct kvec iov[1]; |
| 8186 | size_t len; |
| 8187 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8188 | hdr = smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8189 | if (work->next_smb2_rcv_hdr_off) |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 8190 | hdr = ksmbd_req_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8191 | |
| 8192 | if (!hdr->NextCommand && !work->next_smb2_rcv_hdr_off) |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8193 | len = get_rfc1002_len(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8194 | else if (hdr->NextCommand) |
| 8195 | len = le32_to_cpu(hdr->NextCommand); |
| 8196 | else |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8197 | len = get_rfc1002_len(work->request_buf) - |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8198 | work->next_smb2_rcv_hdr_off; |
| 8199 | |
| 8200 | memcpy(signature_req, hdr->Signature, SMB2_SIGNATURE_SIZE); |
| 8201 | memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE); |
| 8202 | |
| 8203 | iov[0].iov_base = (char *)&hdr->ProtocolId; |
| 8204 | iov[0].iov_len = len; |
| 8205 | |
| 8206 | 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] | 8207 | signature)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8208 | return 0; |
| 8209 | |
| 8210 | if (memcmp(signature, signature_req, SMB2_SIGNATURE_SIZE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 8211 | pr_err("bad smb2 signature\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8212 | return 0; |
| 8213 | } |
| 8214 | |
| 8215 | return 1; |
| 8216 | } |
| 8217 | |
| 8218 | /** |
| 8219 | * smb2_set_sign_rsp() - handler for rsp packet sign processing |
| 8220 | * @work: smb work containing notify command buffer |
| 8221 | * |
| 8222 | */ |
| 8223 | void smb2_set_sign_rsp(struct ksmbd_work *work) |
| 8224 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8225 | struct smb2_hdr *hdr; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8226 | struct smb2_hdr *req_hdr; |
| 8227 | char signature[SMB2_HMACSHA256_SIZE]; |
| 8228 | struct kvec iov[2]; |
| 8229 | size_t len; |
| 8230 | int n_vec = 1; |
| 8231 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8232 | hdr = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8233 | if (work->next_smb2_rsp_hdr_off) |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 8234 | hdr = ksmbd_resp_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8235 | |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 8236 | req_hdr = ksmbd_req_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8237 | |
| 8238 | if (!work->next_smb2_rsp_hdr_off) { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8239 | len = get_rfc1002_len(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8240 | if (req_hdr->NextCommand) |
| 8241 | len = ALIGN(len, 8); |
| 8242 | } else { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8243 | len = get_rfc1002_len(work->response_buf) - |
| 8244 | work->next_smb2_rsp_hdr_off; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8245 | len = ALIGN(len, 8); |
| 8246 | } |
| 8247 | |
| 8248 | if (req_hdr->NextCommand) |
| 8249 | hdr->NextCommand = cpu_to_le32(len); |
| 8250 | |
| 8251 | hdr->Flags |= SMB2_FLAGS_SIGNED; |
| 8252 | memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE); |
| 8253 | |
| 8254 | iov[0].iov_base = (char *)&hdr->ProtocolId; |
| 8255 | iov[0].iov_len = len; |
| 8256 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8257 | if (work->aux_payload_sz) { |
| 8258 | iov[0].iov_len -= work->aux_payload_sz; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8259 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8260 | iov[1].iov_base = work->aux_payload_buf; |
| 8261 | iov[1].iov_len = work->aux_payload_sz; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8262 | n_vec++; |
| 8263 | } |
| 8264 | |
| 8265 | 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] | 8266 | signature)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8267 | memcpy(hdr->Signature, signature, SMB2_SIGNATURE_SIZE); |
| 8268 | } |
| 8269 | |
| 8270 | /** |
| 8271 | * smb3_check_sign_req() - handler for req packet sign processing |
| 8272 | * @work: smb work containing notify command buffer |
| 8273 | * |
| 8274 | * Return: 1 on success, 0 otherwise |
| 8275 | */ |
| 8276 | int smb3_check_sign_req(struct ksmbd_work *work) |
| 8277 | { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 8278 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8279 | char *signing_key; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8280 | struct smb2_hdr *hdr; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8281 | struct channel *chann; |
| 8282 | char signature_req[SMB2_SIGNATURE_SIZE]; |
| 8283 | char signature[SMB2_CMACAES_SIZE]; |
| 8284 | struct kvec iov[1]; |
| 8285 | size_t len; |
| 8286 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8287 | hdr = smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8288 | if (work->next_smb2_rcv_hdr_off) |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 8289 | hdr = ksmbd_req_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8290 | |
| 8291 | if (!hdr->NextCommand && !work->next_smb2_rcv_hdr_off) |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8292 | len = get_rfc1002_len(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8293 | else if (hdr->NextCommand) |
| 8294 | len = le32_to_cpu(hdr->NextCommand); |
| 8295 | else |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8296 | len = get_rfc1002_len(work->request_buf) - |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8297 | work->next_smb2_rcv_hdr_off; |
| 8298 | |
| 8299 | if (le16_to_cpu(hdr->Command) == SMB2_SESSION_SETUP_HE) { |
| 8300 | signing_key = work->sess->smb3signingkey; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8301 | } else { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 8302 | chann = lookup_chann_list(work->sess, conn); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8303 | if (!chann) |
| 8304 | return 0; |
| 8305 | signing_key = chann->smb3signingkey; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8306 | } |
| 8307 | |
| 8308 | if (!signing_key) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 8309 | pr_err("SMB3 signing key is not generated\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8310 | return 0; |
| 8311 | } |
| 8312 | |
| 8313 | memcpy(signature_req, hdr->Signature, SMB2_SIGNATURE_SIZE); |
| 8314 | memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE); |
| 8315 | iov[0].iov_base = (char *)&hdr->ProtocolId; |
| 8316 | iov[0].iov_len = len; |
| 8317 | |
| 8318 | if (ksmbd_sign_smb3_pdu(conn, signing_key, iov, 1, signature)) |
| 8319 | return 0; |
| 8320 | |
| 8321 | if (memcmp(signature, signature_req, SMB2_SIGNATURE_SIZE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 8322 | pr_err("bad smb2 signature\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8323 | return 0; |
| 8324 | } |
| 8325 | |
| 8326 | return 1; |
| 8327 | } |
| 8328 | |
| 8329 | /** |
| 8330 | * smb3_set_sign_rsp() - handler for rsp packet sign processing |
| 8331 | * @work: smb work containing notify command buffer |
| 8332 | * |
| 8333 | */ |
| 8334 | void smb3_set_sign_rsp(struct ksmbd_work *work) |
| 8335 | { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 8336 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8337 | struct smb2_hdr *req_hdr, *hdr; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8338 | struct channel *chann; |
| 8339 | char signature[SMB2_CMACAES_SIZE]; |
| 8340 | struct kvec iov[2]; |
| 8341 | int n_vec = 1; |
| 8342 | size_t len; |
| 8343 | char *signing_key; |
| 8344 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8345 | hdr = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8346 | if (work->next_smb2_rsp_hdr_off) |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 8347 | hdr = ksmbd_resp_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8348 | |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 8349 | req_hdr = ksmbd_req_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8350 | |
| 8351 | if (!work->next_smb2_rsp_hdr_off) { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8352 | len = get_rfc1002_len(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8353 | if (req_hdr->NextCommand) |
| 8354 | len = ALIGN(len, 8); |
| 8355 | } else { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8356 | len = get_rfc1002_len(work->response_buf) - |
| 8357 | work->next_smb2_rsp_hdr_off; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8358 | len = ALIGN(len, 8); |
| 8359 | } |
| 8360 | |
Namjae Jeon | 08bdbc6 | 2021-07-27 09:30:29 +0900 | [diff] [blame] | 8361 | if (conn->binding == false && |
| 8362 | le16_to_cpu(hdr->Command) == SMB2_SESSION_SETUP_HE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8363 | signing_key = work->sess->smb3signingkey; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8364 | } else { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 8365 | chann = lookup_chann_list(work->sess, work->conn); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8366 | if (!chann) |
| 8367 | return; |
| 8368 | signing_key = chann->smb3signingkey; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8369 | } |
| 8370 | |
| 8371 | if (!signing_key) |
| 8372 | return; |
| 8373 | |
| 8374 | if (req_hdr->NextCommand) |
| 8375 | hdr->NextCommand = cpu_to_le32(len); |
| 8376 | |
| 8377 | hdr->Flags |= SMB2_FLAGS_SIGNED; |
| 8378 | memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE); |
| 8379 | iov[0].iov_base = (char *)&hdr->ProtocolId; |
| 8380 | iov[0].iov_len = len; |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8381 | if (work->aux_payload_sz) { |
| 8382 | iov[0].iov_len -= work->aux_payload_sz; |
| 8383 | iov[1].iov_base = work->aux_payload_buf; |
| 8384 | iov[1].iov_len = work->aux_payload_sz; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8385 | n_vec++; |
| 8386 | } |
| 8387 | |
| 8388 | if (!ksmbd_sign_smb3_pdu(conn, signing_key, iov, n_vec, signature)) |
| 8389 | memcpy(hdr->Signature, signature, SMB2_SIGNATURE_SIZE); |
| 8390 | } |
| 8391 | |
| 8392 | /** |
| 8393 | * smb3_preauth_hash_rsp() - handler for computing preauth hash on response |
| 8394 | * @work: smb work containing response buffer |
| 8395 | * |
| 8396 | */ |
| 8397 | void smb3_preauth_hash_rsp(struct ksmbd_work *work) |
| 8398 | { |
| 8399 | struct ksmbd_conn *conn = work->conn; |
| 8400 | struct ksmbd_session *sess = work->sess; |
| 8401 | struct smb2_hdr *req, *rsp; |
| 8402 | |
| 8403 | if (conn->dialect != SMB311_PROT_ID) |
| 8404 | return; |
| 8405 | |
| 8406 | WORK_BUFFERS(work, req, rsp); |
| 8407 | |
Namjae Jeon | 442ff9e | 2021-09-29 15:44:32 +0900 | [diff] [blame] | 8408 | if (le16_to_cpu(req->Command) == SMB2_NEGOTIATE_HE && |
| 8409 | conn->preauth_info) |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8410 | ksmbd_gen_preauth_integrity_hash(conn, work->response_buf, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 8411 | conn->preauth_info->Preauth_HashValue); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8412 | |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 8413 | if (le16_to_cpu(rsp->Command) == SMB2_SESSION_SETUP_HE && sess) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8414 | __u8 *hash_value; |
| 8415 | |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 8416 | if (conn->binding) { |
| 8417 | struct preauth_session *preauth_sess; |
| 8418 | |
| 8419 | preauth_sess = ksmbd_preauth_session_lookup(conn, sess->id); |
| 8420 | if (!preauth_sess) |
| 8421 | return; |
| 8422 | hash_value = preauth_sess->Preauth_HashValue; |
| 8423 | } else { |
| 8424 | hash_value = sess->Preauth_HashValue; |
| 8425 | if (!hash_value) |
| 8426 | return; |
| 8427 | } |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8428 | ksmbd_gen_preauth_integrity_hash(conn, work->response_buf, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 8429 | hash_value); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8430 | } |
| 8431 | } |
| 8432 | |
Namjae Jeon | 2dd9129 | 2021-11-03 08:25:54 +0900 | [diff] [blame] | 8433 | static void fill_transform_hdr(void *tr_buf, char *old_buf, __le16 cipher_type) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8434 | { |
Namjae Jeon | 2dd9129 | 2021-11-03 08:25:54 +0900 | [diff] [blame] | 8435 | struct smb2_transform_hdr *tr_hdr = tr_buf + 4; |
| 8436 | struct smb2_hdr *hdr = smb2_get_msg(old_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8437 | unsigned int orig_len = get_rfc1002_len(old_buf); |
| 8438 | |
Namjae Jeon | 2dd9129 | 2021-11-03 08:25:54 +0900 | [diff] [blame] | 8439 | memset(tr_buf, 0, sizeof(struct smb2_transform_hdr) + 4); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8440 | tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM; |
| 8441 | tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len); |
Ronnie Sahlberg | 4355a8f | 2021-11-03 08:43:42 +0900 | [diff] [blame] | 8442 | tr_hdr->Flags = cpu_to_le16(TRANSFORM_FLAG_ENCRYPTED); |
Namjae Jeon | 5a0ca77 | 2021-05-06 11:43:37 +0900 | [diff] [blame] | 8443 | if (cipher_type == SMB2_ENCRYPTION_AES128_GCM || |
| 8444 | cipher_type == SMB2_ENCRYPTION_AES256_GCM) |
| 8445 | get_random_bytes(&tr_hdr->Nonce, SMB3_AES_GCM_NONCE); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8446 | else |
Namjae Jeon | 5a0ca77 | 2021-05-06 11:43:37 +0900 | [diff] [blame] | 8447 | get_random_bytes(&tr_hdr->Nonce, SMB3_AES_CCM_NONCE); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8448 | memcpy(&tr_hdr->SessionId, &hdr->SessionId, 8); |
Namjae Jeon | 2dd9129 | 2021-11-03 08:25:54 +0900 | [diff] [blame] | 8449 | inc_rfc1001_len(tr_buf, sizeof(struct smb2_transform_hdr)); |
| 8450 | inc_rfc1001_len(tr_buf, orig_len); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8451 | } |
| 8452 | |
| 8453 | int smb3_encrypt_resp(struct ksmbd_work *work) |
| 8454 | { |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8455 | char *buf = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8456 | struct kvec iov[3]; |
| 8457 | int rc = -ENOMEM; |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8458 | 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] | 8459 | |
| 8460 | if (ARRAY_SIZE(iov) < rq_nvec) |
| 8461 | return -ENOMEM; |
| 8462 | |
Namjae Jeon | 2dd9129 | 2021-11-03 08:25:54 +0900 | [diff] [blame] | 8463 | work->tr_buf = kzalloc(sizeof(struct smb2_transform_hdr) + 4, GFP_KERNEL); |
| 8464 | if (!work->tr_buf) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8465 | return rc; |
| 8466 | |
| 8467 | /* fill transform header */ |
Namjae Jeon | 2dd9129 | 2021-11-03 08:25:54 +0900 | [diff] [blame] | 8468 | fill_transform_hdr(work->tr_buf, buf, work->conn->cipher_type); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8469 | |
Namjae Jeon | 2dd9129 | 2021-11-03 08:25:54 +0900 | [diff] [blame] | 8470 | iov[0].iov_base = work->tr_buf; |
| 8471 | iov[0].iov_len = sizeof(struct smb2_transform_hdr) + 4; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8472 | buf_size += iov[0].iov_len - 4; |
| 8473 | |
| 8474 | iov[1].iov_base = buf + 4; |
| 8475 | iov[1].iov_len = get_rfc1002_len(buf); |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8476 | if (work->aux_payload_sz) { |
| 8477 | iov[1].iov_len = work->resp_hdr_sz - 4; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8478 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8479 | iov[2].iov_base = work->aux_payload_buf; |
| 8480 | iov[2].iov_len = work->aux_payload_sz; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8481 | buf_size += iov[2].iov_len; |
| 8482 | } |
| 8483 | buf_size += iov[1].iov_len; |
| 8484 | work->resp_hdr_sz = iov[1].iov_len; |
| 8485 | |
| 8486 | rc = ksmbd_crypt_message(work->conn, iov, rq_nvec, 1); |
| 8487 | if (rc) |
| 8488 | return rc; |
| 8489 | |
| 8490 | memmove(buf, iov[1].iov_base, iov[1].iov_len); |
Namjae Jeon | 2dd9129 | 2021-11-03 08:25:54 +0900 | [diff] [blame] | 8491 | *(__be32 *)work->tr_buf = cpu_to_be32(buf_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8492 | |
| 8493 | return rc; |
| 8494 | } |
| 8495 | |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 8496 | bool smb3_is_transform_hdr(void *buf) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8497 | { |
Namjae Jeon | 2dd9129 | 2021-11-03 08:25:54 +0900 | [diff] [blame] | 8498 | struct smb2_transform_hdr *trhdr = smb2_get_msg(buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8499 | |
| 8500 | return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM; |
| 8501 | } |
| 8502 | |
| 8503 | int smb3_decrypt_req(struct ksmbd_work *work) |
| 8504 | { |
| 8505 | struct ksmbd_conn *conn = work->conn; |
| 8506 | struct ksmbd_session *sess; |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8507 | char *buf = work->request_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8508 | unsigned int pdu_length = get_rfc1002_len(buf); |
| 8509 | struct kvec iov[2]; |
Namjae Jeon | 2dd9129 | 2021-11-03 08:25:54 +0900 | [diff] [blame] | 8510 | int buf_data_size = pdu_length - sizeof(struct smb2_transform_hdr); |
| 8511 | struct smb2_transform_hdr *tr_hdr = smb2_get_msg(buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8512 | int rc = 0; |
| 8513 | |
Namjae Jeon | c7705ee | 2021-10-04 20:44:52 +0900 | [diff] [blame] | 8514 | if (buf_data_size < sizeof(struct smb2_hdr)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 8515 | pr_err("Transform message is too small (%u)\n", |
| 8516 | pdu_length); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8517 | return -ECONNABORTED; |
| 8518 | } |
| 8519 | |
Namjae Jeon | c7705ee | 2021-10-04 20:44:52 +0900 | [diff] [blame] | 8520 | if (buf_data_size < le32_to_cpu(tr_hdr->OriginalMessageSize)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 8521 | pr_err("Transform message is broken\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8522 | return -ECONNABORTED; |
| 8523 | } |
| 8524 | |
Namjae Jeon | 4227f81 | 2021-09-29 19:52:51 +0900 | [diff] [blame] | 8525 | sess = ksmbd_session_lookup_all(conn, le64_to_cpu(tr_hdr->SessionId)); |
| 8526 | if (!sess) { |
| 8527 | pr_err("invalid session id(%llx) in transform header\n", |
| 8528 | le64_to_cpu(tr_hdr->SessionId)); |
| 8529 | return -ECONNABORTED; |
| 8530 | } |
| 8531 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8532 | iov[0].iov_base = buf; |
Namjae Jeon | 2dd9129 | 2021-11-03 08:25:54 +0900 | [diff] [blame] | 8533 | iov[0].iov_len = sizeof(struct smb2_transform_hdr) + 4; |
| 8534 | iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr) + 4; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8535 | iov[1].iov_len = buf_data_size; |
| 8536 | rc = ksmbd_crypt_message(conn, iov, 2, 0); |
| 8537 | if (rc) |
| 8538 | return rc; |
| 8539 | |
| 8540 | memmove(buf + 4, iov[1].iov_base, buf_data_size); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8541 | *(__be32 *)buf = cpu_to_be32(buf_data_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8542 | |
| 8543 | return rc; |
| 8544 | } |
| 8545 | |
| 8546 | bool smb3_11_final_sess_setup_resp(struct ksmbd_work *work) |
| 8547 | { |
| 8548 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8549 | struct smb2_hdr *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8550 | |
| 8551 | if (conn->dialect < SMB30_PROT_ID) |
| 8552 | return false; |
| 8553 | |
| 8554 | if (work->next_smb2_rcv_hdr_off) |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 8555 | rsp = ksmbd_resp_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8556 | |
| 8557 | if (le16_to_cpu(rsp->Command) == SMB2_SESSION_SETUP_HE && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 8558 | rsp->Status == STATUS_SUCCESS) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8559 | return true; |
| 8560 | return false; |
| 8561 | } |