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)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 716 | attr = ATTR_DIRECTORY | |
| 717 | (attribute & (ATTR_HIDDEN | ATTR_SYSTEM)); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 718 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 719 | attr = (attribute & 0x00005137) | ATTR_ARCHIVE; |
| 720 | attr &= ~(ATTR_DIRECTORY); |
| 721 | if (S_ISREG(stat->mode) && (server_conf.share_fake_fscaps & |
| 722 | FILE_SUPPORTS_SPARSE_FILES)) |
| 723 | attr |= ATTR_SPARSE; |
| 724 | |
| 725 | if (smb2_get_reparse_tag_special_file(stat->mode)) |
| 726 | attr |= ATTR_REPARSE; |
| 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) || |
Marios Makassikis | 0d994cd | 2021-10-19 17:39:38 +0200 | [diff] [blame] | 1700 | negblob_len < offsetof(struct negotiate_message, NegotiateFlags)) |
| 1701 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1702 | |
Marios Makassikis | 0d994cd | 2021-10-19 17:39:38 +0200 | [diff] [blame] | 1703 | negblob = (struct negotiate_message *)((char *)&req->hdr.ProtocolId + |
| 1704 | negblob_off); |
| 1705 | |
| 1706 | if (decode_negotiation_token(conn, negblob, negblob_len) == 0) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1707 | if (conn->mechToken) |
| 1708 | negblob = (struct negotiate_message *)conn->mechToken; |
| 1709 | } |
| 1710 | |
| 1711 | if (server_conf.auth_mechs & conn->auth_mechs) { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1712 | rc = generate_preauth_hash(work); |
| 1713 | if (rc) |
| 1714 | goto out_err; |
| 1715 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1716 | if (conn->preferred_auth_mech & |
| 1717 | (KSMBD_AUTH_KRB5 | KSMBD_AUTH_MSKRB5)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1718 | rc = krb5_authenticate(work); |
| 1719 | if (rc) { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1720 | rc = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1721 | goto out_err; |
| 1722 | } |
| 1723 | |
| 1724 | ksmbd_conn_set_good(work); |
| 1725 | sess->state = SMB2_SESSION_VALID; |
Muhammad Usama Anjum | 822bc8e | 2021-04-02 09:25:35 +0900 | [diff] [blame] | 1726 | kfree(sess->Preauth_HashValue); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1727 | sess->Preauth_HashValue = NULL; |
| 1728 | } else if (conn->preferred_auth_mech == KSMBD_AUTH_NTLMSSP) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1729 | if (negblob->MessageType == NtLmNegotiate) { |
Marios Makassikis | 0d994cd | 2021-10-19 17:39:38 +0200 | [diff] [blame] | 1730 | rc = ntlm_negotiate(work, negblob, negblob_len); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1731 | if (rc) |
| 1732 | goto out_err; |
| 1733 | rsp->hdr.Status = |
| 1734 | STATUS_MORE_PROCESSING_REQUIRED; |
| 1735 | /* |
| 1736 | * Note: here total size -1 is done as an |
| 1737 | * adjustment for 0 size blob |
| 1738 | */ |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1739 | inc_rfc1001_len(work->response_buf, |
| 1740 | le16_to_cpu(rsp->SecurityBufferLength) - 1); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1741 | |
| 1742 | } else if (negblob->MessageType == NtLmAuthenticate) { |
| 1743 | rc = ntlm_authenticate(work); |
| 1744 | if (rc) |
| 1745 | goto out_err; |
| 1746 | |
| 1747 | ksmbd_conn_set_good(work); |
| 1748 | sess->state = SMB2_SESSION_VALID; |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1749 | if (conn->binding) { |
| 1750 | struct preauth_session *preauth_sess; |
| 1751 | |
| 1752 | preauth_sess = |
| 1753 | ksmbd_preauth_session_lookup(conn, sess->id); |
| 1754 | if (preauth_sess) { |
| 1755 | list_del(&preauth_sess->preauth_entry); |
| 1756 | kfree(preauth_sess); |
| 1757 | } |
| 1758 | } |
Muhammad Usama Anjum | 822bc8e | 2021-04-02 09:25:35 +0900 | [diff] [blame] | 1759 | kfree(sess->Preauth_HashValue); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1760 | sess->Preauth_HashValue = NULL; |
| 1761 | } |
| 1762 | } else { |
| 1763 | /* TODO: need one more negotiation */ |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 1764 | pr_err("Not support the preferred authentication\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1765 | rc = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1766 | } |
| 1767 | } else { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 1768 | pr_err("Not support authentication\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1769 | rc = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1770 | } |
| 1771 | |
| 1772 | out_err: |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1773 | if (rc == -EINVAL) |
| 1774 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 1775 | else if (rc == -ENOENT) |
| 1776 | rsp->hdr.Status = STATUS_USER_SESSION_DELETED; |
| 1777 | else if (rc == -EACCES) |
| 1778 | rsp->hdr.Status = STATUS_REQUEST_NOT_ACCEPTED; |
| 1779 | else if (rc == -EFAULT) |
| 1780 | rsp->hdr.Status = STATUS_NETWORK_SESSION_EXPIRED; |
Namjae Jeon | 58090b1 | 2021-07-16 14:52:09 +0900 | [diff] [blame] | 1781 | else if (rc == -ENOMEM) |
| 1782 | rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES; |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1783 | else if (rc) |
| 1784 | rsp->hdr.Status = STATUS_LOGON_FAILURE; |
| 1785 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1786 | if (conn->use_spnego && conn->mechToken) { |
| 1787 | kfree(conn->mechToken); |
| 1788 | conn->mechToken = NULL; |
| 1789 | } |
| 1790 | |
Namjae Jeon | 621be84 | 2021-10-13 17:28:31 +0900 | [diff] [blame] | 1791 | if (rc < 0) { |
| 1792 | /* |
| 1793 | * SecurityBufferOffset should be set to zero |
| 1794 | * in session setup error response. |
| 1795 | */ |
| 1796 | rsp->SecurityBufferOffset = 0; |
| 1797 | |
| 1798 | if (sess) { |
| 1799 | bool try_delay = false; |
| 1800 | |
| 1801 | /* |
| 1802 | * To avoid dictionary attacks (repeated session setups rapidly sent) to |
| 1803 | * connect to server, ksmbd make a delay of a 5 seconds on session setup |
| 1804 | * failure to make it harder to send enough random connection requests |
| 1805 | * to break into a server. |
| 1806 | */ |
| 1807 | if (sess->user && sess->user->flags & KSMBD_USER_FLAG_DELAY_SESSION) |
| 1808 | try_delay = true; |
| 1809 | |
| 1810 | ksmbd_session_destroy(sess); |
| 1811 | work->sess = NULL; |
| 1812 | if (try_delay) |
| 1813 | ssleep(5); |
| 1814 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1815 | } |
| 1816 | |
| 1817 | return rc; |
| 1818 | } |
| 1819 | |
| 1820 | /** |
| 1821 | * smb2_tree_connect() - handler for smb2 tree connect command |
| 1822 | * @work: smb work containing smb request buffer |
| 1823 | * |
| 1824 | * Return: 0 on success, otherwise error |
| 1825 | */ |
| 1826 | int smb2_tree_connect(struct ksmbd_work *work) |
| 1827 | { |
| 1828 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1829 | struct smb2_tree_connect_req *req = smb2_get_msg(work->request_buf); |
| 1830 | struct smb2_tree_connect_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1831 | struct ksmbd_session *sess = work->sess; |
| 1832 | char *treename = NULL, *name = NULL; |
| 1833 | struct ksmbd_tree_conn_status status; |
| 1834 | struct ksmbd_share_config *share; |
| 1835 | int rc = -EINVAL; |
| 1836 | |
| 1837 | treename = smb_strndup_from_utf16(req->Buffer, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1838 | le16_to_cpu(req->PathLength), true, |
| 1839 | conn->local_nls); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1840 | if (IS_ERR(treename)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 1841 | pr_err("treename is NULL\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1842 | status.ret = KSMBD_TREE_CONN_STATUS_ERROR; |
| 1843 | goto out_err1; |
| 1844 | } |
| 1845 | |
Stephen Rothwell | 36ba386 | 2021-03-17 17:01:15 +0900 | [diff] [blame] | 1846 | name = ksmbd_extract_sharename(treename); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1847 | if (IS_ERR(name)) { |
| 1848 | status.ret = KSMBD_TREE_CONN_STATUS_ERROR; |
| 1849 | goto out_err1; |
| 1850 | } |
| 1851 | |
| 1852 | ksmbd_debug(SMB, "tree connect request for tree %s treename %s\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1853 | name, treename); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1854 | |
| 1855 | status = ksmbd_tree_conn_connect(sess, name); |
| 1856 | if (status.ret == KSMBD_TREE_CONN_STATUS_OK) |
| 1857 | rsp->hdr.Id.SyncId.TreeId = cpu_to_le32(status.tree_conn->id); |
| 1858 | else |
| 1859 | goto out_err1; |
| 1860 | |
| 1861 | share = status.tree_conn->share_conf; |
| 1862 | if (test_share_config_flag(share, KSMBD_SHARE_FLAG_PIPE)) { |
| 1863 | ksmbd_debug(SMB, "IPC share path request\n"); |
| 1864 | rsp->ShareType = SMB2_SHARE_TYPE_PIPE; |
| 1865 | rsp->MaximalAccess = FILE_READ_DATA_LE | FILE_READ_EA_LE | |
| 1866 | FILE_EXECUTE_LE | FILE_READ_ATTRIBUTES_LE | |
| 1867 | FILE_DELETE_LE | FILE_READ_CONTROL_LE | |
| 1868 | FILE_WRITE_DAC_LE | FILE_WRITE_OWNER_LE | |
| 1869 | FILE_SYNCHRONIZE_LE; |
| 1870 | } else { |
| 1871 | rsp->ShareType = SMB2_SHARE_TYPE_DISK; |
| 1872 | rsp->MaximalAccess = FILE_READ_DATA_LE | FILE_READ_EA_LE | |
| 1873 | FILE_EXECUTE_LE | FILE_READ_ATTRIBUTES_LE; |
| 1874 | if (test_tree_conn_flag(status.tree_conn, |
| 1875 | KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
| 1876 | rsp->MaximalAccess |= FILE_WRITE_DATA_LE | |
| 1877 | FILE_APPEND_DATA_LE | FILE_WRITE_EA_LE | |
Wan Jiabing | 3aefd54 | 2021-06-07 12:54:32 +0800 | [diff] [blame] | 1878 | FILE_DELETE_LE | FILE_WRITE_ATTRIBUTES_LE | |
| 1879 | FILE_DELETE_CHILD_LE | FILE_READ_CONTROL_LE | |
| 1880 | FILE_WRITE_DAC_LE | FILE_WRITE_OWNER_LE | |
| 1881 | FILE_SYNCHRONIZE_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1882 | } |
| 1883 | } |
| 1884 | |
| 1885 | status.tree_conn->maximal_access = le32_to_cpu(rsp->MaximalAccess); |
| 1886 | if (conn->posix_ext_supported) |
| 1887 | status.tree_conn->posix_extensions = true; |
| 1888 | |
| 1889 | out_err1: |
| 1890 | rsp->StructureSize = cpu_to_le16(16); |
| 1891 | rsp->Capabilities = 0; |
| 1892 | rsp->Reserved = 0; |
| 1893 | /* default manual caching */ |
| 1894 | rsp->ShareFlags = SMB2_SHAREFLAG_MANUAL_CACHING; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1895 | inc_rfc1001_len(work->response_buf, 16); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1896 | |
| 1897 | if (!IS_ERR(treename)) |
| 1898 | kfree(treename); |
| 1899 | if (!IS_ERR(name)) |
| 1900 | kfree(name); |
| 1901 | |
| 1902 | switch (status.ret) { |
| 1903 | case KSMBD_TREE_CONN_STATUS_OK: |
| 1904 | rsp->hdr.Status = STATUS_SUCCESS; |
| 1905 | rc = 0; |
| 1906 | break; |
| 1907 | case KSMBD_TREE_CONN_STATUS_NO_SHARE: |
| 1908 | rsp->hdr.Status = STATUS_BAD_NETWORK_PATH; |
| 1909 | break; |
| 1910 | case -ENOMEM: |
| 1911 | case KSMBD_TREE_CONN_STATUS_NOMEM: |
| 1912 | rsp->hdr.Status = STATUS_NO_MEMORY; |
| 1913 | break; |
| 1914 | case KSMBD_TREE_CONN_STATUS_ERROR: |
| 1915 | case KSMBD_TREE_CONN_STATUS_TOO_MANY_CONNS: |
| 1916 | case KSMBD_TREE_CONN_STATUS_TOO_MANY_SESSIONS: |
| 1917 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 1918 | break; |
| 1919 | case -EINVAL: |
| 1920 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 1921 | break; |
| 1922 | default: |
| 1923 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 1924 | } |
| 1925 | |
| 1926 | return rc; |
| 1927 | } |
| 1928 | |
| 1929 | /** |
| 1930 | * smb2_create_open_flags() - convert smb open flags to unix open flags |
| 1931 | * @file_present: is file already present |
| 1932 | * @access: file access flags |
| 1933 | * @disposition: file disposition flags |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 1934 | * @may_flags: set with MAY_ flags |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1935 | * |
| 1936 | * Return: file open flags |
| 1937 | */ |
| 1938 | static int smb2_create_open_flags(bool file_present, __le32 access, |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 1939 | __le32 disposition, |
| 1940 | int *may_flags) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1941 | { |
| 1942 | int oflags = O_NONBLOCK | O_LARGEFILE; |
| 1943 | |
| 1944 | if (access & FILE_READ_DESIRED_ACCESS_LE && |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 1945 | access & FILE_WRITE_DESIRE_ACCESS_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1946 | oflags |= O_RDWR; |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 1947 | *may_flags = MAY_OPEN | MAY_READ | MAY_WRITE; |
| 1948 | } else if (access & FILE_WRITE_DESIRE_ACCESS_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1949 | oflags |= O_WRONLY; |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 1950 | *may_flags = MAY_OPEN | MAY_WRITE; |
| 1951 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1952 | oflags |= O_RDONLY; |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 1953 | *may_flags = MAY_OPEN | MAY_READ; |
| 1954 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1955 | |
| 1956 | if (access == FILE_READ_ATTRIBUTES_LE) |
| 1957 | oflags |= O_PATH; |
| 1958 | |
| 1959 | if (file_present) { |
| 1960 | switch (disposition & FILE_CREATE_MASK_LE) { |
| 1961 | case FILE_OPEN_LE: |
| 1962 | case FILE_CREATE_LE: |
| 1963 | break; |
| 1964 | case FILE_SUPERSEDE_LE: |
| 1965 | case FILE_OVERWRITE_LE: |
| 1966 | case FILE_OVERWRITE_IF_LE: |
| 1967 | oflags |= O_TRUNC; |
| 1968 | break; |
| 1969 | default: |
| 1970 | break; |
| 1971 | } |
| 1972 | } else { |
| 1973 | switch (disposition & FILE_CREATE_MASK_LE) { |
| 1974 | case FILE_SUPERSEDE_LE: |
| 1975 | case FILE_CREATE_LE: |
| 1976 | case FILE_OPEN_IF_LE: |
| 1977 | case FILE_OVERWRITE_IF_LE: |
| 1978 | oflags |= O_CREAT; |
| 1979 | break; |
| 1980 | case FILE_OPEN_LE: |
| 1981 | case FILE_OVERWRITE_LE: |
| 1982 | oflags &= ~O_CREAT; |
| 1983 | break; |
| 1984 | default: |
| 1985 | break; |
| 1986 | } |
| 1987 | } |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 1988 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1989 | return oflags; |
| 1990 | } |
| 1991 | |
| 1992 | /** |
| 1993 | * smb2_tree_disconnect() - handler for smb tree connect request |
| 1994 | * @work: smb work containing request buffer |
| 1995 | * |
| 1996 | * Return: 0 |
| 1997 | */ |
| 1998 | int smb2_tree_disconnect(struct ksmbd_work *work) |
| 1999 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 2000 | struct smb2_tree_disconnect_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2001 | struct ksmbd_session *sess = work->sess; |
| 2002 | struct ksmbd_tree_connect *tcon = work->tcon; |
| 2003 | |
| 2004 | rsp->StructureSize = cpu_to_le16(4); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 2005 | inc_rfc1001_len(work->response_buf, 4); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2006 | |
| 2007 | ksmbd_debug(SMB, "request\n"); |
| 2008 | |
| 2009 | if (!tcon) { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 2010 | struct smb2_tree_disconnect_req *req = |
| 2011 | smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2012 | |
| 2013 | ksmbd_debug(SMB, "Invalid tid %d\n", req->hdr.Id.SyncId.TreeId); |
| 2014 | rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED; |
| 2015 | smb2_set_err_rsp(work); |
| 2016 | return 0; |
| 2017 | } |
| 2018 | |
| 2019 | ksmbd_close_tree_conn_fds(work); |
| 2020 | ksmbd_tree_conn_disconnect(sess, tcon); |
| 2021 | return 0; |
| 2022 | } |
| 2023 | |
| 2024 | /** |
| 2025 | * smb2_session_logoff() - handler for session log off request |
| 2026 | * @work: smb work containing request buffer |
| 2027 | * |
| 2028 | * Return: 0 |
| 2029 | */ |
| 2030 | int smb2_session_logoff(struct ksmbd_work *work) |
| 2031 | { |
| 2032 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 2033 | struct smb2_logoff_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2034 | struct ksmbd_session *sess = work->sess; |
| 2035 | |
| 2036 | rsp->StructureSize = cpu_to_le16(4); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 2037 | inc_rfc1001_len(work->response_buf, 4); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2038 | |
| 2039 | ksmbd_debug(SMB, "request\n"); |
| 2040 | |
| 2041 | /* Got a valid session, set connection state */ |
| 2042 | WARN_ON(sess->conn != conn); |
| 2043 | |
| 2044 | /* setting CifsExiting here may race with start_tcp_sess */ |
| 2045 | ksmbd_conn_set_need_reconnect(work); |
| 2046 | ksmbd_close_session_fds(work); |
| 2047 | ksmbd_conn_wait_idle(conn); |
| 2048 | |
| 2049 | if (ksmbd_tree_conn_session_logoff(sess)) { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 2050 | struct smb2_logoff_req *req = smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2051 | |
| 2052 | ksmbd_debug(SMB, "Invalid tid %d\n", req->hdr.Id.SyncId.TreeId); |
| 2053 | rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED; |
| 2054 | smb2_set_err_rsp(work); |
| 2055 | return 0; |
| 2056 | } |
| 2057 | |
| 2058 | ksmbd_destroy_file_table(&sess->file_table); |
| 2059 | sess->state = SMB2_SESSION_EXPIRED; |
| 2060 | |
| 2061 | ksmbd_free_user(sess->user); |
| 2062 | sess->user = NULL; |
| 2063 | |
| 2064 | /* let start_tcp_sess free connection info now */ |
| 2065 | ksmbd_conn_set_need_negotiate(work); |
| 2066 | return 0; |
| 2067 | } |
| 2068 | |
| 2069 | /** |
| 2070 | * create_smb2_pipe() - create IPC pipe |
| 2071 | * @work: smb work containing request buffer |
| 2072 | * |
| 2073 | * Return: 0 on success, otherwise error |
| 2074 | */ |
| 2075 | static noinline int create_smb2_pipe(struct ksmbd_work *work) |
| 2076 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 2077 | struct smb2_create_rsp *rsp = smb2_get_msg(work->response_buf); |
| 2078 | struct smb2_create_req *req = smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2079 | int id; |
| 2080 | int err; |
| 2081 | char *name; |
| 2082 | |
| 2083 | name = smb_strndup_from_utf16(req->Buffer, le16_to_cpu(req->NameLength), |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2084 | 1, work->conn->local_nls); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2085 | if (IS_ERR(name)) { |
| 2086 | rsp->hdr.Status = STATUS_NO_MEMORY; |
| 2087 | err = PTR_ERR(name); |
| 2088 | goto out; |
| 2089 | } |
| 2090 | |
| 2091 | id = ksmbd_session_rpc_open(work->sess, name); |
Marios Makassikis | 79caa96 | 2021-05-06 11:38:35 +0900 | [diff] [blame] | 2092 | if (id < 0) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2093 | pr_err("Unable to open RPC pipe: %d\n", id); |
Marios Makassikis | 79caa96 | 2021-05-06 11:38:35 +0900 | [diff] [blame] | 2094 | err = id; |
| 2095 | goto out; |
| 2096 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2097 | |
Marios Makassikis | 79caa96 | 2021-05-06 11:38:35 +0900 | [diff] [blame] | 2098 | rsp->hdr.Status = STATUS_SUCCESS; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2099 | rsp->StructureSize = cpu_to_le16(89); |
| 2100 | rsp->OplockLevel = SMB2_OPLOCK_LEVEL_NONE; |
| 2101 | rsp->Reserved = 0; |
| 2102 | rsp->CreateAction = cpu_to_le32(FILE_OPENED); |
| 2103 | |
| 2104 | rsp->CreationTime = cpu_to_le64(0); |
| 2105 | rsp->LastAccessTime = cpu_to_le64(0); |
| 2106 | rsp->ChangeTime = cpu_to_le64(0); |
| 2107 | rsp->AllocationSize = cpu_to_le64(0); |
| 2108 | rsp->EndofFile = cpu_to_le64(0); |
| 2109 | rsp->FileAttributes = ATTR_NORMAL_LE; |
| 2110 | rsp->Reserved2 = 0; |
| 2111 | rsp->VolatileFileId = cpu_to_le64(id); |
| 2112 | rsp->PersistentFileId = 0; |
| 2113 | rsp->CreateContextsOffset = 0; |
| 2114 | rsp->CreateContextsLength = 0; |
| 2115 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 2116 | inc_rfc1001_len(work->response_buf, 88); /* StructureSize - 1*/ |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2117 | kfree(name); |
| 2118 | return 0; |
| 2119 | |
| 2120 | out: |
Marios Makassikis | 79caa96 | 2021-05-06 11:38:35 +0900 | [diff] [blame] | 2121 | switch (err) { |
| 2122 | case -EINVAL: |
| 2123 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 2124 | break; |
| 2125 | case -ENOSPC: |
| 2126 | case -ENOMEM: |
| 2127 | rsp->hdr.Status = STATUS_NO_MEMORY; |
| 2128 | break; |
| 2129 | } |
| 2130 | |
| 2131 | if (!IS_ERR(name)) |
| 2132 | kfree(name); |
| 2133 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2134 | smb2_set_err_rsp(work); |
| 2135 | return err; |
| 2136 | } |
| 2137 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2138 | /** |
| 2139 | * smb2_set_ea() - handler for setting extended attributes using set |
| 2140 | * info command |
| 2141 | * @eabuf: set info command buffer |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 2142 | * @buf_len: set info command buffer length |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2143 | * @path: dentry path for get ea |
| 2144 | * |
| 2145 | * Return: 0 on success, otherwise error |
| 2146 | */ |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 2147 | static int smb2_set_ea(struct smb2_ea_info *eabuf, unsigned int buf_len, |
| 2148 | struct path *path) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2149 | { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2150 | struct user_namespace *user_ns = mnt_user_ns(path->mnt); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2151 | char *attr_name = NULL, *value; |
| 2152 | int rc = 0; |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 2153 | unsigned int next = 0; |
| 2154 | |
| 2155 | if (buf_len < sizeof(struct smb2_ea_info) + eabuf->EaNameLength + |
| 2156 | le16_to_cpu(eabuf->EaValueLength)) |
| 2157 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2158 | |
| 2159 | attr_name = kmalloc(XATTR_NAME_MAX + 1, GFP_KERNEL); |
| 2160 | if (!attr_name) |
| 2161 | return -ENOMEM; |
| 2162 | |
| 2163 | do { |
| 2164 | if (!eabuf->EaNameLength) |
| 2165 | goto next; |
| 2166 | |
| 2167 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2168 | "name : <%s>, name_len : %u, value_len : %u, next : %u\n", |
| 2169 | eabuf->name, eabuf->EaNameLength, |
| 2170 | le16_to_cpu(eabuf->EaValueLength), |
| 2171 | le32_to_cpu(eabuf->NextEntryOffset)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2172 | |
| 2173 | if (eabuf->EaNameLength > |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2174 | (XATTR_NAME_MAX - XATTR_USER_PREFIX_LEN)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2175 | rc = -EINVAL; |
| 2176 | break; |
| 2177 | } |
| 2178 | |
| 2179 | memcpy(attr_name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN); |
| 2180 | memcpy(&attr_name[XATTR_USER_PREFIX_LEN], eabuf->name, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2181 | eabuf->EaNameLength); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2182 | attr_name[XATTR_USER_PREFIX_LEN + eabuf->EaNameLength] = '\0'; |
| 2183 | value = (char *)&eabuf->name + eabuf->EaNameLength + 1; |
| 2184 | |
| 2185 | if (!eabuf->EaValueLength) { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2186 | rc = ksmbd_vfs_casexattr_len(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2187 | path->dentry, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2188 | attr_name, |
| 2189 | XATTR_USER_PREFIX_LEN + |
| 2190 | eabuf->EaNameLength); |
| 2191 | |
| 2192 | /* delete the EA only when it exits */ |
| 2193 | if (rc > 0) { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2194 | rc = ksmbd_vfs_remove_xattr(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2195 | path->dentry, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2196 | attr_name); |
| 2197 | |
| 2198 | if (rc < 0) { |
| 2199 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2200 | "remove xattr failed(%d)\n", |
| 2201 | rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2202 | break; |
| 2203 | } |
| 2204 | } |
| 2205 | |
| 2206 | /* if the EA doesn't exist, just do nothing. */ |
| 2207 | rc = 0; |
| 2208 | } else { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2209 | rc = ksmbd_vfs_setxattr(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2210 | path->dentry, attr_name, value, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2211 | le16_to_cpu(eabuf->EaValueLength), 0); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2212 | if (rc < 0) { |
| 2213 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2214 | "ksmbd_vfs_setxattr is failed(%d)\n", |
| 2215 | rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2216 | break; |
| 2217 | } |
| 2218 | } |
| 2219 | |
| 2220 | next: |
| 2221 | next = le32_to_cpu(eabuf->NextEntryOffset); |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 2222 | if (next == 0 || buf_len < next) |
| 2223 | break; |
| 2224 | buf_len -= next; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2225 | eabuf = (struct smb2_ea_info *)((char *)eabuf + next); |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 2226 | if (next < (u32)eabuf->EaNameLength + le16_to_cpu(eabuf->EaValueLength)) |
| 2227 | break; |
| 2228 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2229 | } while (next != 0); |
| 2230 | |
| 2231 | kfree(attr_name); |
| 2232 | return rc; |
| 2233 | } |
| 2234 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2235 | static noinline int smb2_set_stream_name_xattr(struct path *path, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2236 | struct ksmbd_file *fp, |
| 2237 | char *stream_name, int s_type) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2238 | { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2239 | struct user_namespace *user_ns = mnt_user_ns(path->mnt); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2240 | size_t xattr_stream_size; |
| 2241 | char *xattr_stream_name; |
| 2242 | int rc; |
| 2243 | |
| 2244 | rc = ksmbd_vfs_xattr_stream_name(stream_name, |
| 2245 | &xattr_stream_name, |
| 2246 | &xattr_stream_size, |
| 2247 | s_type); |
| 2248 | if (rc) |
| 2249 | return rc; |
| 2250 | |
| 2251 | fp->stream.name = xattr_stream_name; |
| 2252 | fp->stream.size = xattr_stream_size; |
| 2253 | |
| 2254 | /* Check if there is stream prefix in xattr space */ |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2255 | rc = ksmbd_vfs_casexattr_len(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2256 | path->dentry, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2257 | xattr_stream_name, |
| 2258 | xattr_stream_size); |
| 2259 | if (rc >= 0) |
| 2260 | return 0; |
| 2261 | |
| 2262 | if (fp->cdoption == FILE_OPEN_LE) { |
| 2263 | ksmbd_debug(SMB, "XATTR stream name lookup failed: %d\n", rc); |
| 2264 | return -EBADF; |
| 2265 | } |
| 2266 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2267 | rc = ksmbd_vfs_setxattr(user_ns, path->dentry, |
| 2268 | xattr_stream_name, NULL, 0, 0); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2269 | if (rc < 0) |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2270 | pr_err("Failed to store XATTR stream name :%d\n", rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2271 | return 0; |
| 2272 | } |
| 2273 | |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 2274 | static int smb2_remove_smb_xattrs(struct path *path) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2275 | { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2276 | struct user_namespace *user_ns = mnt_user_ns(path->mnt); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2277 | char *name, *xattr_list = NULL; |
| 2278 | ssize_t xattr_list_len; |
| 2279 | int err = 0; |
| 2280 | |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 2281 | xattr_list_len = ksmbd_vfs_listxattr(path->dentry, &xattr_list); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2282 | if (xattr_list_len < 0) { |
| 2283 | goto out; |
| 2284 | } else if (!xattr_list_len) { |
| 2285 | ksmbd_debug(SMB, "empty xattr in the file\n"); |
| 2286 | goto out; |
| 2287 | } |
| 2288 | |
| 2289 | for (name = xattr_list; name - xattr_list < xattr_list_len; |
| 2290 | name += strlen(name) + 1) { |
| 2291 | ksmbd_debug(SMB, "%s, len %zd\n", name, strlen(name)); |
| 2292 | |
| 2293 | if (strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2294 | strncmp(&name[XATTR_USER_PREFIX_LEN], DOS_ATTRIBUTE_PREFIX, |
| 2295 | DOS_ATTRIBUTE_PREFIX_LEN) && |
| 2296 | strncmp(&name[XATTR_USER_PREFIX_LEN], STREAM_PREFIX, STREAM_PREFIX_LEN)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2297 | continue; |
| 2298 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2299 | err = ksmbd_vfs_remove_xattr(user_ns, path->dentry, name); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2300 | if (err) |
| 2301 | ksmbd_debug(SMB, "remove xattr failed : %s\n", name); |
| 2302 | } |
| 2303 | out: |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 2304 | kvfree(xattr_list); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2305 | return err; |
| 2306 | } |
| 2307 | |
| 2308 | static int smb2_create_truncate(struct path *path) |
| 2309 | { |
| 2310 | int rc = vfs_truncate(path, 0); |
| 2311 | |
| 2312 | if (rc) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2313 | pr_err("vfs_truncate failed, rc %d\n", rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2314 | return rc; |
| 2315 | } |
| 2316 | |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 2317 | rc = smb2_remove_smb_xattrs(path); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2318 | if (rc == -EOPNOTSUPP) |
| 2319 | rc = 0; |
| 2320 | if (rc) |
| 2321 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2322 | "ksmbd_truncate_stream_name_xattr failed, rc %d\n", |
| 2323 | rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2324 | return rc; |
| 2325 | } |
| 2326 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2327 | 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] | 2328 | struct ksmbd_file *fp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2329 | { |
| 2330 | struct xattr_dos_attrib da = {0}; |
| 2331 | int rc; |
| 2332 | |
| 2333 | if (!test_share_config_flag(tcon->share_conf, |
| 2334 | KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) |
| 2335 | return; |
| 2336 | |
| 2337 | da.version = 4; |
| 2338 | da.attr = le32_to_cpu(fp->f_ci->m_fattr); |
| 2339 | da.itime = da.create_time = fp->create_time; |
| 2340 | da.flags = XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME | |
| 2341 | XATTR_DOSINFO_ITIME; |
| 2342 | |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2343 | rc = ksmbd_vfs_set_dos_attrib_xattr(mnt_user_ns(path->mnt), |
| 2344 | path->dentry, &da); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2345 | if (rc) |
| 2346 | ksmbd_debug(SMB, "failed to store file attribute into xattr\n"); |
| 2347 | } |
| 2348 | |
| 2349 | static void smb2_update_xattrs(struct ksmbd_tree_connect *tcon, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2350 | struct path *path, struct ksmbd_file *fp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2351 | { |
| 2352 | struct xattr_dos_attrib da; |
| 2353 | int rc; |
| 2354 | |
| 2355 | fp->f_ci->m_fattr &= ~(ATTR_HIDDEN_LE | ATTR_SYSTEM_LE); |
| 2356 | |
| 2357 | /* get FileAttributes from XATTR_NAME_DOS_ATTRIBUTE */ |
| 2358 | if (!test_share_config_flag(tcon->share_conf, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2359 | KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2360 | return; |
| 2361 | |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2362 | rc = ksmbd_vfs_get_dos_attrib_xattr(mnt_user_ns(path->mnt), |
| 2363 | path->dentry, &da); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2364 | if (rc > 0) { |
| 2365 | fp->f_ci->m_fattr = cpu_to_le32(da.attr); |
| 2366 | fp->create_time = da.create_time; |
| 2367 | fp->itime = da.itime; |
| 2368 | } |
| 2369 | } |
| 2370 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2371 | 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] | 2372 | int open_flags, umode_t posix_mode, bool is_dir) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2373 | { |
| 2374 | struct ksmbd_tree_connect *tcon = work->tcon; |
| 2375 | struct ksmbd_share_config *share = tcon->share_conf; |
| 2376 | umode_t mode; |
| 2377 | int rc; |
| 2378 | |
| 2379 | if (!(open_flags & O_CREAT)) |
| 2380 | return -EBADF; |
| 2381 | |
| 2382 | ksmbd_debug(SMB, "file does not exist, so creating\n"); |
| 2383 | if (is_dir == true) { |
| 2384 | ksmbd_debug(SMB, "creating directory\n"); |
| 2385 | |
| 2386 | mode = share_config_directory_mode(share, posix_mode); |
| 2387 | rc = ksmbd_vfs_mkdir(work, name, mode); |
| 2388 | if (rc) |
| 2389 | return rc; |
| 2390 | } else { |
| 2391 | ksmbd_debug(SMB, "creating regular file\n"); |
| 2392 | |
| 2393 | mode = share_config_create_mode(share, posix_mode); |
| 2394 | rc = ksmbd_vfs_create(work, name, mode); |
| 2395 | if (rc) |
| 2396 | return rc; |
| 2397 | } |
| 2398 | |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 2399 | rc = ksmbd_vfs_kern_path(work, name, 0, path, 0); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2400 | if (rc) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2401 | pr_err("cannot get linux path (%s), err = %d\n", |
| 2402 | name, rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2403 | return rc; |
| 2404 | } |
| 2405 | return 0; |
| 2406 | } |
| 2407 | |
| 2408 | static int smb2_create_sd_buffer(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2409 | struct smb2_create_req *req, |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 2410 | struct path *path) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2411 | { |
| 2412 | struct create_context *context; |
Hyunchul Lee | 21dd1fd | 2021-07-09 17:06:34 +0900 | [diff] [blame] | 2413 | struct create_sd_buf_req *sd_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2414 | |
| 2415 | if (!req->CreateContextsOffset) |
Hyunchul Lee | 21dd1fd | 2021-07-09 17:06:34 +0900 | [diff] [blame] | 2416 | return -ENOENT; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2417 | |
| 2418 | /* Parse SD BUFFER create contexts */ |
| 2419 | context = smb2_find_context_vals(req, SMB2_CREATE_SD_BUFFER); |
Hyunchul Lee | 21dd1fd | 2021-07-09 17:06:34 +0900 | [diff] [blame] | 2420 | if (!context) |
| 2421 | return -ENOENT; |
| 2422 | else if (IS_ERR(context)) |
| 2423 | return PTR_ERR(context); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2424 | |
Hyunchul Lee | 21dd1fd | 2021-07-09 17:06:34 +0900 | [diff] [blame] | 2425 | ksmbd_debug(SMB, |
| 2426 | "Set ACLs using SMB2_CREATE_SD_BUFFER context\n"); |
| 2427 | sd_buf = (struct create_sd_buf_req *)context; |
Hyunchul Lee | 8f77150 | 2021-09-24 22:22:22 +0900 | [diff] [blame] | 2428 | if (le16_to_cpu(context->DataOffset) + |
| 2429 | le32_to_cpu(context->DataLength) < |
| 2430 | sizeof(struct create_sd_buf_req)) |
| 2431 | return -EINVAL; |
Hyunchul Lee | 21dd1fd | 2021-07-09 17:06:34 +0900 | [diff] [blame] | 2432 | return set_info_sec(work->conn, work->tcon, path, &sd_buf->ntsd, |
| 2433 | le32_to_cpu(sd_buf->ccontext.DataLength), true); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2434 | } |
| 2435 | |
Christian Brauner | 43205ca | 2021-08-23 17:13:50 +0200 | [diff] [blame] | 2436 | static void ksmbd_acls_fattr(struct smb_fattr *fattr, |
| 2437 | struct user_namespace *mnt_userns, |
| 2438 | struct inode *inode) |
Namjae Jeon | 3d47e54 | 2021-04-20 14:25:35 +0900 | [diff] [blame] | 2439 | { |
Christian Brauner | 43205ca | 2021-08-23 17:13:50 +0200 | [diff] [blame] | 2440 | fattr->cf_uid = i_uid_into_mnt(mnt_userns, inode); |
| 2441 | fattr->cf_gid = i_gid_into_mnt(mnt_userns, inode); |
Namjae Jeon | 3d47e54 | 2021-04-20 14:25:35 +0900 | [diff] [blame] | 2442 | fattr->cf_mode = inode->i_mode; |
Namjae Jeon | 777cad1 | 2021-08-13 08:15:33 +0900 | [diff] [blame] | 2443 | fattr->cf_acls = NULL; |
Namjae Jeon | 3d47e54 | 2021-04-20 14:25:35 +0900 | [diff] [blame] | 2444 | fattr->cf_dacls = NULL; |
| 2445 | |
Namjae Jeon | 777cad1 | 2021-08-13 08:15:33 +0900 | [diff] [blame] | 2446 | if (IS_ENABLED(CONFIG_FS_POSIX_ACL)) { |
| 2447 | fattr->cf_acls = get_acl(inode, ACL_TYPE_ACCESS); |
| 2448 | if (S_ISDIR(inode->i_mode)) |
| 2449 | fattr->cf_dacls = get_acl(inode, ACL_TYPE_DEFAULT); |
| 2450 | } |
Namjae Jeon | 3d47e54 | 2021-04-20 14:25:35 +0900 | [diff] [blame] | 2451 | } |
| 2452 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2453 | /** |
| 2454 | * smb2_open() - handler for smb file open request |
| 2455 | * @work: smb work containing request buffer |
| 2456 | * |
| 2457 | * Return: 0 on success, otherwise error |
| 2458 | */ |
| 2459 | int smb2_open(struct ksmbd_work *work) |
| 2460 | { |
| 2461 | struct ksmbd_conn *conn = work->conn; |
| 2462 | struct ksmbd_session *sess = work->sess; |
| 2463 | struct ksmbd_tree_connect *tcon = work->tcon; |
| 2464 | struct smb2_create_req *req; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 2465 | struct smb2_create_rsp *rsp; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2466 | struct path path; |
| 2467 | struct ksmbd_share_config *share = tcon->share_conf; |
| 2468 | struct ksmbd_file *fp = NULL; |
| 2469 | struct file *filp = NULL; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2470 | struct user_namespace *user_ns = NULL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2471 | struct kstat stat; |
| 2472 | struct create_context *context; |
| 2473 | struct lease_ctx_info *lc = NULL; |
| 2474 | struct create_ea_buf_req *ea_buf = NULL; |
| 2475 | struct oplock_info *opinfo; |
| 2476 | __le32 *next_ptr = NULL; |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 2477 | 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] | 2478 | int rc = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2479 | int contxt_cnt = 0, query_disk_id = 0; |
| 2480 | int maximal_access_ctxt = 0, posix_ctxt = 0; |
| 2481 | int s_type = 0; |
| 2482 | int next_off = 0; |
| 2483 | char *name = NULL; |
| 2484 | char *stream_name = NULL; |
| 2485 | bool file_present = false, created = false, already_permitted = false; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2486 | int share_ret, need_truncate = 0; |
| 2487 | u64 time; |
| 2488 | umode_t posix_mode = 0; |
| 2489 | __le32 daccess, maximal_access = 0; |
| 2490 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2491 | WORK_BUFFERS(work, req, rsp); |
| 2492 | |
| 2493 | if (req->hdr.NextCommand && !work->next_smb2_rcv_hdr_off && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2494 | (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2495 | ksmbd_debug(SMB, "invalid flag in chained command\n"); |
| 2496 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 2497 | smb2_set_err_rsp(work); |
| 2498 | return -EINVAL; |
| 2499 | } |
| 2500 | |
| 2501 | if (test_share_config_flag(share, KSMBD_SHARE_FLAG_PIPE)) { |
| 2502 | ksmbd_debug(SMB, "IPC pipe create request\n"); |
| 2503 | return create_smb2_pipe(work); |
| 2504 | } |
| 2505 | |
| 2506 | if (req->NameLength) { |
| 2507 | if ((req->CreateOptions & FILE_DIRECTORY_FILE_LE) && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2508 | *(char *)req->Buffer == '\\') { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2509 | pr_err("not allow directory name included leading slash\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2510 | rc = -EINVAL; |
| 2511 | goto err_out1; |
| 2512 | } |
| 2513 | |
| 2514 | name = smb2_get_name(share, |
| 2515 | req->Buffer, |
| 2516 | le16_to_cpu(req->NameLength), |
| 2517 | work->conn->local_nls); |
| 2518 | if (IS_ERR(name)) { |
| 2519 | rc = PTR_ERR(name); |
| 2520 | if (rc != -ENOMEM) |
| 2521 | rc = -ENOENT; |
Dan Carpenter | 8b99f35 | 2021-08-02 08:14:03 +0900 | [diff] [blame] | 2522 | name = NULL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2523 | goto err_out1; |
| 2524 | } |
| 2525 | |
| 2526 | ksmbd_debug(SMB, "converted name = %s\n", name); |
| 2527 | if (strchr(name, ':')) { |
| 2528 | if (!test_share_config_flag(work->tcon->share_conf, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2529 | KSMBD_SHARE_FLAG_STREAMS)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2530 | rc = -EBADF; |
| 2531 | goto err_out1; |
| 2532 | } |
| 2533 | rc = parse_stream_name(name, &stream_name, &s_type); |
| 2534 | if (rc < 0) |
| 2535 | goto err_out1; |
| 2536 | } |
| 2537 | |
| 2538 | rc = ksmbd_validate_filename(name); |
| 2539 | if (rc < 0) |
| 2540 | goto err_out1; |
| 2541 | |
| 2542 | if (ksmbd_share_veto_filename(share, name)) { |
| 2543 | rc = -ENOENT; |
| 2544 | ksmbd_debug(SMB, "Reject open(), vetoed file: %s\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2545 | name); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2546 | goto err_out1; |
| 2547 | } |
| 2548 | } else { |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 2549 | name = kstrdup("", GFP_KERNEL); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2550 | if (!name) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2551 | rc = -ENOMEM; |
| 2552 | goto err_out1; |
| 2553 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2554 | } |
| 2555 | |
| 2556 | req_op_level = req->RequestedOplockLevel; |
Namjae Jeon | 73f9dad | 2021-04-16 14:12:06 +0900 | [diff] [blame] | 2557 | if (req_op_level == SMB2_OPLOCK_LEVEL_LEASE) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2558 | lc = parse_lease_state(req); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2559 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2560 | if (le32_to_cpu(req->ImpersonationLevel) > le32_to_cpu(IL_DELEGATE_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2561 | pr_err("Invalid impersonationlevel : 0x%x\n", |
| 2562 | le32_to_cpu(req->ImpersonationLevel)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2563 | rc = -EIO; |
| 2564 | rsp->hdr.Status = STATUS_BAD_IMPERSONATION_LEVEL; |
| 2565 | goto err_out1; |
| 2566 | } |
| 2567 | |
| 2568 | if (req->CreateOptions && !(req->CreateOptions & CREATE_OPTIONS_MASK)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2569 | pr_err("Invalid create options : 0x%x\n", |
| 2570 | le32_to_cpu(req->CreateOptions)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2571 | rc = -EINVAL; |
| 2572 | goto err_out1; |
| 2573 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2574 | if (req->CreateOptions & FILE_SEQUENTIAL_ONLY_LE && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2575 | req->CreateOptions & FILE_RANDOM_ACCESS_LE) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2576 | req->CreateOptions = ~(FILE_SEQUENTIAL_ONLY_LE); |
| 2577 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2578 | if (req->CreateOptions & |
| 2579 | (FILE_OPEN_BY_FILE_ID_LE | CREATE_TREE_CONNECTION | |
| 2580 | FILE_RESERVE_OPFILTER_LE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2581 | rc = -EOPNOTSUPP; |
| 2582 | goto err_out1; |
| 2583 | } |
| 2584 | |
| 2585 | if (req->CreateOptions & FILE_DIRECTORY_FILE_LE) { |
| 2586 | if (req->CreateOptions & FILE_NON_DIRECTORY_FILE_LE) { |
| 2587 | rc = -EINVAL; |
| 2588 | goto err_out1; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2589 | } else if (req->CreateOptions & FILE_NO_COMPRESSION_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2590 | req->CreateOptions = ~(FILE_NO_COMPRESSION_LE); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2591 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2592 | } |
| 2593 | } |
| 2594 | |
| 2595 | if (le32_to_cpu(req->CreateDisposition) > |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2596 | le32_to_cpu(FILE_OVERWRITE_IF_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2597 | pr_err("Invalid create disposition : 0x%x\n", |
| 2598 | le32_to_cpu(req->CreateDisposition)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2599 | rc = -EINVAL; |
| 2600 | goto err_out1; |
| 2601 | } |
| 2602 | |
| 2603 | if (!(req->DesiredAccess & DESIRED_ACCESS_MASK)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2604 | pr_err("Invalid desired access : 0x%x\n", |
| 2605 | le32_to_cpu(req->DesiredAccess)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2606 | rc = -EACCES; |
| 2607 | goto err_out1; |
| 2608 | } |
| 2609 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2610 | if (req->FileAttributes && !(req->FileAttributes & ATTR_MASK_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2611 | pr_err("Invalid file attribute : 0x%x\n", |
| 2612 | le32_to_cpu(req->FileAttributes)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2613 | rc = -EINVAL; |
| 2614 | goto err_out1; |
| 2615 | } |
| 2616 | |
| 2617 | if (req->CreateContextsOffset) { |
| 2618 | /* Parse non-durable handle create contexts */ |
| 2619 | context = smb2_find_context_vals(req, SMB2_CREATE_EA_BUFFER); |
Namjae Jeon | f19b396 | 2021-07-13 09:59:34 +0900 | [diff] [blame] | 2620 | if (IS_ERR(context)) { |
| 2621 | rc = PTR_ERR(context); |
| 2622 | goto err_out1; |
| 2623 | } else if (context) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2624 | ea_buf = (struct create_ea_buf_req *)context; |
Hyunchul Lee | 8f77150 | 2021-09-24 22:22:22 +0900 | [diff] [blame] | 2625 | if (le16_to_cpu(context->DataOffset) + |
| 2626 | le32_to_cpu(context->DataLength) < |
| 2627 | sizeof(struct create_ea_buf_req)) { |
| 2628 | rc = -EINVAL; |
| 2629 | goto err_out1; |
| 2630 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2631 | if (req->CreateOptions & FILE_NO_EA_KNOWLEDGE_LE) { |
| 2632 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 2633 | rc = -EACCES; |
| 2634 | goto err_out1; |
| 2635 | } |
| 2636 | } |
| 2637 | |
| 2638 | context = smb2_find_context_vals(req, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2639 | SMB2_CREATE_QUERY_MAXIMAL_ACCESS_REQUEST); |
Namjae Jeon | f19b396 | 2021-07-13 09:59:34 +0900 | [diff] [blame] | 2640 | if (IS_ERR(context)) { |
| 2641 | rc = PTR_ERR(context); |
| 2642 | goto err_out1; |
| 2643 | } else if (context) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2644 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2645 | "get query maximal access context\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2646 | maximal_access_ctxt = 1; |
| 2647 | } |
| 2648 | |
| 2649 | context = smb2_find_context_vals(req, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2650 | SMB2_CREATE_TIMEWARP_REQUEST); |
Namjae Jeon | f19b396 | 2021-07-13 09:59:34 +0900 | [diff] [blame] | 2651 | if (IS_ERR(context)) { |
| 2652 | rc = PTR_ERR(context); |
| 2653 | goto err_out1; |
| 2654 | } else if (context) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2655 | ksmbd_debug(SMB, "get timewarp context\n"); |
| 2656 | rc = -EBADF; |
| 2657 | goto err_out1; |
| 2658 | } |
| 2659 | |
| 2660 | if (tcon->posix_extensions) { |
| 2661 | context = smb2_find_context_vals(req, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2662 | SMB2_CREATE_TAG_POSIX); |
Namjae Jeon | f19b396 | 2021-07-13 09:59:34 +0900 | [diff] [blame] | 2663 | if (IS_ERR(context)) { |
| 2664 | rc = PTR_ERR(context); |
| 2665 | goto err_out1; |
| 2666 | } else if (context) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2667 | struct create_posix *posix = |
| 2668 | (struct create_posix *)context; |
Hyunchul Lee | 8f77150 | 2021-09-24 22:22:22 +0900 | [diff] [blame] | 2669 | if (le16_to_cpu(context->DataOffset) + |
| 2670 | le32_to_cpu(context->DataLength) < |
| 2671 | sizeof(struct create_posix)) { |
| 2672 | rc = -EINVAL; |
| 2673 | goto err_out1; |
| 2674 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2675 | ksmbd_debug(SMB, "get posix context\n"); |
| 2676 | |
| 2677 | posix_mode = le32_to_cpu(posix->Mode); |
| 2678 | posix_ctxt = 1; |
| 2679 | } |
| 2680 | } |
| 2681 | } |
| 2682 | |
| 2683 | if (ksmbd_override_fsids(work)) { |
| 2684 | rc = -ENOMEM; |
| 2685 | goto err_out1; |
| 2686 | } |
| 2687 | |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 2688 | rc = ksmbd_vfs_kern_path(work, name, LOOKUP_NO_SYMLINKS, &path, 1); |
Namjae Jeon | 4ea4779 | 2021-09-21 14:19:33 +0900 | [diff] [blame] | 2689 | if (!rc) { |
| 2690 | if (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2691 | /* |
| 2692 | * If file exists with under flags, return access |
| 2693 | * denied error. |
| 2694 | */ |
| 2695 | if (req->CreateDisposition == FILE_OVERWRITE_IF_LE || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2696 | req->CreateDisposition == FILE_OPEN_IF_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2697 | rc = -EACCES; |
| 2698 | path_put(&path); |
| 2699 | goto err_out; |
| 2700 | } |
| 2701 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2702 | if (!test_tree_conn_flag(tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2703 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2704 | "User does not have write permission\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2705 | rc = -EACCES; |
| 2706 | path_put(&path); |
| 2707 | goto err_out; |
| 2708 | } |
Namjae Jeon | 4ea4779 | 2021-09-21 14:19:33 +0900 | [diff] [blame] | 2709 | } else if (d_is_symlink(path.dentry)) { |
| 2710 | rc = -EACCES; |
| 2711 | path_put(&path); |
| 2712 | goto err_out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2713 | } |
| 2714 | } |
| 2715 | |
| 2716 | if (rc) { |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 2717 | if (rc != -ENOENT) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2718 | goto err_out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2719 | 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] | 2720 | name, rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2721 | rc = 0; |
| 2722 | } else { |
| 2723 | file_present = true; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2724 | user_ns = mnt_user_ns(path.mnt); |
| 2725 | generic_fillattr(user_ns, d_inode(path.dentry), &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2726 | } |
| 2727 | if (stream_name) { |
| 2728 | if (req->CreateOptions & FILE_DIRECTORY_FILE_LE) { |
| 2729 | if (s_type == DATA_STREAM) { |
| 2730 | rc = -EIO; |
| 2731 | rsp->hdr.Status = STATUS_NOT_A_DIRECTORY; |
| 2732 | } |
| 2733 | } else { |
| 2734 | if (S_ISDIR(stat.mode) && s_type == DATA_STREAM) { |
| 2735 | rc = -EIO; |
| 2736 | rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY; |
| 2737 | } |
| 2738 | } |
| 2739 | |
| 2740 | if (req->CreateOptions & FILE_DIRECTORY_FILE_LE && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2741 | req->FileAttributes & ATTR_NORMAL_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2742 | rsp->hdr.Status = STATUS_NOT_A_DIRECTORY; |
| 2743 | rc = -EIO; |
| 2744 | } |
| 2745 | |
| 2746 | if (rc < 0) |
| 2747 | goto err_out; |
| 2748 | } |
| 2749 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2750 | if (file_present && req->CreateOptions & FILE_NON_DIRECTORY_FILE_LE && |
| 2751 | S_ISDIR(stat.mode) && !(req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2752 | ksmbd_debug(SMB, "open() argument is a directory: %s, %x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2753 | name, req->CreateOptions); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2754 | rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY; |
| 2755 | rc = -EIO; |
| 2756 | goto err_out; |
| 2757 | } |
| 2758 | |
| 2759 | if (file_present && (req->CreateOptions & FILE_DIRECTORY_FILE_LE) && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2760 | !(req->CreateDisposition == FILE_CREATE_LE) && |
| 2761 | !S_ISDIR(stat.mode)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2762 | rsp->hdr.Status = STATUS_NOT_A_DIRECTORY; |
| 2763 | rc = -EIO; |
| 2764 | goto err_out; |
| 2765 | } |
| 2766 | |
| 2767 | if (!stream_name && file_present && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2768 | req->CreateDisposition == FILE_CREATE_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2769 | rc = -EEXIST; |
| 2770 | goto err_out; |
| 2771 | } |
| 2772 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2773 | daccess = smb_map_generic_desired_access(req->DesiredAccess); |
| 2774 | |
| 2775 | if (file_present && !(req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) { |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 2776 | rc = smb_check_perm_dacl(conn, &path, &daccess, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2777 | sess->user->uid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2778 | if (rc) |
| 2779 | goto err_out; |
| 2780 | } |
| 2781 | |
| 2782 | if (daccess & FILE_MAXIMAL_ACCESS_LE) { |
| 2783 | if (!file_present) { |
| 2784 | daccess = cpu_to_le32(GENERIC_ALL_FLAGS); |
| 2785 | } else { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2786 | rc = ksmbd_vfs_query_maximal_access(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2787 | path.dentry, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2788 | &daccess); |
| 2789 | if (rc) |
| 2790 | goto err_out; |
| 2791 | already_permitted = true; |
| 2792 | } |
| 2793 | maximal_access = daccess; |
| 2794 | } |
| 2795 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2796 | open_flags = smb2_create_open_flags(file_present, daccess, |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 2797 | req->CreateDisposition, |
| 2798 | &may_flags); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2799 | |
| 2800 | if (!test_tree_conn_flag(tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
| 2801 | if (open_flags & O_CREAT) { |
| 2802 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2803 | "User does not have write permission\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2804 | rc = -EACCES; |
| 2805 | goto err_out; |
| 2806 | } |
| 2807 | } |
| 2808 | |
| 2809 | /*create file if not present */ |
| 2810 | if (!file_present) { |
| 2811 | rc = smb2_creat(work, &path, name, open_flags, posix_mode, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2812 | req->CreateOptions & FILE_DIRECTORY_FILE_LE); |
Marios Makassikis | d337a44 | 2021-07-27 09:24:51 +0900 | [diff] [blame] | 2813 | if (rc) { |
| 2814 | if (rc == -ENOENT) { |
| 2815 | rc = -EIO; |
| 2816 | rsp->hdr.Status = STATUS_OBJECT_PATH_NOT_FOUND; |
| 2817 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2818 | goto err_out; |
Marios Makassikis | d337a44 | 2021-07-27 09:24:51 +0900 | [diff] [blame] | 2819 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2820 | |
| 2821 | created = true; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2822 | user_ns = mnt_user_ns(path.mnt); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2823 | if (ea_buf) { |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 2824 | if (le32_to_cpu(ea_buf->ccontext.DataLength) < |
| 2825 | sizeof(struct smb2_ea_info)) { |
| 2826 | rc = -EINVAL; |
| 2827 | goto err_out; |
| 2828 | } |
| 2829 | |
| 2830 | rc = smb2_set_ea(&ea_buf->ea, |
| 2831 | le32_to_cpu(ea_buf->ccontext.DataLength), |
| 2832 | &path); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2833 | if (rc == -EOPNOTSUPP) |
| 2834 | rc = 0; |
| 2835 | else if (rc) |
| 2836 | goto err_out; |
| 2837 | } |
| 2838 | } else if (!already_permitted) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2839 | /* FILE_READ_ATTRIBUTE is allowed without inode_permission, |
| 2840 | * because execute(search) permission on a parent directory, |
| 2841 | * is already granted. |
| 2842 | */ |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2843 | if (daccess & ~(FILE_READ_ATTRIBUTES_LE | FILE_READ_CONTROL_LE)) { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2844 | rc = inode_permission(user_ns, |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 2845 | d_inode(path.dentry), |
| 2846 | may_flags); |
Namjae Jeon | ff1d572 | 2021-04-13 13:18:10 +0900 | [diff] [blame] | 2847 | if (rc) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2848 | goto err_out; |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 2849 | |
| 2850 | if ((daccess & FILE_DELETE_LE) || |
| 2851 | (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2852 | rc = ksmbd_vfs_may_delete(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2853 | path.dentry); |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 2854 | if (rc) |
| 2855 | goto err_out; |
| 2856 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2857 | } |
| 2858 | } |
| 2859 | |
| 2860 | rc = ksmbd_query_inode_status(d_inode(path.dentry->d_parent)); |
| 2861 | if (rc == KSMBD_INODE_STATUS_PENDING_DELETE) { |
| 2862 | rc = -EBUSY; |
| 2863 | goto err_out; |
| 2864 | } |
| 2865 | |
| 2866 | rc = 0; |
| 2867 | filp = dentry_open(&path, open_flags, current_cred()); |
| 2868 | if (IS_ERR(filp)) { |
| 2869 | rc = PTR_ERR(filp); |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2870 | pr_err("dentry open for dir failed, rc %d\n", rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2871 | goto err_out; |
| 2872 | } |
| 2873 | |
| 2874 | if (file_present) { |
| 2875 | if (!(open_flags & O_TRUNC)) |
| 2876 | file_info = FILE_OPENED; |
| 2877 | else |
| 2878 | file_info = FILE_OVERWRITTEN; |
| 2879 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2880 | if ((req->CreateDisposition & FILE_CREATE_MASK_LE) == |
| 2881 | FILE_SUPERSEDE_LE) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2882 | file_info = FILE_SUPERSEDED; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2883 | } else if (open_flags & O_CREAT) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2884 | file_info = FILE_CREATED; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2885 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2886 | |
| 2887 | ksmbd_vfs_set_fadvise(filp, req->CreateOptions); |
| 2888 | |
| 2889 | /* Obtain Volatile-ID */ |
| 2890 | fp = ksmbd_open_fd(work, filp); |
| 2891 | if (IS_ERR(fp)) { |
| 2892 | fput(filp); |
| 2893 | rc = PTR_ERR(fp); |
| 2894 | fp = NULL; |
| 2895 | goto err_out; |
| 2896 | } |
| 2897 | |
| 2898 | /* Get Persistent-ID */ |
| 2899 | ksmbd_open_durable_fd(fp); |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 2900 | if (!has_file_id(fp->persistent_id)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2901 | rc = -ENOMEM; |
| 2902 | goto err_out; |
| 2903 | } |
| 2904 | |
| 2905 | fp->filename = name; |
| 2906 | fp->cdoption = req->CreateDisposition; |
| 2907 | fp->daccess = daccess; |
| 2908 | fp->saccess = req->ShareAccess; |
| 2909 | fp->coption = req->CreateOptions; |
| 2910 | |
| 2911 | /* Set default windows and posix acls if creating new file */ |
| 2912 | if (created) { |
| 2913 | int posix_acl_rc; |
Namjae Jeon | fba08fa | 2021-04-15 10:29:39 +0900 | [diff] [blame] | 2914 | struct inode *inode = d_inode(path.dentry); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2915 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2916 | posix_acl_rc = ksmbd_vfs_inherit_posix_acl(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2917 | inode, |
| 2918 | d_inode(path.dentry->d_parent)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2919 | if (posix_acl_rc) |
| 2920 | ksmbd_debug(SMB, "inherit posix acl failed : %d\n", posix_acl_rc); |
| 2921 | |
| 2922 | if (test_share_config_flag(work->tcon->share_conf, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2923 | KSMBD_SHARE_FLAG_ACL_XATTR)) { |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 2924 | rc = smb_inherit_dacl(conn, &path, sess->user->uid, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2925 | sess->user->gid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2926 | } |
| 2927 | |
| 2928 | if (rc) { |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 2929 | rc = smb2_create_sd_buffer(work, req, &path); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2930 | if (rc) { |
| 2931 | if (posix_acl_rc) |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2932 | ksmbd_vfs_set_init_posix_acl(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2933 | inode); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2934 | |
| 2935 | if (test_share_config_flag(work->tcon->share_conf, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2936 | KSMBD_SHARE_FLAG_ACL_XATTR)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2937 | struct smb_fattr fattr; |
| 2938 | struct smb_ntsd *pntsd; |
Namjae Jeon | 3d47e54 | 2021-04-20 14:25:35 +0900 | [diff] [blame] | 2939 | int pntsd_size, ace_num = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2940 | |
Christian Brauner | 43205ca | 2021-08-23 17:13:50 +0200 | [diff] [blame] | 2941 | ksmbd_acls_fattr(&fattr, user_ns, inode); |
Marios Makassikis | e6b1059 | 2021-04-15 10:24:56 +0900 | [diff] [blame] | 2942 | if (fattr.cf_acls) |
| 2943 | ace_num = fattr.cf_acls->a_count; |
Namjae Jeon | 3d47e54 | 2021-04-20 14:25:35 +0900 | [diff] [blame] | 2944 | if (fattr.cf_dacls) |
| 2945 | ace_num += fattr.cf_dacls->a_count; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2946 | |
| 2947 | pntsd = kmalloc(sizeof(struct smb_ntsd) + |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2948 | sizeof(struct smb_sid) * 3 + |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2949 | sizeof(struct smb_acl) + |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2950 | sizeof(struct smb_ace) * ace_num * 2, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2951 | GFP_KERNEL); |
| 2952 | if (!pntsd) |
| 2953 | goto err_out; |
| 2954 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2955 | rc = build_sec_desc(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2956 | pntsd, NULL, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2957 | OWNER_SECINFO | |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2958 | GROUP_SECINFO | |
| 2959 | DACL_SECINFO, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2960 | &pntsd_size, &fattr); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2961 | posix_acl_release(fattr.cf_acls); |
| 2962 | posix_acl_release(fattr.cf_dacls); |
| 2963 | |
| 2964 | rc = ksmbd_vfs_set_sd_xattr(conn, |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2965 | user_ns, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2966 | path.dentry, |
| 2967 | pntsd, |
| 2968 | pntsd_size); |
Namjae Jeon | 3d47e54 | 2021-04-20 14:25:35 +0900 | [diff] [blame] | 2969 | kfree(pntsd); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2970 | if (rc) |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2971 | pr_err("failed to store ntacl in xattr : %d\n", |
| 2972 | rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2973 | } |
| 2974 | } |
| 2975 | } |
| 2976 | rc = 0; |
| 2977 | } |
| 2978 | |
| 2979 | if (stream_name) { |
| 2980 | rc = smb2_set_stream_name_xattr(&path, |
| 2981 | fp, |
| 2982 | stream_name, |
| 2983 | s_type); |
| 2984 | if (rc) |
| 2985 | goto err_out; |
| 2986 | file_info = FILE_CREATED; |
| 2987 | } |
| 2988 | |
| 2989 | fp->attrib_only = !(req->DesiredAccess & ~(FILE_READ_ATTRIBUTES_LE | |
| 2990 | FILE_WRITE_ATTRIBUTES_LE | FILE_SYNCHRONIZE_LE)); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2991 | if (!S_ISDIR(file_inode(filp)->i_mode) && open_flags & O_TRUNC && |
| 2992 | !fp->attrib_only && !stream_name) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2993 | smb_break_all_oplock(work, fp); |
| 2994 | need_truncate = 1; |
| 2995 | } |
| 2996 | |
| 2997 | /* fp should be searchable through ksmbd_inode.m_fp_list |
| 2998 | * after daccess, saccess, attrib_only, and stream are |
| 2999 | * initialized. |
| 3000 | */ |
| 3001 | write_lock(&fp->f_ci->m_lock); |
| 3002 | list_add(&fp->node, &fp->f_ci->m_fp_list); |
| 3003 | write_unlock(&fp->f_ci->m_lock); |
| 3004 | |
| 3005 | rc = ksmbd_vfs_getattr(&path, &stat); |
| 3006 | if (rc) { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 3007 | generic_fillattr(user_ns, d_inode(path.dentry), &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3008 | rc = 0; |
| 3009 | } |
| 3010 | |
| 3011 | /* Check delete pending among previous fp before oplock break */ |
| 3012 | if (ksmbd_inode_pending_delete(fp)) { |
| 3013 | rc = -EBUSY; |
| 3014 | goto err_out; |
| 3015 | } |
| 3016 | |
| 3017 | share_ret = ksmbd_smb_check_shared_mode(fp->filp, fp); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3018 | if (!test_share_config_flag(work->tcon->share_conf, KSMBD_SHARE_FLAG_OPLOCKS) || |
| 3019 | (req_op_level == SMB2_OPLOCK_LEVEL_LEASE && |
| 3020 | !(conn->vals->capabilities & SMB2_GLOBAL_CAP_LEASING))) { |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 3021 | if (share_ret < 0 && !S_ISDIR(file_inode(fp->filp)->i_mode)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3022 | rc = share_ret; |
| 3023 | goto err_out; |
| 3024 | } |
| 3025 | } else { |
| 3026 | if (req_op_level == SMB2_OPLOCK_LEVEL_LEASE) { |
| 3027 | req_op_level = smb2_map_lease_to_oplock(lc->req_state); |
| 3028 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3029 | "lease req for(%s) req oplock state 0x%x, lease state 0x%x\n", |
| 3030 | name, req_op_level, lc->req_state); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3031 | rc = find_same_lease_key(sess, fp->f_ci, lc); |
| 3032 | if (rc) |
| 3033 | goto err_out; |
| 3034 | } else if (open_flags == O_RDONLY && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3035 | (req_op_level == SMB2_OPLOCK_LEVEL_BATCH || |
| 3036 | req_op_level == SMB2_OPLOCK_LEVEL_EXCLUSIVE)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3037 | req_op_level = SMB2_OPLOCK_LEVEL_II; |
| 3038 | |
| 3039 | rc = smb_grant_oplock(work, req_op_level, |
| 3040 | fp->persistent_id, fp, |
| 3041 | le32_to_cpu(req->hdr.Id.SyncId.TreeId), |
| 3042 | lc, share_ret); |
| 3043 | if (rc < 0) |
| 3044 | goto err_out; |
| 3045 | } |
| 3046 | |
| 3047 | if (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE) |
| 3048 | ksmbd_fd_set_delete_on_close(fp, file_info); |
| 3049 | |
| 3050 | if (need_truncate) { |
| 3051 | rc = smb2_create_truncate(&path); |
| 3052 | if (rc) |
| 3053 | goto err_out; |
| 3054 | } |
| 3055 | |
| 3056 | if (req->CreateContextsOffset) { |
| 3057 | struct create_alloc_size_req *az_req; |
| 3058 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3059 | az_req = (struct create_alloc_size_req *)smb2_find_context_vals(req, |
| 3060 | SMB2_CREATE_ALLOCATION_SIZE); |
Namjae Jeon | f19b396 | 2021-07-13 09:59:34 +0900 | [diff] [blame] | 3061 | if (IS_ERR(az_req)) { |
| 3062 | rc = PTR_ERR(az_req); |
| 3063 | goto err_out; |
| 3064 | } else if (az_req) { |
Hyunchul Lee | 8f77150 | 2021-09-24 22:22:22 +0900 | [diff] [blame] | 3065 | loff_t alloc_size; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3066 | int err; |
| 3067 | |
Hyunchul Lee | 8f77150 | 2021-09-24 22:22:22 +0900 | [diff] [blame] | 3068 | if (le16_to_cpu(az_req->ccontext.DataOffset) + |
| 3069 | le32_to_cpu(az_req->ccontext.DataLength) < |
| 3070 | sizeof(struct create_alloc_size_req)) { |
| 3071 | rc = -EINVAL; |
| 3072 | goto err_out; |
| 3073 | } |
| 3074 | alloc_size = le64_to_cpu(az_req->AllocationSize); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3075 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3076 | "request smb2 create allocate size : %llu\n", |
| 3077 | alloc_size); |
Namjae Jeon | e8c0619 | 2021-06-22 11:06:11 +0900 | [diff] [blame] | 3078 | smb_break_all_levII_oplock(work, fp, 1); |
| 3079 | err = vfs_fallocate(fp->filp, FALLOC_FL_KEEP_SIZE, 0, |
| 3080 | alloc_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3081 | if (err < 0) |
| 3082 | ksmbd_debug(SMB, |
Namjae Jeon | e8c0619 | 2021-06-22 11:06:11 +0900 | [diff] [blame] | 3083 | "vfs_fallocate is failed : %d\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3084 | err); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3085 | } |
| 3086 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3087 | context = smb2_find_context_vals(req, SMB2_CREATE_QUERY_ON_DISK_ID); |
Namjae Jeon | f19b396 | 2021-07-13 09:59:34 +0900 | [diff] [blame] | 3088 | if (IS_ERR(context)) { |
| 3089 | rc = PTR_ERR(context); |
| 3090 | goto err_out; |
| 3091 | } else if (context) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3092 | ksmbd_debug(SMB, "get query on disk id context\n"); |
| 3093 | query_disk_id = 1; |
| 3094 | } |
| 3095 | } |
| 3096 | |
| 3097 | if (stat.result_mask & STATX_BTIME) |
| 3098 | fp->create_time = ksmbd_UnixTimeToNT(stat.btime); |
| 3099 | else |
| 3100 | fp->create_time = ksmbd_UnixTimeToNT(stat.ctime); |
| 3101 | if (req->FileAttributes || fp->f_ci->m_fattr == 0) |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3102 | fp->f_ci->m_fattr = |
| 3103 | 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] | 3104 | |
| 3105 | if (!created) |
| 3106 | smb2_update_xattrs(tcon, &path, fp); |
| 3107 | else |
| 3108 | smb2_new_xattrs(tcon, &path, fp); |
| 3109 | |
| 3110 | memcpy(fp->client_guid, conn->ClientGUID, SMB2_CLIENT_GUID_SIZE); |
| 3111 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 3112 | generic_fillattr(user_ns, file_inode(fp->filp), |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 3113 | &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3114 | |
| 3115 | rsp->StructureSize = cpu_to_le16(89); |
| 3116 | rcu_read_lock(); |
| 3117 | opinfo = rcu_dereference(fp->f_opinfo); |
| 3118 | rsp->OplockLevel = opinfo != NULL ? opinfo->level : 0; |
| 3119 | rcu_read_unlock(); |
| 3120 | rsp->Reserved = 0; |
| 3121 | rsp->CreateAction = cpu_to_le32(file_info); |
| 3122 | rsp->CreationTime = cpu_to_le64(fp->create_time); |
| 3123 | time = ksmbd_UnixTimeToNT(stat.atime); |
| 3124 | rsp->LastAccessTime = cpu_to_le64(time); |
| 3125 | time = ksmbd_UnixTimeToNT(stat.mtime); |
| 3126 | rsp->LastWriteTime = cpu_to_le64(time); |
| 3127 | time = ksmbd_UnixTimeToNT(stat.ctime); |
| 3128 | rsp->ChangeTime = cpu_to_le64(time); |
| 3129 | rsp->AllocationSize = S_ISDIR(stat.mode) ? 0 : |
| 3130 | cpu_to_le64(stat.blocks << 9); |
| 3131 | rsp->EndofFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size); |
| 3132 | rsp->FileAttributes = fp->f_ci->m_fattr; |
| 3133 | |
| 3134 | rsp->Reserved2 = 0; |
| 3135 | |
| 3136 | rsp->PersistentFileId = cpu_to_le64(fp->persistent_id); |
| 3137 | rsp->VolatileFileId = cpu_to_le64(fp->volatile_id); |
| 3138 | |
| 3139 | rsp->CreateContextsOffset = 0; |
| 3140 | rsp->CreateContextsLength = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 3141 | inc_rfc1001_len(work->response_buf, 88); /* StructureSize - 1*/ |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3142 | |
| 3143 | /* If lease is request send lease context response */ |
| 3144 | if (opinfo && opinfo->is_lease) { |
| 3145 | struct create_context *lease_ccontext; |
| 3146 | |
| 3147 | ksmbd_debug(SMB, "lease granted on(%s) lease state 0x%x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3148 | name, opinfo->o_lease->state); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3149 | rsp->OplockLevel = SMB2_OPLOCK_LEVEL_LEASE; |
| 3150 | |
| 3151 | lease_ccontext = (struct create_context *)rsp->Buffer; |
| 3152 | contxt_cnt++; |
| 3153 | create_lease_buf(rsp->Buffer, opinfo->o_lease); |
| 3154 | le32_add_cpu(&rsp->CreateContextsLength, |
| 3155 | conn->vals->create_lease_size); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 3156 | inc_rfc1001_len(work->response_buf, |
| 3157 | conn->vals->create_lease_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3158 | next_ptr = &lease_ccontext->Next; |
| 3159 | next_off = conn->vals->create_lease_size; |
| 3160 | } |
| 3161 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3162 | if (maximal_access_ctxt) { |
| 3163 | struct create_context *mxac_ccontext; |
| 3164 | |
| 3165 | if (maximal_access == 0) |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 3166 | ksmbd_vfs_query_maximal_access(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 3167 | path.dentry, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3168 | &maximal_access); |
| 3169 | mxac_ccontext = (struct create_context *)(rsp->Buffer + |
| 3170 | le32_to_cpu(rsp->CreateContextsLength)); |
| 3171 | contxt_cnt++; |
| 3172 | create_mxac_rsp_buf(rsp->Buffer + |
| 3173 | le32_to_cpu(rsp->CreateContextsLength), |
| 3174 | le32_to_cpu(maximal_access)); |
| 3175 | le32_add_cpu(&rsp->CreateContextsLength, |
| 3176 | conn->vals->create_mxac_size); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 3177 | inc_rfc1001_len(work->response_buf, |
| 3178 | conn->vals->create_mxac_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3179 | if (next_ptr) |
| 3180 | *next_ptr = cpu_to_le32(next_off); |
| 3181 | next_ptr = &mxac_ccontext->Next; |
| 3182 | next_off = conn->vals->create_mxac_size; |
| 3183 | } |
| 3184 | |
| 3185 | if (query_disk_id) { |
| 3186 | struct create_context *disk_id_ccontext; |
| 3187 | |
| 3188 | disk_id_ccontext = (struct create_context *)(rsp->Buffer + |
| 3189 | le32_to_cpu(rsp->CreateContextsLength)); |
| 3190 | contxt_cnt++; |
| 3191 | create_disk_id_rsp_buf(rsp->Buffer + |
| 3192 | le32_to_cpu(rsp->CreateContextsLength), |
| 3193 | stat.ino, tcon->id); |
| 3194 | le32_add_cpu(&rsp->CreateContextsLength, |
| 3195 | conn->vals->create_disk_id_size); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 3196 | inc_rfc1001_len(work->response_buf, |
| 3197 | conn->vals->create_disk_id_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3198 | if (next_ptr) |
| 3199 | *next_ptr = cpu_to_le32(next_off); |
| 3200 | next_ptr = &disk_id_ccontext->Next; |
| 3201 | next_off = conn->vals->create_disk_id_size; |
| 3202 | } |
| 3203 | |
| 3204 | if (posix_ctxt) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3205 | contxt_cnt++; |
| 3206 | create_posix_rsp_buf(rsp->Buffer + |
| 3207 | le32_to_cpu(rsp->CreateContextsLength), |
| 3208 | fp); |
| 3209 | le32_add_cpu(&rsp->CreateContextsLength, |
| 3210 | conn->vals->create_posix_size); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 3211 | inc_rfc1001_len(work->response_buf, |
| 3212 | conn->vals->create_posix_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3213 | if (next_ptr) |
| 3214 | *next_ptr = cpu_to_le32(next_off); |
| 3215 | } |
| 3216 | |
| 3217 | if (contxt_cnt > 0) { |
| 3218 | rsp->CreateContextsOffset = |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 3219 | cpu_to_le32(offsetof(struct smb2_create_rsp, Buffer)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3220 | } |
| 3221 | |
| 3222 | err_out: |
| 3223 | if (file_present || created) |
| 3224 | path_put(&path); |
| 3225 | ksmbd_revert_fsids(work); |
| 3226 | err_out1: |
| 3227 | if (rc) { |
| 3228 | if (rc == -EINVAL) |
| 3229 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 3230 | else if (rc == -EOPNOTSUPP) |
| 3231 | rsp->hdr.Status = STATUS_NOT_SUPPORTED; |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 3232 | else if (rc == -EACCES || rc == -ESTALE || rc == -EXDEV) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3233 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 3234 | else if (rc == -ENOENT) |
| 3235 | rsp->hdr.Status = STATUS_OBJECT_NAME_INVALID; |
| 3236 | else if (rc == -EPERM) |
| 3237 | rsp->hdr.Status = STATUS_SHARING_VIOLATION; |
| 3238 | else if (rc == -EBUSY) |
| 3239 | rsp->hdr.Status = STATUS_DELETE_PENDING; |
| 3240 | else if (rc == -EBADF) |
| 3241 | rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND; |
| 3242 | else if (rc == -ENOEXEC) |
| 3243 | rsp->hdr.Status = STATUS_DUPLICATE_OBJECTID; |
| 3244 | else if (rc == -ENXIO) |
| 3245 | rsp->hdr.Status = STATUS_NO_SUCH_DEVICE; |
| 3246 | else if (rc == -EEXIST) |
| 3247 | rsp->hdr.Status = STATUS_OBJECT_NAME_COLLISION; |
| 3248 | else if (rc == -EMFILE) |
| 3249 | rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES; |
| 3250 | if (!rsp->hdr.Status) |
| 3251 | rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR; |
| 3252 | |
| 3253 | if (!fp || !fp->filename) |
| 3254 | kfree(name); |
| 3255 | if (fp) |
| 3256 | ksmbd_fd_put(work, fp); |
| 3257 | smb2_set_err_rsp(work); |
| 3258 | ksmbd_debug(SMB, "Error response: %x\n", rsp->hdr.Status); |
| 3259 | } |
| 3260 | |
| 3261 | kfree(lc); |
| 3262 | |
| 3263 | return 0; |
| 3264 | } |
| 3265 | |
| 3266 | static int readdir_info_level_struct_sz(int info_level) |
| 3267 | { |
| 3268 | switch (info_level) { |
| 3269 | case FILE_FULL_DIRECTORY_INFORMATION: |
| 3270 | return sizeof(struct file_full_directory_info); |
| 3271 | case FILE_BOTH_DIRECTORY_INFORMATION: |
| 3272 | return sizeof(struct file_both_directory_info); |
| 3273 | case FILE_DIRECTORY_INFORMATION: |
| 3274 | return sizeof(struct file_directory_info); |
| 3275 | case FILE_NAMES_INFORMATION: |
| 3276 | return sizeof(struct file_names_info); |
| 3277 | case FILEID_FULL_DIRECTORY_INFORMATION: |
| 3278 | return sizeof(struct file_id_full_dir_info); |
| 3279 | case FILEID_BOTH_DIRECTORY_INFORMATION: |
| 3280 | return sizeof(struct file_id_both_directory_info); |
| 3281 | case SMB_FIND_FILE_POSIX_INFO: |
| 3282 | return sizeof(struct smb2_posix_info); |
| 3283 | default: |
| 3284 | return -EOPNOTSUPP; |
| 3285 | } |
| 3286 | } |
| 3287 | |
| 3288 | static int dentry_name(struct ksmbd_dir_info *d_info, int info_level) |
| 3289 | { |
| 3290 | switch (info_level) { |
| 3291 | case FILE_FULL_DIRECTORY_INFORMATION: |
| 3292 | { |
| 3293 | struct file_full_directory_info *ffdinfo; |
| 3294 | |
| 3295 | ffdinfo = (struct file_full_directory_info *)d_info->rptr; |
| 3296 | d_info->rptr += le32_to_cpu(ffdinfo->NextEntryOffset); |
| 3297 | d_info->name = ffdinfo->FileName; |
| 3298 | d_info->name_len = le32_to_cpu(ffdinfo->FileNameLength); |
| 3299 | return 0; |
| 3300 | } |
| 3301 | case FILE_BOTH_DIRECTORY_INFORMATION: |
| 3302 | { |
| 3303 | struct file_both_directory_info *fbdinfo; |
| 3304 | |
| 3305 | fbdinfo = (struct file_both_directory_info *)d_info->rptr; |
| 3306 | d_info->rptr += le32_to_cpu(fbdinfo->NextEntryOffset); |
| 3307 | d_info->name = fbdinfo->FileName; |
| 3308 | d_info->name_len = le32_to_cpu(fbdinfo->FileNameLength); |
| 3309 | return 0; |
| 3310 | } |
| 3311 | case FILE_DIRECTORY_INFORMATION: |
| 3312 | { |
| 3313 | struct file_directory_info *fdinfo; |
| 3314 | |
| 3315 | fdinfo = (struct file_directory_info *)d_info->rptr; |
| 3316 | d_info->rptr += le32_to_cpu(fdinfo->NextEntryOffset); |
| 3317 | d_info->name = fdinfo->FileName; |
| 3318 | d_info->name_len = le32_to_cpu(fdinfo->FileNameLength); |
| 3319 | return 0; |
| 3320 | } |
| 3321 | case FILE_NAMES_INFORMATION: |
| 3322 | { |
| 3323 | struct file_names_info *fninfo; |
| 3324 | |
| 3325 | fninfo = (struct file_names_info *)d_info->rptr; |
| 3326 | d_info->rptr += le32_to_cpu(fninfo->NextEntryOffset); |
| 3327 | d_info->name = fninfo->FileName; |
| 3328 | d_info->name_len = le32_to_cpu(fninfo->FileNameLength); |
| 3329 | return 0; |
| 3330 | } |
| 3331 | case FILEID_FULL_DIRECTORY_INFORMATION: |
| 3332 | { |
| 3333 | struct file_id_full_dir_info *dinfo; |
| 3334 | |
| 3335 | dinfo = (struct file_id_full_dir_info *)d_info->rptr; |
| 3336 | d_info->rptr += le32_to_cpu(dinfo->NextEntryOffset); |
| 3337 | d_info->name = dinfo->FileName; |
| 3338 | d_info->name_len = le32_to_cpu(dinfo->FileNameLength); |
| 3339 | return 0; |
| 3340 | } |
| 3341 | case FILEID_BOTH_DIRECTORY_INFORMATION: |
| 3342 | { |
| 3343 | struct file_id_both_directory_info *fibdinfo; |
| 3344 | |
| 3345 | fibdinfo = (struct file_id_both_directory_info *)d_info->rptr; |
| 3346 | d_info->rptr += le32_to_cpu(fibdinfo->NextEntryOffset); |
| 3347 | d_info->name = fibdinfo->FileName; |
| 3348 | d_info->name_len = le32_to_cpu(fibdinfo->FileNameLength); |
| 3349 | return 0; |
| 3350 | } |
| 3351 | case SMB_FIND_FILE_POSIX_INFO: |
| 3352 | { |
| 3353 | struct smb2_posix_info *posix_info; |
| 3354 | |
| 3355 | posix_info = (struct smb2_posix_info *)d_info->rptr; |
| 3356 | d_info->rptr += le32_to_cpu(posix_info->NextEntryOffset); |
| 3357 | d_info->name = posix_info->name; |
| 3358 | d_info->name_len = le32_to_cpu(posix_info->name_len); |
| 3359 | return 0; |
| 3360 | } |
| 3361 | default: |
| 3362 | return -EINVAL; |
| 3363 | } |
| 3364 | } |
| 3365 | |
| 3366 | /** |
| 3367 | * smb2_populate_readdir_entry() - encode directory entry in smb2 response |
| 3368 | * buffer |
| 3369 | * @conn: connection instance |
| 3370 | * @info_level: smb information level |
| 3371 | * @d_info: structure included variables for query dir |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 3372 | * @user_ns: user namespace |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3373 | * @ksmbd_kstat: ksmbd wrapper of dirent stat information |
| 3374 | * |
| 3375 | * if directory has many entries, find first can't read it fully. |
| 3376 | * find next might be called multiple times to read remaining dir entries |
| 3377 | * |
| 3378 | * Return: 0 on success, otherwise error |
| 3379 | */ |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3380 | 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] | 3381 | struct ksmbd_dir_info *d_info, |
| 3382 | struct ksmbd_kstat *ksmbd_kstat) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3383 | { |
| 3384 | int next_entry_offset = 0; |
| 3385 | char *conv_name; |
| 3386 | int conv_len; |
| 3387 | void *kstat; |
Namjae Jeon | dac0ec6 | 2021-07-07 14:57:24 +0900 | [diff] [blame] | 3388 | int struct_sz, rc = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3389 | |
| 3390 | conv_name = ksmbd_convert_dir_info_name(d_info, |
| 3391 | conn->local_nls, |
| 3392 | &conv_len); |
| 3393 | if (!conv_name) |
| 3394 | return -ENOMEM; |
| 3395 | |
| 3396 | /* Somehow the name has only terminating NULL bytes */ |
| 3397 | if (conv_len < 0) { |
Namjae Jeon | dac0ec6 | 2021-07-07 14:57:24 +0900 | [diff] [blame] | 3398 | rc = -EINVAL; |
| 3399 | goto free_conv_name; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3400 | } |
| 3401 | |
| 3402 | struct_sz = readdir_info_level_struct_sz(info_level); |
| 3403 | next_entry_offset = ALIGN(struct_sz - 1 + conv_len, |
| 3404 | KSMBD_DIR_INFO_ALIGNMENT); |
| 3405 | |
| 3406 | if (next_entry_offset > d_info->out_buf_len) { |
| 3407 | d_info->out_buf_len = 0; |
Namjae Jeon | dac0ec6 | 2021-07-07 14:57:24 +0900 | [diff] [blame] | 3408 | rc = -ENOSPC; |
| 3409 | goto free_conv_name; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3410 | } |
| 3411 | |
| 3412 | kstat = d_info->wptr; |
| 3413 | if (info_level != FILE_NAMES_INFORMATION) |
| 3414 | kstat = ksmbd_vfs_init_kstat(&d_info->wptr, ksmbd_kstat); |
| 3415 | |
| 3416 | switch (info_level) { |
| 3417 | case FILE_FULL_DIRECTORY_INFORMATION: |
| 3418 | { |
| 3419 | struct file_full_directory_info *ffdinfo; |
| 3420 | |
| 3421 | ffdinfo = (struct file_full_directory_info *)kstat; |
| 3422 | ffdinfo->FileNameLength = cpu_to_le32(conv_len); |
| 3423 | ffdinfo->EaSize = |
| 3424 | smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode); |
| 3425 | if (ffdinfo->EaSize) |
| 3426 | ffdinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE; |
| 3427 | if (d_info->hide_dot_file && d_info->name[0] == '.') |
| 3428 | ffdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE; |
| 3429 | memcpy(ffdinfo->FileName, conv_name, conv_len); |
| 3430 | ffdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3431 | break; |
| 3432 | } |
| 3433 | case FILE_BOTH_DIRECTORY_INFORMATION: |
| 3434 | { |
| 3435 | struct file_both_directory_info *fbdinfo; |
| 3436 | |
| 3437 | fbdinfo = (struct file_both_directory_info *)kstat; |
| 3438 | fbdinfo->FileNameLength = cpu_to_le32(conv_len); |
| 3439 | fbdinfo->EaSize = |
| 3440 | smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode); |
| 3441 | if (fbdinfo->EaSize) |
| 3442 | fbdinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE; |
| 3443 | fbdinfo->ShortNameLength = 0; |
| 3444 | fbdinfo->Reserved = 0; |
| 3445 | if (d_info->hide_dot_file && d_info->name[0] == '.') |
| 3446 | fbdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE; |
| 3447 | memcpy(fbdinfo->FileName, conv_name, conv_len); |
| 3448 | fbdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3449 | break; |
| 3450 | } |
| 3451 | case FILE_DIRECTORY_INFORMATION: |
| 3452 | { |
| 3453 | struct file_directory_info *fdinfo; |
| 3454 | |
| 3455 | fdinfo = (struct file_directory_info *)kstat; |
| 3456 | fdinfo->FileNameLength = cpu_to_le32(conv_len); |
| 3457 | if (d_info->hide_dot_file && d_info->name[0] == '.') |
| 3458 | fdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE; |
| 3459 | memcpy(fdinfo->FileName, conv_name, conv_len); |
| 3460 | fdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3461 | break; |
| 3462 | } |
| 3463 | case FILE_NAMES_INFORMATION: |
| 3464 | { |
| 3465 | struct file_names_info *fninfo; |
| 3466 | |
| 3467 | fninfo = (struct file_names_info *)kstat; |
| 3468 | fninfo->FileNameLength = cpu_to_le32(conv_len); |
| 3469 | memcpy(fninfo->FileName, conv_name, conv_len); |
| 3470 | fninfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3471 | break; |
| 3472 | } |
| 3473 | case FILEID_FULL_DIRECTORY_INFORMATION: |
| 3474 | { |
| 3475 | struct file_id_full_dir_info *dinfo; |
| 3476 | |
| 3477 | dinfo = (struct file_id_full_dir_info *)kstat; |
| 3478 | dinfo->FileNameLength = cpu_to_le32(conv_len); |
| 3479 | dinfo->EaSize = |
| 3480 | smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode); |
| 3481 | if (dinfo->EaSize) |
| 3482 | dinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE; |
| 3483 | dinfo->Reserved = 0; |
| 3484 | dinfo->UniqueId = cpu_to_le64(ksmbd_kstat->kstat->ino); |
| 3485 | if (d_info->hide_dot_file && d_info->name[0] == '.') |
| 3486 | dinfo->ExtFileAttributes |= ATTR_HIDDEN_LE; |
| 3487 | memcpy(dinfo->FileName, conv_name, conv_len); |
| 3488 | dinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3489 | break; |
| 3490 | } |
| 3491 | case FILEID_BOTH_DIRECTORY_INFORMATION: |
| 3492 | { |
| 3493 | struct file_id_both_directory_info *fibdinfo; |
| 3494 | |
| 3495 | fibdinfo = (struct file_id_both_directory_info *)kstat; |
| 3496 | fibdinfo->FileNameLength = cpu_to_le32(conv_len); |
| 3497 | fibdinfo->EaSize = |
| 3498 | smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode); |
| 3499 | if (fibdinfo->EaSize) |
| 3500 | fibdinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE; |
| 3501 | fibdinfo->UniqueId = cpu_to_le64(ksmbd_kstat->kstat->ino); |
| 3502 | fibdinfo->ShortNameLength = 0; |
| 3503 | fibdinfo->Reserved = 0; |
| 3504 | fibdinfo->Reserved2 = cpu_to_le16(0); |
| 3505 | if (d_info->hide_dot_file && d_info->name[0] == '.') |
| 3506 | fibdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE; |
| 3507 | memcpy(fibdinfo->FileName, conv_name, conv_len); |
| 3508 | fibdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3509 | break; |
| 3510 | } |
| 3511 | case SMB_FIND_FILE_POSIX_INFO: |
| 3512 | { |
| 3513 | struct smb2_posix_info *posix_info; |
| 3514 | u64 time; |
| 3515 | |
| 3516 | posix_info = (struct smb2_posix_info *)kstat; |
| 3517 | posix_info->Ignored = 0; |
| 3518 | posix_info->CreationTime = cpu_to_le64(ksmbd_kstat->create_time); |
| 3519 | time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->ctime); |
| 3520 | posix_info->ChangeTime = cpu_to_le64(time); |
| 3521 | time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->atime); |
| 3522 | posix_info->LastAccessTime = cpu_to_le64(time); |
| 3523 | time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->mtime); |
| 3524 | posix_info->LastWriteTime = cpu_to_le64(time); |
| 3525 | posix_info->EndOfFile = cpu_to_le64(ksmbd_kstat->kstat->size); |
| 3526 | posix_info->AllocationSize = cpu_to_le64(ksmbd_kstat->kstat->blocks << 9); |
| 3527 | posix_info->DeviceId = cpu_to_le32(ksmbd_kstat->kstat->rdev); |
| 3528 | posix_info->HardLinks = cpu_to_le32(ksmbd_kstat->kstat->nlink); |
| 3529 | posix_info->Mode = cpu_to_le32(ksmbd_kstat->kstat->mode); |
| 3530 | posix_info->Inode = cpu_to_le64(ksmbd_kstat->kstat->ino); |
| 3531 | posix_info->DosAttributes = |
| 3532 | S_ISDIR(ksmbd_kstat->kstat->mode) ? ATTR_DIRECTORY_LE : ATTR_ARCHIVE_LE; |
| 3533 | if (d_info->hide_dot_file && d_info->name[0] == '.') |
| 3534 | posix_info->DosAttributes |= ATTR_HIDDEN_LE; |
Christian Brauner | 475d6f9 | 2021-08-23 17:13:48 +0200 | [diff] [blame] | 3535 | 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] | 3536 | SIDNFS_USER, (struct smb_sid *)&posix_info->SidBuffer[0]); |
Christian Brauner | 475d6f9 | 2021-08-23 17:13:48 +0200 | [diff] [blame] | 3537 | 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] | 3538 | SIDNFS_GROUP, (struct smb_sid *)&posix_info->SidBuffer[20]); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3539 | memcpy(posix_info->name, conv_name, conv_len); |
| 3540 | posix_info->name_len = cpu_to_le32(conv_len); |
| 3541 | posix_info->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3542 | break; |
| 3543 | } |
| 3544 | |
| 3545 | } /* switch (info_level) */ |
| 3546 | |
| 3547 | d_info->last_entry_offset = d_info->data_count; |
| 3548 | d_info->data_count += next_entry_offset; |
Marios Makassikis | e7735c8 | 2021-05-06 11:40:02 +0900 | [diff] [blame] | 3549 | d_info->out_buf_len -= next_entry_offset; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3550 | d_info->wptr += next_entry_offset; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3551 | |
| 3552 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3553 | "info_level : %d, buf_len :%d, next_offset : %d, data_count : %d\n", |
| 3554 | info_level, d_info->out_buf_len, |
| 3555 | next_entry_offset, d_info->data_count); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3556 | |
Namjae Jeon | dac0ec6 | 2021-07-07 14:57:24 +0900 | [diff] [blame] | 3557 | free_conv_name: |
| 3558 | kfree(conv_name); |
| 3559 | return rc; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3560 | } |
| 3561 | |
| 3562 | struct smb2_query_dir_private { |
| 3563 | struct ksmbd_work *work; |
| 3564 | char *search_pattern; |
| 3565 | struct ksmbd_file *dir_fp; |
| 3566 | |
| 3567 | struct ksmbd_dir_info *d_info; |
| 3568 | int info_level; |
| 3569 | }; |
| 3570 | |
| 3571 | static void lock_dir(struct ksmbd_file *dir_fp) |
| 3572 | { |
| 3573 | struct dentry *dir = dir_fp->filp->f_path.dentry; |
| 3574 | |
| 3575 | inode_lock_nested(d_inode(dir), I_MUTEX_PARENT); |
| 3576 | } |
| 3577 | |
| 3578 | static void unlock_dir(struct ksmbd_file *dir_fp) |
| 3579 | { |
| 3580 | struct dentry *dir = dir_fp->filp->f_path.dentry; |
| 3581 | |
| 3582 | inode_unlock(d_inode(dir)); |
| 3583 | } |
| 3584 | |
| 3585 | static int process_query_dir_entries(struct smb2_query_dir_private *priv) |
| 3586 | { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 3587 | 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] | 3588 | struct kstat kstat; |
| 3589 | struct ksmbd_kstat ksmbd_kstat; |
| 3590 | int rc; |
| 3591 | int i; |
| 3592 | |
| 3593 | for (i = 0; i < priv->d_info->num_entry; i++) { |
| 3594 | struct dentry *dent; |
| 3595 | |
| 3596 | if (dentry_name(priv->d_info, priv->info_level)) |
| 3597 | return -EINVAL; |
| 3598 | |
| 3599 | lock_dir(priv->dir_fp); |
Christian Brauner | da1e7ad | 2021-08-23 17:13:47 +0200 | [diff] [blame] | 3600 | dent = lookup_one(user_ns, priv->d_info->name, |
| 3601 | priv->dir_fp->filp->f_path.dentry, |
| 3602 | priv->d_info->name_len); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3603 | unlock_dir(priv->dir_fp); |
| 3604 | |
| 3605 | if (IS_ERR(dent)) { |
| 3606 | ksmbd_debug(SMB, "Cannot lookup `%s' [%ld]\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3607 | priv->d_info->name, |
| 3608 | PTR_ERR(dent)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3609 | continue; |
| 3610 | } |
| 3611 | if (unlikely(d_is_negative(dent))) { |
| 3612 | dput(dent); |
| 3613 | ksmbd_debug(SMB, "Negative dentry `%s'\n", |
| 3614 | priv->d_info->name); |
| 3615 | continue; |
| 3616 | } |
| 3617 | |
| 3618 | ksmbd_kstat.kstat = &kstat; |
| 3619 | if (priv->info_level != FILE_NAMES_INFORMATION) |
| 3620 | ksmbd_vfs_fill_dentry_attrs(priv->work, |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 3621 | user_ns, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3622 | dent, |
| 3623 | &ksmbd_kstat); |
| 3624 | |
| 3625 | rc = smb2_populate_readdir_entry(priv->work->conn, |
| 3626 | priv->info_level, |
| 3627 | priv->d_info, |
| 3628 | &ksmbd_kstat); |
| 3629 | dput(dent); |
| 3630 | if (rc) |
| 3631 | return rc; |
| 3632 | } |
| 3633 | return 0; |
| 3634 | } |
| 3635 | |
| 3636 | static int reserve_populate_dentry(struct ksmbd_dir_info *d_info, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3637 | int info_level) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3638 | { |
| 3639 | int struct_sz; |
| 3640 | int conv_len; |
| 3641 | int next_entry_offset; |
| 3642 | |
| 3643 | struct_sz = readdir_info_level_struct_sz(info_level); |
| 3644 | if (struct_sz == -EOPNOTSUPP) |
| 3645 | return -EOPNOTSUPP; |
| 3646 | |
| 3647 | conv_len = (d_info->name_len + 1) * 2; |
| 3648 | next_entry_offset = ALIGN(struct_sz - 1 + conv_len, |
| 3649 | KSMBD_DIR_INFO_ALIGNMENT); |
| 3650 | |
| 3651 | if (next_entry_offset > d_info->out_buf_len) { |
| 3652 | d_info->out_buf_len = 0; |
| 3653 | return -ENOSPC; |
| 3654 | } |
| 3655 | |
| 3656 | switch (info_level) { |
| 3657 | case FILE_FULL_DIRECTORY_INFORMATION: |
| 3658 | { |
| 3659 | struct file_full_directory_info *ffdinfo; |
| 3660 | |
| 3661 | ffdinfo = (struct file_full_directory_info *)d_info->wptr; |
| 3662 | memcpy(ffdinfo->FileName, d_info->name, d_info->name_len); |
| 3663 | ffdinfo->FileName[d_info->name_len] = 0x00; |
| 3664 | ffdinfo->FileNameLength = cpu_to_le32(d_info->name_len); |
| 3665 | ffdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3666 | break; |
| 3667 | } |
| 3668 | case FILE_BOTH_DIRECTORY_INFORMATION: |
| 3669 | { |
| 3670 | struct file_both_directory_info *fbdinfo; |
| 3671 | |
| 3672 | fbdinfo = (struct file_both_directory_info *)d_info->wptr; |
| 3673 | memcpy(fbdinfo->FileName, d_info->name, d_info->name_len); |
| 3674 | fbdinfo->FileName[d_info->name_len] = 0x00; |
| 3675 | fbdinfo->FileNameLength = cpu_to_le32(d_info->name_len); |
| 3676 | fbdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3677 | break; |
| 3678 | } |
| 3679 | case FILE_DIRECTORY_INFORMATION: |
| 3680 | { |
| 3681 | struct file_directory_info *fdinfo; |
| 3682 | |
| 3683 | fdinfo = (struct file_directory_info *)d_info->wptr; |
| 3684 | memcpy(fdinfo->FileName, d_info->name, d_info->name_len); |
| 3685 | fdinfo->FileName[d_info->name_len] = 0x00; |
| 3686 | fdinfo->FileNameLength = cpu_to_le32(d_info->name_len); |
| 3687 | fdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3688 | break; |
| 3689 | } |
| 3690 | case FILE_NAMES_INFORMATION: |
| 3691 | { |
| 3692 | struct file_names_info *fninfo; |
| 3693 | |
| 3694 | fninfo = (struct file_names_info *)d_info->wptr; |
| 3695 | memcpy(fninfo->FileName, d_info->name, d_info->name_len); |
| 3696 | fninfo->FileName[d_info->name_len] = 0x00; |
| 3697 | fninfo->FileNameLength = cpu_to_le32(d_info->name_len); |
| 3698 | fninfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3699 | break; |
| 3700 | } |
| 3701 | case FILEID_FULL_DIRECTORY_INFORMATION: |
| 3702 | { |
| 3703 | struct file_id_full_dir_info *dinfo; |
| 3704 | |
| 3705 | dinfo = (struct file_id_full_dir_info *)d_info->wptr; |
| 3706 | memcpy(dinfo->FileName, d_info->name, d_info->name_len); |
| 3707 | dinfo->FileName[d_info->name_len] = 0x00; |
| 3708 | dinfo->FileNameLength = cpu_to_le32(d_info->name_len); |
| 3709 | dinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3710 | break; |
| 3711 | } |
| 3712 | case FILEID_BOTH_DIRECTORY_INFORMATION: |
| 3713 | { |
| 3714 | struct file_id_both_directory_info *fibdinfo; |
| 3715 | |
| 3716 | fibdinfo = (struct file_id_both_directory_info *)d_info->wptr; |
| 3717 | memcpy(fibdinfo->FileName, d_info->name, d_info->name_len); |
| 3718 | fibdinfo->FileName[d_info->name_len] = 0x00; |
| 3719 | fibdinfo->FileNameLength = cpu_to_le32(d_info->name_len); |
| 3720 | fibdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3721 | break; |
| 3722 | } |
| 3723 | case SMB_FIND_FILE_POSIX_INFO: |
| 3724 | { |
| 3725 | struct smb2_posix_info *posix_info; |
| 3726 | |
| 3727 | posix_info = (struct smb2_posix_info *)d_info->wptr; |
| 3728 | memcpy(posix_info->name, d_info->name, d_info->name_len); |
| 3729 | posix_info->name[d_info->name_len] = 0x00; |
| 3730 | posix_info->name_len = cpu_to_le32(d_info->name_len); |
| 3731 | posix_info->NextEntryOffset = |
| 3732 | cpu_to_le32(next_entry_offset); |
| 3733 | break; |
| 3734 | } |
| 3735 | } /* switch (info_level) */ |
| 3736 | |
| 3737 | d_info->num_entry++; |
| 3738 | d_info->out_buf_len -= next_entry_offset; |
| 3739 | d_info->wptr += next_entry_offset; |
| 3740 | return 0; |
| 3741 | } |
| 3742 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3743 | 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] | 3744 | loff_t offset, u64 ino, unsigned int d_type) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3745 | { |
| 3746 | struct ksmbd_readdir_data *buf; |
| 3747 | struct smb2_query_dir_private *priv; |
| 3748 | struct ksmbd_dir_info *d_info; |
| 3749 | int rc; |
| 3750 | |
| 3751 | buf = container_of(ctx, struct ksmbd_readdir_data, ctx); |
| 3752 | priv = buf->private; |
| 3753 | d_info = priv->d_info; |
| 3754 | |
| 3755 | /* dot and dotdot entries are already reserved */ |
| 3756 | if (!strcmp(".", name) || !strcmp("..", name)) |
| 3757 | return 0; |
| 3758 | if (ksmbd_share_veto_filename(priv->work->tcon->share_conf, name)) |
| 3759 | return 0; |
Namjae Jeon | b24c933 | 2021-03-21 17:32:19 +0900 | [diff] [blame] | 3760 | if (!match_pattern(name, namlen, priv->search_pattern)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3761 | return 0; |
| 3762 | |
| 3763 | d_info->name = name; |
| 3764 | d_info->name_len = namlen; |
| 3765 | rc = reserve_populate_dentry(d_info, priv->info_level); |
| 3766 | if (rc) |
| 3767 | return rc; |
| 3768 | if (d_info->flags & SMB2_RETURN_SINGLE_ENTRY) { |
| 3769 | d_info->out_buf_len = 0; |
| 3770 | return 0; |
| 3771 | } |
| 3772 | return 0; |
| 3773 | } |
| 3774 | |
| 3775 | static void restart_ctx(struct dir_context *ctx) |
| 3776 | { |
| 3777 | ctx->pos = 0; |
| 3778 | } |
| 3779 | |
| 3780 | static int verify_info_level(int info_level) |
| 3781 | { |
| 3782 | switch (info_level) { |
| 3783 | case FILE_FULL_DIRECTORY_INFORMATION: |
| 3784 | case FILE_BOTH_DIRECTORY_INFORMATION: |
| 3785 | case FILE_DIRECTORY_INFORMATION: |
| 3786 | case FILE_NAMES_INFORMATION: |
| 3787 | case FILEID_FULL_DIRECTORY_INFORMATION: |
| 3788 | case FILEID_BOTH_DIRECTORY_INFORMATION: |
| 3789 | case SMB_FIND_FILE_POSIX_INFO: |
| 3790 | break; |
| 3791 | default: |
| 3792 | return -EOPNOTSUPP; |
| 3793 | } |
| 3794 | |
| 3795 | return 0; |
| 3796 | } |
| 3797 | |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 3798 | static int smb2_calc_max_out_buf_len(struct ksmbd_work *work, |
| 3799 | unsigned short hdr2_len, |
| 3800 | unsigned int out_buf_len) |
| 3801 | { |
| 3802 | int free_len; |
| 3803 | |
| 3804 | if (out_buf_len > work->conn->vals->max_trans_size) |
| 3805 | return -EINVAL; |
| 3806 | |
| 3807 | free_len = (int)(work->response_sz - |
| 3808 | (get_rfc1002_len(work->response_buf) + 4)) - |
| 3809 | hdr2_len; |
| 3810 | if (free_len < 0) |
| 3811 | return -EINVAL; |
| 3812 | |
| 3813 | return min_t(int, out_buf_len, free_len); |
| 3814 | } |
| 3815 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3816 | int smb2_query_dir(struct ksmbd_work *work) |
| 3817 | { |
| 3818 | struct ksmbd_conn *conn = work->conn; |
| 3819 | struct smb2_query_directory_req *req; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 3820 | struct smb2_query_directory_rsp *rsp; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3821 | struct ksmbd_share_config *share = work->tcon->share_conf; |
| 3822 | struct ksmbd_file *dir_fp = NULL; |
| 3823 | struct ksmbd_dir_info d_info; |
| 3824 | int rc = 0; |
| 3825 | char *srch_ptr = NULL; |
| 3826 | unsigned char srch_flag; |
| 3827 | int buffer_sz; |
| 3828 | struct smb2_query_dir_private query_dir_private = {NULL, }; |
| 3829 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3830 | WORK_BUFFERS(work, req, rsp); |
| 3831 | |
| 3832 | if (ksmbd_override_fsids(work)) { |
| 3833 | rsp->hdr.Status = STATUS_NO_MEMORY; |
| 3834 | smb2_set_err_rsp(work); |
| 3835 | return -ENOMEM; |
| 3836 | } |
| 3837 | |
| 3838 | rc = verify_info_level(req->FileInformationClass); |
| 3839 | if (rc) { |
| 3840 | rc = -EFAULT; |
| 3841 | goto err_out2; |
| 3842 | } |
| 3843 | |
| 3844 | dir_fp = ksmbd_lookup_fd_slow(work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3845 | le64_to_cpu(req->VolatileFileId), |
| 3846 | le64_to_cpu(req->PersistentFileId)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3847 | if (!dir_fp) { |
| 3848 | rc = -EBADF; |
| 3849 | goto err_out2; |
| 3850 | } |
| 3851 | |
| 3852 | if (!(dir_fp->daccess & FILE_LIST_DIRECTORY_LE) || |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 3853 | inode_permission(file_mnt_user_ns(dir_fp->filp), |
| 3854 | file_inode(dir_fp->filp), |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3855 | MAY_READ | MAY_EXEC)) { |
Namjae Jeon | 493fa2f | 2021-06-29 09:22:16 +0900 | [diff] [blame] | 3856 | pr_err("no right to enumerate directory (%pd)\n", |
| 3857 | dir_fp->filp->f_path.dentry); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3858 | rc = -EACCES; |
| 3859 | goto err_out2; |
| 3860 | } |
| 3861 | |
| 3862 | if (!S_ISDIR(file_inode(dir_fp->filp)->i_mode)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 3863 | pr_err("can't do query dir for a file\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3864 | rc = -EINVAL; |
| 3865 | goto err_out2; |
| 3866 | } |
| 3867 | |
| 3868 | srch_flag = req->Flags; |
| 3869 | srch_ptr = smb_strndup_from_utf16(req->Buffer, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3870 | le16_to_cpu(req->FileNameLength), 1, |
| 3871 | conn->local_nls); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3872 | if (IS_ERR(srch_ptr)) { |
| 3873 | ksmbd_debug(SMB, "Search Pattern not found\n"); |
| 3874 | rc = -EINVAL; |
| 3875 | goto err_out2; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3876 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3877 | ksmbd_debug(SMB, "Search pattern is %s\n", srch_ptr); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3878 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3879 | |
| 3880 | ksmbd_debug(SMB, "Directory name is %s\n", dir_fp->filename); |
| 3881 | |
| 3882 | if (srch_flag & SMB2_REOPEN || srch_flag & SMB2_RESTART_SCANS) { |
| 3883 | ksmbd_debug(SMB, "Restart directory scan\n"); |
| 3884 | generic_file_llseek(dir_fp->filp, 0, SEEK_SET); |
| 3885 | restart_ctx(&dir_fp->readdir_data.ctx); |
| 3886 | } |
| 3887 | |
| 3888 | memset(&d_info, 0, sizeof(struct ksmbd_dir_info)); |
| 3889 | d_info.wptr = (char *)rsp->Buffer; |
| 3890 | d_info.rptr = (char *)rsp->Buffer; |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 3891 | d_info.out_buf_len = |
| 3892 | smb2_calc_max_out_buf_len(work, 8, |
| 3893 | le32_to_cpu(req->OutputBufferLength)); |
| 3894 | if (d_info.out_buf_len < 0) { |
| 3895 | rc = -EINVAL; |
| 3896 | goto err_out; |
| 3897 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3898 | d_info.flags = srch_flag; |
| 3899 | |
| 3900 | /* |
| 3901 | * reserve dot and dotdot entries in head of buffer |
| 3902 | * in first response |
| 3903 | */ |
| 3904 | rc = ksmbd_populate_dot_dotdot_entries(work, req->FileInformationClass, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3905 | dir_fp, &d_info, srch_ptr, |
| 3906 | smb2_populate_readdir_entry); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3907 | if (rc == -ENOSPC) |
| 3908 | rc = 0; |
| 3909 | else if (rc) |
| 3910 | goto err_out; |
| 3911 | |
| 3912 | if (test_share_config_flag(share, KSMBD_SHARE_FLAG_HIDE_DOT_FILES)) |
| 3913 | d_info.hide_dot_file = true; |
| 3914 | |
| 3915 | buffer_sz = d_info.out_buf_len; |
| 3916 | d_info.rptr = d_info.wptr; |
| 3917 | query_dir_private.work = work; |
| 3918 | query_dir_private.search_pattern = srch_ptr; |
| 3919 | query_dir_private.dir_fp = dir_fp; |
| 3920 | query_dir_private.d_info = &d_info; |
| 3921 | query_dir_private.info_level = req->FileInformationClass; |
| 3922 | dir_fp->readdir_data.private = &query_dir_private; |
| 3923 | set_ctx_actor(&dir_fp->readdir_data.ctx, __query_dir); |
| 3924 | |
Namjae Jeon | e8c0619 | 2021-06-22 11:06:11 +0900 | [diff] [blame] | 3925 | rc = iterate_dir(dir_fp->filp, &dir_fp->readdir_data.ctx); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3926 | if (rc == 0) |
| 3927 | restart_ctx(&dir_fp->readdir_data.ctx); |
| 3928 | if (rc == -ENOSPC) |
| 3929 | rc = 0; |
| 3930 | if (rc) |
| 3931 | goto err_out; |
| 3932 | |
| 3933 | d_info.wptr = d_info.rptr; |
| 3934 | d_info.out_buf_len = buffer_sz; |
| 3935 | rc = process_query_dir_entries(&query_dir_private); |
| 3936 | if (rc) |
| 3937 | goto err_out; |
| 3938 | |
| 3939 | if (!d_info.data_count && d_info.out_buf_len >= 0) { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3940 | if (srch_flag & SMB2_RETURN_SINGLE_ENTRY && !is_asterisk(srch_ptr)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3941 | rsp->hdr.Status = STATUS_NO_SUCH_FILE; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3942 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3943 | dir_fp->dot_dotdot[0] = dir_fp->dot_dotdot[1] = 0; |
| 3944 | rsp->hdr.Status = STATUS_NO_MORE_FILES; |
| 3945 | } |
| 3946 | rsp->StructureSize = cpu_to_le16(9); |
| 3947 | rsp->OutputBufferOffset = cpu_to_le16(0); |
| 3948 | rsp->OutputBufferLength = cpu_to_le32(0); |
| 3949 | rsp->Buffer[0] = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 3950 | inc_rfc1001_len(work->response_buf, 9); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3951 | } else { |
| 3952 | ((struct file_directory_info *) |
| 3953 | ((char *)rsp->Buffer + d_info.last_entry_offset)) |
| 3954 | ->NextEntryOffset = 0; |
| 3955 | |
| 3956 | rsp->StructureSize = cpu_to_le16(9); |
| 3957 | rsp->OutputBufferOffset = cpu_to_le16(72); |
| 3958 | rsp->OutputBufferLength = cpu_to_le32(d_info.data_count); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 3959 | inc_rfc1001_len(work->response_buf, 8 + d_info.data_count); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3960 | } |
| 3961 | |
| 3962 | kfree(srch_ptr); |
| 3963 | ksmbd_fd_put(work, dir_fp); |
| 3964 | ksmbd_revert_fsids(work); |
| 3965 | return 0; |
| 3966 | |
| 3967 | err_out: |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 3968 | pr_err("error while processing smb2 query dir rc = %d\n", rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3969 | kfree(srch_ptr); |
| 3970 | |
| 3971 | err_out2: |
| 3972 | if (rc == -EINVAL) |
| 3973 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 3974 | else if (rc == -EACCES) |
| 3975 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 3976 | else if (rc == -ENOENT) |
| 3977 | rsp->hdr.Status = STATUS_NO_SUCH_FILE; |
| 3978 | else if (rc == -EBADF) |
| 3979 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 3980 | else if (rc == -ENOMEM) |
| 3981 | rsp->hdr.Status = STATUS_NO_MEMORY; |
| 3982 | else if (rc == -EFAULT) |
| 3983 | rsp->hdr.Status = STATUS_INVALID_INFO_CLASS; |
| 3984 | if (!rsp->hdr.Status) |
| 3985 | rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR; |
| 3986 | |
| 3987 | smb2_set_err_rsp(work); |
| 3988 | ksmbd_fd_put(work, dir_fp); |
| 3989 | ksmbd_revert_fsids(work); |
| 3990 | return 0; |
| 3991 | } |
| 3992 | |
| 3993 | /** |
| 3994 | * buffer_check_err() - helper function to check buffer errors |
| 3995 | * @reqOutputBufferLength: max buffer length expected in command response |
| 3996 | * @rsp: query info response buffer contains output buffer length |
| 3997 | * @infoclass_size: query info class response buffer size |
| 3998 | * |
| 3999 | * Return: 0 on success, otherwise error |
| 4000 | */ |
| 4001 | static int buffer_check_err(int reqOutputBufferLength, |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4002 | struct smb2_query_info_rsp *rsp, |
| 4003 | void *rsp_org, int infoclass_size) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4004 | { |
| 4005 | if (reqOutputBufferLength < le32_to_cpu(rsp->OutputBufferLength)) { |
| 4006 | if (reqOutputBufferLength < infoclass_size) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4007 | pr_err("Invalid Buffer Size Requested\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4008 | rsp->hdr.Status = STATUS_INFO_LENGTH_MISMATCH; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4009 | *(__be32 *)rsp_org = cpu_to_be32(sizeof(struct smb2_hdr)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4010 | return -EINVAL; |
| 4011 | } |
| 4012 | |
| 4013 | ksmbd_debug(SMB, "Buffer Overflow\n"); |
| 4014 | rsp->hdr.Status = STATUS_BUFFER_OVERFLOW; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4015 | *(__be32 *)rsp_org = cpu_to_be32(sizeof(struct smb2_hdr) + |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4016 | reqOutputBufferLength); |
| 4017 | rsp->OutputBufferLength = cpu_to_le32(reqOutputBufferLength); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4018 | } |
| 4019 | return 0; |
| 4020 | } |
| 4021 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4022 | static void get_standard_info_pipe(struct smb2_query_info_rsp *rsp, |
| 4023 | void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4024 | { |
| 4025 | struct smb2_file_standard_info *sinfo; |
| 4026 | |
| 4027 | sinfo = (struct smb2_file_standard_info *)rsp->Buffer; |
| 4028 | |
| 4029 | sinfo->AllocationSize = cpu_to_le64(4096); |
| 4030 | sinfo->EndOfFile = cpu_to_le64(0); |
| 4031 | sinfo->NumberOfLinks = cpu_to_le32(1); |
| 4032 | sinfo->DeletePending = 1; |
| 4033 | sinfo->Directory = 0; |
| 4034 | rsp->OutputBufferLength = |
| 4035 | cpu_to_le32(sizeof(struct smb2_file_standard_info)); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4036 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_standard_info)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4037 | } |
| 4038 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4039 | static void get_internal_info_pipe(struct smb2_query_info_rsp *rsp, u64 num, |
| 4040 | void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4041 | { |
| 4042 | struct smb2_file_internal_info *file_info; |
| 4043 | |
| 4044 | file_info = (struct smb2_file_internal_info *)rsp->Buffer; |
| 4045 | |
| 4046 | /* any unique number */ |
| 4047 | file_info->IndexNumber = cpu_to_le64(num | (1ULL << 63)); |
| 4048 | rsp->OutputBufferLength = |
| 4049 | cpu_to_le32(sizeof(struct smb2_file_internal_info)); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4050 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_internal_info)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4051 | } |
| 4052 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4053 | static int smb2_get_info_file_pipe(struct ksmbd_session *sess, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4054 | struct smb2_query_info_req *req, |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4055 | struct smb2_query_info_rsp *rsp, |
| 4056 | void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4057 | { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4058 | u64 id; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4059 | int rc; |
| 4060 | |
| 4061 | /* |
| 4062 | * Windows can sometime send query file info request on |
| 4063 | * pipe without opening it, checking error condition here |
| 4064 | */ |
| 4065 | id = le64_to_cpu(req->VolatileFileId); |
| 4066 | if (!ksmbd_session_rpc_method(sess, id)) |
| 4067 | return -ENOENT; |
| 4068 | |
| 4069 | ksmbd_debug(SMB, "FileInfoClass %u, FileId 0x%llx\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4070 | req->FileInfoClass, le64_to_cpu(req->VolatileFileId)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4071 | |
| 4072 | switch (req->FileInfoClass) { |
| 4073 | case FILE_STANDARD_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4074 | get_standard_info_pipe(rsp, rsp_org); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4075 | rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength), |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4076 | rsp, rsp_org, |
| 4077 | FILE_STANDARD_INFORMATION_SIZE); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4078 | break; |
| 4079 | case FILE_INTERNAL_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4080 | get_internal_info_pipe(rsp, id, rsp_org); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4081 | rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength), |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4082 | rsp, rsp_org, |
| 4083 | FILE_INTERNAL_INFORMATION_SIZE); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4084 | break; |
| 4085 | default: |
| 4086 | ksmbd_debug(SMB, "smb2_info_file_pipe for %u not supported\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4087 | req->FileInfoClass); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4088 | rc = -EOPNOTSUPP; |
| 4089 | } |
| 4090 | return rc; |
| 4091 | } |
| 4092 | |
| 4093 | /** |
| 4094 | * smb2_get_ea() - handler for smb2 get extended attribute command |
| 4095 | * @work: smb work containing query info command buffer |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 4096 | * @fp: ksmbd_file pointer |
| 4097 | * @req: get extended attribute request |
| 4098 | * @rsp: response buffer pointer |
| 4099 | * @rsp_org: base response buffer pointer in case of chained response |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4100 | * |
| 4101 | * Return: 0 on success, otherwise error |
| 4102 | */ |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4103 | 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] | 4104 | struct smb2_query_info_req *req, |
| 4105 | struct smb2_query_info_rsp *rsp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4106 | { |
| 4107 | struct smb2_ea_info *eainfo, *prev_eainfo; |
| 4108 | char *name, *ptr, *xattr_list = NULL, *buf; |
| 4109 | int rc, name_len, value_len, xattr_list_len, idx; |
| 4110 | ssize_t buf_free_len, alignment_bytes, next_offset, rsp_data_cnt = 0; |
| 4111 | struct smb2_ea_info_req *ea_req = NULL; |
| 4112 | struct path *path; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 4113 | struct user_namespace *user_ns = file_mnt_user_ns(fp->filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4114 | |
| 4115 | if (!(fp->daccess & FILE_READ_EA_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4116 | pr_err("Not permitted to read ext attr : 0x%x\n", |
| 4117 | fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4118 | return -EACCES; |
| 4119 | } |
| 4120 | |
| 4121 | path = &fp->filp->f_path; |
| 4122 | /* single EA entry is requested with given user.* name */ |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4123 | if (req->InputBufferLength) { |
Namjae Jeon | 6d56262 | 2021-09-18 18:45:12 +0900 | [diff] [blame] | 4124 | if (le32_to_cpu(req->InputBufferLength) < |
| 4125 | sizeof(struct smb2_ea_info_req)) |
| 4126 | return -EINVAL; |
| 4127 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4128 | ea_req = (struct smb2_ea_info_req *)req->Buffer; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4129 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4130 | /* need to send all EAs, if no specific EA is requested*/ |
| 4131 | if (le32_to_cpu(req->Flags) & SL_RETURN_SINGLE_ENTRY) |
| 4132 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4133 | "All EAs are requested but need to send single EA entry in rsp flags 0x%x\n", |
| 4134 | le32_to_cpu(req->Flags)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4135 | } |
| 4136 | |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 4137 | buf_free_len = |
| 4138 | smb2_calc_max_out_buf_len(work, 8, |
| 4139 | le32_to_cpu(req->OutputBufferLength)); |
| 4140 | if (buf_free_len < 0) |
| 4141 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4142 | |
| 4143 | rc = ksmbd_vfs_listxattr(path->dentry, &xattr_list); |
| 4144 | if (rc < 0) { |
| 4145 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 4146 | goto out; |
| 4147 | } else if (!rc) { /* there is no EA in the file */ |
| 4148 | ksmbd_debug(SMB, "no ea data in the file\n"); |
| 4149 | goto done; |
| 4150 | } |
| 4151 | xattr_list_len = rc; |
| 4152 | |
| 4153 | ptr = (char *)rsp->Buffer; |
| 4154 | eainfo = (struct smb2_ea_info *)ptr; |
| 4155 | prev_eainfo = eainfo; |
| 4156 | idx = 0; |
| 4157 | |
| 4158 | while (idx < xattr_list_len) { |
| 4159 | name = xattr_list + idx; |
| 4160 | name_len = strlen(name); |
| 4161 | |
| 4162 | ksmbd_debug(SMB, "%s, len %d\n", name, name_len); |
| 4163 | idx += name_len + 1; |
| 4164 | |
| 4165 | /* |
| 4166 | * CIFS does not support EA other than user.* namespace, |
| 4167 | * still keep the framework generic, to list other attrs |
| 4168 | * in future. |
| 4169 | */ |
| 4170 | if (strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) |
| 4171 | continue; |
| 4172 | |
| 4173 | if (!strncmp(&name[XATTR_USER_PREFIX_LEN], STREAM_PREFIX, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4174 | STREAM_PREFIX_LEN)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4175 | continue; |
| 4176 | |
| 4177 | if (req->InputBufferLength && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4178 | strncmp(&name[XATTR_USER_PREFIX_LEN], ea_req->name, |
| 4179 | ea_req->EaNameLength)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4180 | continue; |
| 4181 | |
| 4182 | if (!strncmp(&name[XATTR_USER_PREFIX_LEN], |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4183 | DOS_ATTRIBUTE_PREFIX, DOS_ATTRIBUTE_PREFIX_LEN)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4184 | continue; |
| 4185 | |
| 4186 | if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) |
| 4187 | name_len -= XATTR_USER_PREFIX_LEN; |
| 4188 | |
| 4189 | ptr = (char *)(&eainfo->name + name_len + 1); |
| 4190 | buf_free_len -= (offsetof(struct smb2_ea_info, name) + |
| 4191 | name_len + 1); |
| 4192 | /* bailout if xattr can't fit in buf_free_len */ |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 4193 | value_len = ksmbd_vfs_getxattr(user_ns, path->dentry, |
| 4194 | name, &buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4195 | if (value_len <= 0) { |
| 4196 | rc = -ENOENT; |
| 4197 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 4198 | goto out; |
| 4199 | } |
| 4200 | |
| 4201 | buf_free_len -= value_len; |
| 4202 | if (buf_free_len < 0) { |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 4203 | kfree(buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4204 | break; |
| 4205 | } |
| 4206 | |
| 4207 | memcpy(ptr, buf, value_len); |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 4208 | kfree(buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4209 | |
| 4210 | ptr += value_len; |
| 4211 | eainfo->Flags = 0; |
| 4212 | eainfo->EaNameLength = name_len; |
| 4213 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4214 | if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4215 | memcpy(eainfo->name, &name[XATTR_USER_PREFIX_LEN], |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4216 | name_len); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4217 | else |
| 4218 | memcpy(eainfo->name, name, name_len); |
| 4219 | |
| 4220 | eainfo->name[name_len] = '\0'; |
| 4221 | eainfo->EaValueLength = cpu_to_le16(value_len); |
| 4222 | next_offset = offsetof(struct smb2_ea_info, name) + |
| 4223 | name_len + 1 + value_len; |
| 4224 | |
| 4225 | /* align next xattr entry at 4 byte bundary */ |
| 4226 | alignment_bytes = ((next_offset + 3) & ~3) - next_offset; |
| 4227 | if (alignment_bytes) { |
| 4228 | memset(ptr, '\0', alignment_bytes); |
| 4229 | ptr += alignment_bytes; |
| 4230 | next_offset += alignment_bytes; |
| 4231 | buf_free_len -= alignment_bytes; |
| 4232 | } |
| 4233 | eainfo->NextEntryOffset = cpu_to_le32(next_offset); |
| 4234 | prev_eainfo = eainfo; |
| 4235 | eainfo = (struct smb2_ea_info *)ptr; |
| 4236 | rsp_data_cnt += next_offset; |
| 4237 | |
| 4238 | if (req->InputBufferLength) { |
| 4239 | ksmbd_debug(SMB, "single entry requested\n"); |
| 4240 | break; |
| 4241 | } |
| 4242 | } |
| 4243 | |
| 4244 | /* no more ea entries */ |
| 4245 | prev_eainfo->NextEntryOffset = 0; |
| 4246 | done: |
| 4247 | rc = 0; |
| 4248 | if (rsp_data_cnt == 0) |
| 4249 | rsp->hdr.Status = STATUS_NO_EAS_ON_FILE; |
| 4250 | rsp->OutputBufferLength = cpu_to_le32(rsp_data_cnt); |
| 4251 | inc_rfc1001_len(rsp_org, rsp_data_cnt); |
| 4252 | out: |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 4253 | kvfree(xattr_list); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4254 | return rc; |
| 4255 | } |
| 4256 | |
| 4257 | static void get_file_access_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4258 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4259 | { |
| 4260 | struct smb2_file_access_info *file_info; |
| 4261 | |
| 4262 | file_info = (struct smb2_file_access_info *)rsp->Buffer; |
| 4263 | file_info->AccessFlags = fp->daccess; |
| 4264 | rsp->OutputBufferLength = |
| 4265 | cpu_to_le32(sizeof(struct smb2_file_access_info)); |
| 4266 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_access_info)); |
| 4267 | } |
| 4268 | |
| 4269 | static int get_file_basic_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4270 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4271 | { |
Namjae Jeon | 88d3005 | 2021-09-29 15:37:18 +0900 | [diff] [blame] | 4272 | struct smb2_file_basic_info *basic_info; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4273 | struct kstat stat; |
| 4274 | u64 time; |
| 4275 | |
| 4276 | if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4277 | pr_err("no right to read the attributes : 0x%x\n", |
| 4278 | fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4279 | return -EACCES; |
| 4280 | } |
| 4281 | |
Namjae Jeon | 88d3005 | 2021-09-29 15:37:18 +0900 | [diff] [blame] | 4282 | basic_info = (struct smb2_file_basic_info *)rsp->Buffer; |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 4283 | generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp), |
| 4284 | &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4285 | basic_info->CreationTime = cpu_to_le64(fp->create_time); |
| 4286 | time = ksmbd_UnixTimeToNT(stat.atime); |
| 4287 | basic_info->LastAccessTime = cpu_to_le64(time); |
| 4288 | time = ksmbd_UnixTimeToNT(stat.mtime); |
| 4289 | basic_info->LastWriteTime = cpu_to_le64(time); |
| 4290 | time = ksmbd_UnixTimeToNT(stat.ctime); |
| 4291 | basic_info->ChangeTime = cpu_to_le64(time); |
| 4292 | basic_info->Attributes = fp->f_ci->m_fattr; |
| 4293 | basic_info->Pad1 = 0; |
| 4294 | rsp->OutputBufferLength = |
Namjae Jeon | 88d3005 | 2021-09-29 15:37:18 +0900 | [diff] [blame] | 4295 | cpu_to_le32(sizeof(struct smb2_file_basic_info)); |
| 4296 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_basic_info)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4297 | return 0; |
| 4298 | } |
| 4299 | |
| 4300 | static unsigned long long get_allocation_size(struct inode *inode, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4301 | struct kstat *stat) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4302 | { |
| 4303 | unsigned long long alloc_size = 0; |
| 4304 | |
| 4305 | if (!S_ISDIR(stat->mode)) { |
| 4306 | if ((inode->i_blocks << 9) <= stat->size) |
| 4307 | alloc_size = stat->size; |
| 4308 | else |
| 4309 | alloc_size = inode->i_blocks << 9; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4310 | } |
| 4311 | |
| 4312 | return alloc_size; |
| 4313 | } |
| 4314 | |
| 4315 | static void get_file_standard_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4316 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4317 | { |
| 4318 | struct smb2_file_standard_info *sinfo; |
| 4319 | unsigned int delete_pending; |
| 4320 | struct inode *inode; |
| 4321 | struct kstat stat; |
| 4322 | |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 4323 | inode = file_inode(fp->filp); |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 4324 | generic_fillattr(file_mnt_user_ns(fp->filp), inode, &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4325 | |
| 4326 | sinfo = (struct smb2_file_standard_info *)rsp->Buffer; |
| 4327 | delete_pending = ksmbd_inode_pending_delete(fp); |
| 4328 | |
| 4329 | sinfo->AllocationSize = cpu_to_le64(get_allocation_size(inode, &stat)); |
| 4330 | sinfo->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size); |
| 4331 | sinfo->NumberOfLinks = cpu_to_le32(get_nlink(&stat) - delete_pending); |
| 4332 | sinfo->DeletePending = delete_pending; |
| 4333 | sinfo->Directory = S_ISDIR(stat.mode) ? 1 : 0; |
| 4334 | rsp->OutputBufferLength = |
| 4335 | cpu_to_le32(sizeof(struct smb2_file_standard_info)); |
| 4336 | inc_rfc1001_len(rsp_org, |
| 4337 | sizeof(struct smb2_file_standard_info)); |
| 4338 | } |
| 4339 | |
| 4340 | static void get_file_alignment_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4341 | void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4342 | { |
| 4343 | struct smb2_file_alignment_info *file_info; |
| 4344 | |
| 4345 | file_info = (struct smb2_file_alignment_info *)rsp->Buffer; |
| 4346 | file_info->AlignmentRequirement = 0; |
| 4347 | rsp->OutputBufferLength = |
| 4348 | cpu_to_le32(sizeof(struct smb2_file_alignment_info)); |
| 4349 | inc_rfc1001_len(rsp_org, |
| 4350 | sizeof(struct smb2_file_alignment_info)); |
| 4351 | } |
| 4352 | |
| 4353 | static int get_file_all_info(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4354 | struct smb2_query_info_rsp *rsp, |
| 4355 | struct ksmbd_file *fp, |
| 4356 | void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4357 | { |
| 4358 | struct ksmbd_conn *conn = work->conn; |
| 4359 | struct smb2_file_all_info *file_info; |
| 4360 | unsigned int delete_pending; |
| 4361 | struct inode *inode; |
| 4362 | struct kstat stat; |
| 4363 | int conv_len; |
| 4364 | char *filename; |
| 4365 | u64 time; |
| 4366 | |
| 4367 | if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) { |
| 4368 | ksmbd_debug(SMB, "no right to read the attributes : 0x%x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4369 | fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4370 | return -EACCES; |
| 4371 | } |
| 4372 | |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 4373 | filename = convert_to_nt_pathname(fp->filename); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4374 | if (!filename) |
| 4375 | return -ENOMEM; |
| 4376 | |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 4377 | inode = file_inode(fp->filp); |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 4378 | generic_fillattr(file_mnt_user_ns(fp->filp), inode, &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4379 | |
| 4380 | ksmbd_debug(SMB, "filename = %s\n", filename); |
| 4381 | delete_pending = ksmbd_inode_pending_delete(fp); |
| 4382 | file_info = (struct smb2_file_all_info *)rsp->Buffer; |
| 4383 | |
| 4384 | file_info->CreationTime = cpu_to_le64(fp->create_time); |
| 4385 | time = ksmbd_UnixTimeToNT(stat.atime); |
| 4386 | file_info->LastAccessTime = cpu_to_le64(time); |
| 4387 | time = ksmbd_UnixTimeToNT(stat.mtime); |
| 4388 | file_info->LastWriteTime = cpu_to_le64(time); |
| 4389 | time = ksmbd_UnixTimeToNT(stat.ctime); |
| 4390 | file_info->ChangeTime = cpu_to_le64(time); |
| 4391 | file_info->Attributes = fp->f_ci->m_fattr; |
| 4392 | file_info->Pad1 = 0; |
| 4393 | file_info->AllocationSize = |
| 4394 | cpu_to_le64(get_allocation_size(inode, &stat)); |
| 4395 | file_info->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size); |
| 4396 | file_info->NumberOfLinks = |
| 4397 | cpu_to_le32(get_nlink(&stat) - delete_pending); |
| 4398 | file_info->DeletePending = delete_pending; |
| 4399 | file_info->Directory = S_ISDIR(stat.mode) ? 1 : 0; |
| 4400 | file_info->Pad2 = 0; |
| 4401 | file_info->IndexNumber = cpu_to_le64(stat.ino); |
| 4402 | file_info->EASize = 0; |
| 4403 | file_info->AccessFlags = fp->daccess; |
| 4404 | file_info->CurrentByteOffset = cpu_to_le64(fp->filp->f_pos); |
| 4405 | file_info->Mode = fp->coption; |
| 4406 | file_info->AlignmentRequirement = 0; |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4407 | conv_len = smbConvertToUTF16((__le16 *)file_info->FileName, filename, |
| 4408 | PATH_MAX, conn->local_nls, 0); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4409 | conv_len *= 2; |
| 4410 | file_info->FileNameLength = cpu_to_le32(conv_len); |
| 4411 | rsp->OutputBufferLength = |
| 4412 | cpu_to_le32(sizeof(struct smb2_file_all_info) + conv_len - 1); |
| 4413 | kfree(filename); |
| 4414 | inc_rfc1001_len(rsp_org, le32_to_cpu(rsp->OutputBufferLength)); |
| 4415 | return 0; |
| 4416 | } |
| 4417 | |
| 4418 | static void get_file_alternate_info(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4419 | struct smb2_query_info_rsp *rsp, |
| 4420 | struct ksmbd_file *fp, |
| 4421 | void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4422 | { |
| 4423 | struct ksmbd_conn *conn = work->conn; |
| 4424 | struct smb2_file_alt_name_info *file_info; |
Namjae Jeon | 493fa2f | 2021-06-29 09:22:16 +0900 | [diff] [blame] | 4425 | struct dentry *dentry = fp->filp->f_path.dentry; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4426 | int conv_len; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4427 | |
Namjae Jeon | 493fa2f | 2021-06-29 09:22:16 +0900 | [diff] [blame] | 4428 | spin_lock(&dentry->d_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4429 | file_info = (struct smb2_file_alt_name_info *)rsp->Buffer; |
| 4430 | conv_len = ksmbd_extract_shortname(conn, |
Namjae Jeon | 493fa2f | 2021-06-29 09:22:16 +0900 | [diff] [blame] | 4431 | dentry->d_name.name, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4432 | file_info->FileName); |
Namjae Jeon | 493fa2f | 2021-06-29 09:22:16 +0900 | [diff] [blame] | 4433 | spin_unlock(&dentry->d_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4434 | file_info->FileNameLength = cpu_to_le32(conv_len); |
| 4435 | rsp->OutputBufferLength = |
| 4436 | cpu_to_le32(sizeof(struct smb2_file_alt_name_info) + conv_len); |
| 4437 | inc_rfc1001_len(rsp_org, le32_to_cpu(rsp->OutputBufferLength)); |
| 4438 | } |
| 4439 | |
| 4440 | static void get_file_stream_info(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4441 | struct smb2_query_info_rsp *rsp, |
| 4442 | struct ksmbd_file *fp, |
| 4443 | void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4444 | { |
| 4445 | struct ksmbd_conn *conn = work->conn; |
| 4446 | struct smb2_file_stream_info *file_info; |
| 4447 | char *stream_name, *xattr_list = NULL, *stream_buf; |
| 4448 | struct kstat stat; |
| 4449 | struct path *path = &fp->filp->f_path; |
| 4450 | ssize_t xattr_list_len; |
| 4451 | int nbytes = 0, streamlen, stream_name_len, next, idx = 0; |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 4452 | int buf_free_len; |
| 4453 | struct smb2_query_info_req *req = ksmbd_req_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4454 | |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 4455 | generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp), |
| 4456 | &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4457 | file_info = (struct smb2_file_stream_info *)rsp->Buffer; |
| 4458 | |
| 4459 | xattr_list_len = ksmbd_vfs_listxattr(path->dentry, &xattr_list); |
| 4460 | if (xattr_list_len < 0) { |
| 4461 | goto out; |
| 4462 | } else if (!xattr_list_len) { |
| 4463 | ksmbd_debug(SMB, "empty xattr in the file\n"); |
| 4464 | goto out; |
| 4465 | } |
| 4466 | |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 4467 | buf_free_len = |
| 4468 | smb2_calc_max_out_buf_len(work, 8, |
| 4469 | le32_to_cpu(req->OutputBufferLength)); |
| 4470 | if (buf_free_len < 0) |
| 4471 | goto out; |
| 4472 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4473 | while (idx < xattr_list_len) { |
| 4474 | stream_name = xattr_list + idx; |
| 4475 | streamlen = strlen(stream_name); |
| 4476 | idx += streamlen + 1; |
| 4477 | |
| 4478 | ksmbd_debug(SMB, "%s, len %d\n", stream_name, streamlen); |
| 4479 | |
| 4480 | if (strncmp(&stream_name[XATTR_USER_PREFIX_LEN], |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4481 | STREAM_PREFIX, STREAM_PREFIX_LEN)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4482 | continue; |
| 4483 | |
| 4484 | stream_name_len = streamlen - (XATTR_USER_PREFIX_LEN + |
| 4485 | STREAM_PREFIX_LEN); |
| 4486 | streamlen = stream_name_len; |
| 4487 | |
| 4488 | /* plus : size */ |
| 4489 | streamlen += 1; |
| 4490 | stream_buf = kmalloc(streamlen + 1, GFP_KERNEL); |
| 4491 | if (!stream_buf) |
| 4492 | break; |
| 4493 | |
| 4494 | streamlen = snprintf(stream_buf, streamlen + 1, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4495 | ":%s", &stream_name[XATTR_NAME_STREAM_LEN]); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4496 | |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 4497 | next = sizeof(struct smb2_file_stream_info) + streamlen * 2; |
| 4498 | if (next > buf_free_len) |
| 4499 | break; |
| 4500 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4501 | file_info = (struct smb2_file_stream_info *)&rsp->Buffer[nbytes]; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4502 | streamlen = smbConvertToUTF16((__le16 *)file_info->StreamName, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4503 | stream_buf, streamlen, |
| 4504 | conn->local_nls, 0); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4505 | streamlen *= 2; |
| 4506 | kfree(stream_buf); |
| 4507 | file_info->StreamNameLength = cpu_to_le32(streamlen); |
| 4508 | file_info->StreamSize = cpu_to_le64(stream_name_len); |
| 4509 | file_info->StreamAllocationSize = cpu_to_le64(stream_name_len); |
| 4510 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4511 | nbytes += next; |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 4512 | buf_free_len -= next; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4513 | file_info->NextEntryOffset = cpu_to_le32(next); |
| 4514 | } |
| 4515 | |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 4516 | if (!S_ISDIR(stat.mode) && |
| 4517 | buf_free_len >= sizeof(struct smb2_file_stream_info) + 7 * 2) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4518 | file_info = (struct smb2_file_stream_info *) |
| 4519 | &rsp->Buffer[nbytes]; |
| 4520 | streamlen = smbConvertToUTF16((__le16 *)file_info->StreamName, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4521 | "::$DATA", 7, conn->local_nls, 0); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4522 | streamlen *= 2; |
| 4523 | file_info->StreamNameLength = cpu_to_le32(streamlen); |
Namjae Jeon | 9f632331 | 2021-09-18 21:02:39 +0900 | [diff] [blame] | 4524 | file_info->StreamSize = 0; |
| 4525 | file_info->StreamAllocationSize = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4526 | nbytes += sizeof(struct smb2_file_stream_info) + streamlen; |
| 4527 | } |
| 4528 | |
| 4529 | /* last entry offset should be 0 */ |
| 4530 | file_info->NextEntryOffset = 0; |
| 4531 | out: |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 4532 | kvfree(xattr_list); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4533 | |
| 4534 | rsp->OutputBufferLength = cpu_to_le32(nbytes); |
| 4535 | inc_rfc1001_len(rsp_org, nbytes); |
| 4536 | } |
| 4537 | |
| 4538 | static void get_file_internal_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4539 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4540 | { |
| 4541 | struct smb2_file_internal_info *file_info; |
| 4542 | struct kstat stat; |
| 4543 | |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 4544 | generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp), |
| 4545 | &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4546 | file_info = (struct smb2_file_internal_info *)rsp->Buffer; |
| 4547 | file_info->IndexNumber = cpu_to_le64(stat.ino); |
| 4548 | rsp->OutputBufferLength = |
| 4549 | cpu_to_le32(sizeof(struct smb2_file_internal_info)); |
| 4550 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_internal_info)); |
| 4551 | } |
| 4552 | |
| 4553 | 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] | 4554 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4555 | { |
| 4556 | struct smb2_file_ntwrk_info *file_info; |
| 4557 | struct inode *inode; |
| 4558 | struct kstat stat; |
| 4559 | u64 time; |
| 4560 | |
| 4561 | if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4562 | pr_err("no right to read the attributes : 0x%x\n", |
| 4563 | fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4564 | return -EACCES; |
| 4565 | } |
| 4566 | |
| 4567 | file_info = (struct smb2_file_ntwrk_info *)rsp->Buffer; |
| 4568 | |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 4569 | inode = file_inode(fp->filp); |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 4570 | generic_fillattr(file_mnt_user_ns(fp->filp), inode, &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4571 | |
| 4572 | file_info->CreationTime = cpu_to_le64(fp->create_time); |
| 4573 | time = ksmbd_UnixTimeToNT(stat.atime); |
| 4574 | file_info->LastAccessTime = cpu_to_le64(time); |
| 4575 | time = ksmbd_UnixTimeToNT(stat.mtime); |
| 4576 | file_info->LastWriteTime = cpu_to_le64(time); |
| 4577 | time = ksmbd_UnixTimeToNT(stat.ctime); |
| 4578 | file_info->ChangeTime = cpu_to_le64(time); |
| 4579 | file_info->Attributes = fp->f_ci->m_fattr; |
| 4580 | file_info->AllocationSize = |
| 4581 | cpu_to_le64(get_allocation_size(inode, &stat)); |
| 4582 | file_info->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size); |
| 4583 | file_info->Reserved = cpu_to_le32(0); |
| 4584 | rsp->OutputBufferLength = |
| 4585 | cpu_to_le32(sizeof(struct smb2_file_ntwrk_info)); |
| 4586 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_ntwrk_info)); |
| 4587 | return 0; |
| 4588 | } |
| 4589 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4590 | 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] | 4591 | { |
| 4592 | struct smb2_file_ea_info *file_info; |
| 4593 | |
| 4594 | file_info = (struct smb2_file_ea_info *)rsp->Buffer; |
| 4595 | file_info->EASize = 0; |
| 4596 | rsp->OutputBufferLength = |
| 4597 | cpu_to_le32(sizeof(struct smb2_file_ea_info)); |
| 4598 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_ea_info)); |
| 4599 | } |
| 4600 | |
| 4601 | static void get_file_position_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4602 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4603 | { |
| 4604 | struct smb2_file_pos_info *file_info; |
| 4605 | |
| 4606 | file_info = (struct smb2_file_pos_info *)rsp->Buffer; |
| 4607 | file_info->CurrentByteOffset = cpu_to_le64(fp->filp->f_pos); |
| 4608 | rsp->OutputBufferLength = |
| 4609 | cpu_to_le32(sizeof(struct smb2_file_pos_info)); |
| 4610 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_pos_info)); |
| 4611 | } |
| 4612 | |
| 4613 | static void get_file_mode_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4614 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4615 | { |
| 4616 | struct smb2_file_mode_info *file_info; |
| 4617 | |
| 4618 | file_info = (struct smb2_file_mode_info *)rsp->Buffer; |
| 4619 | file_info->Mode = fp->coption & FILE_MODE_INFO_MASK; |
| 4620 | rsp->OutputBufferLength = |
| 4621 | cpu_to_le32(sizeof(struct smb2_file_mode_info)); |
| 4622 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_mode_info)); |
| 4623 | } |
| 4624 | |
| 4625 | static void get_file_compression_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4626 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4627 | { |
| 4628 | struct smb2_file_comp_info *file_info; |
| 4629 | struct kstat stat; |
| 4630 | |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 4631 | generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp), |
| 4632 | &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4633 | |
| 4634 | file_info = (struct smb2_file_comp_info *)rsp->Buffer; |
| 4635 | file_info->CompressedFileSize = cpu_to_le64(stat.blocks << 9); |
| 4636 | file_info->CompressionFormat = COMPRESSION_FORMAT_NONE; |
| 4637 | file_info->CompressionUnitShift = 0; |
| 4638 | file_info->ChunkShift = 0; |
| 4639 | file_info->ClusterShift = 0; |
| 4640 | memset(&file_info->Reserved[0], 0, 3); |
| 4641 | |
| 4642 | rsp->OutputBufferLength = |
| 4643 | cpu_to_le32(sizeof(struct smb2_file_comp_info)); |
| 4644 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_comp_info)); |
| 4645 | } |
| 4646 | |
| 4647 | 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] | 4648 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4649 | { |
| 4650 | struct smb2_file_attr_tag_info *file_info; |
| 4651 | |
| 4652 | if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4653 | pr_err("no right to read the attributes : 0x%x\n", |
| 4654 | fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4655 | return -EACCES; |
| 4656 | } |
| 4657 | |
| 4658 | file_info = (struct smb2_file_attr_tag_info *)rsp->Buffer; |
| 4659 | file_info->FileAttributes = fp->f_ci->m_fattr; |
| 4660 | file_info->ReparseTag = 0; |
| 4661 | rsp->OutputBufferLength = |
| 4662 | cpu_to_le32(sizeof(struct smb2_file_attr_tag_info)); |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4663 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_attr_tag_info)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4664 | return 0; |
| 4665 | } |
| 4666 | |
| 4667 | static int find_file_posix_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4668 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4669 | { |
| 4670 | struct smb311_posix_qinfo *file_info; |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 4671 | struct inode *inode = file_inode(fp->filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4672 | u64 time; |
| 4673 | |
| 4674 | file_info = (struct smb311_posix_qinfo *)rsp->Buffer; |
| 4675 | file_info->CreationTime = cpu_to_le64(fp->create_time); |
| 4676 | time = ksmbd_UnixTimeToNT(inode->i_atime); |
| 4677 | file_info->LastAccessTime = cpu_to_le64(time); |
| 4678 | time = ksmbd_UnixTimeToNT(inode->i_mtime); |
| 4679 | file_info->LastWriteTime = cpu_to_le64(time); |
| 4680 | time = ksmbd_UnixTimeToNT(inode->i_ctime); |
| 4681 | file_info->ChangeTime = cpu_to_le64(time); |
| 4682 | file_info->DosAttributes = fp->f_ci->m_fattr; |
| 4683 | file_info->Inode = cpu_to_le64(inode->i_ino); |
| 4684 | file_info->EndOfFile = cpu_to_le64(inode->i_size); |
| 4685 | file_info->AllocationSize = cpu_to_le64(inode->i_blocks << 9); |
| 4686 | file_info->HardLinks = cpu_to_le32(inode->i_nlink); |
| 4687 | file_info->Mode = cpu_to_le32(inode->i_mode); |
| 4688 | file_info->DeviceId = cpu_to_le32(inode->i_rdev); |
| 4689 | rsp->OutputBufferLength = |
| 4690 | cpu_to_le32(sizeof(struct smb311_posix_qinfo)); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4691 | inc_rfc1001_len(rsp_org, sizeof(struct smb311_posix_qinfo)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4692 | return 0; |
| 4693 | } |
| 4694 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4695 | static int smb2_get_info_file(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4696 | struct smb2_query_info_req *req, |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4697 | struct smb2_query_info_rsp *rsp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4698 | { |
| 4699 | struct ksmbd_file *fp; |
| 4700 | int fileinfoclass = 0; |
| 4701 | int rc = 0; |
| 4702 | int file_infoclass_size; |
| 4703 | unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID; |
| 4704 | |
| 4705 | if (test_share_config_flag(work->tcon->share_conf, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4706 | KSMBD_SHARE_FLAG_PIPE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4707 | /* smb2 info file called for pipe */ |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4708 | return smb2_get_info_file_pipe(work->sess, req, rsp, |
| 4709 | work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4710 | } |
| 4711 | |
| 4712 | if (work->next_smb2_rcv_hdr_off) { |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 4713 | if (!has_file_id(le64_to_cpu(req->VolatileFileId))) { |
| 4714 | ksmbd_debug(SMB, "Compound request set FID = %llu\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4715 | work->compound_fid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4716 | id = work->compound_fid; |
| 4717 | pid = work->compound_pfid; |
| 4718 | } |
| 4719 | } |
| 4720 | |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 4721 | if (!has_file_id(id)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4722 | id = le64_to_cpu(req->VolatileFileId); |
| 4723 | pid = le64_to_cpu(req->PersistentFileId); |
| 4724 | } |
| 4725 | |
| 4726 | fp = ksmbd_lookup_fd_slow(work, id, pid); |
| 4727 | if (!fp) |
| 4728 | return -ENOENT; |
| 4729 | |
| 4730 | fileinfoclass = req->FileInfoClass; |
| 4731 | |
| 4732 | switch (fileinfoclass) { |
| 4733 | case FILE_ACCESS_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4734 | get_file_access_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4735 | file_infoclass_size = FILE_ACCESS_INFORMATION_SIZE; |
| 4736 | break; |
| 4737 | |
| 4738 | case FILE_BASIC_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4739 | rc = get_file_basic_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4740 | file_infoclass_size = FILE_BASIC_INFORMATION_SIZE; |
| 4741 | break; |
| 4742 | |
| 4743 | case FILE_STANDARD_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4744 | get_file_standard_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4745 | file_infoclass_size = FILE_STANDARD_INFORMATION_SIZE; |
| 4746 | break; |
| 4747 | |
| 4748 | case FILE_ALIGNMENT_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4749 | get_file_alignment_info(rsp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4750 | file_infoclass_size = FILE_ALIGNMENT_INFORMATION_SIZE; |
| 4751 | break; |
| 4752 | |
| 4753 | case FILE_ALL_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4754 | rc = get_file_all_info(work, rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4755 | file_infoclass_size = FILE_ALL_INFORMATION_SIZE; |
| 4756 | break; |
| 4757 | |
| 4758 | case FILE_ALTERNATE_NAME_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4759 | get_file_alternate_info(work, rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4760 | file_infoclass_size = FILE_ALTERNATE_NAME_INFORMATION_SIZE; |
| 4761 | break; |
| 4762 | |
| 4763 | case FILE_STREAM_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4764 | get_file_stream_info(work, rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4765 | file_infoclass_size = FILE_STREAM_INFORMATION_SIZE; |
| 4766 | break; |
| 4767 | |
| 4768 | case FILE_INTERNAL_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4769 | get_file_internal_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4770 | file_infoclass_size = FILE_INTERNAL_INFORMATION_SIZE; |
| 4771 | break; |
| 4772 | |
| 4773 | case FILE_NETWORK_OPEN_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4774 | rc = get_file_network_open_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4775 | file_infoclass_size = FILE_NETWORK_OPEN_INFORMATION_SIZE; |
| 4776 | break; |
| 4777 | |
| 4778 | case FILE_EA_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4779 | get_file_ea_info(rsp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4780 | file_infoclass_size = FILE_EA_INFORMATION_SIZE; |
| 4781 | break; |
| 4782 | |
| 4783 | case FILE_FULL_EA_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4784 | rc = smb2_get_ea(work, fp, req, rsp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4785 | file_infoclass_size = FILE_FULL_EA_INFORMATION_SIZE; |
| 4786 | break; |
| 4787 | |
| 4788 | case FILE_POSITION_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4789 | get_file_position_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4790 | file_infoclass_size = FILE_POSITION_INFORMATION_SIZE; |
| 4791 | break; |
| 4792 | |
| 4793 | case FILE_MODE_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4794 | get_file_mode_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4795 | file_infoclass_size = FILE_MODE_INFORMATION_SIZE; |
| 4796 | break; |
| 4797 | |
| 4798 | case FILE_COMPRESSION_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4799 | get_file_compression_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4800 | file_infoclass_size = FILE_COMPRESSION_INFORMATION_SIZE; |
| 4801 | break; |
| 4802 | |
| 4803 | case FILE_ATTRIBUTE_TAG_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4804 | rc = get_file_attribute_tag_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4805 | file_infoclass_size = FILE_ATTRIBUTE_TAG_INFORMATION_SIZE; |
| 4806 | break; |
| 4807 | case SMB_FIND_FILE_POSIX_INFO: |
| 4808 | if (!work->tcon->posix_extensions) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4809 | 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] | 4810 | rc = -EOPNOTSUPP; |
| 4811 | } else { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4812 | rc = find_file_posix_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4813 | file_infoclass_size = sizeof(struct smb311_posix_qinfo); |
| 4814 | } |
| 4815 | break; |
| 4816 | default: |
| 4817 | ksmbd_debug(SMB, "fileinfoclass %d not supported yet\n", |
| 4818 | fileinfoclass); |
| 4819 | rc = -EOPNOTSUPP; |
| 4820 | } |
| 4821 | if (!rc) |
| 4822 | rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength), |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4823 | rsp, work->response_buf, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4824 | file_infoclass_size); |
| 4825 | ksmbd_fd_put(work, fp); |
| 4826 | return rc; |
| 4827 | } |
| 4828 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4829 | static int smb2_get_info_filesystem(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4830 | struct smb2_query_info_req *req, |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4831 | struct smb2_query_info_rsp *rsp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4832 | { |
| 4833 | struct ksmbd_session *sess = work->sess; |
| 4834 | struct ksmbd_conn *conn = sess->conn; |
| 4835 | struct ksmbd_share_config *share = work->tcon->share_conf; |
| 4836 | int fsinfoclass = 0; |
| 4837 | struct kstatfs stfs; |
| 4838 | struct path path; |
| 4839 | int rc = 0, len; |
| 4840 | int fs_infoclass_size = 0; |
| 4841 | |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 4842 | rc = kern_path(share->path, LOOKUP_NO_SYMLINKS, &path); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4843 | if (rc) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4844 | pr_err("cannot create vfs path\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4845 | return -EIO; |
| 4846 | } |
| 4847 | |
| 4848 | rc = vfs_statfs(&path, &stfs); |
| 4849 | if (rc) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4850 | pr_err("cannot do stat of path %s\n", share->path); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4851 | path_put(&path); |
| 4852 | return -EIO; |
| 4853 | } |
| 4854 | |
| 4855 | fsinfoclass = req->FileInfoClass; |
| 4856 | |
| 4857 | switch (fsinfoclass) { |
| 4858 | case FS_DEVICE_INFORMATION: |
| 4859 | { |
| 4860 | struct filesystem_device_info *info; |
| 4861 | |
| 4862 | info = (struct filesystem_device_info *)rsp->Buffer; |
| 4863 | |
| 4864 | info->DeviceType = cpu_to_le32(stfs.f_type); |
| 4865 | info->DeviceCharacteristics = cpu_to_le32(0x00000020); |
| 4866 | rsp->OutputBufferLength = cpu_to_le32(8); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4867 | inc_rfc1001_len(work->response_buf, 8); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4868 | fs_infoclass_size = FS_DEVICE_INFORMATION_SIZE; |
| 4869 | break; |
| 4870 | } |
| 4871 | case FS_ATTRIBUTE_INFORMATION: |
| 4872 | { |
| 4873 | struct filesystem_attribute_info *info; |
| 4874 | size_t sz; |
| 4875 | |
| 4876 | info = (struct filesystem_attribute_info *)rsp->Buffer; |
| 4877 | info->Attributes = cpu_to_le32(FILE_SUPPORTS_OBJECT_IDS | |
| 4878 | FILE_PERSISTENT_ACLS | |
| 4879 | FILE_UNICODE_ON_DISK | |
| 4880 | FILE_CASE_PRESERVED_NAMES | |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 4881 | FILE_CASE_SENSITIVE_SEARCH | |
| 4882 | FILE_SUPPORTS_BLOCK_REFCOUNTING); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4883 | |
| 4884 | info->Attributes |= cpu_to_le32(server_conf.share_fake_fscaps); |
| 4885 | |
| 4886 | info->MaxPathNameComponentLength = cpu_to_le32(stfs.f_namelen); |
| 4887 | len = smbConvertToUTF16((__le16 *)info->FileSystemName, |
| 4888 | "NTFS", PATH_MAX, conn->local_nls, 0); |
| 4889 | len = len * 2; |
| 4890 | info->FileSystemNameLen = cpu_to_le32(len); |
| 4891 | sz = sizeof(struct filesystem_attribute_info) - 2 + len; |
| 4892 | rsp->OutputBufferLength = cpu_to_le32(sz); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4893 | inc_rfc1001_len(work->response_buf, sz); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4894 | fs_infoclass_size = FS_ATTRIBUTE_INFORMATION_SIZE; |
| 4895 | break; |
| 4896 | } |
| 4897 | case FS_VOLUME_INFORMATION: |
| 4898 | { |
| 4899 | struct filesystem_vol_info *info; |
| 4900 | size_t sz; |
Namjae Jeon | 5d2f0b1 | 2021-10-31 09:53:50 +0900 | [diff] [blame] | 4901 | unsigned int serial_crc = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4902 | |
| 4903 | info = (struct filesystem_vol_info *)(rsp->Buffer); |
| 4904 | info->VolumeCreationTime = 0; |
Namjae Jeon | 5d2f0b1 | 2021-10-31 09:53:50 +0900 | [diff] [blame] | 4905 | serial_crc = crc32_le(serial_crc, share->name, |
| 4906 | strlen(share->name)); |
| 4907 | serial_crc = crc32_le(serial_crc, share->path, |
| 4908 | strlen(share->path)); |
| 4909 | serial_crc = crc32_le(serial_crc, ksmbd_netbios_name(), |
| 4910 | strlen(ksmbd_netbios_name())); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4911 | /* Taking dummy value of serial number*/ |
Namjae Jeon | 5d2f0b1 | 2021-10-31 09:53:50 +0900 | [diff] [blame] | 4912 | info->SerialNumber = cpu_to_le32(serial_crc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4913 | len = smbConvertToUTF16((__le16 *)info->VolumeLabel, |
| 4914 | share->name, PATH_MAX, |
| 4915 | conn->local_nls, 0); |
| 4916 | len = len * 2; |
| 4917 | info->VolumeLabelSize = cpu_to_le32(len); |
| 4918 | info->Reserved = 0; |
| 4919 | sz = sizeof(struct filesystem_vol_info) - 2 + len; |
| 4920 | rsp->OutputBufferLength = cpu_to_le32(sz); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4921 | inc_rfc1001_len(work->response_buf, sz); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4922 | fs_infoclass_size = FS_VOLUME_INFORMATION_SIZE; |
| 4923 | break; |
| 4924 | } |
| 4925 | case FS_SIZE_INFORMATION: |
| 4926 | { |
| 4927 | struct filesystem_info *info; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4928 | |
| 4929 | info = (struct filesystem_info *)(rsp->Buffer); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4930 | info->TotalAllocationUnits = cpu_to_le64(stfs.f_blocks); |
| 4931 | info->FreeAllocationUnits = cpu_to_le64(stfs.f_bfree); |
Namjae Jeon | ee81cae | 2021-06-26 22:32:34 +0900 | [diff] [blame] | 4932 | info->SectorsPerAllocationUnit = cpu_to_le32(1); |
| 4933 | info->BytesPerSector = cpu_to_le32(stfs.f_bsize); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4934 | rsp->OutputBufferLength = cpu_to_le32(24); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4935 | inc_rfc1001_len(work->response_buf, 24); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4936 | fs_infoclass_size = FS_SIZE_INFORMATION_SIZE; |
| 4937 | break; |
| 4938 | } |
| 4939 | case FS_FULL_SIZE_INFORMATION: |
| 4940 | { |
| 4941 | struct smb2_fs_full_size_info *info; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4942 | |
| 4943 | info = (struct smb2_fs_full_size_info *)(rsp->Buffer); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4944 | info->TotalAllocationUnits = cpu_to_le64(stfs.f_blocks); |
| 4945 | info->CallerAvailableAllocationUnits = |
| 4946 | cpu_to_le64(stfs.f_bavail); |
| 4947 | info->ActualAvailableAllocationUnits = |
| 4948 | cpu_to_le64(stfs.f_bfree); |
Namjae Jeon | ee81cae | 2021-06-26 22:32:34 +0900 | [diff] [blame] | 4949 | info->SectorsPerAllocationUnit = cpu_to_le32(1); |
| 4950 | info->BytesPerSector = cpu_to_le32(stfs.f_bsize); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4951 | rsp->OutputBufferLength = cpu_to_le32(32); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4952 | inc_rfc1001_len(work->response_buf, 32); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4953 | fs_infoclass_size = FS_FULL_SIZE_INFORMATION_SIZE; |
| 4954 | break; |
| 4955 | } |
| 4956 | case FS_OBJECT_ID_INFORMATION: |
| 4957 | { |
| 4958 | struct object_id_info *info; |
| 4959 | |
| 4960 | info = (struct object_id_info *)(rsp->Buffer); |
| 4961 | |
| 4962 | if (!user_guest(sess->user)) |
| 4963 | memcpy(info->objid, user_passkey(sess->user), 16); |
| 4964 | else |
| 4965 | memset(info->objid, 0, 16); |
| 4966 | |
| 4967 | info->extended_info.magic = cpu_to_le32(EXTENDED_INFO_MAGIC); |
| 4968 | info->extended_info.version = cpu_to_le32(1); |
| 4969 | info->extended_info.release = cpu_to_le32(1); |
| 4970 | info->extended_info.rel_date = 0; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4971 | 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] | 4972 | rsp->OutputBufferLength = cpu_to_le32(64); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4973 | inc_rfc1001_len(work->response_buf, 64); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4974 | fs_infoclass_size = FS_OBJECT_ID_INFORMATION_SIZE; |
| 4975 | break; |
| 4976 | } |
| 4977 | case FS_SECTOR_SIZE_INFORMATION: |
| 4978 | { |
| 4979 | struct smb3_fs_ss_info *info; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4980 | |
| 4981 | info = (struct smb3_fs_ss_info *)(rsp->Buffer); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4982 | |
Namjae Jeon | 131bac1 | 2021-06-22 16:20:47 +0900 | [diff] [blame] | 4983 | info->LogicalBytesPerSector = cpu_to_le32(stfs.f_bsize); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4984 | info->PhysicalBytesPerSectorForAtomicity = |
Namjae Jeon | 131bac1 | 2021-06-22 16:20:47 +0900 | [diff] [blame] | 4985 | cpu_to_le32(stfs.f_bsize); |
| 4986 | info->PhysicalBytesPerSectorForPerf = cpu_to_le32(stfs.f_bsize); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4987 | info->FSEffPhysicalBytesPerSectorForAtomicity = |
Namjae Jeon | 131bac1 | 2021-06-22 16:20:47 +0900 | [diff] [blame] | 4988 | cpu_to_le32(stfs.f_bsize); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4989 | info->Flags = cpu_to_le32(SSINFO_FLAGS_ALIGNED_DEVICE | |
| 4990 | SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE); |
| 4991 | info->ByteOffsetForSectorAlignment = 0; |
| 4992 | info->ByteOffsetForPartitionAlignment = 0; |
| 4993 | rsp->OutputBufferLength = cpu_to_le32(28); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4994 | inc_rfc1001_len(work->response_buf, 28); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4995 | fs_infoclass_size = FS_SECTOR_SIZE_INFORMATION_SIZE; |
| 4996 | break; |
| 4997 | } |
| 4998 | case FS_CONTROL_INFORMATION: |
| 4999 | { |
| 5000 | /* |
| 5001 | * TODO : The current implementation is based on |
| 5002 | * test result with win7(NTFS) server. It's need to |
| 5003 | * modify this to get valid Quota values |
| 5004 | * from Linux kernel |
| 5005 | */ |
| 5006 | struct smb2_fs_control_info *info; |
| 5007 | |
| 5008 | info = (struct smb2_fs_control_info *)(rsp->Buffer); |
| 5009 | info->FreeSpaceStartFiltering = 0; |
| 5010 | info->FreeSpaceThreshold = 0; |
| 5011 | info->FreeSpaceStopFiltering = 0; |
| 5012 | info->DefaultQuotaThreshold = cpu_to_le64(SMB2_NO_FID); |
| 5013 | info->DefaultQuotaLimit = cpu_to_le64(SMB2_NO_FID); |
| 5014 | info->Padding = 0; |
| 5015 | rsp->OutputBufferLength = cpu_to_le32(48); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5016 | inc_rfc1001_len(work->response_buf, 48); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5017 | fs_infoclass_size = FS_CONTROL_INFORMATION_SIZE; |
| 5018 | break; |
| 5019 | } |
| 5020 | case FS_POSIX_INFORMATION: |
| 5021 | { |
| 5022 | struct filesystem_posix_info *info; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5023 | |
| 5024 | if (!work->tcon->posix_extensions) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5025 | 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] | 5026 | rc = -EOPNOTSUPP; |
| 5027 | } else { |
| 5028 | info = (struct filesystem_posix_info *)(rsp->Buffer); |
Namjae Jeon | ee81cae | 2021-06-26 22:32:34 +0900 | [diff] [blame] | 5029 | info->OptimalTransferSize = cpu_to_le32(stfs.f_bsize); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5030 | info->BlockSize = cpu_to_le32(stfs.f_bsize); |
| 5031 | info->TotalBlocks = cpu_to_le64(stfs.f_blocks); |
| 5032 | info->BlocksAvail = cpu_to_le64(stfs.f_bfree); |
| 5033 | info->UserBlocksAvail = cpu_to_le64(stfs.f_bavail); |
| 5034 | info->TotalFileNodes = cpu_to_le64(stfs.f_files); |
| 5035 | info->FreeFileNodes = cpu_to_le64(stfs.f_ffree); |
| 5036 | rsp->OutputBufferLength = cpu_to_le32(56); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5037 | inc_rfc1001_len(work->response_buf, 56); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5038 | fs_infoclass_size = FS_POSIX_INFORMATION_SIZE; |
| 5039 | } |
| 5040 | break; |
| 5041 | } |
| 5042 | default: |
| 5043 | path_put(&path); |
| 5044 | return -EOPNOTSUPP; |
| 5045 | } |
| 5046 | rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength), |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5047 | rsp, work->response_buf, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5048 | fs_infoclass_size); |
| 5049 | path_put(&path); |
| 5050 | return rc; |
| 5051 | } |
| 5052 | |
| 5053 | static int smb2_get_info_sec(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5054 | struct smb2_query_info_req *req, |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5055 | struct smb2_query_info_rsp *rsp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5056 | { |
| 5057 | struct ksmbd_file *fp; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 5058 | struct user_namespace *user_ns; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5059 | struct smb_ntsd *pntsd = (struct smb_ntsd *)rsp->Buffer, *ppntsd = NULL; |
| 5060 | struct smb_fattr fattr = {{0}}; |
| 5061 | struct inode *inode; |
| 5062 | __u32 secdesclen; |
| 5063 | unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID; |
| 5064 | int addition_info = le32_to_cpu(req->AdditionalInformation); |
| 5065 | int rc; |
| 5066 | |
Namjae Jeon | e294f78 | 2021-06-28 15:26:37 +0900 | [diff] [blame] | 5067 | if (addition_info & ~(OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO | |
| 5068 | PROTECTED_DACL_SECINFO | |
| 5069 | UNPROTECTED_DACL_SECINFO)) { |
| 5070 | pr_err("Unsupported addition info: 0x%x)\n", |
| 5071 | addition_info); |
Sebastian Gottschall | ced2b26 | 2021-04-27 15:33:54 +0900 | [diff] [blame] | 5072 | |
| 5073 | pntsd->revision = cpu_to_le16(1); |
| 5074 | pntsd->type = cpu_to_le16(SELF_RELATIVE | DACL_PROTECTED); |
| 5075 | pntsd->osidoffset = 0; |
| 5076 | pntsd->gsidoffset = 0; |
| 5077 | pntsd->sacloffset = 0; |
| 5078 | pntsd->dacloffset = 0; |
| 5079 | |
| 5080 | secdesclen = sizeof(struct smb_ntsd); |
| 5081 | rsp->OutputBufferLength = cpu_to_le32(secdesclen); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5082 | inc_rfc1001_len(work->response_buf, secdesclen); |
Sebastian Gottschall | ced2b26 | 2021-04-27 15:33:54 +0900 | [diff] [blame] | 5083 | |
| 5084 | return 0; |
| 5085 | } |
| 5086 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5087 | if (work->next_smb2_rcv_hdr_off) { |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 5088 | if (!has_file_id(le64_to_cpu(req->VolatileFileId))) { |
| 5089 | ksmbd_debug(SMB, "Compound request set FID = %llu\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5090 | work->compound_fid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5091 | id = work->compound_fid; |
| 5092 | pid = work->compound_pfid; |
| 5093 | } |
| 5094 | } |
| 5095 | |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 5096 | if (!has_file_id(id)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5097 | id = le64_to_cpu(req->VolatileFileId); |
| 5098 | pid = le64_to_cpu(req->PersistentFileId); |
| 5099 | } |
| 5100 | |
| 5101 | fp = ksmbd_lookup_fd_slow(work, id, pid); |
| 5102 | if (!fp) |
| 5103 | return -ENOENT; |
| 5104 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 5105 | user_ns = file_mnt_user_ns(fp->filp); |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 5106 | inode = file_inode(fp->filp); |
Christian Brauner | 43205ca | 2021-08-23 17:13:50 +0200 | [diff] [blame] | 5107 | ksmbd_acls_fattr(&fattr, user_ns, inode); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5108 | |
| 5109 | if (test_share_config_flag(work->tcon->share_conf, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5110 | KSMBD_SHARE_FLAG_ACL_XATTR)) |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 5111 | ksmbd_vfs_get_sd_xattr(work->conn, user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 5112 | fp->filp->f_path.dentry, &ppntsd); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5113 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 5114 | rc = build_sec_desc(user_ns, pntsd, ppntsd, addition_info, |
| 5115 | &secdesclen, &fattr); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5116 | posix_acl_release(fattr.cf_acls); |
| 5117 | posix_acl_release(fattr.cf_dacls); |
| 5118 | kfree(ppntsd); |
| 5119 | ksmbd_fd_put(work, fp); |
| 5120 | if (rc) |
| 5121 | return rc; |
| 5122 | |
| 5123 | rsp->OutputBufferLength = cpu_to_le32(secdesclen); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5124 | inc_rfc1001_len(work->response_buf, secdesclen); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5125 | return 0; |
| 5126 | } |
| 5127 | |
| 5128 | /** |
| 5129 | * smb2_query_info() - handler for smb2 query info command |
| 5130 | * @work: smb work containing query info request buffer |
| 5131 | * |
| 5132 | * Return: 0 on success, otherwise error |
| 5133 | */ |
| 5134 | int smb2_query_info(struct ksmbd_work *work) |
| 5135 | { |
| 5136 | struct smb2_query_info_req *req; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5137 | struct smb2_query_info_rsp *rsp; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5138 | int rc = 0; |
| 5139 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5140 | WORK_BUFFERS(work, req, rsp); |
| 5141 | |
| 5142 | ksmbd_debug(SMB, "GOT query info request\n"); |
| 5143 | |
| 5144 | switch (req->InfoType) { |
| 5145 | case SMB2_O_INFO_FILE: |
| 5146 | ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILE\n"); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5147 | rc = smb2_get_info_file(work, req, rsp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5148 | break; |
| 5149 | case SMB2_O_INFO_FILESYSTEM: |
| 5150 | ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILESYSTEM\n"); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5151 | rc = smb2_get_info_filesystem(work, req, rsp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5152 | break; |
| 5153 | case SMB2_O_INFO_SECURITY: |
| 5154 | ksmbd_debug(SMB, "GOT SMB2_O_INFO_SECURITY\n"); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5155 | rc = smb2_get_info_sec(work, req, rsp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5156 | break; |
| 5157 | default: |
| 5158 | ksmbd_debug(SMB, "InfoType %d not supported yet\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5159 | req->InfoType); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5160 | rc = -EOPNOTSUPP; |
| 5161 | } |
| 5162 | |
| 5163 | if (rc < 0) { |
| 5164 | if (rc == -EACCES) |
| 5165 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 5166 | else if (rc == -ENOENT) |
| 5167 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 5168 | else if (rc == -EIO) |
| 5169 | rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR; |
| 5170 | else if (rc == -EOPNOTSUPP || rsp->hdr.Status == 0) |
| 5171 | rsp->hdr.Status = STATUS_INVALID_INFO_CLASS; |
| 5172 | smb2_set_err_rsp(work); |
| 5173 | |
| 5174 | ksmbd_debug(SMB, "error while processing smb2 query rc = %d\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5175 | rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5176 | return rc; |
| 5177 | } |
| 5178 | rsp->StructureSize = cpu_to_le16(9); |
| 5179 | rsp->OutputBufferOffset = cpu_to_le16(72); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5180 | inc_rfc1001_len(work->response_buf, 8); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5181 | return 0; |
| 5182 | } |
| 5183 | |
| 5184 | /** |
| 5185 | * smb2_close_pipe() - handler for closing IPC pipe |
| 5186 | * @work: smb work containing close request buffer |
| 5187 | * |
| 5188 | * Return: 0 |
| 5189 | */ |
| 5190 | static noinline int smb2_close_pipe(struct ksmbd_work *work) |
| 5191 | { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5192 | u64 id; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5193 | struct smb2_close_req *req = smb2_get_msg(work->request_buf); |
| 5194 | struct smb2_close_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5195 | |
| 5196 | id = le64_to_cpu(req->VolatileFileId); |
| 5197 | ksmbd_session_rpc_close(work->sess, id); |
| 5198 | |
| 5199 | rsp->StructureSize = cpu_to_le16(60); |
| 5200 | rsp->Flags = 0; |
| 5201 | rsp->Reserved = 0; |
| 5202 | rsp->CreationTime = 0; |
| 5203 | rsp->LastAccessTime = 0; |
| 5204 | rsp->LastWriteTime = 0; |
| 5205 | rsp->ChangeTime = 0; |
| 5206 | rsp->AllocationSize = 0; |
| 5207 | rsp->EndOfFile = 0; |
| 5208 | rsp->Attributes = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5209 | inc_rfc1001_len(work->response_buf, 60); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5210 | return 0; |
| 5211 | } |
| 5212 | |
| 5213 | /** |
| 5214 | * smb2_close() - handler for smb2 close file command |
| 5215 | * @work: smb work containing close request buffer |
| 5216 | * |
| 5217 | * Return: 0 |
| 5218 | */ |
| 5219 | int smb2_close(struct ksmbd_work *work) |
| 5220 | { |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 5221 | u64 volatile_id = KSMBD_NO_FID; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5222 | u64 sess_id; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5223 | struct smb2_close_req *req; |
| 5224 | struct smb2_close_rsp *rsp; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5225 | struct ksmbd_conn *conn = work->conn; |
| 5226 | struct ksmbd_file *fp; |
| 5227 | struct inode *inode; |
| 5228 | u64 time; |
| 5229 | int err = 0; |
| 5230 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5231 | WORK_BUFFERS(work, req, rsp); |
| 5232 | |
| 5233 | if (test_share_config_flag(work->tcon->share_conf, |
| 5234 | KSMBD_SHARE_FLAG_PIPE)) { |
| 5235 | ksmbd_debug(SMB, "IPC pipe close request\n"); |
| 5236 | return smb2_close_pipe(work); |
| 5237 | } |
| 5238 | |
| 5239 | sess_id = le64_to_cpu(req->hdr.SessionId); |
| 5240 | if (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS) |
| 5241 | sess_id = work->compound_sid; |
| 5242 | |
| 5243 | work->compound_sid = 0; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5244 | if (check_session_id(conn, sess_id)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5245 | work->compound_sid = sess_id; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5246 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5247 | rsp->hdr.Status = STATUS_USER_SESSION_DELETED; |
| 5248 | if (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS) |
| 5249 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 5250 | err = -EBADF; |
| 5251 | goto out; |
| 5252 | } |
| 5253 | |
| 5254 | if (work->next_smb2_rcv_hdr_off && |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 5255 | !has_file_id(le64_to_cpu(req->VolatileFileId))) { |
| 5256 | if (!has_file_id(work->compound_fid)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5257 | /* file already closed, return FILE_CLOSED */ |
| 5258 | ksmbd_debug(SMB, "file already closed\n"); |
| 5259 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 5260 | err = -EBADF; |
| 5261 | goto out; |
| 5262 | } else { |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 5263 | ksmbd_debug(SMB, |
| 5264 | "Compound request set FID = %llu:%llu\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5265 | work->compound_fid, |
| 5266 | work->compound_pfid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5267 | volatile_id = work->compound_fid; |
| 5268 | |
| 5269 | /* file closed, stored id is not valid anymore */ |
| 5270 | work->compound_fid = KSMBD_NO_FID; |
| 5271 | work->compound_pfid = KSMBD_NO_FID; |
| 5272 | } |
| 5273 | } else { |
| 5274 | volatile_id = le64_to_cpu(req->VolatileFileId); |
| 5275 | } |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 5276 | ksmbd_debug(SMB, "volatile_id = %llu\n", volatile_id); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5277 | |
| 5278 | rsp->StructureSize = cpu_to_le16(60); |
| 5279 | rsp->Reserved = 0; |
| 5280 | |
| 5281 | if (req->Flags == SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB) { |
| 5282 | fp = ksmbd_lookup_fd_fast(work, volatile_id); |
| 5283 | if (!fp) { |
| 5284 | err = -ENOENT; |
| 5285 | goto out; |
| 5286 | } |
| 5287 | |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 5288 | inode = file_inode(fp->filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5289 | rsp->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB; |
| 5290 | rsp->AllocationSize = S_ISDIR(inode->i_mode) ? 0 : |
| 5291 | cpu_to_le64(inode->i_blocks << 9); |
| 5292 | rsp->EndOfFile = cpu_to_le64(inode->i_size); |
| 5293 | rsp->Attributes = fp->f_ci->m_fattr; |
| 5294 | rsp->CreationTime = cpu_to_le64(fp->create_time); |
| 5295 | time = ksmbd_UnixTimeToNT(inode->i_atime); |
| 5296 | rsp->LastAccessTime = cpu_to_le64(time); |
| 5297 | time = ksmbd_UnixTimeToNT(inode->i_mtime); |
| 5298 | rsp->LastWriteTime = cpu_to_le64(time); |
| 5299 | time = ksmbd_UnixTimeToNT(inode->i_ctime); |
| 5300 | rsp->ChangeTime = cpu_to_le64(time); |
| 5301 | ksmbd_fd_put(work, fp); |
| 5302 | } else { |
| 5303 | rsp->Flags = 0; |
| 5304 | rsp->AllocationSize = 0; |
| 5305 | rsp->EndOfFile = 0; |
| 5306 | rsp->Attributes = 0; |
| 5307 | rsp->CreationTime = 0; |
| 5308 | rsp->LastAccessTime = 0; |
| 5309 | rsp->LastWriteTime = 0; |
| 5310 | rsp->ChangeTime = 0; |
| 5311 | } |
| 5312 | |
| 5313 | err = ksmbd_close_fd(work, volatile_id); |
| 5314 | out: |
| 5315 | if (err) { |
| 5316 | if (rsp->hdr.Status == 0) |
| 5317 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 5318 | smb2_set_err_rsp(work); |
| 5319 | } else { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5320 | inc_rfc1001_len(work->response_buf, 60); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5321 | } |
| 5322 | |
| 5323 | return 0; |
| 5324 | } |
| 5325 | |
| 5326 | /** |
| 5327 | * smb2_echo() - handler for smb2 echo(ping) command |
| 5328 | * @work: smb work containing echo request buffer |
| 5329 | * |
| 5330 | * Return: 0 |
| 5331 | */ |
| 5332 | int smb2_echo(struct ksmbd_work *work) |
| 5333 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5334 | struct smb2_echo_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5335 | |
| 5336 | rsp->StructureSize = cpu_to_le16(4); |
| 5337 | rsp->Reserved = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5338 | inc_rfc1001_len(work->response_buf, 4); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5339 | return 0; |
| 5340 | } |
| 5341 | |
Christian Brauner | da1e7ad | 2021-08-23 17:13:47 +0200 | [diff] [blame] | 5342 | static int smb2_rename(struct ksmbd_work *work, |
| 5343 | struct ksmbd_file *fp, |
| 5344 | struct user_namespace *user_ns, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5345 | struct smb2_file_rename_info *file_info, |
| 5346 | struct nls_table *local_nls) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5347 | { |
| 5348 | struct ksmbd_share_config *share = fp->tcon->share_conf; |
| 5349 | char *new_name = NULL, *abs_oldname = NULL, *old_name = NULL; |
| 5350 | char *pathname = NULL; |
| 5351 | struct path path; |
| 5352 | bool file_present = true; |
| 5353 | int rc; |
| 5354 | |
| 5355 | ksmbd_debug(SMB, "setting FILE_RENAME_INFO\n"); |
| 5356 | pathname = kmalloc(PATH_MAX, GFP_KERNEL); |
| 5357 | if (!pathname) |
| 5358 | return -ENOMEM; |
| 5359 | |
| 5360 | abs_oldname = d_path(&fp->filp->f_path, pathname, PATH_MAX); |
| 5361 | if (IS_ERR(abs_oldname)) { |
| 5362 | rc = -EINVAL; |
| 5363 | goto out; |
| 5364 | } |
| 5365 | old_name = strrchr(abs_oldname, '/'); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5366 | if (old_name && old_name[1] != '\0') { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5367 | old_name++; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5368 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5369 | ksmbd_debug(SMB, "can't get last component in path %s\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5370 | abs_oldname); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5371 | rc = -ENOENT; |
| 5372 | goto out; |
| 5373 | } |
| 5374 | |
| 5375 | new_name = smb2_get_name(share, |
| 5376 | file_info->FileName, |
| 5377 | le32_to_cpu(file_info->FileNameLength), |
| 5378 | local_nls); |
| 5379 | if (IS_ERR(new_name)) { |
| 5380 | rc = PTR_ERR(new_name); |
| 5381 | goto out; |
| 5382 | } |
| 5383 | |
| 5384 | if (strchr(new_name, ':')) { |
| 5385 | int s_type; |
| 5386 | char *xattr_stream_name, *stream_name = NULL; |
| 5387 | size_t xattr_stream_size; |
| 5388 | int len; |
| 5389 | |
| 5390 | rc = parse_stream_name(new_name, &stream_name, &s_type); |
| 5391 | if (rc < 0) |
| 5392 | goto out; |
| 5393 | |
| 5394 | len = strlen(new_name); |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 5395 | if (len > 0 && new_name[len - 1] != '/') { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5396 | pr_err("not allow base filename in rename\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5397 | rc = -ESHARE; |
| 5398 | goto out; |
| 5399 | } |
| 5400 | |
| 5401 | rc = ksmbd_vfs_xattr_stream_name(stream_name, |
| 5402 | &xattr_stream_name, |
| 5403 | &xattr_stream_size, |
| 5404 | s_type); |
| 5405 | if (rc) |
| 5406 | goto out; |
| 5407 | |
Christian Brauner | da1e7ad | 2021-08-23 17:13:47 +0200 | [diff] [blame] | 5408 | rc = ksmbd_vfs_setxattr(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 5409 | fp->filp->f_path.dentry, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5410 | xattr_stream_name, |
| 5411 | NULL, 0, 0); |
| 5412 | if (rc < 0) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5413 | pr_err("failed to store stream name in xattr: %d\n", |
| 5414 | rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5415 | rc = -EINVAL; |
| 5416 | goto out; |
| 5417 | } |
| 5418 | |
| 5419 | goto out; |
| 5420 | } |
| 5421 | |
| 5422 | ksmbd_debug(SMB, "new name %s\n", new_name); |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 5423 | rc = ksmbd_vfs_kern_path(work, new_name, LOOKUP_NO_SYMLINKS, &path, 1); |
| 5424 | if (rc) { |
| 5425 | if (rc != -ENOENT) |
| 5426 | goto out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5427 | file_present = false; |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 5428 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5429 | path_put(&path); |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 5430 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5431 | |
| 5432 | if (ksmbd_share_veto_filename(share, new_name)) { |
| 5433 | rc = -ENOENT; |
| 5434 | ksmbd_debug(SMB, "Can't rename vetoed file: %s\n", new_name); |
| 5435 | goto out; |
| 5436 | } |
| 5437 | |
| 5438 | if (file_info->ReplaceIfExists) { |
| 5439 | if (file_present) { |
| 5440 | rc = ksmbd_vfs_remove_file(work, new_name); |
| 5441 | if (rc) { |
| 5442 | if (rc != -ENOTEMPTY) |
| 5443 | rc = -EINVAL; |
| 5444 | ksmbd_debug(SMB, "cannot delete %s, rc %d\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5445 | new_name, rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5446 | goto out; |
| 5447 | } |
| 5448 | } |
| 5449 | } else { |
| 5450 | if (file_present && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5451 | strncmp(old_name, path.dentry->d_name.name, strlen(old_name))) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5452 | rc = -EEXIST; |
| 5453 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5454 | "cannot rename already existing file\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5455 | goto out; |
| 5456 | } |
| 5457 | } |
| 5458 | |
| 5459 | rc = ksmbd_vfs_fp_rename(work, fp, new_name); |
| 5460 | out: |
| 5461 | kfree(pathname); |
| 5462 | if (!IS_ERR(new_name)) |
Marios Makassikis | 915f570 | 2021-04-13 13:25:57 +0900 | [diff] [blame] | 5463 | kfree(new_name); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5464 | return rc; |
| 5465 | } |
| 5466 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5467 | static int smb2_create_link(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5468 | struct ksmbd_share_config *share, |
| 5469 | struct smb2_file_link_info *file_info, |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5470 | unsigned int buf_len, struct file *filp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5471 | struct nls_table *local_nls) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5472 | { |
| 5473 | char *link_name = NULL, *target_name = NULL, *pathname = NULL; |
| 5474 | struct path path; |
| 5475 | bool file_present = true; |
| 5476 | int rc; |
| 5477 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5478 | if (buf_len < (u64)sizeof(struct smb2_file_link_info) + |
| 5479 | le32_to_cpu(file_info->FileNameLength)) |
| 5480 | return -EINVAL; |
| 5481 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5482 | ksmbd_debug(SMB, "setting FILE_LINK_INFORMATION\n"); |
| 5483 | pathname = kmalloc(PATH_MAX, GFP_KERNEL); |
| 5484 | if (!pathname) |
| 5485 | return -ENOMEM; |
| 5486 | |
| 5487 | link_name = smb2_get_name(share, |
| 5488 | file_info->FileName, |
| 5489 | le32_to_cpu(file_info->FileNameLength), |
| 5490 | local_nls); |
| 5491 | if (IS_ERR(link_name) || S_ISDIR(file_inode(filp)->i_mode)) { |
| 5492 | rc = -EINVAL; |
| 5493 | goto out; |
| 5494 | } |
| 5495 | |
| 5496 | ksmbd_debug(SMB, "link name is %s\n", link_name); |
| 5497 | target_name = d_path(&filp->f_path, pathname, PATH_MAX); |
| 5498 | if (IS_ERR(target_name)) { |
| 5499 | rc = -EINVAL; |
| 5500 | goto out; |
| 5501 | } |
| 5502 | |
| 5503 | ksmbd_debug(SMB, "target name is %s\n", target_name); |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 5504 | rc = ksmbd_vfs_kern_path(work, link_name, LOOKUP_NO_SYMLINKS, &path, 0); |
| 5505 | if (rc) { |
| 5506 | if (rc != -ENOENT) |
| 5507 | goto out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5508 | file_present = false; |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 5509 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5510 | path_put(&path); |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 5511 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5512 | |
| 5513 | if (file_info->ReplaceIfExists) { |
| 5514 | if (file_present) { |
| 5515 | rc = ksmbd_vfs_remove_file(work, link_name); |
| 5516 | if (rc) { |
| 5517 | rc = -EINVAL; |
| 5518 | ksmbd_debug(SMB, "cannot delete %s\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5519 | link_name); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5520 | goto out; |
| 5521 | } |
| 5522 | } |
| 5523 | } else { |
| 5524 | if (file_present) { |
| 5525 | rc = -EEXIST; |
| 5526 | ksmbd_debug(SMB, "link already exists\n"); |
| 5527 | goto out; |
| 5528 | } |
| 5529 | } |
| 5530 | |
| 5531 | rc = ksmbd_vfs_link(work, target_name, link_name); |
| 5532 | if (rc) |
| 5533 | rc = -EINVAL; |
| 5534 | out: |
| 5535 | if (!IS_ERR(link_name)) |
Marios Makassikis | 915f570 | 2021-04-13 13:25:57 +0900 | [diff] [blame] | 5536 | kfree(link_name); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5537 | kfree(pathname); |
| 5538 | return rc; |
| 5539 | } |
| 5540 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5541 | static int set_file_basic_info(struct ksmbd_file *fp, |
| 5542 | struct smb2_file_basic_info *file_info, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5543 | struct ksmbd_share_config *share) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5544 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5545 | struct iattr attrs; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5546 | struct file *filp; |
| 5547 | struct inode *inode; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 5548 | struct user_namespace *user_ns; |
Namjae Jeon | 4ffd526 | 2021-09-07 08:15:21 +0900 | [diff] [blame] | 5549 | int rc = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5550 | |
Marios Makassikis | 7adfd4f | 2021-04-27 15:30:22 +0900 | [diff] [blame] | 5551 | if (!(fp->daccess & FILE_WRITE_ATTRIBUTES_LE)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5552 | return -EACCES; |
| 5553 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5554 | attrs.ia_valid = 0; |
| 5555 | filp = fp->filp; |
| 5556 | inode = file_inode(filp); |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 5557 | user_ns = file_mnt_user_ns(filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5558 | |
| 5559 | if (file_info->CreationTime) |
| 5560 | fp->create_time = le64_to_cpu(file_info->CreationTime); |
| 5561 | |
| 5562 | if (file_info->LastAccessTime) { |
| 5563 | attrs.ia_atime = ksmbd_NTtimeToUnix(file_info->LastAccessTime); |
| 5564 | attrs.ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET); |
| 5565 | } |
| 5566 | |
Namjae Jeon | 64e7875 | 2021-10-03 13:19:00 +0900 | [diff] [blame] | 5567 | attrs.ia_valid |= ATTR_CTIME; |
| 5568 | if (file_info->ChangeTime) |
Christian Brauner | db7fb6f | 2021-08-26 10:07:05 +0900 | [diff] [blame] | 5569 | attrs.ia_ctime = ksmbd_NTtimeToUnix(file_info->ChangeTime); |
Namjae Jeon | 64e7875 | 2021-10-03 13:19:00 +0900 | [diff] [blame] | 5570 | else |
| 5571 | attrs.ia_ctime = inode->i_ctime; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5572 | |
| 5573 | if (file_info->LastWriteTime) { |
| 5574 | attrs.ia_mtime = ksmbd_NTtimeToUnix(file_info->LastWriteTime); |
| 5575 | attrs.ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET); |
| 5576 | } |
| 5577 | |
| 5578 | if (file_info->Attributes) { |
| 5579 | if (!S_ISDIR(inode->i_mode) && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5580 | file_info->Attributes & ATTR_DIRECTORY_LE) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5581 | pr_err("can't change a file to a directory\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5582 | return -EINVAL; |
| 5583 | } |
| 5584 | |
| 5585 | if (!(S_ISDIR(inode->i_mode) && file_info->Attributes == ATTR_NORMAL_LE)) |
| 5586 | fp->f_ci->m_fattr = file_info->Attributes | |
| 5587 | (fp->f_ci->m_fattr & ATTR_DIRECTORY_LE); |
| 5588 | } |
| 5589 | |
| 5590 | if (test_share_config_flag(share, KSMBD_SHARE_FLAG_STORE_DOS_ATTRS) && |
| 5591 | (file_info->CreationTime || file_info->Attributes)) { |
| 5592 | struct xattr_dos_attrib da = {0}; |
| 5593 | |
| 5594 | da.version = 4; |
| 5595 | da.itime = fp->itime; |
| 5596 | da.create_time = fp->create_time; |
| 5597 | da.attr = le32_to_cpu(fp->f_ci->m_fattr); |
| 5598 | da.flags = XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME | |
| 5599 | XATTR_DOSINFO_ITIME; |
| 5600 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 5601 | rc = ksmbd_vfs_set_dos_attrib_xattr(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 5602 | filp->f_path.dentry, &da); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5603 | if (rc) |
| 5604 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5605 | "failed to restore file attribute in EA\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5606 | rc = 0; |
| 5607 | } |
| 5608 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5609 | if (attrs.ia_valid) { |
| 5610 | struct dentry *dentry = filp->f_path.dentry; |
| 5611 | struct inode *inode = d_inode(dentry); |
| 5612 | |
| 5613 | if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) |
| 5614 | return -EACCES; |
| 5615 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5616 | inode_lock(inode); |
Namjae Jeon | 64e7875 | 2021-10-03 13:19:00 +0900 | [diff] [blame] | 5617 | inode->i_ctime = attrs.ia_ctime; |
| 5618 | attrs.ia_valid &= ~ATTR_CTIME; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 5619 | rc = notify_change(user_ns, dentry, &attrs, NULL); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5620 | inode_unlock(inode); |
| 5621 | } |
Christian Brauner | eb5784f | 2021-08-23 17:13:55 +0200 | [diff] [blame] | 5622 | return rc; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5623 | } |
| 5624 | |
| 5625 | static int set_file_allocation_info(struct ksmbd_work *work, |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5626 | struct ksmbd_file *fp, |
| 5627 | struct smb2_file_alloc_info *file_alloc_info) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5628 | { |
| 5629 | /* |
| 5630 | * TODO : It's working fine only when store dos attributes |
| 5631 | * is not yes. need to implement a logic which works |
| 5632 | * properly with any smb.conf option |
| 5633 | */ |
| 5634 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5635 | loff_t alloc_blks; |
| 5636 | struct inode *inode; |
| 5637 | int rc; |
| 5638 | |
Marios Makassikis | a299669 | 2021-04-27 15:29:01 +0900 | [diff] [blame] | 5639 | if (!(fp->daccess & FILE_WRITE_DATA_LE)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5640 | return -EACCES; |
| 5641 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5642 | alloc_blks = (le64_to_cpu(file_alloc_info->AllocationSize) + 511) >> 9; |
| 5643 | inode = file_inode(fp->filp); |
| 5644 | |
| 5645 | if (alloc_blks > inode->i_blocks) { |
Namjae Jeon | e8c0619 | 2021-06-22 11:06:11 +0900 | [diff] [blame] | 5646 | smb_break_all_levII_oplock(work, fp, 1); |
| 5647 | rc = vfs_fallocate(fp->filp, FALLOC_FL_KEEP_SIZE, 0, |
| 5648 | alloc_blks * 512); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5649 | if (rc && rc != -EOPNOTSUPP) { |
Namjae Jeon | e8c0619 | 2021-06-22 11:06:11 +0900 | [diff] [blame] | 5650 | pr_err("vfs_fallocate is failed : %d\n", rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5651 | return rc; |
| 5652 | } |
| 5653 | } else if (alloc_blks < inode->i_blocks) { |
| 5654 | loff_t size; |
| 5655 | |
| 5656 | /* |
| 5657 | * Allocation size could be smaller than original one |
| 5658 | * which means allocated blocks in file should be |
| 5659 | * deallocated. use truncate to cut out it, but inode |
| 5660 | * size is also updated with truncate offset. |
| 5661 | * inode size is retained by backup inode size. |
| 5662 | */ |
| 5663 | size = i_size_read(inode); |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 5664 | rc = ksmbd_vfs_truncate(work, fp, alloc_blks * 512); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5665 | if (rc) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5666 | pr_err("truncate failed! filename : %s, err %d\n", |
| 5667 | fp->filename, rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5668 | return rc; |
| 5669 | } |
| 5670 | if (size < alloc_blks * 512) |
| 5671 | i_size_write(inode, size); |
| 5672 | } |
| 5673 | return 0; |
| 5674 | } |
| 5675 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5676 | 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] | 5677 | struct smb2_file_eof_info *file_eof_info) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5678 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5679 | loff_t newsize; |
| 5680 | struct inode *inode; |
| 5681 | int rc; |
| 5682 | |
Marios Makassikis | a299669 | 2021-04-27 15:29:01 +0900 | [diff] [blame] | 5683 | if (!(fp->daccess & FILE_WRITE_DATA_LE)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5684 | return -EACCES; |
| 5685 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5686 | newsize = le64_to_cpu(file_eof_info->EndOfFile); |
| 5687 | inode = file_inode(fp->filp); |
| 5688 | |
| 5689 | /* |
| 5690 | * If FILE_END_OF_FILE_INFORMATION of set_info_file is called |
| 5691 | * on FAT32 shared device, truncate execution time is too long |
| 5692 | * and network error could cause from windows client. because |
| 5693 | * truncate of some filesystem like FAT32 fill zero data in |
| 5694 | * truncated range. |
| 5695 | */ |
| 5696 | if (inode->i_sb->s_magic != MSDOS_SUPER_MAGIC) { |
| 5697 | ksmbd_debug(SMB, "filename : %s truncated to newsize %lld\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5698 | fp->filename, newsize); |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 5699 | rc = ksmbd_vfs_truncate(work, fp, newsize); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5700 | if (rc) { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5701 | ksmbd_debug(SMB, "truncate failed! filename : %s err %d\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5702 | fp->filename, rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5703 | if (rc != -EAGAIN) |
| 5704 | rc = -EBADF; |
| 5705 | return rc; |
| 5706 | } |
| 5707 | } |
| 5708 | return 0; |
| 5709 | } |
| 5710 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5711 | 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] | 5712 | struct smb2_file_rename_info *rename_info, |
| 5713 | unsigned int buf_len) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5714 | { |
Christian Brauner | da1e7ad | 2021-08-23 17:13:47 +0200 | [diff] [blame] | 5715 | struct user_namespace *user_ns; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5716 | struct ksmbd_file *parent_fp; |
Namjae Jeon | 12202c0 | 2021-06-29 09:23:56 +0900 | [diff] [blame] | 5717 | struct dentry *parent; |
| 5718 | struct dentry *dentry = fp->filp->f_path.dentry; |
| 5719 | int ret; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5720 | |
| 5721 | if (!(fp->daccess & FILE_DELETE_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5722 | pr_err("no right to delete : 0x%x\n", fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5723 | return -EACCES; |
| 5724 | } |
| 5725 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5726 | if (buf_len < (u64)sizeof(struct smb2_file_rename_info) + |
| 5727 | le32_to_cpu(rename_info->FileNameLength)) |
| 5728 | return -EINVAL; |
| 5729 | |
Christian Brauner | da1e7ad | 2021-08-23 17:13:47 +0200 | [diff] [blame] | 5730 | user_ns = file_mnt_user_ns(fp->filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5731 | if (ksmbd_stream_fd(fp)) |
| 5732 | goto next; |
| 5733 | |
Namjae Jeon | 12202c0 | 2021-06-29 09:23:56 +0900 | [diff] [blame] | 5734 | parent = dget_parent(dentry); |
Christian Brauner | da1e7ad | 2021-08-23 17:13:47 +0200 | [diff] [blame] | 5735 | ret = ksmbd_vfs_lock_parent(user_ns, parent, dentry); |
Namjae Jeon | 12202c0 | 2021-06-29 09:23:56 +0900 | [diff] [blame] | 5736 | if (ret) { |
| 5737 | dput(parent); |
| 5738 | return ret; |
| 5739 | } |
| 5740 | |
| 5741 | parent_fp = ksmbd_lookup_fd_inode(d_inode(parent)); |
| 5742 | inode_unlock(d_inode(parent)); |
| 5743 | dput(parent); |
| 5744 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5745 | if (parent_fp) { |
| 5746 | if (parent_fp->daccess & FILE_DELETE_LE) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5747 | pr_err("parent dir is opened with delete access\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5748 | return -ESHARE; |
| 5749 | } |
| 5750 | } |
| 5751 | next: |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5752 | return smb2_rename(work, fp, user_ns, rename_info, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5753 | work->sess->conn->local_nls); |
| 5754 | } |
| 5755 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5756 | static int set_file_disposition_info(struct ksmbd_file *fp, |
| 5757 | struct smb2_file_disposition_info *file_info) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5758 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5759 | struct inode *inode; |
| 5760 | |
| 5761 | if (!(fp->daccess & FILE_DELETE_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5762 | pr_err("no right to delete : 0x%x\n", fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5763 | return -EACCES; |
| 5764 | } |
| 5765 | |
| 5766 | inode = file_inode(fp->filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5767 | if (file_info->DeletePending) { |
| 5768 | if (S_ISDIR(inode->i_mode) && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5769 | ksmbd_vfs_empty_dir(fp) == -ENOTEMPTY) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5770 | return -EBUSY; |
| 5771 | ksmbd_set_inode_pending_delete(fp); |
| 5772 | } else { |
| 5773 | ksmbd_clear_inode_pending_delete(fp); |
| 5774 | } |
| 5775 | return 0; |
| 5776 | } |
| 5777 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5778 | static int set_file_position_info(struct ksmbd_file *fp, |
| 5779 | struct smb2_file_pos_info *file_info) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5780 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5781 | loff_t current_byte_offset; |
Namjae Jeon | ee81cae | 2021-06-26 22:32:34 +0900 | [diff] [blame] | 5782 | unsigned long sector_size; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5783 | struct inode *inode; |
| 5784 | |
| 5785 | inode = file_inode(fp->filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5786 | current_byte_offset = le64_to_cpu(file_info->CurrentByteOffset); |
Namjae Jeon | ee81cae | 2021-06-26 22:32:34 +0900 | [diff] [blame] | 5787 | sector_size = inode->i_sb->s_blocksize; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5788 | |
| 5789 | if (current_byte_offset < 0 || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5790 | (fp->coption == FILE_NO_INTERMEDIATE_BUFFERING_LE && |
| 5791 | current_byte_offset & (sector_size - 1))) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5792 | pr_err("CurrentByteOffset is not valid : %llu\n", |
| 5793 | current_byte_offset); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5794 | return -EINVAL; |
| 5795 | } |
| 5796 | |
| 5797 | fp->filp->f_pos = current_byte_offset; |
| 5798 | return 0; |
| 5799 | } |
| 5800 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5801 | static int set_file_mode_info(struct ksmbd_file *fp, |
| 5802 | struct smb2_file_mode_info *file_info) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5803 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5804 | __le32 mode; |
| 5805 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5806 | mode = file_info->Mode; |
| 5807 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5808 | if ((mode & ~FILE_MODE_INFO_MASK) || |
| 5809 | (mode & FILE_SYNCHRONOUS_IO_ALERT_LE && |
| 5810 | mode & FILE_SYNCHRONOUS_IO_NONALERT_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5811 | 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] | 5812 | return -EINVAL; |
| 5813 | } |
| 5814 | |
| 5815 | /* |
| 5816 | * TODO : need to implement consideration for |
| 5817 | * FILE_SYNCHRONOUS_IO_ALERT and FILE_SYNCHRONOUS_IO_NONALERT |
| 5818 | */ |
| 5819 | ksmbd_vfs_set_fadvise(fp->filp, mode); |
| 5820 | fp->coption = mode; |
| 5821 | return 0; |
| 5822 | } |
| 5823 | |
| 5824 | /** |
| 5825 | * smb2_set_info_file() - handler for smb2 set info command |
| 5826 | * @work: smb work containing set info command buffer |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 5827 | * @fp: ksmbd_file pointer |
| 5828 | * @info_class: smb2 set info class |
| 5829 | * @share: ksmbd_share_config pointer |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5830 | * |
| 5831 | * Return: 0 on success, otherwise error |
| 5832 | * TODO: need to implement an error handling for STATUS_INFO_LENGTH_MISMATCH |
| 5833 | */ |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5834 | 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] | 5835 | struct smb2_set_info_req *req, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5836 | struct ksmbd_share_config *share) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5837 | { |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5838 | unsigned int buf_len = le32_to_cpu(req->BufferLength); |
| 5839 | |
| 5840 | switch (req->FileInfoClass) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5841 | case FILE_BASIC_INFORMATION: |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5842 | { |
| 5843 | if (buf_len < sizeof(struct smb2_file_basic_info)) |
| 5844 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5845 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5846 | return set_file_basic_info(fp, (struct smb2_file_basic_info *)req->Buffer, share); |
| 5847 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5848 | case FILE_ALLOCATION_INFORMATION: |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5849 | { |
| 5850 | if (buf_len < sizeof(struct smb2_file_alloc_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_allocation_info(work, fp, |
| 5854 | (struct smb2_file_alloc_info *)req->Buffer); |
| 5855 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5856 | case FILE_END_OF_FILE_INFORMATION: |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5857 | { |
| 5858 | if (buf_len < sizeof(struct smb2_file_eof_info)) |
| 5859 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5860 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5861 | return set_end_of_file_info(work, fp, |
| 5862 | (struct smb2_file_eof_info *)req->Buffer); |
| 5863 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5864 | case FILE_RENAME_INFORMATION: |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5865 | { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5866 | if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5867 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5868 | "User does not have write permission\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5869 | return -EACCES; |
| 5870 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5871 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5872 | if (buf_len < sizeof(struct smb2_file_rename_info)) |
| 5873 | return -EINVAL; |
| 5874 | |
| 5875 | return set_rename_info(work, fp, |
| 5876 | (struct smb2_file_rename_info *)req->Buffer, |
| 5877 | buf_len); |
| 5878 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5879 | case FILE_LINK_INFORMATION: |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5880 | { |
| 5881 | if (buf_len < sizeof(struct smb2_file_link_info)) |
| 5882 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5883 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5884 | return smb2_create_link(work, work->tcon->share_conf, |
| 5885 | (struct smb2_file_link_info *)req->Buffer, |
| 5886 | buf_len, fp->filp, |
| 5887 | work->sess->conn->local_nls); |
| 5888 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5889 | case FILE_DISPOSITION_INFORMATION: |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5890 | { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5891 | if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5892 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5893 | "User does not have write permission\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5894 | return -EACCES; |
| 5895 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5896 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5897 | if (buf_len < sizeof(struct smb2_file_disposition_info)) |
| 5898 | return -EINVAL; |
| 5899 | |
| 5900 | return set_file_disposition_info(fp, |
| 5901 | (struct smb2_file_disposition_info *)req->Buffer); |
| 5902 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5903 | case FILE_FULL_EA_INFORMATION: |
| 5904 | { |
| 5905 | if (!(fp->daccess & FILE_WRITE_EA_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5906 | pr_err("Not permitted to write ext attr: 0x%x\n", |
| 5907 | fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5908 | return -EACCES; |
| 5909 | } |
| 5910 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5911 | if (buf_len < sizeof(struct smb2_ea_info)) |
| 5912 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5913 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5914 | return smb2_set_ea((struct smb2_ea_info *)req->Buffer, |
| 5915 | buf_len, &fp->filp->f_path); |
| 5916 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5917 | case FILE_POSITION_INFORMATION: |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5918 | { |
| 5919 | if (buf_len < sizeof(struct smb2_file_pos_info)) |
| 5920 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5921 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5922 | return set_file_position_info(fp, (struct smb2_file_pos_info *)req->Buffer); |
| 5923 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5924 | case FILE_MODE_INFORMATION: |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5925 | { |
| 5926 | if (buf_len < sizeof(struct smb2_file_mode_info)) |
| 5927 | return -EINVAL; |
| 5928 | |
| 5929 | return set_file_mode_info(fp, (struct smb2_file_mode_info *)req->Buffer); |
| 5930 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5931 | } |
| 5932 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5933 | pr_err("Unimplemented Fileinfoclass :%d\n", req->FileInfoClass); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5934 | return -EOPNOTSUPP; |
| 5935 | } |
| 5936 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5937 | 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] | 5938 | char *buffer, int buf_len) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5939 | { |
| 5940 | struct smb_ntsd *pntsd = (struct smb_ntsd *)buffer; |
| 5941 | |
| 5942 | fp->saccess |= FILE_SHARE_DELETE_LE; |
| 5943 | |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 5944 | 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] | 5945 | buf_len, false); |
| 5946 | } |
| 5947 | |
| 5948 | /** |
| 5949 | * smb2_set_info() - handler for smb2 set info command handler |
| 5950 | * @work: smb work containing set info request buffer |
| 5951 | * |
| 5952 | * Return: 0 on success, otherwise error |
| 5953 | */ |
| 5954 | int smb2_set_info(struct ksmbd_work *work) |
| 5955 | { |
| 5956 | struct smb2_set_info_req *req; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5957 | struct smb2_set_info_rsp *rsp; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5958 | struct ksmbd_file *fp; |
| 5959 | int rc = 0; |
| 5960 | unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID; |
| 5961 | |
| 5962 | ksmbd_debug(SMB, "Received set info request\n"); |
| 5963 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5964 | if (work->next_smb2_rcv_hdr_off) { |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 5965 | req = ksmbd_req_buf_next(work); |
| 5966 | rsp = ksmbd_resp_buf_next(work); |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 5967 | if (!has_file_id(le64_to_cpu(req->VolatileFileId))) { |
| 5968 | ksmbd_debug(SMB, "Compound request set FID = %llu\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5969 | work->compound_fid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5970 | id = work->compound_fid; |
| 5971 | pid = work->compound_pfid; |
| 5972 | } |
| 5973 | } else { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5974 | req = smb2_get_msg(work->request_buf); |
| 5975 | rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5976 | } |
| 5977 | |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 5978 | if (!has_file_id(id)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5979 | id = le64_to_cpu(req->VolatileFileId); |
| 5980 | pid = le64_to_cpu(req->PersistentFileId); |
| 5981 | } |
| 5982 | |
| 5983 | fp = ksmbd_lookup_fd_slow(work, id, pid); |
| 5984 | if (!fp) { |
| 5985 | ksmbd_debug(SMB, "Invalid id for close: %u\n", id); |
| 5986 | rc = -ENOENT; |
| 5987 | goto err_out; |
| 5988 | } |
| 5989 | |
| 5990 | switch (req->InfoType) { |
| 5991 | case SMB2_O_INFO_FILE: |
| 5992 | ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILE\n"); |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5993 | rc = smb2_set_info_file(work, fp, req, work->tcon->share_conf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5994 | break; |
| 5995 | case SMB2_O_INFO_SECURITY: |
| 5996 | ksmbd_debug(SMB, "GOT SMB2_O_INFO_SECURITY\n"); |
Namjae Jeon | e70e392 | 2021-08-21 23:26:01 +0900 | [diff] [blame] | 5997 | if (ksmbd_override_fsids(work)) { |
| 5998 | rc = -ENOMEM; |
| 5999 | goto err_out; |
| 6000 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6001 | rc = smb2_set_info_sec(fp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6002 | le32_to_cpu(req->AdditionalInformation), |
| 6003 | req->Buffer, |
| 6004 | le32_to_cpu(req->BufferLength)); |
Namjae Jeon | e70e392 | 2021-08-21 23:26:01 +0900 | [diff] [blame] | 6005 | ksmbd_revert_fsids(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6006 | break; |
| 6007 | default: |
| 6008 | rc = -EOPNOTSUPP; |
| 6009 | } |
| 6010 | |
| 6011 | if (rc < 0) |
| 6012 | goto err_out; |
| 6013 | |
| 6014 | rsp->StructureSize = cpu_to_le16(2); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6015 | inc_rfc1001_len(work->response_buf, 2); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6016 | ksmbd_fd_put(work, fp); |
| 6017 | return 0; |
| 6018 | |
| 6019 | err_out: |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 6020 | if (rc == -EACCES || rc == -EPERM || rc == -EXDEV) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6021 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 6022 | else if (rc == -EINVAL) |
| 6023 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 6024 | else if (rc == -ESHARE) |
| 6025 | rsp->hdr.Status = STATUS_SHARING_VIOLATION; |
| 6026 | else if (rc == -ENOENT) |
| 6027 | rsp->hdr.Status = STATUS_OBJECT_NAME_INVALID; |
| 6028 | else if (rc == -EBUSY || rc == -ENOTEMPTY) |
| 6029 | rsp->hdr.Status = STATUS_DIRECTORY_NOT_EMPTY; |
| 6030 | else if (rc == -EAGAIN) |
| 6031 | rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT; |
Namjae Jeon | ff1d572 | 2021-04-13 13:18:10 +0900 | [diff] [blame] | 6032 | else if (rc == -EBADF || rc == -ESTALE) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6033 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 6034 | else if (rc == -EEXIST) |
| 6035 | rsp->hdr.Status = STATUS_OBJECT_NAME_COLLISION; |
| 6036 | else if (rsp->hdr.Status == 0 || rc == -EOPNOTSUPP) |
| 6037 | rsp->hdr.Status = STATUS_INVALID_INFO_CLASS; |
| 6038 | smb2_set_err_rsp(work); |
| 6039 | ksmbd_fd_put(work, fp); |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6040 | ksmbd_debug(SMB, "error while processing smb2 query rc = %d\n", rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6041 | return rc; |
| 6042 | } |
| 6043 | |
| 6044 | /** |
| 6045 | * smb2_read_pipe() - handler for smb2 read from IPC pipe |
| 6046 | * @work: smb work containing read IPC pipe command buffer |
| 6047 | * |
| 6048 | * Return: 0 on success, otherwise error |
| 6049 | */ |
| 6050 | static noinline int smb2_read_pipe(struct ksmbd_work *work) |
| 6051 | { |
| 6052 | int nbytes = 0, err; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6053 | u64 id; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6054 | struct ksmbd_rpc_command *rpc_resp; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6055 | struct smb2_read_req *req = smb2_get_msg(work->request_buf); |
| 6056 | struct smb2_read_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6057 | |
| 6058 | id = le64_to_cpu(req->VolatileFileId); |
| 6059 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6060 | inc_rfc1001_len(work->response_buf, 16); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6061 | rpc_resp = ksmbd_rpc_read(work->sess, id); |
| 6062 | if (rpc_resp) { |
| 6063 | if (rpc_resp->flags != KSMBD_RPC_OK) { |
| 6064 | err = -EINVAL; |
| 6065 | goto out; |
| 6066 | } |
| 6067 | |
| 6068 | work->aux_payload_buf = |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6069 | kvmalloc(rpc_resp->payload_sz, GFP_KERNEL | __GFP_ZERO); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6070 | if (!work->aux_payload_buf) { |
| 6071 | err = -ENOMEM; |
| 6072 | goto out; |
| 6073 | } |
| 6074 | |
| 6075 | memcpy(work->aux_payload_buf, rpc_resp->payload, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6076 | rpc_resp->payload_sz); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6077 | |
| 6078 | nbytes = rpc_resp->payload_sz; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6079 | work->resp_hdr_sz = get_rfc1002_len(work->response_buf) + 4; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6080 | work->aux_payload_sz = nbytes; |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6081 | kvfree(rpc_resp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6082 | } |
| 6083 | |
| 6084 | rsp->StructureSize = cpu_to_le16(17); |
| 6085 | rsp->DataOffset = 80; |
| 6086 | rsp->Reserved = 0; |
| 6087 | rsp->DataLength = cpu_to_le32(nbytes); |
| 6088 | rsp->DataRemaining = 0; |
Ronnie Sahlberg | 699230f | 2021-09-09 12:26:12 +0900 | [diff] [blame^] | 6089 | rsp->Flags = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6090 | inc_rfc1001_len(work->response_buf, nbytes); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6091 | return 0; |
| 6092 | |
| 6093 | out: |
| 6094 | rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR; |
| 6095 | smb2_set_err_rsp(work); |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6096 | kvfree(rpc_resp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6097 | return err; |
| 6098 | } |
| 6099 | |
| 6100 | static ssize_t smb2_read_rdma_channel(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6101 | struct smb2_read_req *req, void *data_buf, |
| 6102 | size_t length) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6103 | { |
| 6104 | struct smb2_buffer_desc_v1 *desc = |
| 6105 | (struct smb2_buffer_desc_v1 *)&req->Buffer[0]; |
| 6106 | int err; |
| 6107 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6108 | if (work->conn->dialect == SMB30_PROT_ID && |
| 6109 | req->Channel != SMB2_CHANNEL_RDMA_V1) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6110 | return -EINVAL; |
| 6111 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6112 | if (req->ReadChannelInfoOffset == 0 || |
| 6113 | le16_to_cpu(req->ReadChannelInfoLength) < sizeof(*desc)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6114 | return -EINVAL; |
| 6115 | |
| 6116 | work->need_invalidate_rkey = |
| 6117 | (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE); |
| 6118 | work->remote_key = le32_to_cpu(desc->token); |
| 6119 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6120 | err = ksmbd_conn_rdma_write(work->conn, data_buf, length, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6121 | le32_to_cpu(desc->token), |
| 6122 | le64_to_cpu(desc->offset), |
| 6123 | le32_to_cpu(desc->length)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6124 | if (err) |
| 6125 | return err; |
| 6126 | |
| 6127 | return length; |
| 6128 | } |
| 6129 | |
| 6130 | /** |
| 6131 | * smb2_read() - handler for smb2 read from file |
| 6132 | * @work: smb work containing read command buffer |
| 6133 | * |
| 6134 | * Return: 0 on success, otherwise error |
| 6135 | */ |
| 6136 | int smb2_read(struct ksmbd_work *work) |
| 6137 | { |
| 6138 | struct ksmbd_conn *conn = work->conn; |
| 6139 | struct smb2_read_req *req; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6140 | struct smb2_read_rsp *rsp; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6141 | struct ksmbd_file *fp; |
| 6142 | loff_t offset; |
| 6143 | size_t length, mincount; |
| 6144 | ssize_t nbytes = 0, remain_bytes = 0; |
| 6145 | int err = 0; |
| 6146 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6147 | WORK_BUFFERS(work, req, rsp); |
| 6148 | |
| 6149 | if (test_share_config_flag(work->tcon->share_conf, |
| 6150 | KSMBD_SHARE_FLAG_PIPE)) { |
| 6151 | ksmbd_debug(SMB, "IPC pipe read request\n"); |
| 6152 | return smb2_read_pipe(work); |
| 6153 | } |
| 6154 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6155 | fp = ksmbd_lookup_fd_slow(work, le64_to_cpu(req->VolatileFileId), |
| 6156 | le64_to_cpu(req->PersistentFileId)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6157 | if (!fp) { |
Marios Makassikis | a4382db | 2021-05-06 11:34:52 +0900 | [diff] [blame] | 6158 | err = -ENOENT; |
| 6159 | goto out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6160 | } |
| 6161 | |
| 6162 | if (!(fp->daccess & (FILE_READ_DATA_LE | FILE_READ_ATTRIBUTES_LE))) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 6163 | pr_err("Not permitted to read : 0x%x\n", fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6164 | err = -EACCES; |
| 6165 | goto out; |
| 6166 | } |
| 6167 | |
| 6168 | offset = le64_to_cpu(req->Offset); |
| 6169 | length = le32_to_cpu(req->Length); |
| 6170 | mincount = le32_to_cpu(req->MinimumCount); |
| 6171 | |
| 6172 | if (length > conn->vals->max_read_size) { |
| 6173 | ksmbd_debug(SMB, "limiting read size to max size(%u)\n", |
| 6174 | conn->vals->max_read_size); |
| 6175 | err = -EINVAL; |
| 6176 | goto out; |
| 6177 | } |
| 6178 | |
Namjae Jeon | 493fa2f | 2021-06-29 09:22:16 +0900 | [diff] [blame] | 6179 | ksmbd_debug(SMB, "filename %pd, offset %lld, len %zu\n", |
| 6180 | fp->filp->f_path.dentry, offset, length); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6181 | |
Namjae Jeon | c30f4eb | 2021-06-18 10:17:37 +0900 | [diff] [blame] | 6182 | work->aux_payload_buf = kvmalloc(length, GFP_KERNEL | __GFP_ZERO); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6183 | if (!work->aux_payload_buf) { |
Dan Carpenter | c1ea111 | 2021-03-22 17:50:11 +0300 | [diff] [blame] | 6184 | err = -ENOMEM; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6185 | goto out; |
| 6186 | } |
| 6187 | |
| 6188 | nbytes = ksmbd_vfs_read(work, fp, length, &offset); |
| 6189 | if (nbytes < 0) { |
| 6190 | err = nbytes; |
| 6191 | goto out; |
| 6192 | } |
| 6193 | |
| 6194 | if ((nbytes == 0 && length != 0) || nbytes < mincount) { |
Namjae Jeon | c30f4eb | 2021-06-18 10:17:37 +0900 | [diff] [blame] | 6195 | kvfree(work->aux_payload_buf); |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 6196 | work->aux_payload_buf = NULL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6197 | rsp->hdr.Status = STATUS_END_OF_FILE; |
| 6198 | smb2_set_err_rsp(work); |
| 6199 | ksmbd_fd_put(work, fp); |
| 6200 | return 0; |
| 6201 | } |
| 6202 | |
| 6203 | ksmbd_debug(SMB, "nbytes %zu, offset %lld mincount %zu\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6204 | nbytes, offset, mincount); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6205 | |
| 6206 | if (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6207 | req->Channel == SMB2_CHANNEL_RDMA_V1) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6208 | /* write data to the client using rdma channel */ |
| 6209 | remain_bytes = smb2_read_rdma_channel(work, req, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6210 | work->aux_payload_buf, |
| 6211 | nbytes); |
Namjae Jeon | c30f4eb | 2021-06-18 10:17:37 +0900 | [diff] [blame] | 6212 | kvfree(work->aux_payload_buf); |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 6213 | work->aux_payload_buf = NULL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6214 | |
| 6215 | nbytes = 0; |
| 6216 | if (remain_bytes < 0) { |
| 6217 | err = (int)remain_bytes; |
| 6218 | goto out; |
| 6219 | } |
| 6220 | } |
| 6221 | |
| 6222 | rsp->StructureSize = cpu_to_le16(17); |
| 6223 | rsp->DataOffset = 80; |
| 6224 | rsp->Reserved = 0; |
| 6225 | rsp->DataLength = cpu_to_le32(nbytes); |
| 6226 | rsp->DataRemaining = cpu_to_le32(remain_bytes); |
Ronnie Sahlberg | 699230f | 2021-09-09 12:26:12 +0900 | [diff] [blame^] | 6227 | rsp->Flags = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6228 | inc_rfc1001_len(work->response_buf, 16); |
| 6229 | work->resp_hdr_sz = get_rfc1002_len(work->response_buf) + 4; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6230 | work->aux_payload_sz = nbytes; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6231 | inc_rfc1001_len(work->response_buf, nbytes); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6232 | ksmbd_fd_put(work, fp); |
| 6233 | return 0; |
| 6234 | |
| 6235 | out: |
| 6236 | if (err) { |
| 6237 | if (err == -EISDIR) |
| 6238 | rsp->hdr.Status = STATUS_INVALID_DEVICE_REQUEST; |
| 6239 | else if (err == -EAGAIN) |
| 6240 | rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT; |
| 6241 | else if (err == -ENOENT) |
| 6242 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 6243 | else if (err == -EACCES) |
| 6244 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 6245 | else if (err == -ESHARE) |
| 6246 | rsp->hdr.Status = STATUS_SHARING_VIOLATION; |
| 6247 | else if (err == -EINVAL) |
| 6248 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 6249 | else |
| 6250 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 6251 | |
| 6252 | smb2_set_err_rsp(work); |
| 6253 | } |
| 6254 | ksmbd_fd_put(work, fp); |
| 6255 | return err; |
| 6256 | } |
| 6257 | |
| 6258 | /** |
| 6259 | * smb2_write_pipe() - handler for smb2 write on IPC pipe |
| 6260 | * @work: smb work containing write IPC pipe command buffer |
| 6261 | * |
| 6262 | * Return: 0 on success, otherwise error |
| 6263 | */ |
| 6264 | static noinline int smb2_write_pipe(struct ksmbd_work *work) |
| 6265 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6266 | struct smb2_write_req *req = smb2_get_msg(work->request_buf); |
| 6267 | struct smb2_write_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6268 | struct ksmbd_rpc_command *rpc_resp; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6269 | u64 id = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6270 | int err = 0, ret = 0; |
| 6271 | char *data_buf; |
| 6272 | size_t length; |
| 6273 | |
| 6274 | length = le32_to_cpu(req->Length); |
| 6275 | id = le64_to_cpu(req->VolatileFileId); |
| 6276 | |
| 6277 | if (le16_to_cpu(req->DataOffset) == |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6278 | offsetof(struct smb2_write_req, Buffer)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6279 | data_buf = (char *)&req->Buffer[0]; |
| 6280 | } else { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6281 | if ((u64)le16_to_cpu(req->DataOffset) + length > |
| 6282 | get_rfc1002_len(work->request_buf)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 6283 | pr_err("invalid write data offset %u, smb_len %u\n", |
| 6284 | le16_to_cpu(req->DataOffset), |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6285 | get_rfc1002_len(work->request_buf)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6286 | err = -EINVAL; |
| 6287 | goto out; |
| 6288 | } |
| 6289 | |
| 6290 | data_buf = (char *)(((char *)&req->hdr.ProtocolId) + |
| 6291 | le16_to_cpu(req->DataOffset)); |
| 6292 | } |
| 6293 | |
| 6294 | rpc_resp = ksmbd_rpc_write(work->sess, id, data_buf, length); |
| 6295 | if (rpc_resp) { |
| 6296 | if (rpc_resp->flags == KSMBD_RPC_ENOTIMPLEMENTED) { |
| 6297 | rsp->hdr.Status = STATUS_NOT_SUPPORTED; |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6298 | kvfree(rpc_resp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6299 | smb2_set_err_rsp(work); |
| 6300 | return -EOPNOTSUPP; |
| 6301 | } |
| 6302 | if (rpc_resp->flags != KSMBD_RPC_OK) { |
| 6303 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 6304 | smb2_set_err_rsp(work); |
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 | return ret; |
| 6307 | } |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6308 | kvfree(rpc_resp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6309 | } |
| 6310 | |
| 6311 | rsp->StructureSize = cpu_to_le16(17); |
| 6312 | rsp->DataOffset = 0; |
| 6313 | rsp->Reserved = 0; |
| 6314 | rsp->DataLength = cpu_to_le32(length); |
| 6315 | rsp->DataRemaining = 0; |
| 6316 | rsp->Reserved2 = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6317 | inc_rfc1001_len(work->response_buf, 16); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6318 | return 0; |
| 6319 | out: |
| 6320 | if (err) { |
| 6321 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 6322 | smb2_set_err_rsp(work); |
| 6323 | } |
| 6324 | |
| 6325 | return err; |
| 6326 | } |
| 6327 | |
| 6328 | static ssize_t smb2_write_rdma_channel(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6329 | struct smb2_write_req *req, |
| 6330 | struct ksmbd_file *fp, |
| 6331 | loff_t offset, size_t length, bool sync) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6332 | { |
| 6333 | struct smb2_buffer_desc_v1 *desc; |
| 6334 | char *data_buf; |
| 6335 | int ret; |
| 6336 | ssize_t nbytes; |
| 6337 | |
| 6338 | desc = (struct smb2_buffer_desc_v1 *)&req->Buffer[0]; |
| 6339 | |
| 6340 | if (work->conn->dialect == SMB30_PROT_ID && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6341 | req->Channel != SMB2_CHANNEL_RDMA_V1) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6342 | return -EINVAL; |
| 6343 | |
| 6344 | if (req->Length != 0 || req->DataOffset != 0) |
| 6345 | return -EINVAL; |
| 6346 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6347 | if (req->WriteChannelInfoOffset == 0 || |
| 6348 | le16_to_cpu(req->WriteChannelInfoLength) < sizeof(*desc)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6349 | return -EINVAL; |
| 6350 | |
| 6351 | work->need_invalidate_rkey = |
| 6352 | (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE); |
| 6353 | work->remote_key = le32_to_cpu(desc->token); |
| 6354 | |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6355 | data_buf = kvmalloc(length, GFP_KERNEL | __GFP_ZERO); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6356 | if (!data_buf) |
| 6357 | return -ENOMEM; |
| 6358 | |
| 6359 | ret = ksmbd_conn_rdma_read(work->conn, data_buf, length, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6360 | le32_to_cpu(desc->token), |
| 6361 | le64_to_cpu(desc->offset), |
| 6362 | le32_to_cpu(desc->length)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6363 | if (ret < 0) { |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6364 | kvfree(data_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6365 | return ret; |
| 6366 | } |
| 6367 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6368 | ret = ksmbd_vfs_write(work, fp, data_buf, length, &offset, sync, &nbytes); |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6369 | kvfree(data_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6370 | if (ret < 0) |
| 6371 | return ret; |
| 6372 | |
| 6373 | return nbytes; |
| 6374 | } |
| 6375 | |
| 6376 | /** |
| 6377 | * smb2_write() - handler for smb2 write from file |
| 6378 | * @work: smb work containing write command buffer |
| 6379 | * |
| 6380 | * Return: 0 on success, otherwise error |
| 6381 | */ |
| 6382 | int smb2_write(struct ksmbd_work *work) |
| 6383 | { |
| 6384 | struct smb2_write_req *req; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6385 | struct smb2_write_rsp *rsp; |
Namjae Jeon | bcd62a3 | 2021-05-10 09:08:19 +0900 | [diff] [blame] | 6386 | struct ksmbd_file *fp = NULL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6387 | loff_t offset; |
| 6388 | size_t length; |
| 6389 | ssize_t nbytes; |
| 6390 | char *data_buf; |
| 6391 | bool writethrough = false; |
| 6392 | int err = 0; |
| 6393 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6394 | WORK_BUFFERS(work, req, rsp); |
| 6395 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6396 | 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] | 6397 | ksmbd_debug(SMB, "IPC pipe write request\n"); |
| 6398 | return smb2_write_pipe(work); |
| 6399 | } |
| 6400 | |
| 6401 | if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
| 6402 | ksmbd_debug(SMB, "User does not have write permission\n"); |
| 6403 | err = -EACCES; |
| 6404 | goto out; |
| 6405 | } |
| 6406 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6407 | fp = ksmbd_lookup_fd_slow(work, le64_to_cpu(req->VolatileFileId), |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6408 | le64_to_cpu(req->PersistentFileId)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6409 | if (!fp) { |
Marios Makassikis | a4382db | 2021-05-06 11:34:52 +0900 | [diff] [blame] | 6410 | err = -ENOENT; |
| 6411 | goto out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6412 | } |
| 6413 | |
| 6414 | if (!(fp->daccess & (FILE_WRITE_DATA_LE | FILE_READ_ATTRIBUTES_LE))) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 6415 | pr_err("Not permitted to write : 0x%x\n", fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6416 | err = -EACCES; |
| 6417 | goto out; |
| 6418 | } |
| 6419 | |
| 6420 | offset = le64_to_cpu(req->Offset); |
| 6421 | length = le32_to_cpu(req->Length); |
| 6422 | |
| 6423 | if (length > work->conn->vals->max_write_size) { |
| 6424 | ksmbd_debug(SMB, "limiting write size to max size(%u)\n", |
| 6425 | work->conn->vals->max_write_size); |
| 6426 | err = -EINVAL; |
| 6427 | goto out; |
| 6428 | } |
| 6429 | |
| 6430 | if (le32_to_cpu(req->Flags) & SMB2_WRITEFLAG_WRITE_THROUGH) |
| 6431 | writethrough = true; |
| 6432 | |
| 6433 | if (req->Channel != SMB2_CHANNEL_RDMA_V1 && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6434 | req->Channel != SMB2_CHANNEL_RDMA_V1_INVALIDATE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6435 | if (le16_to_cpu(req->DataOffset) == |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6436 | offsetof(struct smb2_write_req, Buffer)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6437 | data_buf = (char *)&req->Buffer[0]; |
| 6438 | } else { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6439 | if ((u64)le16_to_cpu(req->DataOffset) + length > |
| 6440 | get_rfc1002_len(work->request_buf)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 6441 | pr_err("invalid write data offset %u, smb_len %u\n", |
| 6442 | le16_to_cpu(req->DataOffset), |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6443 | get_rfc1002_len(work->request_buf)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6444 | err = -EINVAL; |
| 6445 | goto out; |
| 6446 | } |
| 6447 | |
| 6448 | data_buf = (char *)(((char *)&req->hdr.ProtocolId) + |
| 6449 | le16_to_cpu(req->DataOffset)); |
| 6450 | } |
| 6451 | |
| 6452 | ksmbd_debug(SMB, "flags %u\n", le32_to_cpu(req->Flags)); |
| 6453 | if (le32_to_cpu(req->Flags) & SMB2_WRITEFLAG_WRITE_THROUGH) |
| 6454 | writethrough = true; |
| 6455 | |
Namjae Jeon | 493fa2f | 2021-06-29 09:22:16 +0900 | [diff] [blame] | 6456 | ksmbd_debug(SMB, "filename %pd, offset %lld, len %zu\n", |
| 6457 | fp->filp->f_path.dentry, offset, length); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6458 | err = ksmbd_vfs_write(work, fp, data_buf, length, &offset, |
| 6459 | writethrough, &nbytes); |
| 6460 | if (err < 0) |
| 6461 | goto out; |
| 6462 | } else { |
| 6463 | /* read data from the client using rdma channel, and |
| 6464 | * write the data. |
| 6465 | */ |
| 6466 | nbytes = smb2_write_rdma_channel(work, req, fp, offset, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6467 | le32_to_cpu(req->RemainingBytes), |
| 6468 | writethrough); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6469 | if (nbytes < 0) { |
| 6470 | err = (int)nbytes; |
| 6471 | goto out; |
| 6472 | } |
| 6473 | } |
| 6474 | |
| 6475 | rsp->StructureSize = cpu_to_le16(17); |
| 6476 | rsp->DataOffset = 0; |
| 6477 | rsp->Reserved = 0; |
| 6478 | rsp->DataLength = cpu_to_le32(nbytes); |
| 6479 | rsp->DataRemaining = 0; |
| 6480 | rsp->Reserved2 = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6481 | inc_rfc1001_len(work->response_buf, 16); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6482 | ksmbd_fd_put(work, fp); |
| 6483 | return 0; |
| 6484 | |
| 6485 | out: |
| 6486 | if (err == -EAGAIN) |
| 6487 | rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT; |
| 6488 | else if (err == -ENOSPC || err == -EFBIG) |
| 6489 | rsp->hdr.Status = STATUS_DISK_FULL; |
| 6490 | else if (err == -ENOENT) |
| 6491 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 6492 | else if (err == -EACCES) |
| 6493 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 6494 | else if (err == -ESHARE) |
| 6495 | rsp->hdr.Status = STATUS_SHARING_VIOLATION; |
| 6496 | else if (err == -EINVAL) |
| 6497 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 6498 | else |
| 6499 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 6500 | |
| 6501 | smb2_set_err_rsp(work); |
| 6502 | ksmbd_fd_put(work, fp); |
| 6503 | return err; |
| 6504 | } |
| 6505 | |
| 6506 | /** |
| 6507 | * smb2_flush() - handler for smb2 flush file - fsync |
| 6508 | * @work: smb work containing flush command buffer |
| 6509 | * |
| 6510 | * Return: 0 on success, otherwise error |
| 6511 | */ |
| 6512 | int smb2_flush(struct ksmbd_work *work) |
| 6513 | { |
| 6514 | struct smb2_flush_req *req; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6515 | struct smb2_flush_rsp *rsp; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6516 | int err; |
| 6517 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6518 | WORK_BUFFERS(work, req, rsp); |
| 6519 | |
| 6520 | ksmbd_debug(SMB, "SMB2_FLUSH called for fid %llu\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6521 | le64_to_cpu(req->VolatileFileId)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6522 | |
| 6523 | err = ksmbd_vfs_fsync(work, |
| 6524 | le64_to_cpu(req->VolatileFileId), |
| 6525 | le64_to_cpu(req->PersistentFileId)); |
| 6526 | if (err) |
| 6527 | goto out; |
| 6528 | |
| 6529 | rsp->StructureSize = cpu_to_le16(4); |
| 6530 | rsp->Reserved = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6531 | inc_rfc1001_len(work->response_buf, 4); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6532 | return 0; |
| 6533 | |
| 6534 | out: |
| 6535 | if (err) { |
| 6536 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 6537 | smb2_set_err_rsp(work); |
| 6538 | } |
| 6539 | |
| 6540 | return err; |
| 6541 | } |
| 6542 | |
| 6543 | /** |
| 6544 | * smb2_cancel() - handler for smb2 cancel command |
| 6545 | * @work: smb work containing cancel command buffer |
| 6546 | * |
| 6547 | * Return: 0 on success, otherwise error |
| 6548 | */ |
| 6549 | int smb2_cancel(struct ksmbd_work *work) |
| 6550 | { |
| 6551 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6552 | struct smb2_hdr *hdr = smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6553 | struct smb2_hdr *chdr; |
| 6554 | struct ksmbd_work *cancel_work = NULL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6555 | int canceled = 0; |
| 6556 | struct list_head *command_list; |
| 6557 | |
| 6558 | 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] | 6559 | hdr->MessageId, hdr->Flags); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6560 | |
| 6561 | if (hdr->Flags & SMB2_FLAGS_ASYNC_COMMAND) { |
| 6562 | command_list = &conn->async_requests; |
| 6563 | |
| 6564 | spin_lock(&conn->request_lock); |
Namjae Jeon | 6f3d5ee | 2021-06-18 10:28:52 +0900 | [diff] [blame] | 6565 | list_for_each_entry(cancel_work, command_list, |
| 6566 | async_request_entry) { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6567 | chdr = smb2_get_msg(cancel_work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6568 | |
| 6569 | if (cancel_work->async_id != |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6570 | le64_to_cpu(hdr->Id.AsyncId)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6571 | continue; |
| 6572 | |
| 6573 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6574 | "smb2 with AsyncId %llu cancelled command = 0x%x\n", |
| 6575 | le64_to_cpu(hdr->Id.AsyncId), |
| 6576 | le16_to_cpu(chdr->Command)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6577 | canceled = 1; |
| 6578 | break; |
| 6579 | } |
| 6580 | spin_unlock(&conn->request_lock); |
| 6581 | } else { |
| 6582 | command_list = &conn->requests; |
| 6583 | |
| 6584 | spin_lock(&conn->request_lock); |
Namjae Jeon | 6f3d5ee | 2021-06-18 10:28:52 +0900 | [diff] [blame] | 6585 | list_for_each_entry(cancel_work, command_list, request_entry) { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6586 | chdr = smb2_get_msg(cancel_work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6587 | |
| 6588 | if (chdr->MessageId != hdr->MessageId || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6589 | cancel_work == work) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6590 | continue; |
| 6591 | |
| 6592 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6593 | "smb2 with mid %llu cancelled command = 0x%x\n", |
| 6594 | le64_to_cpu(hdr->MessageId), |
| 6595 | le16_to_cpu(chdr->Command)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6596 | canceled = 1; |
| 6597 | break; |
| 6598 | } |
| 6599 | spin_unlock(&conn->request_lock); |
| 6600 | } |
| 6601 | |
| 6602 | if (canceled) { |
| 6603 | cancel_work->state = KSMBD_WORK_CANCELLED; |
| 6604 | if (cancel_work->cancel_fn) |
| 6605 | cancel_work->cancel_fn(cancel_work->cancel_argv); |
| 6606 | } |
| 6607 | |
| 6608 | /* For SMB2_CANCEL command itself send no response*/ |
| 6609 | work->send_no_response = 1; |
| 6610 | return 0; |
| 6611 | } |
| 6612 | |
| 6613 | struct file_lock *smb_flock_init(struct file *f) |
| 6614 | { |
| 6615 | struct file_lock *fl; |
| 6616 | |
| 6617 | fl = locks_alloc_lock(); |
| 6618 | if (!fl) |
| 6619 | goto out; |
| 6620 | |
| 6621 | locks_init_lock(fl); |
| 6622 | |
| 6623 | fl->fl_owner = f; |
| 6624 | fl->fl_pid = current->tgid; |
| 6625 | fl->fl_file = f; |
| 6626 | fl->fl_flags = FL_POSIX; |
| 6627 | fl->fl_ops = NULL; |
| 6628 | fl->fl_lmops = NULL; |
| 6629 | |
| 6630 | out: |
| 6631 | return fl; |
| 6632 | } |
| 6633 | |
| 6634 | static int smb2_set_flock_flags(struct file_lock *flock, int flags) |
| 6635 | { |
| 6636 | int cmd = -EINVAL; |
| 6637 | |
| 6638 | /* Checking for wrong flag combination during lock request*/ |
| 6639 | switch (flags) { |
| 6640 | case SMB2_LOCKFLAG_SHARED: |
| 6641 | ksmbd_debug(SMB, "received shared request\n"); |
| 6642 | cmd = F_SETLKW; |
| 6643 | flock->fl_type = F_RDLCK; |
| 6644 | flock->fl_flags |= FL_SLEEP; |
| 6645 | break; |
| 6646 | case SMB2_LOCKFLAG_EXCLUSIVE: |
| 6647 | ksmbd_debug(SMB, "received exclusive request\n"); |
| 6648 | cmd = F_SETLKW; |
| 6649 | flock->fl_type = F_WRLCK; |
| 6650 | flock->fl_flags |= FL_SLEEP; |
| 6651 | break; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6652 | case SMB2_LOCKFLAG_SHARED | SMB2_LOCKFLAG_FAIL_IMMEDIATELY: |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6653 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6654 | "received shared & fail immediately request\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6655 | cmd = F_SETLK; |
| 6656 | flock->fl_type = F_RDLCK; |
| 6657 | break; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6658 | case SMB2_LOCKFLAG_EXCLUSIVE | SMB2_LOCKFLAG_FAIL_IMMEDIATELY: |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6659 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6660 | "received exclusive & fail immediately request\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6661 | cmd = F_SETLK; |
| 6662 | flock->fl_type = F_WRLCK; |
| 6663 | break; |
| 6664 | case SMB2_LOCKFLAG_UNLOCK: |
| 6665 | ksmbd_debug(SMB, "received unlock request\n"); |
| 6666 | flock->fl_type = F_UNLCK; |
| 6667 | cmd = 0; |
| 6668 | break; |
| 6669 | } |
| 6670 | |
| 6671 | return cmd; |
| 6672 | } |
| 6673 | |
| 6674 | static struct ksmbd_lock *smb2_lock_init(struct file_lock *flock, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6675 | unsigned int cmd, int flags, |
| 6676 | struct list_head *lock_list) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6677 | { |
| 6678 | struct ksmbd_lock *lock; |
| 6679 | |
| 6680 | lock = kzalloc(sizeof(struct ksmbd_lock), GFP_KERNEL); |
| 6681 | if (!lock) |
| 6682 | return NULL; |
| 6683 | |
| 6684 | lock->cmd = cmd; |
| 6685 | lock->fl = flock; |
| 6686 | lock->start = flock->fl_start; |
| 6687 | lock->end = flock->fl_end; |
| 6688 | lock->flags = flags; |
| 6689 | if (lock->start == lock->end) |
| 6690 | lock->zero_len = 1; |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6691 | INIT_LIST_HEAD(&lock->clist); |
| 6692 | INIT_LIST_HEAD(&lock->flist); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6693 | INIT_LIST_HEAD(&lock->llist); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6694 | list_add_tail(&lock->llist, lock_list); |
| 6695 | |
| 6696 | return lock; |
| 6697 | } |
| 6698 | |
| 6699 | static void smb2_remove_blocked_lock(void **argv) |
| 6700 | { |
| 6701 | struct file_lock *flock = (struct file_lock *)argv[0]; |
| 6702 | |
| 6703 | ksmbd_vfs_posix_lock_unblock(flock); |
| 6704 | wake_up(&flock->fl_wait); |
| 6705 | } |
| 6706 | |
| 6707 | static inline bool lock_defer_pending(struct file_lock *fl) |
| 6708 | { |
| 6709 | /* check pending lock waiters */ |
| 6710 | return waitqueue_active(&fl->fl_wait); |
| 6711 | } |
| 6712 | |
| 6713 | /** |
| 6714 | * smb2_lock() - handler for smb2 file lock command |
| 6715 | * @work: smb work containing lock command buffer |
| 6716 | * |
| 6717 | * Return: 0 on success, otherwise error |
| 6718 | */ |
| 6719 | int smb2_lock(struct ksmbd_work *work) |
| 6720 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6721 | struct smb2_lock_req *req = smb2_get_msg(work->request_buf); |
| 6722 | struct smb2_lock_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6723 | struct smb2_lock_element *lock_ele; |
| 6724 | struct ksmbd_file *fp = NULL; |
| 6725 | struct file_lock *flock = NULL; |
| 6726 | struct file *filp = NULL; |
| 6727 | int lock_count; |
| 6728 | int flags = 0; |
| 6729 | int cmd = 0; |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6730 | int err = -EIO, i, rc = 0; |
Namjae Jeon | 50bf80a | 2021-05-14 12:20:07 +0900 | [diff] [blame] | 6731 | u64 lock_start, lock_length; |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6732 | struct ksmbd_lock *smb_lock = NULL, *cmp_lock, *tmp, *tmp2; |
| 6733 | struct ksmbd_conn *conn; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6734 | int nolock = 0; |
| 6735 | LIST_HEAD(lock_list); |
| 6736 | LIST_HEAD(rollback_list); |
| 6737 | int prior_lock = 0; |
| 6738 | |
| 6739 | ksmbd_debug(SMB, "Received lock request\n"); |
| 6740 | fp = ksmbd_lookup_fd_slow(work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6741 | le64_to_cpu(req->VolatileFileId), |
| 6742 | le64_to_cpu(req->PersistentFileId)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6743 | if (!fp) { |
| 6744 | ksmbd_debug(SMB, "Invalid file id for lock : %llu\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6745 | le64_to_cpu(req->VolatileFileId)); |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6746 | err = -ENOENT; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6747 | goto out2; |
| 6748 | } |
| 6749 | |
| 6750 | filp = fp->filp; |
| 6751 | lock_count = le16_to_cpu(req->LockCount); |
| 6752 | lock_ele = req->locks; |
| 6753 | |
| 6754 | ksmbd_debug(SMB, "lock count is %d\n", lock_count); |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6755 | if (!lock_count) { |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6756 | err = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6757 | goto out2; |
| 6758 | } |
| 6759 | |
| 6760 | for (i = 0; i < lock_count; i++) { |
| 6761 | flags = le32_to_cpu(lock_ele[i].Flags); |
| 6762 | |
| 6763 | flock = smb_flock_init(filp); |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6764 | if (!flock) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6765 | goto out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6766 | |
| 6767 | cmd = smb2_set_flock_flags(flock, flags); |
| 6768 | |
Namjae Jeon | 50bf80a | 2021-05-14 12:20:07 +0900 | [diff] [blame] | 6769 | lock_start = le64_to_cpu(lock_ele[i].Offset); |
| 6770 | lock_length = le64_to_cpu(lock_ele[i].Length); |
| 6771 | if (lock_start > U64_MAX - lock_length) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 6772 | pr_err("Invalid lock range requested\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6773 | rsp->hdr.Status = STATUS_INVALID_LOCK_RANGE; |
| 6774 | goto out; |
| 6775 | } |
| 6776 | |
Namjae Jeon | 50bf80a | 2021-05-14 12:20:07 +0900 | [diff] [blame] | 6777 | if (lock_start > OFFSET_MAX) |
| 6778 | flock->fl_start = OFFSET_MAX; |
| 6779 | else |
| 6780 | flock->fl_start = lock_start; |
| 6781 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6782 | lock_length = le64_to_cpu(lock_ele[i].Length); |
Namjae Jeon | 50bf80a | 2021-05-14 12:20:07 +0900 | [diff] [blame] | 6783 | if (lock_length > OFFSET_MAX - flock->fl_start) |
| 6784 | lock_length = OFFSET_MAX - flock->fl_start; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6785 | |
| 6786 | flock->fl_end = flock->fl_start + lock_length; |
| 6787 | |
| 6788 | if (flock->fl_end < flock->fl_start) { |
| 6789 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6790 | "the end offset(%llx) is smaller than the start offset(%llx)\n", |
| 6791 | flock->fl_end, flock->fl_start); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6792 | rsp->hdr.Status = STATUS_INVALID_LOCK_RANGE; |
| 6793 | goto out; |
| 6794 | } |
| 6795 | |
| 6796 | /* Check conflict locks in one request */ |
| 6797 | list_for_each_entry(cmp_lock, &lock_list, llist) { |
| 6798 | if (cmp_lock->fl->fl_start <= flock->fl_start && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6799 | cmp_lock->fl->fl_end >= flock->fl_end) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6800 | if (cmp_lock->fl->fl_type != F_UNLCK && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6801 | flock->fl_type != F_UNLCK) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 6802 | pr_err("conflict two locks in one request\n"); |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6803 | err = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6804 | goto out; |
| 6805 | } |
| 6806 | } |
| 6807 | } |
| 6808 | |
| 6809 | smb_lock = smb2_lock_init(flock, cmd, flags, &lock_list); |
| 6810 | if (!smb_lock) { |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6811 | err = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6812 | goto out; |
| 6813 | } |
| 6814 | } |
| 6815 | |
| 6816 | list_for_each_entry_safe(smb_lock, tmp, &lock_list, llist) { |
| 6817 | if (smb_lock->cmd < 0) { |
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 | if (!(smb_lock->flags & SMB2_LOCKFLAG_MASK)) { |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6823 | err = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6824 | goto out; |
| 6825 | } |
| 6826 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6827 | if ((prior_lock & (SMB2_LOCKFLAG_EXCLUSIVE | SMB2_LOCKFLAG_SHARED) && |
| 6828 | smb_lock->flags & SMB2_LOCKFLAG_UNLOCK) || |
| 6829 | (prior_lock == SMB2_LOCKFLAG_UNLOCK && |
| 6830 | !(smb_lock->flags & SMB2_LOCKFLAG_UNLOCK))) { |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6831 | err = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6832 | goto out; |
| 6833 | } |
| 6834 | |
| 6835 | prior_lock = smb_lock->flags; |
| 6836 | |
| 6837 | if (!(smb_lock->flags & SMB2_LOCKFLAG_UNLOCK) && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6838 | !(smb_lock->flags & SMB2_LOCKFLAG_FAIL_IMMEDIATELY)) |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6839 | goto no_check_cl; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6840 | |
| 6841 | nolock = 1; |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6842 | /* check locks in connection list */ |
| 6843 | read_lock(&conn_list_lock); |
| 6844 | list_for_each_entry(conn, &conn_list, conns_list) { |
| 6845 | spin_lock(&conn->llist_lock); |
| 6846 | list_for_each_entry_safe(cmp_lock, tmp2, &conn->lock_list, clist) { |
| 6847 | if (file_inode(cmp_lock->fl->fl_file) != |
| 6848 | file_inode(smb_lock->fl->fl_file)) |
| 6849 | continue; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6850 | |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6851 | if (smb_lock->fl->fl_type == F_UNLCK) { |
| 6852 | if (cmp_lock->fl->fl_file == smb_lock->fl->fl_file && |
| 6853 | cmp_lock->start == smb_lock->start && |
| 6854 | cmp_lock->end == smb_lock->end && |
| 6855 | !lock_defer_pending(cmp_lock->fl)) { |
| 6856 | nolock = 0; |
| 6857 | list_del(&cmp_lock->flist); |
| 6858 | list_del(&cmp_lock->clist); |
| 6859 | spin_unlock(&conn->llist_lock); |
| 6860 | read_unlock(&conn_list_lock); |
| 6861 | |
| 6862 | locks_free_lock(cmp_lock->fl); |
| 6863 | kfree(cmp_lock); |
| 6864 | goto out_check_cl; |
| 6865 | } |
| 6866 | continue; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6867 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6868 | |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6869 | if (cmp_lock->fl->fl_file == smb_lock->fl->fl_file) { |
| 6870 | if (smb_lock->flags & SMB2_LOCKFLAG_SHARED) |
| 6871 | continue; |
| 6872 | } else { |
| 6873 | if (cmp_lock->flags & SMB2_LOCKFLAG_SHARED) |
| 6874 | continue; |
| 6875 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6876 | |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6877 | /* check zero byte lock range */ |
| 6878 | if (cmp_lock->zero_len && !smb_lock->zero_len && |
| 6879 | cmp_lock->start > smb_lock->start && |
| 6880 | cmp_lock->start < smb_lock->end) { |
| 6881 | spin_unlock(&conn->llist_lock); |
| 6882 | read_unlock(&conn_list_lock); |
| 6883 | pr_err("previous lock conflict with zero byte lock range\n"); |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6884 | goto out; |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6885 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6886 | |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6887 | if (smb_lock->zero_len && !cmp_lock->zero_len && |
| 6888 | smb_lock->start > cmp_lock->start && |
| 6889 | smb_lock->start < cmp_lock->end) { |
| 6890 | spin_unlock(&conn->llist_lock); |
| 6891 | read_unlock(&conn_list_lock); |
| 6892 | pr_err("current lock conflict with zero byte lock range\n"); |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6893 | goto out; |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6894 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6895 | |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6896 | if (((cmp_lock->start <= smb_lock->start && |
| 6897 | cmp_lock->end > smb_lock->start) || |
| 6898 | (cmp_lock->start < smb_lock->end && |
| 6899 | cmp_lock->end >= smb_lock->end)) && |
| 6900 | !cmp_lock->zero_len && !smb_lock->zero_len) { |
| 6901 | spin_unlock(&conn->llist_lock); |
| 6902 | read_unlock(&conn_list_lock); |
| 6903 | pr_err("Not allow lock operation on exclusive lock range\n"); |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6904 | goto out; |
| 6905 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6906 | } |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6907 | spin_unlock(&conn->llist_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6908 | } |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6909 | read_unlock(&conn_list_lock); |
| 6910 | out_check_cl: |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6911 | if (smb_lock->fl->fl_type == F_UNLCK && nolock) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 6912 | pr_err("Try to unlock nolocked range\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6913 | rsp->hdr.Status = STATUS_RANGE_NOT_LOCKED; |
| 6914 | goto out; |
| 6915 | } |
| 6916 | |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6917 | no_check_cl: |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6918 | if (smb_lock->zero_len) { |
| 6919 | err = 0; |
| 6920 | goto skip; |
| 6921 | } |
| 6922 | |
| 6923 | flock = smb_lock->fl; |
| 6924 | list_del(&smb_lock->llist); |
| 6925 | retry: |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6926 | rc = vfs_lock_file(filp, smb_lock->cmd, flock, NULL); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6927 | skip: |
| 6928 | if (flags & SMB2_LOCKFLAG_UNLOCK) { |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6929 | if (!rc) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6930 | ksmbd_debug(SMB, "File unlocked\n"); |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6931 | } else if (rc == -ENOENT) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6932 | rsp->hdr.Status = STATUS_NOT_LOCKED; |
| 6933 | goto out; |
| 6934 | } |
| 6935 | locks_free_lock(flock); |
| 6936 | kfree(smb_lock); |
| 6937 | } else { |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6938 | if (rc == FILE_LOCK_DEFERRED) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6939 | void **argv; |
| 6940 | |
| 6941 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6942 | "would have to wait for getting lock\n"); |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6943 | spin_lock(&work->conn->llist_lock); |
| 6944 | list_add_tail(&smb_lock->clist, |
| 6945 | &work->conn->lock_list); |
| 6946 | spin_unlock(&work->conn->llist_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6947 | list_add(&smb_lock->llist, &rollback_list); |
| 6948 | |
| 6949 | argv = kmalloc(sizeof(void *), GFP_KERNEL); |
| 6950 | if (!argv) { |
| 6951 | err = -ENOMEM; |
| 6952 | goto out; |
| 6953 | } |
| 6954 | argv[0] = flock; |
| 6955 | |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6956 | rc = setup_async_work(work, |
| 6957 | smb2_remove_blocked_lock, |
| 6958 | argv); |
| 6959 | if (rc) { |
| 6960 | err = -ENOMEM; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6961 | goto out; |
| 6962 | } |
| 6963 | spin_lock(&fp->f_lock); |
| 6964 | list_add(&work->fp_entry, &fp->blocked_works); |
| 6965 | spin_unlock(&fp->f_lock); |
| 6966 | |
| 6967 | smb2_send_interim_resp(work, STATUS_PENDING); |
| 6968 | |
Hyunchul Lee | 45a64e8 | 2021-07-10 09:34:20 +0900 | [diff] [blame] | 6969 | ksmbd_vfs_posix_lock_wait(flock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6970 | |
Hyunchul Lee | d4075ab | 2021-06-25 07:02:10 +0900 | [diff] [blame] | 6971 | if (work->state != KSMBD_WORK_ACTIVE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6972 | list_del(&smb_lock->llist); |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6973 | spin_lock(&work->conn->llist_lock); |
| 6974 | list_del(&smb_lock->clist); |
| 6975 | spin_unlock(&work->conn->llist_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6976 | locks_free_lock(flock); |
| 6977 | |
Hyunchul Lee | d4075ab | 2021-06-25 07:02:10 +0900 | [diff] [blame] | 6978 | if (work->state == KSMBD_WORK_CANCELLED) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6979 | spin_lock(&fp->f_lock); |
| 6980 | list_del(&work->fp_entry); |
| 6981 | spin_unlock(&fp->f_lock); |
| 6982 | rsp->hdr.Status = |
| 6983 | STATUS_CANCELLED; |
| 6984 | kfree(smb_lock); |
| 6985 | smb2_send_interim_resp(work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6986 | STATUS_CANCELLED); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6987 | work->send_no_response = 1; |
| 6988 | goto out; |
| 6989 | } |
| 6990 | init_smb2_rsp_hdr(work); |
| 6991 | smb2_set_err_rsp(work); |
| 6992 | rsp->hdr.Status = |
| 6993 | STATUS_RANGE_NOT_LOCKED; |
| 6994 | kfree(smb_lock); |
| 6995 | goto out2; |
| 6996 | } |
| 6997 | |
| 6998 | list_del(&smb_lock->llist); |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6999 | spin_lock(&work->conn->llist_lock); |
| 7000 | list_del(&smb_lock->clist); |
| 7001 | spin_unlock(&work->conn->llist_lock); |
| 7002 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7003 | spin_lock(&fp->f_lock); |
| 7004 | list_del(&work->fp_entry); |
| 7005 | spin_unlock(&fp->f_lock); |
| 7006 | goto retry; |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 7007 | } else if (!rc) { |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 7008 | spin_lock(&work->conn->llist_lock); |
| 7009 | list_add_tail(&smb_lock->clist, |
| 7010 | &work->conn->lock_list); |
| 7011 | list_add_tail(&smb_lock->flist, |
| 7012 | &fp->lock_list); |
| 7013 | spin_unlock(&work->conn->llist_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7014 | list_add(&smb_lock->llist, &rollback_list); |
| 7015 | ksmbd_debug(SMB, "successful in taking lock\n"); |
| 7016 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7017 | goto out; |
| 7018 | } |
| 7019 | } |
| 7020 | } |
| 7021 | |
| 7022 | if (atomic_read(&fp->f_ci->op_count) > 1) |
| 7023 | smb_break_all_oplock(work, fp); |
| 7024 | |
| 7025 | rsp->StructureSize = cpu_to_le16(4); |
| 7026 | ksmbd_debug(SMB, "successful in taking lock\n"); |
| 7027 | rsp->hdr.Status = STATUS_SUCCESS; |
| 7028 | rsp->Reserved = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 7029 | inc_rfc1001_len(work->response_buf, 4); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7030 | ksmbd_fd_put(work, fp); |
Namjae Jeon | 96ad4ec | 2021-07-13 17:17:28 +0900 | [diff] [blame] | 7031 | return 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7032 | |
| 7033 | out: |
| 7034 | list_for_each_entry_safe(smb_lock, tmp, &lock_list, llist) { |
| 7035 | locks_free_lock(smb_lock->fl); |
| 7036 | list_del(&smb_lock->llist); |
| 7037 | kfree(smb_lock); |
| 7038 | } |
| 7039 | |
| 7040 | list_for_each_entry_safe(smb_lock, tmp, &rollback_list, llist) { |
| 7041 | struct file_lock *rlock = NULL; |
| 7042 | |
| 7043 | rlock = smb_flock_init(filp); |
| 7044 | rlock->fl_type = F_UNLCK; |
| 7045 | rlock->fl_start = smb_lock->start; |
| 7046 | rlock->fl_end = smb_lock->end; |
| 7047 | |
Namjae Jeon | 96ad4ec | 2021-07-13 17:17:28 +0900 | [diff] [blame] | 7048 | rc = vfs_lock_file(filp, 0, rlock, NULL); |
| 7049 | if (rc) |
| 7050 | pr_err("rollback unlock fail : %d\n", rc); |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 7051 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7052 | list_del(&smb_lock->llist); |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 7053 | spin_lock(&work->conn->llist_lock); |
| 7054 | if (!list_empty(&smb_lock->flist)) |
| 7055 | list_del(&smb_lock->flist); |
| 7056 | list_del(&smb_lock->clist); |
| 7057 | spin_unlock(&work->conn->llist_lock); |
| 7058 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7059 | locks_free_lock(smb_lock->fl); |
| 7060 | locks_free_lock(rlock); |
| 7061 | kfree(smb_lock); |
| 7062 | } |
| 7063 | out2: |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 7064 | ksmbd_debug(SMB, "failed in taking lock(flags : %x), err : %d\n", flags, err); |
| 7065 | |
| 7066 | if (!rsp->hdr.Status) { |
| 7067 | if (err == -EINVAL) |
| 7068 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 7069 | else if (err == -ENOMEM) |
| 7070 | rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES; |
| 7071 | else if (err == -ENOENT) |
| 7072 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 7073 | else |
| 7074 | rsp->hdr.Status = STATUS_LOCK_NOT_GRANTED; |
| 7075 | } |
| 7076 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7077 | smb2_set_err_rsp(work); |
| 7078 | ksmbd_fd_put(work, fp); |
Namjae Jeon | 96ad4ec | 2021-07-13 17:17:28 +0900 | [diff] [blame] | 7079 | return err; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7080 | } |
| 7081 | |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7082 | static int fsctl_copychunk(struct ksmbd_work *work, |
| 7083 | struct copychunk_ioctl_req *ci_req, |
| 7084 | unsigned int cnt_code, |
| 7085 | unsigned int input_count, |
| 7086 | unsigned long long volatile_id, |
| 7087 | unsigned long long persistent_id, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7088 | struct smb2_ioctl_rsp *rsp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7089 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7090 | struct copychunk_ioctl_rsp *ci_rsp; |
| 7091 | struct ksmbd_file *src_fp = NULL, *dst_fp = NULL; |
| 7092 | struct srv_copychunk *chunks; |
| 7093 | unsigned int i, chunk_count, chunk_count_written = 0; |
| 7094 | unsigned int chunk_size_written = 0; |
| 7095 | loff_t total_size_written = 0; |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7096 | int ret = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7097 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7098 | ci_rsp = (struct copychunk_ioctl_rsp *)&rsp->Buffer[0]; |
| 7099 | |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7100 | rsp->VolatileFileId = cpu_to_le64(volatile_id); |
| 7101 | rsp->PersistentFileId = cpu_to_le64(persistent_id); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7102 | ci_rsp->ChunksWritten = |
| 7103 | cpu_to_le32(ksmbd_server_side_copy_max_chunk_count()); |
| 7104 | ci_rsp->ChunkBytesWritten = |
| 7105 | cpu_to_le32(ksmbd_server_side_copy_max_chunk_size()); |
| 7106 | ci_rsp->TotalBytesWritten = |
| 7107 | cpu_to_le32(ksmbd_server_side_copy_max_total_size()); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7108 | |
| 7109 | chunks = (struct srv_copychunk *)&ci_req->Chunks[0]; |
| 7110 | chunk_count = le32_to_cpu(ci_req->ChunkCount); |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7111 | if (chunk_count == 0) |
| 7112 | goto out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7113 | total_size_written = 0; |
| 7114 | |
| 7115 | /* verify the SRV_COPYCHUNK_COPY packet */ |
| 7116 | if (chunk_count > ksmbd_server_side_copy_max_chunk_count() || |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7117 | input_count < offsetof(struct copychunk_ioctl_req, Chunks) + |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7118 | chunk_count * sizeof(struct srv_copychunk)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7119 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 7120 | return -EINVAL; |
| 7121 | } |
| 7122 | |
| 7123 | for (i = 0; i < chunk_count; i++) { |
| 7124 | if (le32_to_cpu(chunks[i].Length) == 0 || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7125 | 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] | 7126 | break; |
| 7127 | total_size_written += le32_to_cpu(chunks[i].Length); |
| 7128 | } |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7129 | |
| 7130 | if (i < chunk_count || |
| 7131 | total_size_written > ksmbd_server_side_copy_max_total_size()) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7132 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 7133 | return -EINVAL; |
| 7134 | } |
| 7135 | |
| 7136 | src_fp = ksmbd_lookup_foreign_fd(work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7137 | le64_to_cpu(ci_req->ResumeKey[0])); |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7138 | dst_fp = ksmbd_lookup_fd_slow(work, volatile_id, persistent_id); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7139 | ret = -EINVAL; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7140 | if (!src_fp || |
| 7141 | src_fp->persistent_id != le64_to_cpu(ci_req->ResumeKey[1])) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7142 | rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND; |
| 7143 | goto out; |
| 7144 | } |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7145 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7146 | if (!dst_fp) { |
| 7147 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 7148 | goto out; |
| 7149 | } |
| 7150 | |
| 7151 | /* |
| 7152 | * FILE_READ_DATA should only be included in |
| 7153 | * the FSCTL_COPYCHUNK case |
| 7154 | */ |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7155 | if (cnt_code == FSCTL_COPYCHUNK && |
| 7156 | !(dst_fp->daccess & (FILE_READ_DATA_LE | FILE_GENERIC_READ_LE))) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7157 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 7158 | goto out; |
| 7159 | } |
| 7160 | |
| 7161 | ret = ksmbd_vfs_copy_file_ranges(work, src_fp, dst_fp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7162 | chunks, chunk_count, |
| 7163 | &chunk_count_written, |
| 7164 | &chunk_size_written, |
| 7165 | &total_size_written); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7166 | if (ret < 0) { |
| 7167 | if (ret == -EACCES) |
| 7168 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 7169 | if (ret == -EAGAIN) |
| 7170 | rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT; |
| 7171 | else if (ret == -EBADF) |
| 7172 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 7173 | else if (ret == -EFBIG || ret == -ENOSPC) |
| 7174 | rsp->hdr.Status = STATUS_DISK_FULL; |
| 7175 | else if (ret == -EINVAL) |
| 7176 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 7177 | else if (ret == -EISDIR) |
| 7178 | rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY; |
| 7179 | else if (ret == -E2BIG) |
| 7180 | rsp->hdr.Status = STATUS_INVALID_VIEW_SIZE; |
| 7181 | else |
| 7182 | rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR; |
| 7183 | } |
| 7184 | |
| 7185 | ci_rsp->ChunksWritten = cpu_to_le32(chunk_count_written); |
| 7186 | ci_rsp->ChunkBytesWritten = cpu_to_le32(chunk_size_written); |
| 7187 | ci_rsp->TotalBytesWritten = cpu_to_le32(total_size_written); |
| 7188 | out: |
| 7189 | ksmbd_fd_put(work, src_fp); |
| 7190 | ksmbd_fd_put(work, dst_fp); |
| 7191 | return ret; |
| 7192 | } |
| 7193 | |
| 7194 | static __be32 idev_ipv4_address(struct in_device *idev) |
| 7195 | { |
| 7196 | __be32 addr = 0; |
| 7197 | |
| 7198 | struct in_ifaddr *ifa; |
| 7199 | |
| 7200 | rcu_read_lock(); |
| 7201 | in_dev_for_each_ifa_rcu(ifa, idev) { |
| 7202 | if (ifa->ifa_flags & IFA_F_SECONDARY) |
| 7203 | continue; |
| 7204 | |
| 7205 | addr = ifa->ifa_address; |
| 7206 | break; |
| 7207 | } |
| 7208 | rcu_read_unlock(); |
| 7209 | return addr; |
| 7210 | } |
| 7211 | |
| 7212 | static int fsctl_query_iface_info_ioctl(struct ksmbd_conn *conn, |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7213 | struct smb2_ioctl_rsp *rsp, |
| 7214 | unsigned int out_buf_len) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7215 | { |
| 7216 | struct network_interface_info_ioctl_rsp *nii_rsp = NULL; |
| 7217 | int nbytes = 0; |
| 7218 | struct net_device *netdev; |
| 7219 | struct sockaddr_storage_rsp *sockaddr_storage; |
| 7220 | unsigned int flags; |
| 7221 | unsigned long long speed; |
Namjae Jeon | f1abdb7 | 2021-07-27 13:25:13 +0900 | [diff] [blame] | 7222 | struct sockaddr_in6 *csin6 = (struct sockaddr_in6 *)&conn->peer_addr; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7223 | |
| 7224 | rtnl_lock(); |
| 7225 | for_each_netdev(&init_net, netdev) { |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7226 | if (out_buf_len < |
| 7227 | nbytes + sizeof(struct network_interface_info_ioctl_rsp)) { |
| 7228 | rtnl_unlock(); |
| 7229 | return -ENOSPC; |
| 7230 | } |
| 7231 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7232 | if (netdev->type == ARPHRD_LOOPBACK) |
| 7233 | continue; |
| 7234 | |
| 7235 | flags = dev_get_flags(netdev); |
| 7236 | if (!(flags & IFF_RUNNING)) |
| 7237 | continue; |
| 7238 | |
| 7239 | nii_rsp = (struct network_interface_info_ioctl_rsp *) |
| 7240 | &rsp->Buffer[nbytes]; |
| 7241 | nii_rsp->IfIndex = cpu_to_le32(netdev->ifindex); |
| 7242 | |
Hyunchul Lee | 03d8d4f | 2021-07-13 16:09:34 +0900 | [diff] [blame] | 7243 | nii_rsp->Capability = 0; |
Hyunchul Lee | 03d8d4f | 2021-07-13 16:09:34 +0900 | [diff] [blame] | 7244 | if (ksmbd_rdma_capable_netdev(netdev)) |
| 7245 | nii_rsp->Capability |= cpu_to_le32(RDMA_CAPABLE); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7246 | |
| 7247 | nii_rsp->Next = cpu_to_le32(152); |
| 7248 | nii_rsp->Reserved = 0; |
| 7249 | |
| 7250 | if (netdev->ethtool_ops->get_link_ksettings) { |
| 7251 | struct ethtool_link_ksettings cmd; |
| 7252 | |
| 7253 | netdev->ethtool_ops->get_link_ksettings(netdev, &cmd); |
| 7254 | speed = cmd.base.speed; |
| 7255 | } else { |
Per Forlin | d475866 | 2021-08-30 13:23:04 +0900 | [diff] [blame] | 7256 | ksmbd_debug(SMB, "%s %s\n", netdev->name, |
| 7257 | "speed is unknown, defaulting to 1Gb/sec"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7258 | speed = SPEED_1000; |
| 7259 | } |
| 7260 | |
| 7261 | speed *= 1000000; |
| 7262 | nii_rsp->LinkSpeed = cpu_to_le64(speed); |
| 7263 | |
| 7264 | sockaddr_storage = (struct sockaddr_storage_rsp *) |
| 7265 | nii_rsp->SockAddr_Storage; |
| 7266 | memset(sockaddr_storage, 0, 128); |
| 7267 | |
Namjae Jeon | f1abdb7 | 2021-07-27 13:25:13 +0900 | [diff] [blame] | 7268 | if (conn->peer_addr.ss_family == PF_INET || |
| 7269 | ipv6_addr_v4mapped(&csin6->sin6_addr)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7270 | struct in_device *idev; |
| 7271 | |
| 7272 | sockaddr_storage->Family = cpu_to_le16(INTERNETWORK); |
| 7273 | sockaddr_storage->addr4.Port = 0; |
| 7274 | |
| 7275 | idev = __in_dev_get_rtnl(netdev); |
| 7276 | if (!idev) |
| 7277 | continue; |
| 7278 | sockaddr_storage->addr4.IPv4address = |
| 7279 | idev_ipv4_address(idev); |
| 7280 | } else { |
| 7281 | struct inet6_dev *idev6; |
| 7282 | struct inet6_ifaddr *ifa; |
| 7283 | __u8 *ipv6_addr = sockaddr_storage->addr6.IPv6address; |
| 7284 | |
| 7285 | sockaddr_storage->Family = cpu_to_le16(INTERNETWORKV6); |
| 7286 | sockaddr_storage->addr6.Port = 0; |
| 7287 | sockaddr_storage->addr6.FlowInfo = 0; |
| 7288 | |
| 7289 | idev6 = __in6_dev_get(netdev); |
| 7290 | if (!idev6) |
| 7291 | continue; |
| 7292 | |
| 7293 | list_for_each_entry(ifa, &idev6->addr_list, if_list) { |
| 7294 | if (ifa->flags & (IFA_F_TENTATIVE | |
| 7295 | IFA_F_DEPRECATED)) |
| 7296 | continue; |
| 7297 | memcpy(ipv6_addr, ifa->addr.s6_addr, 16); |
| 7298 | break; |
| 7299 | } |
| 7300 | sockaddr_storage->addr6.ScopeId = 0; |
| 7301 | } |
| 7302 | |
| 7303 | nbytes += sizeof(struct network_interface_info_ioctl_rsp); |
| 7304 | } |
| 7305 | rtnl_unlock(); |
| 7306 | |
| 7307 | /* zero if this is last one */ |
| 7308 | if (nii_rsp) |
| 7309 | nii_rsp->Next = 0; |
| 7310 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7311 | rsp->PersistentFileId = cpu_to_le64(SMB2_NO_FID); |
| 7312 | rsp->VolatileFileId = cpu_to_le64(SMB2_NO_FID); |
| 7313 | return nbytes; |
| 7314 | } |
| 7315 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7316 | static int fsctl_validate_negotiate_info(struct ksmbd_conn *conn, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7317 | struct validate_negotiate_info_req *neg_req, |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7318 | struct validate_negotiate_info_rsp *neg_rsp, |
| 7319 | unsigned int in_buf_len) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7320 | { |
| 7321 | int ret = 0; |
| 7322 | int dialect; |
| 7323 | |
Marios Makassikis | 78f1688 | 2021-10-28 21:01:27 +0200 | [diff] [blame] | 7324 | if (in_buf_len < offsetof(struct validate_negotiate_info_req, Dialects) + |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7325 | le16_to_cpu(neg_req->DialectCount) * sizeof(__le16)) |
| 7326 | return -EINVAL; |
| 7327 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7328 | dialect = ksmbd_lookup_dialect_by_id(neg_req->Dialects, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7329 | neg_req->DialectCount); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7330 | if (dialect == BAD_PROT_ID || dialect != conn->dialect) { |
| 7331 | ret = -EINVAL; |
| 7332 | goto err_out; |
| 7333 | } |
| 7334 | |
| 7335 | if (strncmp(neg_req->Guid, conn->ClientGUID, SMB2_CLIENT_GUID_SIZE)) { |
| 7336 | ret = -EINVAL; |
| 7337 | goto err_out; |
| 7338 | } |
| 7339 | |
| 7340 | if (le16_to_cpu(neg_req->SecurityMode) != conn->cli_sec_mode) { |
| 7341 | ret = -EINVAL; |
| 7342 | goto err_out; |
| 7343 | } |
| 7344 | |
| 7345 | if (le32_to_cpu(neg_req->Capabilities) != conn->cli_cap) { |
| 7346 | ret = -EINVAL; |
| 7347 | goto err_out; |
| 7348 | } |
| 7349 | |
| 7350 | neg_rsp->Capabilities = cpu_to_le32(conn->vals->capabilities); |
| 7351 | memset(neg_rsp->Guid, 0, SMB2_CLIENT_GUID_SIZE); |
| 7352 | neg_rsp->SecurityMode = cpu_to_le16(conn->srv_sec_mode); |
| 7353 | neg_rsp->Dialect = cpu_to_le16(conn->dialect); |
| 7354 | err_out: |
| 7355 | return ret; |
| 7356 | } |
| 7357 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7358 | static int fsctl_query_allocated_ranges(struct ksmbd_work *work, u64 id, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7359 | struct file_allocated_range_buffer *qar_req, |
| 7360 | struct file_allocated_range_buffer *qar_rsp, |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7361 | unsigned int in_count, unsigned int *out_count) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7362 | { |
| 7363 | struct ksmbd_file *fp; |
| 7364 | loff_t start, length; |
| 7365 | int ret = 0; |
| 7366 | |
| 7367 | *out_count = 0; |
| 7368 | if (in_count == 0) |
| 7369 | return -EINVAL; |
| 7370 | |
| 7371 | fp = ksmbd_lookup_fd_fast(work, id); |
| 7372 | if (!fp) |
| 7373 | return -ENOENT; |
| 7374 | |
| 7375 | start = le64_to_cpu(qar_req->file_offset); |
| 7376 | length = le64_to_cpu(qar_req->length); |
| 7377 | |
| 7378 | ret = ksmbd_vfs_fqar_lseek(fp, start, length, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7379 | qar_rsp, in_count, out_count); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7380 | if (ret && ret != -E2BIG) |
| 7381 | *out_count = 0; |
| 7382 | |
| 7383 | ksmbd_fd_put(work, fp); |
| 7384 | return ret; |
| 7385 | } |
| 7386 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7387 | static int fsctl_pipe_transceive(struct ksmbd_work *work, u64 id, |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7388 | unsigned int out_buf_len, |
| 7389 | struct smb2_ioctl_req *req, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7390 | struct smb2_ioctl_rsp *rsp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7391 | { |
| 7392 | struct ksmbd_rpc_command *rpc_resp; |
| 7393 | char *data_buf = (char *)&req->Buffer[0]; |
| 7394 | int nbytes = 0; |
| 7395 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7396 | rpc_resp = ksmbd_rpc_ioctl(work->sess, id, data_buf, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7397 | le32_to_cpu(req->InputCount)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7398 | if (rpc_resp) { |
| 7399 | if (rpc_resp->flags == KSMBD_RPC_SOME_NOT_MAPPED) { |
| 7400 | /* |
| 7401 | * set STATUS_SOME_NOT_MAPPED response |
| 7402 | * for unknown domain sid. |
| 7403 | */ |
| 7404 | rsp->hdr.Status = STATUS_SOME_NOT_MAPPED; |
| 7405 | } else if (rpc_resp->flags == KSMBD_RPC_ENOTIMPLEMENTED) { |
| 7406 | rsp->hdr.Status = STATUS_NOT_SUPPORTED; |
| 7407 | goto out; |
| 7408 | } else if (rpc_resp->flags != KSMBD_RPC_OK) { |
| 7409 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 7410 | goto out; |
| 7411 | } |
| 7412 | |
| 7413 | nbytes = rpc_resp->payload_sz; |
| 7414 | if (rpc_resp->payload_sz > out_buf_len) { |
| 7415 | rsp->hdr.Status = STATUS_BUFFER_OVERFLOW; |
| 7416 | nbytes = out_buf_len; |
| 7417 | } |
| 7418 | |
| 7419 | if (!rpc_resp->payload_sz) { |
| 7420 | rsp->hdr.Status = |
| 7421 | STATUS_UNEXPECTED_IO_ERROR; |
| 7422 | goto out; |
| 7423 | } |
| 7424 | |
| 7425 | memcpy((char *)rsp->Buffer, rpc_resp->payload, nbytes); |
| 7426 | } |
| 7427 | out: |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 7428 | kvfree(rpc_resp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7429 | return nbytes; |
| 7430 | } |
| 7431 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7432 | static inline int fsctl_set_sparse(struct ksmbd_work *work, u64 id, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7433 | struct file_sparse *sparse) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7434 | { |
| 7435 | struct ksmbd_file *fp; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 7436 | struct user_namespace *user_ns; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7437 | int ret = 0; |
| 7438 | __le32 old_fattr; |
| 7439 | |
| 7440 | fp = ksmbd_lookup_fd_fast(work, id); |
| 7441 | if (!fp) |
| 7442 | return -ENOENT; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 7443 | user_ns = file_mnt_user_ns(fp->filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7444 | |
| 7445 | old_fattr = fp->f_ci->m_fattr; |
| 7446 | if (sparse->SetSparse) |
| 7447 | fp->f_ci->m_fattr |= ATTR_SPARSE_FILE_LE; |
| 7448 | else |
| 7449 | fp->f_ci->m_fattr &= ~ATTR_SPARSE_FILE_LE; |
| 7450 | |
| 7451 | if (fp->f_ci->m_fattr != old_fattr && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7452 | test_share_config_flag(work->tcon->share_conf, |
| 7453 | KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7454 | struct xattr_dos_attrib da; |
| 7455 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 7456 | ret = ksmbd_vfs_get_dos_attrib_xattr(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 7457 | fp->filp->f_path.dentry, &da); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7458 | if (ret <= 0) |
| 7459 | goto out; |
| 7460 | |
| 7461 | da.attr = le32_to_cpu(fp->f_ci->m_fattr); |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 7462 | ret = ksmbd_vfs_set_dos_attrib_xattr(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 7463 | fp->filp->f_path.dentry, &da); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7464 | if (ret) |
| 7465 | fp->f_ci->m_fattr = old_fattr; |
| 7466 | } |
| 7467 | |
| 7468 | out: |
| 7469 | ksmbd_fd_put(work, fp); |
| 7470 | return ret; |
| 7471 | } |
| 7472 | |
| 7473 | static int fsctl_request_resume_key(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7474 | struct smb2_ioctl_req *req, |
| 7475 | struct resume_key_ioctl_rsp *key_rsp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7476 | { |
| 7477 | struct ksmbd_file *fp; |
| 7478 | |
| 7479 | fp = ksmbd_lookup_fd_slow(work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7480 | le64_to_cpu(req->VolatileFileId), |
| 7481 | le64_to_cpu(req->PersistentFileId)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7482 | if (!fp) |
| 7483 | return -ENOENT; |
| 7484 | |
| 7485 | memset(key_rsp, 0, sizeof(*key_rsp)); |
| 7486 | key_rsp->ResumeKey[0] = req->VolatileFileId; |
| 7487 | key_rsp->ResumeKey[1] = req->PersistentFileId; |
| 7488 | ksmbd_fd_put(work, fp); |
| 7489 | |
| 7490 | return 0; |
| 7491 | } |
| 7492 | |
| 7493 | /** |
| 7494 | * smb2_ioctl() - handler for smb2 ioctl command |
| 7495 | * @work: smb work containing ioctl command buffer |
| 7496 | * |
| 7497 | * Return: 0 on success, otherwise error |
| 7498 | */ |
| 7499 | int smb2_ioctl(struct ksmbd_work *work) |
| 7500 | { |
| 7501 | struct smb2_ioctl_req *req; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 7502 | struct smb2_ioctl_rsp *rsp; |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7503 | unsigned int cnt_code, nbytes = 0, out_buf_len, in_buf_len; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7504 | u64 id = KSMBD_NO_FID; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7505 | struct ksmbd_conn *conn = work->conn; |
| 7506 | int ret = 0; |
| 7507 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7508 | if (work->next_smb2_rcv_hdr_off) { |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 7509 | req = ksmbd_req_buf_next(work); |
| 7510 | rsp = ksmbd_resp_buf_next(work); |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 7511 | if (!has_file_id(le64_to_cpu(req->VolatileFileId))) { |
| 7512 | ksmbd_debug(SMB, "Compound request set FID = %llu\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7513 | work->compound_fid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7514 | id = work->compound_fid; |
| 7515 | } |
| 7516 | } else { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 7517 | req = smb2_get_msg(work->request_buf); |
| 7518 | rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7519 | } |
| 7520 | |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 7521 | if (!has_file_id(id)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7522 | id = le64_to_cpu(req->VolatileFileId); |
| 7523 | |
| 7524 | if (req->Flags != cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL)) { |
| 7525 | rsp->hdr.Status = STATUS_NOT_SUPPORTED; |
| 7526 | goto out; |
| 7527 | } |
| 7528 | |
| 7529 | cnt_code = le32_to_cpu(req->CntCode); |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 7530 | ret = smb2_calc_max_out_buf_len(work, 48, |
| 7531 | le32_to_cpu(req->MaxOutputResponse)); |
| 7532 | if (ret < 0) { |
| 7533 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 7534 | goto out; |
| 7535 | } |
| 7536 | out_buf_len = (unsigned int)ret; |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7537 | in_buf_len = le32_to_cpu(req->InputCount); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7538 | |
| 7539 | switch (cnt_code) { |
| 7540 | case FSCTL_DFS_GET_REFERRALS: |
| 7541 | case FSCTL_DFS_GET_REFERRALS_EX: |
| 7542 | /* Not support DFS yet */ |
| 7543 | rsp->hdr.Status = STATUS_FS_DRIVER_REQUIRED; |
| 7544 | goto out; |
| 7545 | case FSCTL_CREATE_OR_GET_OBJECT_ID: |
| 7546 | { |
| 7547 | struct file_object_buf_type1_ioctl_rsp *obj_buf; |
| 7548 | |
| 7549 | nbytes = sizeof(struct file_object_buf_type1_ioctl_rsp); |
| 7550 | obj_buf = (struct file_object_buf_type1_ioctl_rsp *) |
| 7551 | &rsp->Buffer[0]; |
| 7552 | |
| 7553 | /* |
| 7554 | * TODO: This is dummy implementation to pass smbtorture |
| 7555 | * Need to check correct response later |
| 7556 | */ |
| 7557 | memset(obj_buf->ObjectId, 0x0, 16); |
| 7558 | memset(obj_buf->BirthVolumeId, 0x0, 16); |
| 7559 | memset(obj_buf->BirthObjectId, 0x0, 16); |
| 7560 | memset(obj_buf->DomainId, 0x0, 16); |
| 7561 | |
| 7562 | break; |
| 7563 | } |
| 7564 | case FSCTL_PIPE_TRANSCEIVE: |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7565 | 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] | 7566 | nbytes = fsctl_pipe_transceive(work, id, out_buf_len, req, rsp); |
| 7567 | break; |
| 7568 | case FSCTL_VALIDATE_NEGOTIATE_INFO: |
| 7569 | if (conn->dialect < SMB30_PROT_ID) { |
| 7570 | ret = -EOPNOTSUPP; |
| 7571 | goto out; |
| 7572 | } |
| 7573 | |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7574 | if (in_buf_len < sizeof(struct validate_negotiate_info_req)) |
| 7575 | return -EINVAL; |
| 7576 | |
| 7577 | if (out_buf_len < sizeof(struct validate_negotiate_info_rsp)) |
| 7578 | return -EINVAL; |
| 7579 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7580 | ret = fsctl_validate_negotiate_info(conn, |
| 7581 | (struct validate_negotiate_info_req *)&req->Buffer[0], |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7582 | (struct validate_negotiate_info_rsp *)&rsp->Buffer[0], |
| 7583 | in_buf_len); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7584 | if (ret < 0) |
| 7585 | goto out; |
| 7586 | |
| 7587 | nbytes = sizeof(struct validate_negotiate_info_rsp); |
| 7588 | rsp->PersistentFileId = cpu_to_le64(SMB2_NO_FID); |
| 7589 | rsp->VolatileFileId = cpu_to_le64(SMB2_NO_FID); |
| 7590 | break; |
| 7591 | case FSCTL_QUERY_NETWORK_INTERFACE_INFO: |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7592 | ret = fsctl_query_iface_info_ioctl(conn, rsp, out_buf_len); |
| 7593 | if (ret < 0) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7594 | goto out; |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7595 | nbytes = ret; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7596 | break; |
| 7597 | case FSCTL_REQUEST_RESUME_KEY: |
| 7598 | if (out_buf_len < sizeof(struct resume_key_ioctl_rsp)) { |
| 7599 | ret = -EINVAL; |
| 7600 | goto out; |
| 7601 | } |
| 7602 | |
| 7603 | ret = fsctl_request_resume_key(work, req, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7604 | (struct resume_key_ioctl_rsp *)&rsp->Buffer[0]); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7605 | if (ret < 0) |
| 7606 | goto out; |
| 7607 | rsp->PersistentFileId = req->PersistentFileId; |
| 7608 | rsp->VolatileFileId = req->VolatileFileId; |
| 7609 | nbytes = sizeof(struct resume_key_ioctl_rsp); |
| 7610 | break; |
| 7611 | case FSCTL_COPYCHUNK: |
| 7612 | case FSCTL_COPYCHUNK_WRITE: |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7613 | if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7614 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7615 | "User does not have write permission\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7616 | ret = -EACCES; |
| 7617 | goto out; |
| 7618 | } |
| 7619 | |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7620 | if (in_buf_len < sizeof(struct copychunk_ioctl_req)) { |
| 7621 | ret = -EINVAL; |
| 7622 | goto out; |
| 7623 | } |
| 7624 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7625 | if (out_buf_len < sizeof(struct copychunk_ioctl_rsp)) { |
| 7626 | ret = -EINVAL; |
| 7627 | goto out; |
| 7628 | } |
| 7629 | |
| 7630 | nbytes = sizeof(struct copychunk_ioctl_rsp); |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7631 | rsp->VolatileFileId = req->VolatileFileId; |
| 7632 | rsp->PersistentFileId = req->PersistentFileId; |
| 7633 | fsctl_copychunk(work, |
| 7634 | (struct copychunk_ioctl_req *)&req->Buffer[0], |
| 7635 | le32_to_cpu(req->CntCode), |
| 7636 | le32_to_cpu(req->InputCount), |
| 7637 | le64_to_cpu(req->VolatileFileId), |
| 7638 | le64_to_cpu(req->PersistentFileId), |
| 7639 | rsp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7640 | break; |
| 7641 | case FSCTL_SET_SPARSE: |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7642 | if (in_buf_len < sizeof(struct file_sparse)) { |
| 7643 | ret = -EINVAL; |
| 7644 | goto out; |
| 7645 | } |
| 7646 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7647 | ret = fsctl_set_sparse(work, id, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7648 | (struct file_sparse *)&req->Buffer[0]); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7649 | if (ret < 0) |
| 7650 | goto out; |
| 7651 | break; |
| 7652 | case FSCTL_SET_ZERO_DATA: |
| 7653 | { |
| 7654 | struct file_zero_data_information *zero_data; |
| 7655 | struct ksmbd_file *fp; |
| 7656 | loff_t off, len; |
| 7657 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7658 | if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7659 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7660 | "User does not have write permission\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7661 | ret = -EACCES; |
| 7662 | goto out; |
| 7663 | } |
| 7664 | |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7665 | if (in_buf_len < sizeof(struct file_zero_data_information)) { |
| 7666 | ret = -EINVAL; |
| 7667 | goto out; |
| 7668 | } |
| 7669 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7670 | zero_data = |
| 7671 | (struct file_zero_data_information *)&req->Buffer[0]; |
| 7672 | |
| 7673 | fp = ksmbd_lookup_fd_fast(work, id); |
| 7674 | if (!fp) { |
| 7675 | ret = -ENOENT; |
| 7676 | goto out; |
| 7677 | } |
| 7678 | |
| 7679 | off = le64_to_cpu(zero_data->FileOffset); |
| 7680 | len = le64_to_cpu(zero_data->BeyondFinalZero) - off; |
| 7681 | |
| 7682 | ret = ksmbd_vfs_zero_data(work, fp, off, len); |
| 7683 | ksmbd_fd_put(work, fp); |
| 7684 | if (ret < 0) |
| 7685 | goto out; |
| 7686 | break; |
| 7687 | } |
| 7688 | case FSCTL_QUERY_ALLOCATED_RANGES: |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7689 | if (in_buf_len < sizeof(struct file_allocated_range_buffer)) { |
| 7690 | ret = -EINVAL; |
| 7691 | goto out; |
| 7692 | } |
| 7693 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7694 | ret = fsctl_query_allocated_ranges(work, id, |
| 7695 | (struct file_allocated_range_buffer *)&req->Buffer[0], |
| 7696 | (struct file_allocated_range_buffer *)&rsp->Buffer[0], |
| 7697 | out_buf_len / |
| 7698 | sizeof(struct file_allocated_range_buffer), &nbytes); |
| 7699 | if (ret == -E2BIG) { |
| 7700 | rsp->hdr.Status = STATUS_BUFFER_OVERFLOW; |
| 7701 | } else if (ret < 0) { |
| 7702 | nbytes = 0; |
| 7703 | goto out; |
| 7704 | } |
| 7705 | |
| 7706 | nbytes *= sizeof(struct file_allocated_range_buffer); |
| 7707 | break; |
| 7708 | case FSCTL_GET_REPARSE_POINT: |
| 7709 | { |
| 7710 | struct reparse_data_buffer *reparse_ptr; |
| 7711 | struct ksmbd_file *fp; |
| 7712 | |
| 7713 | reparse_ptr = (struct reparse_data_buffer *)&rsp->Buffer[0]; |
| 7714 | fp = ksmbd_lookup_fd_fast(work, id); |
| 7715 | if (!fp) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 7716 | pr_err("not found fp!!\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7717 | ret = -ENOENT; |
| 7718 | goto out; |
| 7719 | } |
| 7720 | |
| 7721 | reparse_ptr->ReparseTag = |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 7722 | smb2_get_reparse_tag_special_file(file_inode(fp->filp)->i_mode); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7723 | reparse_ptr->ReparseDataLength = 0; |
| 7724 | ksmbd_fd_put(work, fp); |
| 7725 | nbytes = sizeof(struct reparse_data_buffer); |
| 7726 | break; |
| 7727 | } |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 7728 | case FSCTL_DUPLICATE_EXTENTS_TO_FILE: |
| 7729 | { |
| 7730 | struct ksmbd_file *fp_in, *fp_out = NULL; |
| 7731 | struct duplicate_extents_to_file *dup_ext; |
| 7732 | loff_t src_off, dst_off, length, cloned; |
| 7733 | |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7734 | if (in_buf_len < sizeof(struct duplicate_extents_to_file)) { |
| 7735 | ret = -EINVAL; |
| 7736 | goto out; |
| 7737 | } |
| 7738 | |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 7739 | dup_ext = (struct duplicate_extents_to_file *)&req->Buffer[0]; |
| 7740 | |
| 7741 | fp_in = ksmbd_lookup_fd_slow(work, dup_ext->VolatileFileHandle, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7742 | dup_ext->PersistentFileHandle); |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 7743 | if (!fp_in) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 7744 | pr_err("not found file handle in duplicate extent to file\n"); |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 7745 | ret = -ENOENT; |
| 7746 | goto out; |
| 7747 | } |
| 7748 | |
| 7749 | fp_out = ksmbd_lookup_fd_fast(work, id); |
| 7750 | if (!fp_out) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 7751 | pr_err("not found fp\n"); |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 7752 | ret = -ENOENT; |
| 7753 | goto dup_ext_out; |
| 7754 | } |
| 7755 | |
| 7756 | src_off = le64_to_cpu(dup_ext->SourceFileOffset); |
| 7757 | dst_off = le64_to_cpu(dup_ext->TargetFileOffset); |
| 7758 | length = le64_to_cpu(dup_ext->ByteCount); |
| 7759 | 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] | 7760 | dst_off, length, 0); |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 7761 | if (cloned == -EXDEV || cloned == -EOPNOTSUPP) { |
| 7762 | ret = -EOPNOTSUPP; |
| 7763 | goto dup_ext_out; |
| 7764 | } else if (cloned != length) { |
Namjae Jeon | f852477 | 2021-06-18 10:28:00 +0900 | [diff] [blame] | 7765 | cloned = vfs_copy_file_range(fp_in->filp, src_off, |
| 7766 | fp_out->filp, dst_off, length, 0); |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 7767 | if (cloned != length) { |
| 7768 | if (cloned < 0) |
| 7769 | ret = cloned; |
| 7770 | else |
| 7771 | ret = -EINVAL; |
| 7772 | } |
| 7773 | } |
| 7774 | |
| 7775 | dup_ext_out: |
| 7776 | ksmbd_fd_put(work, fp_in); |
| 7777 | ksmbd_fd_put(work, fp_out); |
| 7778 | if (ret < 0) |
| 7779 | goto out; |
| 7780 | break; |
| 7781 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7782 | default: |
| 7783 | ksmbd_debug(SMB, "not implemented yet ioctl command 0x%x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7784 | cnt_code); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7785 | ret = -EOPNOTSUPP; |
| 7786 | goto out; |
| 7787 | } |
| 7788 | |
| 7789 | rsp->CntCode = cpu_to_le32(cnt_code); |
| 7790 | rsp->InputCount = cpu_to_le32(0); |
| 7791 | rsp->InputOffset = cpu_to_le32(112); |
| 7792 | rsp->OutputOffset = cpu_to_le32(112); |
| 7793 | rsp->OutputCount = cpu_to_le32(nbytes); |
| 7794 | rsp->StructureSize = cpu_to_le16(49); |
| 7795 | rsp->Reserved = cpu_to_le16(0); |
| 7796 | rsp->Flags = cpu_to_le32(0); |
| 7797 | rsp->Reserved2 = cpu_to_le32(0); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 7798 | inc_rfc1001_len(work->response_buf, 48 + nbytes); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7799 | |
| 7800 | return 0; |
| 7801 | |
| 7802 | out: |
| 7803 | if (ret == -EACCES) |
| 7804 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 7805 | else if (ret == -ENOENT) |
| 7806 | rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND; |
| 7807 | else if (ret == -EOPNOTSUPP) |
| 7808 | rsp->hdr.Status = STATUS_NOT_SUPPORTED; |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7809 | else if (ret == -ENOSPC) |
| 7810 | rsp->hdr.Status = STATUS_BUFFER_TOO_SMALL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7811 | else if (ret < 0 || rsp->hdr.Status == 0) |
| 7812 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 7813 | smb2_set_err_rsp(work); |
| 7814 | return 0; |
| 7815 | } |
| 7816 | |
| 7817 | /** |
| 7818 | * smb20_oplock_break_ack() - handler for smb2.0 oplock break command |
| 7819 | * @work: smb work containing oplock break command buffer |
| 7820 | * |
| 7821 | * Return: 0 |
| 7822 | */ |
| 7823 | static void smb20_oplock_break_ack(struct ksmbd_work *work) |
| 7824 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 7825 | struct smb2_oplock_break *req = smb2_get_msg(work->request_buf); |
| 7826 | struct smb2_oplock_break *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7827 | struct ksmbd_file *fp; |
| 7828 | struct oplock_info *opinfo = NULL; |
| 7829 | __le32 err = 0; |
| 7830 | int ret = 0; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7831 | u64 volatile_id, persistent_id; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7832 | char req_oplevel = 0, rsp_oplevel = 0; |
| 7833 | unsigned int oplock_change_type; |
| 7834 | |
| 7835 | volatile_id = le64_to_cpu(req->VolatileFid); |
| 7836 | persistent_id = le64_to_cpu(req->PersistentFid); |
| 7837 | req_oplevel = req->OplockLevel; |
| 7838 | ksmbd_debug(OPLOCK, "v_id %llu, p_id %llu request oplock level %d\n", |
| 7839 | volatile_id, persistent_id, req_oplevel); |
| 7840 | |
| 7841 | fp = ksmbd_lookup_fd_slow(work, volatile_id, persistent_id); |
| 7842 | if (!fp) { |
| 7843 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 7844 | smb2_set_err_rsp(work); |
| 7845 | return; |
| 7846 | } |
| 7847 | |
| 7848 | opinfo = opinfo_get(fp); |
| 7849 | if (!opinfo) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 7850 | pr_err("unexpected null oplock_info\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7851 | rsp->hdr.Status = STATUS_INVALID_OPLOCK_PROTOCOL; |
| 7852 | smb2_set_err_rsp(work); |
| 7853 | ksmbd_fd_put(work, fp); |
| 7854 | return; |
| 7855 | } |
| 7856 | |
| 7857 | if (opinfo->level == SMB2_OPLOCK_LEVEL_NONE) { |
| 7858 | rsp->hdr.Status = STATUS_INVALID_OPLOCK_PROTOCOL; |
| 7859 | goto err_out; |
| 7860 | } |
| 7861 | |
| 7862 | if (opinfo->op_state == OPLOCK_STATE_NONE) { |
| 7863 | ksmbd_debug(SMB, "unexpected oplock state 0x%x\n", opinfo->op_state); |
| 7864 | rsp->hdr.Status = STATUS_UNSUCCESSFUL; |
| 7865 | goto err_out; |
| 7866 | } |
| 7867 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7868 | if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE || |
| 7869 | opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) && |
| 7870 | (req_oplevel != SMB2_OPLOCK_LEVEL_II && |
| 7871 | req_oplevel != SMB2_OPLOCK_LEVEL_NONE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7872 | err = STATUS_INVALID_OPLOCK_PROTOCOL; |
| 7873 | oplock_change_type = OPLOCK_WRITE_TO_NONE; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7874 | } else if (opinfo->level == SMB2_OPLOCK_LEVEL_II && |
| 7875 | req_oplevel != SMB2_OPLOCK_LEVEL_NONE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7876 | err = STATUS_INVALID_OPLOCK_PROTOCOL; |
| 7877 | oplock_change_type = OPLOCK_READ_TO_NONE; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7878 | } else if (req_oplevel == SMB2_OPLOCK_LEVEL_II || |
| 7879 | req_oplevel == SMB2_OPLOCK_LEVEL_NONE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7880 | err = STATUS_INVALID_DEVICE_STATE; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7881 | if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE || |
| 7882 | opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) && |
| 7883 | req_oplevel == SMB2_OPLOCK_LEVEL_II) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7884 | oplock_change_type = OPLOCK_WRITE_TO_READ; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7885 | } else if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE || |
| 7886 | opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) && |
| 7887 | req_oplevel == SMB2_OPLOCK_LEVEL_NONE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7888 | oplock_change_type = OPLOCK_WRITE_TO_NONE; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7889 | } else if (opinfo->level == SMB2_OPLOCK_LEVEL_II && |
| 7890 | req_oplevel == SMB2_OPLOCK_LEVEL_NONE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7891 | oplock_change_type = OPLOCK_READ_TO_NONE; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7892 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7893 | oplock_change_type = 0; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7894 | } |
| 7895 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7896 | oplock_change_type = 0; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7897 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7898 | |
| 7899 | switch (oplock_change_type) { |
| 7900 | case OPLOCK_WRITE_TO_READ: |
| 7901 | ret = opinfo_write_to_read(opinfo); |
| 7902 | rsp_oplevel = SMB2_OPLOCK_LEVEL_II; |
| 7903 | break; |
| 7904 | case OPLOCK_WRITE_TO_NONE: |
| 7905 | ret = opinfo_write_to_none(opinfo); |
| 7906 | rsp_oplevel = SMB2_OPLOCK_LEVEL_NONE; |
| 7907 | break; |
| 7908 | case OPLOCK_READ_TO_NONE: |
| 7909 | ret = opinfo_read_to_none(opinfo); |
| 7910 | rsp_oplevel = SMB2_OPLOCK_LEVEL_NONE; |
| 7911 | break; |
| 7912 | default: |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 7913 | pr_err("unknown oplock change 0x%x -> 0x%x\n", |
| 7914 | opinfo->level, rsp_oplevel); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7915 | } |
| 7916 | |
| 7917 | if (ret < 0) { |
| 7918 | rsp->hdr.Status = err; |
| 7919 | goto err_out; |
| 7920 | } |
| 7921 | |
| 7922 | opinfo_put(opinfo); |
| 7923 | ksmbd_fd_put(work, fp); |
| 7924 | opinfo->op_state = OPLOCK_STATE_NONE; |
| 7925 | wake_up_interruptible_all(&opinfo->oplock_q); |
| 7926 | |
| 7927 | rsp->StructureSize = cpu_to_le16(24); |
| 7928 | rsp->OplockLevel = rsp_oplevel; |
| 7929 | rsp->Reserved = 0; |
| 7930 | rsp->Reserved2 = 0; |
| 7931 | rsp->VolatileFid = cpu_to_le64(volatile_id); |
| 7932 | rsp->PersistentFid = cpu_to_le64(persistent_id); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 7933 | inc_rfc1001_len(work->response_buf, 24); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7934 | return; |
| 7935 | |
| 7936 | err_out: |
| 7937 | opinfo->op_state = OPLOCK_STATE_NONE; |
| 7938 | wake_up_interruptible_all(&opinfo->oplock_q); |
| 7939 | |
| 7940 | opinfo_put(opinfo); |
| 7941 | ksmbd_fd_put(work, fp); |
| 7942 | smb2_set_err_rsp(work); |
| 7943 | } |
| 7944 | |
| 7945 | static int check_lease_state(struct lease *lease, __le32 req_state) |
| 7946 | { |
| 7947 | if ((lease->new_state == |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7948 | (SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_HANDLE_CACHING_LE)) && |
| 7949 | !(req_state & SMB2_LEASE_WRITE_CACHING_LE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7950 | lease->new_state = req_state; |
| 7951 | return 0; |
| 7952 | } |
| 7953 | |
| 7954 | if (lease->new_state == req_state) |
| 7955 | return 0; |
| 7956 | |
| 7957 | return 1; |
| 7958 | } |
| 7959 | |
| 7960 | /** |
| 7961 | * smb21_lease_break_ack() - handler for smb2.1 lease break command |
| 7962 | * @work: smb work containing lease break command buffer |
| 7963 | * |
| 7964 | * Return: 0 |
| 7965 | */ |
| 7966 | static void smb21_lease_break_ack(struct ksmbd_work *work) |
| 7967 | { |
| 7968 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 7969 | struct smb2_lease_ack *req = smb2_get_msg(work->request_buf); |
| 7970 | struct smb2_lease_ack *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7971 | struct oplock_info *opinfo; |
| 7972 | __le32 err = 0; |
| 7973 | int ret = 0; |
| 7974 | unsigned int lease_change_type; |
| 7975 | __le32 lease_state; |
| 7976 | struct lease *lease; |
| 7977 | |
| 7978 | ksmbd_debug(OPLOCK, "smb21 lease break, lease state(0x%x)\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7979 | le32_to_cpu(req->LeaseState)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7980 | opinfo = lookup_lease_in_table(conn, req->LeaseKey); |
| 7981 | if (!opinfo) { |
| 7982 | ksmbd_debug(OPLOCK, "file not opened\n"); |
| 7983 | smb2_set_err_rsp(work); |
| 7984 | rsp->hdr.Status = STATUS_UNSUCCESSFUL; |
| 7985 | return; |
| 7986 | } |
| 7987 | lease = opinfo->o_lease; |
| 7988 | |
| 7989 | if (opinfo->op_state == OPLOCK_STATE_NONE) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 7990 | pr_err("unexpected lease break state 0x%x\n", |
| 7991 | opinfo->op_state); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7992 | rsp->hdr.Status = STATUS_UNSUCCESSFUL; |
| 7993 | goto err_out; |
| 7994 | } |
| 7995 | |
| 7996 | if (check_lease_state(lease, req->LeaseState)) { |
| 7997 | rsp->hdr.Status = STATUS_REQUEST_NOT_ACCEPTED; |
| 7998 | ksmbd_debug(OPLOCK, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7999 | "req lease state: 0x%x, expected state: 0x%x\n", |
| 8000 | req->LeaseState, lease->new_state); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8001 | goto err_out; |
| 8002 | } |
| 8003 | |
| 8004 | if (!atomic_read(&opinfo->breaking_cnt)) { |
| 8005 | rsp->hdr.Status = STATUS_UNSUCCESSFUL; |
| 8006 | goto err_out; |
| 8007 | } |
| 8008 | |
| 8009 | /* check for bad lease state */ |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 8010 | if (req->LeaseState & |
| 8011 | (~(SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_HANDLE_CACHING_LE))) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8012 | err = STATUS_INVALID_OPLOCK_PROTOCOL; |
| 8013 | if (lease->state & SMB2_LEASE_WRITE_CACHING_LE) |
| 8014 | lease_change_type = OPLOCK_WRITE_TO_NONE; |
| 8015 | else |
| 8016 | lease_change_type = OPLOCK_READ_TO_NONE; |
| 8017 | ksmbd_debug(OPLOCK, "handle bad lease state 0x%x -> 0x%x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 8018 | le32_to_cpu(lease->state), |
| 8019 | le32_to_cpu(req->LeaseState)); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 8020 | } else if (lease->state == SMB2_LEASE_READ_CACHING_LE && |
| 8021 | req->LeaseState != SMB2_LEASE_NONE_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8022 | err = STATUS_INVALID_OPLOCK_PROTOCOL; |
| 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 | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8027 | } else { |
| 8028 | /* valid lease state changes */ |
| 8029 | err = STATUS_INVALID_DEVICE_STATE; |
| 8030 | if (req->LeaseState == SMB2_LEASE_NONE_LE) { |
| 8031 | if (lease->state & SMB2_LEASE_WRITE_CACHING_LE) |
| 8032 | lease_change_type = OPLOCK_WRITE_TO_NONE; |
| 8033 | else |
| 8034 | lease_change_type = OPLOCK_READ_TO_NONE; |
| 8035 | } else if (req->LeaseState & SMB2_LEASE_READ_CACHING_LE) { |
| 8036 | if (lease->state & SMB2_LEASE_WRITE_CACHING_LE) |
| 8037 | lease_change_type = OPLOCK_WRITE_TO_READ; |
| 8038 | else |
| 8039 | lease_change_type = OPLOCK_READ_HANDLE_TO_READ; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 8040 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8041 | lease_change_type = 0; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 8042 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8043 | } |
| 8044 | |
| 8045 | switch (lease_change_type) { |
| 8046 | case OPLOCK_WRITE_TO_READ: |
| 8047 | ret = opinfo_write_to_read(opinfo); |
| 8048 | break; |
| 8049 | case OPLOCK_READ_HANDLE_TO_READ: |
| 8050 | ret = opinfo_read_handle_to_read(opinfo); |
| 8051 | break; |
| 8052 | case OPLOCK_WRITE_TO_NONE: |
| 8053 | ret = opinfo_write_to_none(opinfo); |
| 8054 | break; |
| 8055 | case OPLOCK_READ_TO_NONE: |
| 8056 | ret = opinfo_read_to_none(opinfo); |
| 8057 | break; |
| 8058 | default: |
| 8059 | ksmbd_debug(OPLOCK, "unknown lease change 0x%x -> 0x%x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 8060 | le32_to_cpu(lease->state), |
| 8061 | le32_to_cpu(req->LeaseState)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8062 | } |
| 8063 | |
| 8064 | lease_state = lease->state; |
| 8065 | opinfo->op_state = OPLOCK_STATE_NONE; |
| 8066 | wake_up_interruptible_all(&opinfo->oplock_q); |
| 8067 | atomic_dec(&opinfo->breaking_cnt); |
| 8068 | wake_up_interruptible_all(&opinfo->oplock_brk); |
| 8069 | opinfo_put(opinfo); |
| 8070 | |
| 8071 | if (ret < 0) { |
| 8072 | rsp->hdr.Status = err; |
| 8073 | goto err_out; |
| 8074 | } |
| 8075 | |
| 8076 | rsp->StructureSize = cpu_to_le16(36); |
| 8077 | rsp->Reserved = 0; |
| 8078 | rsp->Flags = 0; |
| 8079 | memcpy(rsp->LeaseKey, req->LeaseKey, 16); |
| 8080 | rsp->LeaseState = lease_state; |
| 8081 | rsp->LeaseDuration = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8082 | inc_rfc1001_len(work->response_buf, 36); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8083 | return; |
| 8084 | |
| 8085 | err_out: |
| 8086 | opinfo->op_state = OPLOCK_STATE_NONE; |
| 8087 | wake_up_interruptible_all(&opinfo->oplock_q); |
| 8088 | atomic_dec(&opinfo->breaking_cnt); |
| 8089 | wake_up_interruptible_all(&opinfo->oplock_brk); |
| 8090 | |
| 8091 | opinfo_put(opinfo); |
| 8092 | smb2_set_err_rsp(work); |
| 8093 | } |
| 8094 | |
| 8095 | /** |
| 8096 | * smb2_oplock_break() - dispatcher for smb2.0 and 2.1 oplock/lease break |
| 8097 | * @work: smb work containing oplock/lease break command buffer |
| 8098 | * |
| 8099 | * Return: 0 |
| 8100 | */ |
| 8101 | int smb2_oplock_break(struct ksmbd_work *work) |
| 8102 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8103 | struct smb2_oplock_break *req = smb2_get_msg(work->request_buf); |
| 8104 | struct smb2_oplock_break *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8105 | |
| 8106 | switch (le16_to_cpu(req->StructureSize)) { |
| 8107 | case OP_BREAK_STRUCT_SIZE_20: |
| 8108 | smb20_oplock_break_ack(work); |
| 8109 | break; |
| 8110 | case OP_BREAK_STRUCT_SIZE_21: |
| 8111 | smb21_lease_break_ack(work); |
| 8112 | break; |
| 8113 | default: |
| 8114 | ksmbd_debug(OPLOCK, "invalid break cmd %d\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 8115 | le16_to_cpu(req->StructureSize)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8116 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 8117 | smb2_set_err_rsp(work); |
| 8118 | } |
| 8119 | |
| 8120 | return 0; |
| 8121 | } |
| 8122 | |
| 8123 | /** |
| 8124 | * smb2_notify() - handler for smb2 notify request |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 8125 | * @work: smb work containing notify command buffer |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8126 | * |
| 8127 | * Return: 0 |
| 8128 | */ |
| 8129 | int smb2_notify(struct ksmbd_work *work) |
| 8130 | { |
Ronnie Sahlberg | 699230f | 2021-09-09 12:26:12 +0900 | [diff] [blame^] | 8131 | struct smb2_change_notify_req *req; |
| 8132 | struct smb2_change_notify_rsp *rsp; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8133 | |
| 8134 | WORK_BUFFERS(work, req, rsp); |
| 8135 | |
| 8136 | if (work->next_smb2_rcv_hdr_off && req->hdr.NextCommand) { |
| 8137 | rsp->hdr.Status = STATUS_INTERNAL_ERROR; |
| 8138 | smb2_set_err_rsp(work); |
| 8139 | return 0; |
| 8140 | } |
| 8141 | |
| 8142 | smb2_set_err_rsp(work); |
| 8143 | rsp->hdr.Status = STATUS_NOT_IMPLEMENTED; |
| 8144 | return 0; |
| 8145 | } |
| 8146 | |
| 8147 | /** |
| 8148 | * smb2_is_sign_req() - handler for checking packet signing status |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 8149 | * @work: smb work containing notify command buffer |
| 8150 | * @command: SMB2 command id |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8151 | * |
| 8152 | * Return: true if packed is signed, false otherwise |
| 8153 | */ |
| 8154 | bool smb2_is_sign_req(struct ksmbd_work *work, unsigned int command) |
| 8155 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8156 | struct smb2_hdr *rcv_hdr2 = smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8157 | |
| 8158 | if ((rcv_hdr2->Flags & SMB2_FLAGS_SIGNED) && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 8159 | command != SMB2_NEGOTIATE_HE && |
| 8160 | command != SMB2_SESSION_SETUP_HE && |
| 8161 | command != SMB2_OPLOCK_BREAK_HE) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8162 | return true; |
| 8163 | |
kernel test robot | 5616015 | 2021-05-12 09:24:37 +0900 | [diff] [blame] | 8164 | return false; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8165 | } |
| 8166 | |
| 8167 | /** |
| 8168 | * smb2_check_sign_req() - handler for req packet sign processing |
| 8169 | * @work: smb work containing notify command buffer |
| 8170 | * |
| 8171 | * Return: 1 on success, 0 otherwise |
| 8172 | */ |
| 8173 | int smb2_check_sign_req(struct ksmbd_work *work) |
| 8174 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8175 | struct smb2_hdr *hdr; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8176 | char signature_req[SMB2_SIGNATURE_SIZE]; |
| 8177 | char signature[SMB2_HMACSHA256_SIZE]; |
| 8178 | struct kvec iov[1]; |
| 8179 | size_t len; |
| 8180 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8181 | hdr = smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8182 | if (work->next_smb2_rcv_hdr_off) |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 8183 | hdr = ksmbd_req_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8184 | |
| 8185 | if (!hdr->NextCommand && !work->next_smb2_rcv_hdr_off) |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8186 | len = get_rfc1002_len(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8187 | else if (hdr->NextCommand) |
| 8188 | len = le32_to_cpu(hdr->NextCommand); |
| 8189 | else |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8190 | len = get_rfc1002_len(work->request_buf) - |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8191 | work->next_smb2_rcv_hdr_off; |
| 8192 | |
| 8193 | memcpy(signature_req, hdr->Signature, SMB2_SIGNATURE_SIZE); |
| 8194 | memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE); |
| 8195 | |
| 8196 | iov[0].iov_base = (char *)&hdr->ProtocolId; |
| 8197 | iov[0].iov_len = len; |
| 8198 | |
| 8199 | 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] | 8200 | signature)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8201 | return 0; |
| 8202 | |
| 8203 | if (memcmp(signature, signature_req, SMB2_SIGNATURE_SIZE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 8204 | pr_err("bad smb2 signature\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8205 | return 0; |
| 8206 | } |
| 8207 | |
| 8208 | return 1; |
| 8209 | } |
| 8210 | |
| 8211 | /** |
| 8212 | * smb2_set_sign_rsp() - handler for rsp packet sign processing |
| 8213 | * @work: smb work containing notify command buffer |
| 8214 | * |
| 8215 | */ |
| 8216 | void smb2_set_sign_rsp(struct ksmbd_work *work) |
| 8217 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8218 | struct smb2_hdr *hdr; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8219 | struct smb2_hdr *req_hdr; |
| 8220 | char signature[SMB2_HMACSHA256_SIZE]; |
| 8221 | struct kvec iov[2]; |
| 8222 | size_t len; |
| 8223 | int n_vec = 1; |
| 8224 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8225 | hdr = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8226 | if (work->next_smb2_rsp_hdr_off) |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 8227 | hdr = ksmbd_resp_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8228 | |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 8229 | req_hdr = ksmbd_req_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8230 | |
| 8231 | if (!work->next_smb2_rsp_hdr_off) { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8232 | len = get_rfc1002_len(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8233 | if (req_hdr->NextCommand) |
| 8234 | len = ALIGN(len, 8); |
| 8235 | } else { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8236 | len = get_rfc1002_len(work->response_buf) - |
| 8237 | work->next_smb2_rsp_hdr_off; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8238 | len = ALIGN(len, 8); |
| 8239 | } |
| 8240 | |
| 8241 | if (req_hdr->NextCommand) |
| 8242 | hdr->NextCommand = cpu_to_le32(len); |
| 8243 | |
| 8244 | hdr->Flags |= SMB2_FLAGS_SIGNED; |
| 8245 | memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE); |
| 8246 | |
| 8247 | iov[0].iov_base = (char *)&hdr->ProtocolId; |
| 8248 | iov[0].iov_len = len; |
| 8249 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8250 | if (work->aux_payload_sz) { |
| 8251 | iov[0].iov_len -= work->aux_payload_sz; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8252 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8253 | iov[1].iov_base = work->aux_payload_buf; |
| 8254 | iov[1].iov_len = work->aux_payload_sz; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8255 | n_vec++; |
| 8256 | } |
| 8257 | |
| 8258 | 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] | 8259 | signature)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8260 | memcpy(hdr->Signature, signature, SMB2_SIGNATURE_SIZE); |
| 8261 | } |
| 8262 | |
| 8263 | /** |
| 8264 | * smb3_check_sign_req() - handler for req packet sign processing |
| 8265 | * @work: smb work containing notify command buffer |
| 8266 | * |
| 8267 | * Return: 1 on success, 0 otherwise |
| 8268 | */ |
| 8269 | int smb3_check_sign_req(struct ksmbd_work *work) |
| 8270 | { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 8271 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8272 | char *signing_key; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8273 | struct smb2_hdr *hdr; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8274 | struct channel *chann; |
| 8275 | char signature_req[SMB2_SIGNATURE_SIZE]; |
| 8276 | char signature[SMB2_CMACAES_SIZE]; |
| 8277 | struct kvec iov[1]; |
| 8278 | size_t len; |
| 8279 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8280 | hdr = smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8281 | if (work->next_smb2_rcv_hdr_off) |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 8282 | hdr = ksmbd_req_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8283 | |
| 8284 | if (!hdr->NextCommand && !work->next_smb2_rcv_hdr_off) |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8285 | len = get_rfc1002_len(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8286 | else if (hdr->NextCommand) |
| 8287 | len = le32_to_cpu(hdr->NextCommand); |
| 8288 | else |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8289 | len = get_rfc1002_len(work->request_buf) - |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8290 | work->next_smb2_rcv_hdr_off; |
| 8291 | |
| 8292 | if (le16_to_cpu(hdr->Command) == SMB2_SESSION_SETUP_HE) { |
| 8293 | signing_key = work->sess->smb3signingkey; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8294 | } else { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 8295 | chann = lookup_chann_list(work->sess, conn); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8296 | if (!chann) |
| 8297 | return 0; |
| 8298 | signing_key = chann->smb3signingkey; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8299 | } |
| 8300 | |
| 8301 | if (!signing_key) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 8302 | pr_err("SMB3 signing key is not generated\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8303 | return 0; |
| 8304 | } |
| 8305 | |
| 8306 | memcpy(signature_req, hdr->Signature, SMB2_SIGNATURE_SIZE); |
| 8307 | memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE); |
| 8308 | iov[0].iov_base = (char *)&hdr->ProtocolId; |
| 8309 | iov[0].iov_len = len; |
| 8310 | |
| 8311 | if (ksmbd_sign_smb3_pdu(conn, signing_key, iov, 1, signature)) |
| 8312 | return 0; |
| 8313 | |
| 8314 | if (memcmp(signature, signature_req, SMB2_SIGNATURE_SIZE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 8315 | pr_err("bad smb2 signature\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8316 | return 0; |
| 8317 | } |
| 8318 | |
| 8319 | return 1; |
| 8320 | } |
| 8321 | |
| 8322 | /** |
| 8323 | * smb3_set_sign_rsp() - handler for rsp packet sign processing |
| 8324 | * @work: smb work containing notify command buffer |
| 8325 | * |
| 8326 | */ |
| 8327 | void smb3_set_sign_rsp(struct ksmbd_work *work) |
| 8328 | { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 8329 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8330 | struct smb2_hdr *req_hdr, *hdr; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8331 | struct channel *chann; |
| 8332 | char signature[SMB2_CMACAES_SIZE]; |
| 8333 | struct kvec iov[2]; |
| 8334 | int n_vec = 1; |
| 8335 | size_t len; |
| 8336 | char *signing_key; |
| 8337 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8338 | hdr = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8339 | if (work->next_smb2_rsp_hdr_off) |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 8340 | hdr = ksmbd_resp_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8341 | |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 8342 | req_hdr = ksmbd_req_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8343 | |
| 8344 | if (!work->next_smb2_rsp_hdr_off) { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8345 | len = get_rfc1002_len(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8346 | if (req_hdr->NextCommand) |
| 8347 | len = ALIGN(len, 8); |
| 8348 | } else { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8349 | len = get_rfc1002_len(work->response_buf) - |
| 8350 | work->next_smb2_rsp_hdr_off; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8351 | len = ALIGN(len, 8); |
| 8352 | } |
| 8353 | |
Namjae Jeon | 08bdbc6 | 2021-07-27 09:30:29 +0900 | [diff] [blame] | 8354 | if (conn->binding == false && |
| 8355 | le16_to_cpu(hdr->Command) == SMB2_SESSION_SETUP_HE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8356 | signing_key = work->sess->smb3signingkey; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8357 | } else { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 8358 | chann = lookup_chann_list(work->sess, work->conn); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8359 | if (!chann) |
| 8360 | return; |
| 8361 | signing_key = chann->smb3signingkey; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8362 | } |
| 8363 | |
| 8364 | if (!signing_key) |
| 8365 | return; |
| 8366 | |
| 8367 | if (req_hdr->NextCommand) |
| 8368 | hdr->NextCommand = cpu_to_le32(len); |
| 8369 | |
| 8370 | hdr->Flags |= SMB2_FLAGS_SIGNED; |
| 8371 | memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE); |
| 8372 | iov[0].iov_base = (char *)&hdr->ProtocolId; |
| 8373 | iov[0].iov_len = len; |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8374 | if (work->aux_payload_sz) { |
| 8375 | iov[0].iov_len -= work->aux_payload_sz; |
| 8376 | iov[1].iov_base = work->aux_payload_buf; |
| 8377 | iov[1].iov_len = work->aux_payload_sz; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8378 | n_vec++; |
| 8379 | } |
| 8380 | |
| 8381 | if (!ksmbd_sign_smb3_pdu(conn, signing_key, iov, n_vec, signature)) |
| 8382 | memcpy(hdr->Signature, signature, SMB2_SIGNATURE_SIZE); |
| 8383 | } |
| 8384 | |
| 8385 | /** |
| 8386 | * smb3_preauth_hash_rsp() - handler for computing preauth hash on response |
| 8387 | * @work: smb work containing response buffer |
| 8388 | * |
| 8389 | */ |
| 8390 | void smb3_preauth_hash_rsp(struct ksmbd_work *work) |
| 8391 | { |
| 8392 | struct ksmbd_conn *conn = work->conn; |
| 8393 | struct ksmbd_session *sess = work->sess; |
| 8394 | struct smb2_hdr *req, *rsp; |
| 8395 | |
| 8396 | if (conn->dialect != SMB311_PROT_ID) |
| 8397 | return; |
| 8398 | |
| 8399 | WORK_BUFFERS(work, req, rsp); |
| 8400 | |
Namjae Jeon | 442ff9e | 2021-09-29 15:44:32 +0900 | [diff] [blame] | 8401 | if (le16_to_cpu(req->Command) == SMB2_NEGOTIATE_HE && |
| 8402 | conn->preauth_info) |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8403 | ksmbd_gen_preauth_integrity_hash(conn, work->response_buf, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 8404 | conn->preauth_info->Preauth_HashValue); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8405 | |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 8406 | if (le16_to_cpu(rsp->Command) == SMB2_SESSION_SETUP_HE && sess) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8407 | __u8 *hash_value; |
| 8408 | |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 8409 | if (conn->binding) { |
| 8410 | struct preauth_session *preauth_sess; |
| 8411 | |
| 8412 | preauth_sess = ksmbd_preauth_session_lookup(conn, sess->id); |
| 8413 | if (!preauth_sess) |
| 8414 | return; |
| 8415 | hash_value = preauth_sess->Preauth_HashValue; |
| 8416 | } else { |
| 8417 | hash_value = sess->Preauth_HashValue; |
| 8418 | if (!hash_value) |
| 8419 | return; |
| 8420 | } |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8421 | ksmbd_gen_preauth_integrity_hash(conn, work->response_buf, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 8422 | hash_value); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8423 | } |
| 8424 | } |
| 8425 | |
Namjae Jeon | 2dd9129 | 2021-11-03 08:25:54 +0900 | [diff] [blame] | 8426 | 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] | 8427 | { |
Namjae Jeon | 2dd9129 | 2021-11-03 08:25:54 +0900 | [diff] [blame] | 8428 | struct smb2_transform_hdr *tr_hdr = tr_buf + 4; |
| 8429 | struct smb2_hdr *hdr = smb2_get_msg(old_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8430 | unsigned int orig_len = get_rfc1002_len(old_buf); |
| 8431 | |
Namjae Jeon | 2dd9129 | 2021-11-03 08:25:54 +0900 | [diff] [blame] | 8432 | memset(tr_buf, 0, sizeof(struct smb2_transform_hdr) + 4); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8433 | tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM; |
| 8434 | tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len); |
Ronnie Sahlberg | 4355a8f | 2021-11-03 08:43:42 +0900 | [diff] [blame] | 8435 | tr_hdr->Flags = cpu_to_le16(TRANSFORM_FLAG_ENCRYPTED); |
Namjae Jeon | 5a0ca77 | 2021-05-06 11:43:37 +0900 | [diff] [blame] | 8436 | if (cipher_type == SMB2_ENCRYPTION_AES128_GCM || |
| 8437 | cipher_type == SMB2_ENCRYPTION_AES256_GCM) |
| 8438 | get_random_bytes(&tr_hdr->Nonce, SMB3_AES_GCM_NONCE); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8439 | else |
Namjae Jeon | 5a0ca77 | 2021-05-06 11:43:37 +0900 | [diff] [blame] | 8440 | get_random_bytes(&tr_hdr->Nonce, SMB3_AES_CCM_NONCE); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8441 | memcpy(&tr_hdr->SessionId, &hdr->SessionId, 8); |
Namjae Jeon | 2dd9129 | 2021-11-03 08:25:54 +0900 | [diff] [blame] | 8442 | inc_rfc1001_len(tr_buf, sizeof(struct smb2_transform_hdr)); |
| 8443 | inc_rfc1001_len(tr_buf, orig_len); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8444 | } |
| 8445 | |
| 8446 | int smb3_encrypt_resp(struct ksmbd_work *work) |
| 8447 | { |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8448 | char *buf = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8449 | struct kvec iov[3]; |
| 8450 | int rc = -ENOMEM; |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8451 | 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] | 8452 | |
| 8453 | if (ARRAY_SIZE(iov) < rq_nvec) |
| 8454 | return -ENOMEM; |
| 8455 | |
Namjae Jeon | 2dd9129 | 2021-11-03 08:25:54 +0900 | [diff] [blame] | 8456 | work->tr_buf = kzalloc(sizeof(struct smb2_transform_hdr) + 4, GFP_KERNEL); |
| 8457 | if (!work->tr_buf) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8458 | return rc; |
| 8459 | |
| 8460 | /* fill transform header */ |
Namjae Jeon | 2dd9129 | 2021-11-03 08:25:54 +0900 | [diff] [blame] | 8461 | fill_transform_hdr(work->tr_buf, buf, work->conn->cipher_type); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8462 | |
Namjae Jeon | 2dd9129 | 2021-11-03 08:25:54 +0900 | [diff] [blame] | 8463 | iov[0].iov_base = work->tr_buf; |
| 8464 | iov[0].iov_len = sizeof(struct smb2_transform_hdr) + 4; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8465 | buf_size += iov[0].iov_len - 4; |
| 8466 | |
| 8467 | iov[1].iov_base = buf + 4; |
| 8468 | iov[1].iov_len = get_rfc1002_len(buf); |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8469 | if (work->aux_payload_sz) { |
| 8470 | iov[1].iov_len = work->resp_hdr_sz - 4; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8471 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8472 | iov[2].iov_base = work->aux_payload_buf; |
| 8473 | iov[2].iov_len = work->aux_payload_sz; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8474 | buf_size += iov[2].iov_len; |
| 8475 | } |
| 8476 | buf_size += iov[1].iov_len; |
| 8477 | work->resp_hdr_sz = iov[1].iov_len; |
| 8478 | |
| 8479 | rc = ksmbd_crypt_message(work->conn, iov, rq_nvec, 1); |
| 8480 | if (rc) |
| 8481 | return rc; |
| 8482 | |
| 8483 | memmove(buf, iov[1].iov_base, iov[1].iov_len); |
Namjae Jeon | 2dd9129 | 2021-11-03 08:25:54 +0900 | [diff] [blame] | 8484 | *(__be32 *)work->tr_buf = cpu_to_be32(buf_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8485 | |
| 8486 | return rc; |
| 8487 | } |
| 8488 | |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 8489 | bool smb3_is_transform_hdr(void *buf) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8490 | { |
Namjae Jeon | 2dd9129 | 2021-11-03 08:25:54 +0900 | [diff] [blame] | 8491 | struct smb2_transform_hdr *trhdr = smb2_get_msg(buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8492 | |
| 8493 | return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM; |
| 8494 | } |
| 8495 | |
| 8496 | int smb3_decrypt_req(struct ksmbd_work *work) |
| 8497 | { |
| 8498 | struct ksmbd_conn *conn = work->conn; |
| 8499 | struct ksmbd_session *sess; |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8500 | char *buf = work->request_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8501 | unsigned int pdu_length = get_rfc1002_len(buf); |
| 8502 | struct kvec iov[2]; |
Namjae Jeon | 2dd9129 | 2021-11-03 08:25:54 +0900 | [diff] [blame] | 8503 | int buf_data_size = pdu_length - sizeof(struct smb2_transform_hdr); |
| 8504 | struct smb2_transform_hdr *tr_hdr = smb2_get_msg(buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8505 | int rc = 0; |
| 8506 | |
Namjae Jeon | c7705ee | 2021-10-04 20:44:52 +0900 | [diff] [blame] | 8507 | if (buf_data_size < sizeof(struct smb2_hdr)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 8508 | pr_err("Transform message is too small (%u)\n", |
| 8509 | pdu_length); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8510 | return -ECONNABORTED; |
| 8511 | } |
| 8512 | |
Namjae Jeon | c7705ee | 2021-10-04 20:44:52 +0900 | [diff] [blame] | 8513 | if (buf_data_size < le32_to_cpu(tr_hdr->OriginalMessageSize)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 8514 | pr_err("Transform message is broken\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8515 | return -ECONNABORTED; |
| 8516 | } |
| 8517 | |
Namjae Jeon | 4227f81 | 2021-09-29 19:52:51 +0900 | [diff] [blame] | 8518 | sess = ksmbd_session_lookup_all(conn, le64_to_cpu(tr_hdr->SessionId)); |
| 8519 | if (!sess) { |
| 8520 | pr_err("invalid session id(%llx) in transform header\n", |
| 8521 | le64_to_cpu(tr_hdr->SessionId)); |
| 8522 | return -ECONNABORTED; |
| 8523 | } |
| 8524 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8525 | iov[0].iov_base = buf; |
Namjae Jeon | 2dd9129 | 2021-11-03 08:25:54 +0900 | [diff] [blame] | 8526 | iov[0].iov_len = sizeof(struct smb2_transform_hdr) + 4; |
| 8527 | iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr) + 4; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8528 | iov[1].iov_len = buf_data_size; |
| 8529 | rc = ksmbd_crypt_message(conn, iov, 2, 0); |
| 8530 | if (rc) |
| 8531 | return rc; |
| 8532 | |
| 8533 | memmove(buf + 4, iov[1].iov_base, buf_data_size); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8534 | *(__be32 *)buf = cpu_to_be32(buf_data_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8535 | |
| 8536 | return rc; |
| 8537 | } |
| 8538 | |
| 8539 | bool smb3_11_final_sess_setup_resp(struct ksmbd_work *work) |
| 8540 | { |
| 8541 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8542 | struct smb2_hdr *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8543 | |
| 8544 | if (conn->dialect < SMB30_PROT_ID) |
| 8545 | return false; |
| 8546 | |
| 8547 | if (work->next_smb2_rcv_hdr_off) |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 8548 | rsp = ksmbd_resp_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8549 | |
| 8550 | if (le16_to_cpu(rsp->Command) == SMB2_SESSION_SETUP_HE && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 8551 | rsp->Status == STATUS_SUCCESS) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8552 | return true; |
| 8553 | return false; |
| 8554 | } |