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 |
| 613 | * @src: source buffer |
| 614 | * @maxlen: maxlen of source string |
Yang Li | d4eeb82 | 2021-12-21 20:48:57 +0900 | [diff] [blame^] | 615 | * @local_nls: nls_table pointer |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 616 | * |
| 617 | * Return: matching converted filename on success, otherwise error ptr |
| 618 | */ |
| 619 | static char * |
Marios Makassikis | 80917f1 | 2021-12-01 21:41:19 +0100 | [diff] [blame] | 620 | smb2_get_name(const char *src, const int maxlen, struct nls_table *local_nls) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 621 | { |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 622 | char *name; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 623 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 624 | name = smb_strndup_from_utf16(src, maxlen, 1, local_nls); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 625 | if (IS_ERR(name)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 626 | pr_err("failed to get name %ld\n", PTR_ERR(name)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 627 | return name; |
| 628 | } |
| 629 | |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 630 | ksmbd_conv_path_to_unix(name); |
| 631 | ksmbd_strip_last_slash(name); |
| 632 | return name; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 633 | } |
| 634 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 635 | int setup_async_work(struct ksmbd_work *work, void (*fn)(void **), void **arg) |
| 636 | { |
| 637 | struct smb2_hdr *rsp_hdr; |
| 638 | struct ksmbd_conn *conn = work->conn; |
| 639 | int id; |
| 640 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 641 | rsp_hdr = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 642 | rsp_hdr->Flags |= SMB2_FLAGS_ASYNC_COMMAND; |
| 643 | |
Namjae Jeon | d40012a | 2021-04-13 13:06:30 +0900 | [diff] [blame] | 644 | id = ksmbd_acquire_async_msg_id(&conn->async_ida); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 645 | if (id < 0) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 646 | pr_err("Failed to alloc async message id\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 647 | return id; |
| 648 | } |
| 649 | work->syncronous = false; |
| 650 | work->async_id = id; |
| 651 | rsp_hdr->Id.AsyncId = cpu_to_le64(id); |
| 652 | |
| 653 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 654 | "Send interim Response to inform async request id : %d\n", |
| 655 | work->async_id); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 656 | |
| 657 | work->cancel_fn = fn; |
| 658 | work->cancel_argv = arg; |
| 659 | |
Namjae Jeon | 6c4e675 | 2021-06-07 09:08:45 +0900 | [diff] [blame] | 660 | if (list_empty(&work->async_request_entry)) { |
| 661 | spin_lock(&conn->request_lock); |
| 662 | list_add_tail(&work->async_request_entry, &conn->async_requests); |
| 663 | spin_unlock(&conn->request_lock); |
| 664 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 665 | |
| 666 | return 0; |
| 667 | } |
| 668 | |
| 669 | void smb2_send_interim_resp(struct ksmbd_work *work, __le32 status) |
| 670 | { |
| 671 | struct smb2_hdr *rsp_hdr; |
| 672 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 673 | rsp_hdr = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 674 | smb2_set_err_rsp(work); |
| 675 | rsp_hdr->Status = status; |
| 676 | |
| 677 | work->multiRsp = 1; |
| 678 | ksmbd_conn_write(work); |
| 679 | rsp_hdr->Status = 0; |
| 680 | work->multiRsp = 0; |
| 681 | } |
| 682 | |
| 683 | static __le32 smb2_get_reparse_tag_special_file(umode_t mode) |
| 684 | { |
| 685 | if (S_ISDIR(mode) || S_ISREG(mode)) |
| 686 | return 0; |
| 687 | |
| 688 | if (S_ISLNK(mode)) |
| 689 | return IO_REPARSE_TAG_LX_SYMLINK_LE; |
| 690 | else if (S_ISFIFO(mode)) |
| 691 | return IO_REPARSE_TAG_LX_FIFO_LE; |
| 692 | else if (S_ISSOCK(mode)) |
| 693 | return IO_REPARSE_TAG_AF_UNIX_LE; |
| 694 | else if (S_ISCHR(mode)) |
| 695 | return IO_REPARSE_TAG_LX_CHR_LE; |
| 696 | else if (S_ISBLK(mode)) |
| 697 | return IO_REPARSE_TAG_LX_BLK_LE; |
| 698 | |
| 699 | return 0; |
| 700 | } |
| 701 | |
| 702 | /** |
| 703 | * smb2_get_dos_mode() - get file mode in dos format from unix mode |
| 704 | * @stat: kstat containing file mode |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 705 | * @attribute: attribute flags |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 706 | * |
| 707 | * Return: converted dos mode |
| 708 | */ |
| 709 | static int smb2_get_dos_mode(struct kstat *stat, int attribute) |
| 710 | { |
| 711 | int attr = 0; |
| 712 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 713 | if (S_ISDIR(stat->mode)) { |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 714 | attr = FILE_ATTRIBUTE_DIRECTORY | |
| 715 | (attribute & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 716 | } else { |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 717 | attr = (attribute & 0x00005137) | FILE_ATTRIBUTE_ARCHIVE; |
| 718 | attr &= ~(FILE_ATTRIBUTE_DIRECTORY); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 719 | if (S_ISREG(stat->mode) && (server_conf.share_fake_fscaps & |
| 720 | FILE_SUPPORTS_SPARSE_FILES)) |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 721 | attr |= FILE_ATTRIBUTE_SPARSE_FILE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 722 | |
| 723 | if (smb2_get_reparse_tag_special_file(stat->mode)) |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 724 | attr |= FILE_ATTRIBUTE_REPARSE_POINT; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | return attr; |
| 728 | } |
| 729 | |
| 730 | static void build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 731 | __le16 hash_id) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 732 | { |
| 733 | pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES; |
| 734 | pneg_ctxt->DataLength = cpu_to_le16(38); |
| 735 | pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1); |
| 736 | pneg_ctxt->Reserved = cpu_to_le32(0); |
| 737 | pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE); |
| 738 | get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE); |
| 739 | pneg_ctxt->HashAlgorithms = hash_id; |
| 740 | } |
| 741 | |
| 742 | static void build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 743 | __le16 cipher_type) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 744 | { |
| 745 | pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES; |
| 746 | pneg_ctxt->DataLength = cpu_to_le16(4); |
| 747 | pneg_ctxt->Reserved = cpu_to_le32(0); |
| 748 | pneg_ctxt->CipherCount = cpu_to_le16(1); |
| 749 | pneg_ctxt->Ciphers[0] = cipher_type; |
| 750 | } |
| 751 | |
Ronnie Sahlberg | d6c9ad23b | 2021-11-03 08:44:38 +0900 | [diff] [blame] | 752 | static void build_compression_ctxt(struct smb2_compression_capabilities_context *pneg_ctxt, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 753 | __le16 comp_algo) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 754 | { |
| 755 | pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES; |
| 756 | pneg_ctxt->DataLength = |
Ronnie Sahlberg | d6c9ad23b | 2021-11-03 08:44:38 +0900 | [diff] [blame] | 757 | cpu_to_le16(sizeof(struct smb2_compression_capabilities_context) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 758 | - sizeof(struct smb2_neg_context)); |
| 759 | pneg_ctxt->Reserved = cpu_to_le32(0); |
| 760 | pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(1); |
Ronnie Sahlberg | d6c9ad23b | 2021-11-03 08:44:38 +0900 | [diff] [blame] | 761 | pneg_ctxt->Flags = cpu_to_le32(0); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 762 | pneg_ctxt->CompressionAlgorithms[0] = comp_algo; |
| 763 | } |
| 764 | |
Namjae Jeon | 378087c | 2021-07-21 10:05:53 +0900 | [diff] [blame] | 765 | static void build_sign_cap_ctxt(struct smb2_signing_capabilities *pneg_ctxt, |
| 766 | __le16 sign_algo) |
| 767 | { |
| 768 | pneg_ctxt->ContextType = SMB2_SIGNING_CAPABILITIES; |
| 769 | pneg_ctxt->DataLength = |
| 770 | cpu_to_le16((sizeof(struct smb2_signing_capabilities) + 2) |
| 771 | - sizeof(struct smb2_neg_context)); |
| 772 | pneg_ctxt->Reserved = cpu_to_le32(0); |
| 773 | pneg_ctxt->SigningAlgorithmCount = cpu_to_le16(1); |
| 774 | pneg_ctxt->SigningAlgorithms[0] = sign_algo; |
| 775 | } |
| 776 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 777 | static void build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 778 | { |
| 779 | pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE; |
| 780 | pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN); |
| 781 | /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */ |
| 782 | pneg_ctxt->Name[0] = 0x93; |
| 783 | pneg_ctxt->Name[1] = 0xAD; |
| 784 | pneg_ctxt->Name[2] = 0x25; |
| 785 | pneg_ctxt->Name[3] = 0x50; |
| 786 | pneg_ctxt->Name[4] = 0x9C; |
| 787 | pneg_ctxt->Name[5] = 0xB4; |
| 788 | pneg_ctxt->Name[6] = 0x11; |
| 789 | pneg_ctxt->Name[7] = 0xE7; |
| 790 | pneg_ctxt->Name[8] = 0xB4; |
| 791 | pneg_ctxt->Name[9] = 0x23; |
| 792 | pneg_ctxt->Name[10] = 0x83; |
| 793 | pneg_ctxt->Name[11] = 0xDE; |
| 794 | pneg_ctxt->Name[12] = 0x96; |
| 795 | pneg_ctxt->Name[13] = 0x8B; |
| 796 | pneg_ctxt->Name[14] = 0xCD; |
| 797 | pneg_ctxt->Name[15] = 0x7C; |
| 798 | } |
| 799 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 800 | static void assemble_neg_contexts(struct ksmbd_conn *conn, |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 801 | struct smb2_negotiate_rsp *rsp, |
| 802 | void *smb2_buf_len) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 803 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 804 | char *pneg_ctxt = (char *)rsp + |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 805 | le32_to_cpu(rsp->NegotiateContextOffset); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 806 | int neg_ctxt_cnt = 1; |
| 807 | int ctxt_size; |
| 808 | |
| 809 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 810 | "assemble SMB2_PREAUTH_INTEGRITY_CAPABILITIES context\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 811 | build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 812 | conn->preauth_info->Preauth_HashId); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 813 | rsp->NegotiateContextCount = cpu_to_le16(neg_ctxt_cnt); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 814 | inc_rfc1001_len(smb2_buf_len, AUTH_GSS_PADDING); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 815 | ctxt_size = sizeof(struct smb2_preauth_neg_context); |
| 816 | /* Round to 8 byte boundary */ |
| 817 | pneg_ctxt += round_up(sizeof(struct smb2_preauth_neg_context), 8); |
| 818 | |
| 819 | if (conn->cipher_type) { |
| 820 | ctxt_size = round_up(ctxt_size, 8); |
| 821 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 822 | "assemble SMB2_ENCRYPTION_CAPABILITIES context\n"); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 823 | build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 824 | conn->cipher_type); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 825 | rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt); |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 826 | ctxt_size += sizeof(struct smb2_encryption_neg_context) + 2; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 827 | /* Round to 8 byte boundary */ |
| 828 | pneg_ctxt += |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 829 | round_up(sizeof(struct smb2_encryption_neg_context) + 2, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 830 | 8); |
| 831 | } |
| 832 | |
| 833 | if (conn->compress_algorithm) { |
| 834 | ctxt_size = round_up(ctxt_size, 8); |
| 835 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 836 | "assemble SMB2_COMPRESSION_CAPABILITIES context\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 837 | /* Temporarily set to SMB3_COMPRESS_NONE */ |
Ronnie Sahlberg | d6c9ad23b | 2021-11-03 08:44:38 +0900 | [diff] [blame] | 838 | build_compression_ctxt((struct smb2_compression_capabilities_context *)pneg_ctxt, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 839 | conn->compress_algorithm); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 840 | rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt); |
Ronnie Sahlberg | d6c9ad23b | 2021-11-03 08:44:38 +0900 | [diff] [blame] | 841 | ctxt_size += sizeof(struct smb2_compression_capabilities_context) + 2; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 842 | /* Round to 8 byte boundary */ |
Ronnie Sahlberg | d6c9ad23b | 2021-11-03 08:44:38 +0900 | [diff] [blame] | 843 | pneg_ctxt += round_up(sizeof(struct smb2_compression_capabilities_context) + 2, |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 844 | 8); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | if (conn->posix_ext_supported) { |
| 848 | ctxt_size = round_up(ctxt_size, 8); |
| 849 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 850 | "assemble SMB2_POSIX_EXTENSIONS_AVAILABLE context\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 851 | build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt); |
| 852 | rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt); |
| 853 | ctxt_size += sizeof(struct smb2_posix_neg_context); |
Namjae Jeon | 378087c | 2021-07-21 10:05:53 +0900 | [diff] [blame] | 854 | /* Round to 8 byte boundary */ |
| 855 | pneg_ctxt += round_up(sizeof(struct smb2_posix_neg_context), 8); |
| 856 | } |
| 857 | |
| 858 | if (conn->signing_negotiated) { |
| 859 | ctxt_size = round_up(ctxt_size, 8); |
| 860 | ksmbd_debug(SMB, |
| 861 | "assemble SMB2_SIGNING_CAPABILITIES context\n"); |
| 862 | build_sign_cap_ctxt((struct smb2_signing_capabilities *)pneg_ctxt, |
| 863 | conn->signing_algorithm); |
| 864 | rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt); |
| 865 | ctxt_size += sizeof(struct smb2_signing_capabilities) + 2; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 866 | } |
| 867 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 868 | inc_rfc1001_len(smb2_buf_len, ctxt_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 869 | } |
| 870 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 871 | static __le32 decode_preauth_ctxt(struct ksmbd_conn *conn, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 872 | struct smb2_preauth_neg_context *pneg_ctxt) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 873 | { |
| 874 | __le32 err = STATUS_NO_PREAUTH_INTEGRITY_HASH_OVERLAP; |
| 875 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 876 | if (pneg_ctxt->HashAlgorithms == SMB2_PREAUTH_INTEGRITY_SHA512) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 877 | conn->preauth_info->Preauth_HashId = |
| 878 | SMB2_PREAUTH_INTEGRITY_SHA512; |
| 879 | err = STATUS_SUCCESS; |
| 880 | } |
| 881 | |
| 882 | return err; |
| 883 | } |
| 884 | |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 885 | static void decode_encrypt_ctxt(struct ksmbd_conn *conn, |
| 886 | struct smb2_encryption_neg_context *pneg_ctxt, |
| 887 | int len_of_ctxts) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 888 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 889 | int cph_cnt = le16_to_cpu(pneg_ctxt->CipherCount); |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 890 | int i, cphs_size = cph_cnt * sizeof(__le16); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 891 | |
| 892 | conn->cipher_type = 0; |
| 893 | |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 894 | if (sizeof(struct smb2_encryption_neg_context) + cphs_size > |
| 895 | len_of_ctxts) { |
| 896 | pr_err("Invalid cipher count(%d)\n", cph_cnt); |
| 897 | return; |
| 898 | } |
| 899 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 900 | if (!(server_conf.flags & KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION)) |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 901 | return; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 902 | |
| 903 | for (i = 0; i < cph_cnt; i++) { |
| 904 | if (pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES128_GCM || |
Namjae Jeon | 5a0ca77 | 2021-05-06 11:43:37 +0900 | [diff] [blame] | 905 | pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES128_CCM || |
| 906 | pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES256_CCM || |
| 907 | pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES256_GCM) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 908 | ksmbd_debug(SMB, "Cipher ID = 0x%x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 909 | pneg_ctxt->Ciphers[i]); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 910 | conn->cipher_type = pneg_ctxt->Ciphers[i]; |
| 911 | break; |
| 912 | } |
| 913 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 914 | } |
| 915 | |
Marcos Del Sol Vives | 83912d6 | 2021-12-16 11:37:22 +0100 | [diff] [blame] | 916 | /** |
| 917 | * smb3_encryption_negotiated() - checks if server and client agreed on enabling encryption |
| 918 | * @conn: smb connection |
| 919 | * |
| 920 | * Return: true if connection should be encrypted, else false |
| 921 | */ |
| 922 | static bool smb3_encryption_negotiated(struct ksmbd_conn *conn) |
| 923 | { |
| 924 | if (!conn->ops->generate_encryptionkey) |
| 925 | return false; |
| 926 | |
| 927 | /* |
| 928 | * SMB 3.0 and 3.0.2 dialects use the SMB2_GLOBAL_CAP_ENCRYPTION flag. |
| 929 | * SMB 3.1.1 uses the cipher_type field. |
| 930 | */ |
| 931 | return (conn->vals->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION) || |
| 932 | conn->cipher_type; |
| 933 | } |
| 934 | |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 935 | static void decode_compress_ctxt(struct ksmbd_conn *conn, |
Ronnie Sahlberg | d6c9ad23b | 2021-11-03 08:44:38 +0900 | [diff] [blame] | 936 | struct smb2_compression_capabilities_context *pneg_ctxt) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 937 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 938 | conn->compress_algorithm = SMB3_COMPRESS_NONE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 939 | } |
| 940 | |
Namjae Jeon | 378087c | 2021-07-21 10:05:53 +0900 | [diff] [blame] | 941 | static void decode_sign_cap_ctxt(struct ksmbd_conn *conn, |
| 942 | struct smb2_signing_capabilities *pneg_ctxt, |
| 943 | int len_of_ctxts) |
| 944 | { |
| 945 | int sign_algo_cnt = le16_to_cpu(pneg_ctxt->SigningAlgorithmCount); |
| 946 | int i, sign_alos_size = sign_algo_cnt * sizeof(__le16); |
| 947 | |
| 948 | conn->signing_negotiated = false; |
| 949 | |
| 950 | if (sizeof(struct smb2_signing_capabilities) + sign_alos_size > |
| 951 | len_of_ctxts) { |
| 952 | pr_err("Invalid signing algorithm count(%d)\n", sign_algo_cnt); |
| 953 | return; |
| 954 | } |
| 955 | |
| 956 | for (i = 0; i < sign_algo_cnt; i++) { |
Ronnie Sahlberg | d6c9ad23b | 2021-11-03 08:44:38 +0900 | [diff] [blame] | 957 | if (pneg_ctxt->SigningAlgorithms[i] == SIGNING_ALG_HMAC_SHA256_LE || |
| 958 | pneg_ctxt->SigningAlgorithms[i] == SIGNING_ALG_AES_CMAC_LE) { |
Namjae Jeon | 378087c | 2021-07-21 10:05:53 +0900 | [diff] [blame] | 959 | ksmbd_debug(SMB, "Signing Algorithm ID = 0x%x\n", |
| 960 | pneg_ctxt->SigningAlgorithms[i]); |
| 961 | conn->signing_negotiated = true; |
| 962 | conn->signing_algorithm = |
| 963 | pneg_ctxt->SigningAlgorithms[i]; |
| 964 | break; |
| 965 | } |
| 966 | } |
| 967 | } |
| 968 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 969 | static __le32 deassemble_neg_contexts(struct ksmbd_conn *conn, |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 970 | struct smb2_negotiate_req *req, |
| 971 | int len_of_smb) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 972 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 973 | /* +4 is to account for the RFC1001 len field */ |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 974 | struct smb2_neg_context *pctx = (struct smb2_neg_context *)req; |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 975 | int i = 0, len_of_ctxts; |
| 976 | int offset = le32_to_cpu(req->NegotiateContextOffset); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 977 | int neg_ctxt_cnt = le16_to_cpu(req->NegotiateContextCount); |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 978 | __le32 status = STATUS_INVALID_PARAMETER; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 979 | |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 980 | ksmbd_debug(SMB, "decoding %d negotiate contexts\n", neg_ctxt_cnt); |
| 981 | if (len_of_smb <= offset) { |
| 982 | ksmbd_debug(SMB, "Invalid response: negotiate context offset\n"); |
| 983 | return status; |
| 984 | } |
| 985 | |
| 986 | len_of_ctxts = len_of_smb - offset; |
| 987 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 988 | while (i++ < neg_ctxt_cnt) { |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 989 | int clen; |
| 990 | |
| 991 | /* check that offset is not beyond end of SMB */ |
| 992 | if (len_of_ctxts == 0) |
| 993 | break; |
| 994 | |
| 995 | if (len_of_ctxts < sizeof(struct smb2_neg_context)) |
| 996 | break; |
| 997 | |
| 998 | pctx = (struct smb2_neg_context *)((char *)pctx + offset); |
| 999 | clen = le16_to_cpu(pctx->DataLength); |
| 1000 | if (clen + sizeof(struct smb2_neg_context) > len_of_ctxts) |
| 1001 | break; |
| 1002 | |
| 1003 | if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1004 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1005 | "deassemble SMB2_PREAUTH_INTEGRITY_CAPABILITIES context\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1006 | if (conn->preauth_info->Preauth_HashId) |
| 1007 | break; |
| 1008 | |
| 1009 | status = decode_preauth_ctxt(conn, |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 1010 | (struct smb2_preauth_neg_context *)pctx); |
| 1011 | if (status != STATUS_SUCCESS) |
| 1012 | break; |
| 1013 | } else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES) { |
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_ENCRYPTION_CAPABILITIES context\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1016 | if (conn->cipher_type) |
| 1017 | break; |
| 1018 | |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 1019 | decode_encrypt_ctxt(conn, |
| 1020 | (struct smb2_encryption_neg_context *)pctx, |
| 1021 | len_of_ctxts); |
| 1022 | } else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1023 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1024 | "deassemble SMB2_COMPRESSION_CAPABILITIES context\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1025 | if (conn->compress_algorithm) |
| 1026 | break; |
| 1027 | |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 1028 | decode_compress_ctxt(conn, |
Ronnie Sahlberg | d6c9ad23b | 2021-11-03 08:44:38 +0900 | [diff] [blame] | 1029 | (struct smb2_compression_capabilities_context *)pctx); |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 1030 | } else if (pctx->ContextType == SMB2_NETNAME_NEGOTIATE_CONTEXT_ID) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1031 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1032 | "deassemble SMB2_NETNAME_NEGOTIATE_CONTEXT_ID context\n"); |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 1033 | } else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1034 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1035 | "deassemble SMB2_POSIX_EXTENSIONS_AVAILABLE context\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1036 | conn->posix_ext_supported = true; |
Namjae Jeon | 378087c | 2021-07-21 10:05:53 +0900 | [diff] [blame] | 1037 | } else if (pctx->ContextType == SMB2_SIGNING_CAPABILITIES) { |
| 1038 | ksmbd_debug(SMB, |
| 1039 | "deassemble SMB2_SIGNING_CAPABILITIES context\n"); |
| 1040 | decode_sign_cap_ctxt(conn, |
| 1041 | (struct smb2_signing_capabilities *)pctx, |
| 1042 | len_of_ctxts); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1043 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1044 | |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 1045 | /* offsets must be 8 byte aligned */ |
| 1046 | clen = (clen + 7) & ~0x7; |
| 1047 | offset = clen + sizeof(struct smb2_neg_context); |
| 1048 | len_of_ctxts -= clen + sizeof(struct smb2_neg_context); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1049 | } |
| 1050 | return status; |
| 1051 | } |
| 1052 | |
| 1053 | /** |
| 1054 | * smb2_handle_negotiate() - handler for smb2 negotiate command |
| 1055 | * @work: smb work containing smb request buffer |
| 1056 | * |
| 1057 | * Return: 0 |
| 1058 | */ |
| 1059 | int smb2_handle_negotiate(struct ksmbd_work *work) |
| 1060 | { |
| 1061 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1062 | struct smb2_negotiate_req *req = smb2_get_msg(work->request_buf); |
| 1063 | struct smb2_negotiate_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1064 | int rc = 0; |
Namjae Jeon | 442ff9e | 2021-09-29 15:44:32 +0900 | [diff] [blame] | 1065 | unsigned int smb2_buf_len, smb2_neg_size; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1066 | __le32 status; |
| 1067 | |
| 1068 | ksmbd_debug(SMB, "Received negotiate request\n"); |
| 1069 | conn->need_neg = false; |
| 1070 | if (ksmbd_conn_good(work)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 1071 | pr_err("conn->tcp_status is already in CifsGood State\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1072 | work->send_no_response = 1; |
| 1073 | return rc; |
| 1074 | } |
| 1075 | |
| 1076 | if (req->DialectCount == 0) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 1077 | pr_err("malformed packet\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1078 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 1079 | rc = -EINVAL; |
| 1080 | goto err_out; |
| 1081 | } |
| 1082 | |
Namjae Jeon | 442ff9e | 2021-09-29 15:44:32 +0900 | [diff] [blame] | 1083 | smb2_buf_len = get_rfc1002_len(work->request_buf); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1084 | smb2_neg_size = offsetof(struct smb2_negotiate_req, Dialects); |
Namjae Jeon | 442ff9e | 2021-09-29 15:44:32 +0900 | [diff] [blame] | 1085 | if (smb2_neg_size > smb2_buf_len) { |
| 1086 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 1087 | rc = -EINVAL; |
| 1088 | goto err_out; |
| 1089 | } |
| 1090 | |
| 1091 | if (conn->dialect == SMB311_PROT_ID) { |
| 1092 | unsigned int nego_ctxt_off = le32_to_cpu(req->NegotiateContextOffset); |
| 1093 | |
| 1094 | if (smb2_buf_len < nego_ctxt_off) { |
| 1095 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 1096 | rc = -EINVAL; |
| 1097 | goto err_out; |
| 1098 | } |
| 1099 | |
| 1100 | if (smb2_neg_size > nego_ctxt_off) { |
| 1101 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 1102 | rc = -EINVAL; |
| 1103 | goto err_out; |
| 1104 | } |
| 1105 | |
| 1106 | if (smb2_neg_size + le16_to_cpu(req->DialectCount) * sizeof(__le16) > |
| 1107 | nego_ctxt_off) { |
| 1108 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 1109 | rc = -EINVAL; |
| 1110 | goto err_out; |
| 1111 | } |
| 1112 | } else { |
| 1113 | if (smb2_neg_size + le16_to_cpu(req->DialectCount) * sizeof(__le16) > |
| 1114 | smb2_buf_len) { |
| 1115 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 1116 | rc = -EINVAL; |
| 1117 | goto err_out; |
| 1118 | } |
| 1119 | } |
| 1120 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1121 | conn->cli_cap = le32_to_cpu(req->Capabilities); |
| 1122 | switch (conn->dialect) { |
| 1123 | case SMB311_PROT_ID: |
| 1124 | conn->preauth_info = |
| 1125 | kzalloc(sizeof(struct preauth_integrity_info), |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1126 | GFP_KERNEL); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1127 | if (!conn->preauth_info) { |
| 1128 | rc = -ENOMEM; |
| 1129 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 1130 | goto err_out; |
| 1131 | } |
| 1132 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1133 | status = deassemble_neg_contexts(conn, req, |
| 1134 | get_rfc1002_len(work->request_buf)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1135 | if (status != STATUS_SUCCESS) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 1136 | pr_err("deassemble_neg_contexts error(0x%x)\n", |
| 1137 | status); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1138 | rsp->hdr.Status = status; |
| 1139 | rc = -EINVAL; |
| 1140 | goto err_out; |
| 1141 | } |
| 1142 | |
| 1143 | rc = init_smb3_11_server(conn); |
| 1144 | if (rc < 0) { |
| 1145 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 1146 | goto err_out; |
| 1147 | } |
| 1148 | |
| 1149 | ksmbd_gen_preauth_integrity_hash(conn, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1150 | work->request_buf, |
| 1151 | conn->preauth_info->Preauth_HashValue); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1152 | rsp->NegotiateContextOffset = |
| 1153 | cpu_to_le32(OFFSET_OF_NEG_CONTEXT); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1154 | assemble_neg_contexts(conn, rsp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1155 | break; |
| 1156 | case SMB302_PROT_ID: |
| 1157 | init_smb3_02_server(conn); |
| 1158 | break; |
| 1159 | case SMB30_PROT_ID: |
| 1160 | init_smb3_0_server(conn); |
| 1161 | break; |
| 1162 | case SMB21_PROT_ID: |
| 1163 | init_smb2_1_server(conn); |
| 1164 | break; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1165 | case SMB2X_PROT_ID: |
| 1166 | case BAD_PROT_ID: |
| 1167 | default: |
| 1168 | ksmbd_debug(SMB, "Server dialect :0x%x not supported\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1169 | conn->dialect); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1170 | rsp->hdr.Status = STATUS_NOT_SUPPORTED; |
| 1171 | rc = -EINVAL; |
| 1172 | goto err_out; |
| 1173 | } |
| 1174 | rsp->Capabilities = cpu_to_le32(conn->vals->capabilities); |
| 1175 | |
| 1176 | /* For stats */ |
| 1177 | conn->connection_type = conn->dialect; |
| 1178 | |
| 1179 | rsp->MaxTransactSize = cpu_to_le32(conn->vals->max_trans_size); |
| 1180 | rsp->MaxReadSize = cpu_to_le32(conn->vals->max_read_size); |
| 1181 | rsp->MaxWriteSize = cpu_to_le32(conn->vals->max_write_size); |
| 1182 | |
Namjae Jeon | 51a1387 | 2021-09-29 13:09:24 +0900 | [diff] [blame] | 1183 | memcpy(conn->ClientGUID, req->ClientGUID, |
| 1184 | SMB2_CLIENT_GUID_SIZE); |
| 1185 | conn->cli_sec_mode = le16_to_cpu(req->SecurityMode); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1186 | |
| 1187 | rsp->StructureSize = cpu_to_le16(65); |
| 1188 | rsp->DialectRevision = cpu_to_le16(conn->dialect); |
| 1189 | /* Not setting conn guid rsp->ServerGUID, as it |
| 1190 | * not used by client for identifying server |
| 1191 | */ |
| 1192 | memset(rsp->ServerGUID, 0, SMB2_CLIENT_GUID_SIZE); |
| 1193 | |
| 1194 | rsp->SystemTime = cpu_to_le64(ksmbd_systime()); |
| 1195 | rsp->ServerStartTime = 0; |
| 1196 | ksmbd_debug(SMB, "negotiate context offset %d, count %d\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1197 | le32_to_cpu(rsp->NegotiateContextOffset), |
| 1198 | le16_to_cpu(rsp->NegotiateContextCount)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1199 | |
| 1200 | rsp->SecurityBufferOffset = cpu_to_le16(128); |
| 1201 | rsp->SecurityBufferLength = cpu_to_le16(AUTH_GSS_LENGTH); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1202 | ksmbd_copy_gss_neg_header((char *)(&rsp->hdr) + |
| 1203 | le16_to_cpu(rsp->SecurityBufferOffset)); |
| 1204 | inc_rfc1001_len(work->response_buf, sizeof(struct smb2_negotiate_rsp) - |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1205 | sizeof(struct smb2_hdr) - sizeof(rsp->Buffer) + |
| 1206 | AUTH_GSS_LENGTH); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1207 | rsp->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED_LE; |
| 1208 | conn->use_spnego = true; |
| 1209 | |
| 1210 | if ((server_conf.signing == KSMBD_CONFIG_OPT_AUTO || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1211 | server_conf.signing == KSMBD_CONFIG_OPT_DISABLED) && |
| 1212 | req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED_LE) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1213 | conn->sign = true; |
| 1214 | else if (server_conf.signing == KSMBD_CONFIG_OPT_MANDATORY) { |
| 1215 | server_conf.enforced_signing = true; |
| 1216 | rsp->SecurityMode |= SMB2_NEGOTIATE_SIGNING_REQUIRED_LE; |
| 1217 | conn->sign = true; |
| 1218 | } |
| 1219 | |
| 1220 | conn->srv_sec_mode = le16_to_cpu(rsp->SecurityMode); |
| 1221 | ksmbd_conn_set_need_negotiate(work); |
| 1222 | |
| 1223 | err_out: |
| 1224 | if (rc < 0) |
| 1225 | smb2_set_err_rsp(work); |
| 1226 | |
| 1227 | return rc; |
| 1228 | } |
| 1229 | |
| 1230 | static int alloc_preauth_hash(struct ksmbd_session *sess, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1231 | struct ksmbd_conn *conn) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1232 | { |
| 1233 | if (sess->Preauth_HashValue) |
| 1234 | return 0; |
| 1235 | |
kernel test robot | 86f5297 | 2021-04-02 12:17:24 +0900 | [diff] [blame] | 1236 | sess->Preauth_HashValue = kmemdup(conn->preauth_info->Preauth_HashValue, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1237 | PREAUTH_HASHVALUE_SIZE, GFP_KERNEL); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1238 | if (!sess->Preauth_HashValue) |
| 1239 | return -ENOMEM; |
| 1240 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1241 | return 0; |
| 1242 | } |
| 1243 | |
| 1244 | static int generate_preauth_hash(struct ksmbd_work *work) |
| 1245 | { |
| 1246 | struct ksmbd_conn *conn = work->conn; |
| 1247 | struct ksmbd_session *sess = work->sess; |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1248 | u8 *preauth_hash; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1249 | |
| 1250 | if (conn->dialect != SMB311_PROT_ID) |
| 1251 | return 0; |
| 1252 | |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1253 | if (conn->binding) { |
| 1254 | struct preauth_session *preauth_sess; |
| 1255 | |
| 1256 | preauth_sess = ksmbd_preauth_session_lookup(conn, sess->id); |
| 1257 | if (!preauth_sess) { |
| 1258 | preauth_sess = ksmbd_preauth_session_alloc(conn, sess->id); |
| 1259 | if (!preauth_sess) |
| 1260 | return -ENOMEM; |
| 1261 | } |
| 1262 | |
| 1263 | preauth_hash = preauth_sess->Preauth_HashValue; |
| 1264 | } else { |
| 1265 | if (!sess->Preauth_HashValue) |
| 1266 | if (alloc_preauth_hash(sess, conn)) |
| 1267 | return -ENOMEM; |
| 1268 | preauth_hash = sess->Preauth_HashValue; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1269 | } |
| 1270 | |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1271 | ksmbd_gen_preauth_integrity_hash(conn, work->request_buf, preauth_hash); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1272 | return 0; |
| 1273 | } |
| 1274 | |
Marios Makassikis | 0d994cd | 2021-10-19 17:39:38 +0200 | [diff] [blame] | 1275 | static int decode_negotiation_token(struct ksmbd_conn *conn, |
| 1276 | struct negotiate_message *negblob, |
| 1277 | size_t sz) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1278 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1279 | if (!conn->use_spnego) |
| 1280 | return -EINVAL; |
| 1281 | |
Hyunchul Lee | fad4161 | 2021-04-19 17:26:15 +0900 | [diff] [blame] | 1282 | if (ksmbd_decode_negTokenInit((char *)negblob, sz, conn)) { |
| 1283 | if (ksmbd_decode_negTokenTarg((char *)negblob, sz, conn)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1284 | conn->auth_mechs |= KSMBD_AUTH_NTLMSSP; |
| 1285 | conn->preferred_auth_mech = KSMBD_AUTH_NTLMSSP; |
| 1286 | conn->use_spnego = false; |
| 1287 | } |
| 1288 | } |
| 1289 | return 0; |
| 1290 | } |
| 1291 | |
| 1292 | static int ntlm_negotiate(struct ksmbd_work *work, |
Marios Makassikis | 0d994cd | 2021-10-19 17:39:38 +0200 | [diff] [blame] | 1293 | struct negotiate_message *negblob, |
| 1294 | size_t negblob_len) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1295 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1296 | struct smb2_sess_setup_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1297 | struct challenge_message *chgblob; |
| 1298 | unsigned char *spnego_blob = NULL; |
| 1299 | u16 spnego_blob_len; |
| 1300 | char *neg_blob; |
| 1301 | int sz, rc; |
| 1302 | |
| 1303 | ksmbd_debug(SMB, "negotiate phase\n"); |
Namjae Jeon | ce53d36 | 2021-12-15 14:57:27 +0900 | [diff] [blame] | 1304 | rc = ksmbd_decode_ntlmssp_neg_blob(negblob, negblob_len, work->conn); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1305 | if (rc) |
| 1306 | return rc; |
| 1307 | |
| 1308 | sz = le16_to_cpu(rsp->SecurityBufferOffset); |
| 1309 | chgblob = |
| 1310 | (struct challenge_message *)((char *)&rsp->hdr.ProtocolId + sz); |
| 1311 | memset(chgblob, 0, sizeof(struct challenge_message)); |
| 1312 | |
| 1313 | if (!work->conn->use_spnego) { |
Namjae Jeon | ce53d36 | 2021-12-15 14:57:27 +0900 | [diff] [blame] | 1314 | sz = ksmbd_build_ntlmssp_challenge_blob(chgblob, work->conn); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1315 | if (sz < 0) |
| 1316 | return -ENOMEM; |
| 1317 | |
| 1318 | rsp->SecurityBufferLength = cpu_to_le16(sz); |
| 1319 | return 0; |
| 1320 | } |
| 1321 | |
| 1322 | sz = sizeof(struct challenge_message); |
| 1323 | sz += (strlen(ksmbd_netbios_name()) * 2 + 1 + 4) * 6; |
| 1324 | |
| 1325 | neg_blob = kzalloc(sz, GFP_KERNEL); |
| 1326 | if (!neg_blob) |
| 1327 | return -ENOMEM; |
| 1328 | |
| 1329 | chgblob = (struct challenge_message *)neg_blob; |
Namjae Jeon | ce53d36 | 2021-12-15 14:57:27 +0900 | [diff] [blame] | 1330 | sz = ksmbd_build_ntlmssp_challenge_blob(chgblob, work->conn); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1331 | if (sz < 0) { |
| 1332 | rc = -ENOMEM; |
| 1333 | goto out; |
| 1334 | } |
| 1335 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1336 | rc = build_spnego_ntlmssp_neg_blob(&spnego_blob, &spnego_blob_len, |
| 1337 | neg_blob, sz); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1338 | if (rc) { |
| 1339 | rc = -ENOMEM; |
| 1340 | goto out; |
| 1341 | } |
| 1342 | |
| 1343 | sz = le16_to_cpu(rsp->SecurityBufferOffset); |
| 1344 | memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len); |
| 1345 | rsp->SecurityBufferLength = cpu_to_le16(spnego_blob_len); |
| 1346 | |
| 1347 | out: |
| 1348 | kfree(spnego_blob); |
| 1349 | kfree(neg_blob); |
| 1350 | return rc; |
| 1351 | } |
| 1352 | |
| 1353 | static struct authenticate_message *user_authblob(struct ksmbd_conn *conn, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1354 | struct smb2_sess_setup_req *req) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1355 | { |
| 1356 | int sz; |
| 1357 | |
| 1358 | if (conn->use_spnego && conn->mechToken) |
| 1359 | return (struct authenticate_message *)conn->mechToken; |
| 1360 | |
| 1361 | sz = le16_to_cpu(req->SecurityBufferOffset); |
| 1362 | return (struct authenticate_message *)((char *)&req->hdr.ProtocolId |
| 1363 | + sz); |
| 1364 | } |
| 1365 | |
| 1366 | static struct ksmbd_user *session_user(struct ksmbd_conn *conn, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1367 | struct smb2_sess_setup_req *req) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1368 | { |
| 1369 | struct authenticate_message *authblob; |
| 1370 | struct ksmbd_user *user; |
| 1371 | char *name; |
Marios Makassikis | 0d994cd | 2021-10-19 17:39:38 +0200 | [diff] [blame] | 1372 | unsigned int auth_msg_len, name_off, name_len, secbuf_len; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1373 | |
Marios Makassikis | 0d994cd | 2021-10-19 17:39:38 +0200 | [diff] [blame] | 1374 | secbuf_len = le16_to_cpu(req->SecurityBufferLength); |
| 1375 | if (secbuf_len < sizeof(struct authenticate_message)) { |
| 1376 | ksmbd_debug(SMB, "blob len %d too small\n", secbuf_len); |
| 1377 | return NULL; |
| 1378 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1379 | authblob = user_authblob(conn, req); |
Marios Makassikis | 0d994cd | 2021-10-19 17:39:38 +0200 | [diff] [blame] | 1380 | name_off = le32_to_cpu(authblob->UserName.BufferOffset); |
| 1381 | name_len = le16_to_cpu(authblob->UserName.Length); |
| 1382 | auth_msg_len = le16_to_cpu(req->SecurityBufferOffset) + secbuf_len; |
| 1383 | |
| 1384 | if (auth_msg_len < (u64)name_off + name_len) |
| 1385 | return NULL; |
| 1386 | |
| 1387 | name = smb_strndup_from_utf16((const char *)authblob + name_off, |
| 1388 | name_len, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1389 | true, |
| 1390 | conn->local_nls); |
| 1391 | if (IS_ERR(name)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 1392 | pr_err("cannot allocate memory\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1393 | return NULL; |
| 1394 | } |
| 1395 | |
| 1396 | ksmbd_debug(SMB, "session setup request for user %s\n", name); |
| 1397 | user = ksmbd_login_user(name); |
| 1398 | kfree(name); |
| 1399 | return user; |
| 1400 | } |
| 1401 | |
| 1402 | static int ntlm_authenticate(struct ksmbd_work *work) |
| 1403 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1404 | struct smb2_sess_setup_req *req = smb2_get_msg(work->request_buf); |
| 1405 | struct smb2_sess_setup_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1406 | struct ksmbd_conn *conn = work->conn; |
| 1407 | struct ksmbd_session *sess = work->sess; |
| 1408 | struct channel *chann = NULL; |
| 1409 | struct ksmbd_user *user; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1410 | u64 prev_id; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1411 | int sz, rc; |
| 1412 | |
| 1413 | ksmbd_debug(SMB, "authenticate phase\n"); |
| 1414 | if (conn->use_spnego) { |
| 1415 | unsigned char *spnego_blob; |
| 1416 | u16 spnego_blob_len; |
| 1417 | |
| 1418 | rc = build_spnego_ntlmssp_auth_blob(&spnego_blob, |
| 1419 | &spnego_blob_len, |
| 1420 | 0); |
| 1421 | if (rc) |
| 1422 | return -ENOMEM; |
| 1423 | |
| 1424 | sz = le16_to_cpu(rsp->SecurityBufferOffset); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1425 | memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1426 | rsp->SecurityBufferLength = cpu_to_le16(spnego_blob_len); |
| 1427 | kfree(spnego_blob); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1428 | inc_rfc1001_len(work->response_buf, spnego_blob_len - 1); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1429 | } |
| 1430 | |
| 1431 | user = session_user(conn, req); |
| 1432 | if (!user) { |
| 1433 | ksmbd_debug(SMB, "Unknown user name or an error\n"); |
Namjae Jeon | 58090b1 | 2021-07-16 14:52:09 +0900 | [diff] [blame] | 1434 | return -EPERM; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1435 | } |
| 1436 | |
| 1437 | /* Check for previous session */ |
| 1438 | prev_id = le64_to_cpu(req->PreviousSessionId); |
| 1439 | if (prev_id && prev_id != sess->id) |
| 1440 | destroy_previous_session(user, prev_id); |
| 1441 | |
| 1442 | if (sess->state == SMB2_SESSION_VALID) { |
| 1443 | /* |
| 1444 | * Reuse session if anonymous try to connect |
| 1445 | * on reauthetication. |
| 1446 | */ |
| 1447 | if (ksmbd_anonymous_user(user)) { |
| 1448 | ksmbd_free_user(user); |
| 1449 | return 0; |
| 1450 | } |
Namjae Jeon | a58b45a | 2021-12-16 10:26:43 +0900 | [diff] [blame] | 1451 | |
| 1452 | if (!ksmbd_compare_user(sess->user, user)) { |
| 1453 | ksmbd_free_user(user); |
| 1454 | return -EPERM; |
| 1455 | } |
| 1456 | ksmbd_free_user(user); |
| 1457 | } else { |
| 1458 | sess->user = user; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1459 | } |
| 1460 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1461 | if (user_guest(sess->user)) { |
| 1462 | if (conn->sign) { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1463 | ksmbd_debug(SMB, "Guest login not allowed when signing enabled\n"); |
Namjae Jeon | 58090b1 | 2021-07-16 14:52:09 +0900 | [diff] [blame] | 1464 | return -EPERM; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1465 | } |
| 1466 | |
| 1467 | rsp->SessionFlags = SMB2_SESSION_FLAG_IS_GUEST_LE; |
| 1468 | } else { |
| 1469 | struct authenticate_message *authblob; |
| 1470 | |
| 1471 | authblob = user_authblob(conn, req); |
| 1472 | sz = le16_to_cpu(req->SecurityBufferLength); |
Namjae Jeon | ce53d36 | 2021-12-15 14:57:27 +0900 | [diff] [blame] | 1473 | rc = ksmbd_decode_ntlmssp_auth_blob(authblob, sz, conn, sess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1474 | if (rc) { |
| 1475 | set_user_flag(sess->user, KSMBD_USER_FLAG_BAD_PASSWORD); |
| 1476 | ksmbd_debug(SMB, "authentication failed\n"); |
Namjae Jeon | 58090b1 | 2021-07-16 14:52:09 +0900 | [diff] [blame] | 1477 | return -EPERM; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1478 | } |
| 1479 | |
| 1480 | /* |
| 1481 | * If session state is SMB2_SESSION_VALID, We can assume |
| 1482 | * that it is reauthentication. And the user/password |
| 1483 | * has been verified, so return it here. |
| 1484 | */ |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1485 | if (sess->state == SMB2_SESSION_VALID) { |
| 1486 | if (conn->binding) |
| 1487 | goto binding_session; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1488 | return 0; |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1489 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1490 | |
| 1491 | if ((conn->sign || server_conf.enforced_signing) || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1492 | (req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1493 | sess->sign = true; |
| 1494 | |
Marcos Del Sol Vives | 83912d6 | 2021-12-16 11:37:22 +0100 | [diff] [blame] | 1495 | if (smb3_encryption_negotiated(conn) && |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1496 | !(req->Flags & SMB2_SESSION_REQ_FLAG_BINDING)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1497 | rc = conn->ops->generate_encryptionkey(sess); |
| 1498 | if (rc) { |
| 1499 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1500 | "SMB3 encryption key generation failed\n"); |
Namjae Jeon | 58090b1 | 2021-07-16 14:52:09 +0900 | [diff] [blame] | 1501 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1502 | } |
| 1503 | sess->enc = true; |
| 1504 | rsp->SessionFlags = SMB2_SESSION_FLAG_ENCRYPT_DATA_LE; |
| 1505 | /* |
| 1506 | * signing is disable if encryption is enable |
| 1507 | * on this session |
| 1508 | */ |
| 1509 | sess->sign = false; |
| 1510 | } |
| 1511 | } |
| 1512 | |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1513 | binding_session: |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1514 | if (conn->dialect >= SMB30_PROT_ID) { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1515 | chann = lookup_chann_list(sess, conn); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1516 | if (!chann) { |
| 1517 | chann = kmalloc(sizeof(struct channel), GFP_KERNEL); |
| 1518 | if (!chann) |
| 1519 | return -ENOMEM; |
| 1520 | |
| 1521 | chann->conn = conn; |
| 1522 | INIT_LIST_HEAD(&chann->chann_list); |
| 1523 | list_add(&chann->chann_list, &sess->ksmbd_chann_list); |
| 1524 | } |
| 1525 | } |
| 1526 | |
| 1527 | if (conn->ops->generate_signingkey) { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1528 | rc = conn->ops->generate_signingkey(sess, conn); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1529 | if (rc) { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1530 | ksmbd_debug(SMB, "SMB3 signing key generation failed\n"); |
Namjae Jeon | 58090b1 | 2021-07-16 14:52:09 +0900 | [diff] [blame] | 1531 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1532 | } |
| 1533 | } |
| 1534 | |
Namjae Jeon | 51a1387 | 2021-09-29 13:09:24 +0900 | [diff] [blame] | 1535 | if (!ksmbd_conn_lookup_dialect(conn)) { |
| 1536 | pr_err("fail to verify the dialect\n"); |
| 1537 | return -ENOENT; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1538 | } |
| 1539 | return 0; |
| 1540 | } |
| 1541 | |
| 1542 | #ifdef CONFIG_SMB_SERVER_KERBEROS5 |
| 1543 | static int krb5_authenticate(struct ksmbd_work *work) |
| 1544 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1545 | struct smb2_sess_setup_req *req = smb2_get_msg(work->request_buf); |
| 1546 | struct smb2_sess_setup_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1547 | struct ksmbd_conn *conn = work->conn; |
| 1548 | struct ksmbd_session *sess = work->sess; |
| 1549 | char *in_blob, *out_blob; |
| 1550 | struct channel *chann = NULL; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1551 | u64 prev_sess_id; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1552 | int in_len, out_len; |
| 1553 | int retval; |
| 1554 | |
| 1555 | in_blob = (char *)&req->hdr.ProtocolId + |
| 1556 | le16_to_cpu(req->SecurityBufferOffset); |
| 1557 | in_len = le16_to_cpu(req->SecurityBufferLength); |
| 1558 | out_blob = (char *)&rsp->hdr.ProtocolId + |
| 1559 | le16_to_cpu(rsp->SecurityBufferOffset); |
| 1560 | out_len = work->response_sz - |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1561 | (le16_to_cpu(rsp->SecurityBufferOffset) + 4); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1562 | |
| 1563 | /* Check previous session */ |
| 1564 | prev_sess_id = le64_to_cpu(req->PreviousSessionId); |
| 1565 | if (prev_sess_id && prev_sess_id != sess->id) |
| 1566 | destroy_previous_session(sess->user, prev_sess_id); |
| 1567 | |
| 1568 | if (sess->state == SMB2_SESSION_VALID) |
| 1569 | ksmbd_free_user(sess->user); |
| 1570 | |
| 1571 | retval = ksmbd_krb5_authenticate(sess, in_blob, in_len, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1572 | out_blob, &out_len); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1573 | if (retval) { |
| 1574 | ksmbd_debug(SMB, "krb5 authentication failed\n"); |
Namjae Jeon | 58090b1 | 2021-07-16 14:52:09 +0900 | [diff] [blame] | 1575 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1576 | } |
| 1577 | rsp->SecurityBufferLength = cpu_to_le16(out_len); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1578 | inc_rfc1001_len(work->response_buf, out_len - 1); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1579 | |
| 1580 | if ((conn->sign || server_conf.enforced_signing) || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1581 | (req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1582 | sess->sign = true; |
| 1583 | |
Marcos Del Sol Vives | 83912d6 | 2021-12-16 11:37:22 +0100 | [diff] [blame] | 1584 | if (smb3_encryption_negotiated(conn)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1585 | retval = conn->ops->generate_encryptionkey(sess); |
| 1586 | if (retval) { |
| 1587 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1588 | "SMB3 encryption key generation failed\n"); |
Namjae Jeon | 58090b1 | 2021-07-16 14:52:09 +0900 | [diff] [blame] | 1589 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1590 | } |
| 1591 | sess->enc = true; |
| 1592 | rsp->SessionFlags = SMB2_SESSION_FLAG_ENCRYPT_DATA_LE; |
| 1593 | sess->sign = false; |
| 1594 | } |
| 1595 | |
| 1596 | if (conn->dialect >= SMB30_PROT_ID) { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1597 | chann = lookup_chann_list(sess, conn); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1598 | if (!chann) { |
| 1599 | chann = kmalloc(sizeof(struct channel), GFP_KERNEL); |
| 1600 | if (!chann) |
| 1601 | return -ENOMEM; |
| 1602 | |
| 1603 | chann->conn = conn; |
| 1604 | INIT_LIST_HEAD(&chann->chann_list); |
| 1605 | list_add(&chann->chann_list, &sess->ksmbd_chann_list); |
| 1606 | } |
| 1607 | } |
| 1608 | |
| 1609 | if (conn->ops->generate_signingkey) { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1610 | retval = conn->ops->generate_signingkey(sess, conn); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1611 | if (retval) { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1612 | ksmbd_debug(SMB, "SMB3 signing key generation failed\n"); |
Namjae Jeon | 58090b1 | 2021-07-16 14:52:09 +0900 | [diff] [blame] | 1613 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1614 | } |
| 1615 | } |
| 1616 | |
Namjae Jeon | 51a1387 | 2021-09-29 13:09:24 +0900 | [diff] [blame] | 1617 | if (!ksmbd_conn_lookup_dialect(conn)) { |
| 1618 | pr_err("fail to verify the dialect\n"); |
| 1619 | return -ENOENT; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1620 | } |
| 1621 | return 0; |
| 1622 | } |
| 1623 | #else |
| 1624 | static int krb5_authenticate(struct ksmbd_work *work) |
| 1625 | { |
| 1626 | return -EOPNOTSUPP; |
| 1627 | } |
| 1628 | #endif |
| 1629 | |
| 1630 | int smb2_sess_setup(struct ksmbd_work *work) |
| 1631 | { |
| 1632 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1633 | struct smb2_sess_setup_req *req = smb2_get_msg(work->request_buf); |
| 1634 | struct smb2_sess_setup_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1635 | struct ksmbd_session *sess; |
| 1636 | struct negotiate_message *negblob; |
Marios Makassikis | 0d994cd | 2021-10-19 17:39:38 +0200 | [diff] [blame] | 1637 | unsigned int negblob_len, negblob_off; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1638 | int rc = 0; |
| 1639 | |
| 1640 | ksmbd_debug(SMB, "Received request for session setup\n"); |
| 1641 | |
| 1642 | rsp->StructureSize = cpu_to_le16(9); |
| 1643 | rsp->SessionFlags = 0; |
| 1644 | rsp->SecurityBufferOffset = cpu_to_le16(72); |
| 1645 | rsp->SecurityBufferLength = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1646 | inc_rfc1001_len(work->response_buf, 9); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1647 | |
| 1648 | if (!req->hdr.SessionId) { |
| 1649 | sess = ksmbd_smb2_session_create(); |
| 1650 | if (!sess) { |
| 1651 | rc = -ENOMEM; |
| 1652 | goto out_err; |
| 1653 | } |
| 1654 | rsp->hdr.SessionId = cpu_to_le64(sess->id); |
| 1655 | ksmbd_session_register(conn, sess); |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1656 | } else if (conn->dialect >= SMB30_PROT_ID && |
| 1657 | (server_conf.flags & KSMBD_GLOBAL_FLAG_SMB3_MULTICHANNEL) && |
| 1658 | req->Flags & SMB2_SESSION_REQ_FLAG_BINDING) { |
| 1659 | u64 sess_id = le64_to_cpu(req->hdr.SessionId); |
| 1660 | |
| 1661 | sess = ksmbd_session_lookup_slowpath(sess_id); |
| 1662 | if (!sess) { |
| 1663 | rc = -ENOENT; |
| 1664 | goto out_err; |
| 1665 | } |
| 1666 | |
| 1667 | if (conn->dialect != sess->conn->dialect) { |
| 1668 | rc = -EINVAL; |
| 1669 | goto out_err; |
| 1670 | } |
| 1671 | |
| 1672 | if (!(req->hdr.Flags & SMB2_FLAGS_SIGNED)) { |
| 1673 | rc = -EINVAL; |
| 1674 | goto out_err; |
| 1675 | } |
| 1676 | |
| 1677 | if (strncmp(conn->ClientGUID, sess->conn->ClientGUID, |
| 1678 | SMB2_CLIENT_GUID_SIZE)) { |
| 1679 | rc = -ENOENT; |
| 1680 | goto out_err; |
| 1681 | } |
| 1682 | |
| 1683 | if (sess->state == SMB2_SESSION_IN_PROGRESS) { |
| 1684 | rc = -EACCES; |
| 1685 | goto out_err; |
| 1686 | } |
| 1687 | |
| 1688 | if (sess->state == SMB2_SESSION_EXPIRED) { |
| 1689 | rc = -EFAULT; |
| 1690 | goto out_err; |
| 1691 | } |
| 1692 | |
| 1693 | if (ksmbd_session_lookup(conn, sess_id)) { |
| 1694 | rc = -EACCES; |
| 1695 | goto out_err; |
| 1696 | } |
| 1697 | |
| 1698 | conn->binding = true; |
| 1699 | } else if ((conn->dialect < SMB30_PROT_ID || |
| 1700 | server_conf.flags & KSMBD_GLOBAL_FLAG_SMB3_MULTICHANNEL) && |
| 1701 | (req->Flags & SMB2_SESSION_REQ_FLAG_BINDING)) { |
Colin Ian King | 4951a84 | 2021-07-06 13:05:01 +0100 | [diff] [blame] | 1702 | sess = NULL; |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1703 | rc = -EACCES; |
| 1704 | goto out_err; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1705 | } else { |
| 1706 | sess = ksmbd_session_lookup(conn, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1707 | le64_to_cpu(req->hdr.SessionId)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1708 | if (!sess) { |
| 1709 | rc = -ENOENT; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1710 | goto out_err; |
| 1711 | } |
| 1712 | } |
| 1713 | work->sess = sess; |
| 1714 | |
| 1715 | if (sess->state == SMB2_SESSION_EXPIRED) |
| 1716 | sess->state = SMB2_SESSION_IN_PROGRESS; |
| 1717 | |
Marios Makassikis | 0d994cd | 2021-10-19 17:39:38 +0200 | [diff] [blame] | 1718 | negblob_off = le16_to_cpu(req->SecurityBufferOffset); |
| 1719 | negblob_len = le16_to_cpu(req->SecurityBufferLength); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1720 | if (negblob_off < offsetof(struct smb2_sess_setup_req, Buffer) || |
Christophe JAILLET | f8fbfd8 | 2021-11-07 16:22:57 +0100 | [diff] [blame] | 1721 | negblob_len < offsetof(struct negotiate_message, NegotiateFlags)) { |
| 1722 | rc = -EINVAL; |
| 1723 | goto out_err; |
| 1724 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1725 | |
Marios Makassikis | 0d994cd | 2021-10-19 17:39:38 +0200 | [diff] [blame] | 1726 | negblob = (struct negotiate_message *)((char *)&req->hdr.ProtocolId + |
| 1727 | negblob_off); |
| 1728 | |
| 1729 | if (decode_negotiation_token(conn, negblob, negblob_len) == 0) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1730 | if (conn->mechToken) |
| 1731 | negblob = (struct negotiate_message *)conn->mechToken; |
| 1732 | } |
| 1733 | |
| 1734 | if (server_conf.auth_mechs & conn->auth_mechs) { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1735 | rc = generate_preauth_hash(work); |
| 1736 | if (rc) |
| 1737 | goto out_err; |
| 1738 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1739 | if (conn->preferred_auth_mech & |
| 1740 | (KSMBD_AUTH_KRB5 | KSMBD_AUTH_MSKRB5)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1741 | rc = krb5_authenticate(work); |
| 1742 | if (rc) { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1743 | rc = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1744 | goto out_err; |
| 1745 | } |
| 1746 | |
| 1747 | ksmbd_conn_set_good(work); |
| 1748 | sess->state = SMB2_SESSION_VALID; |
Muhammad Usama Anjum | 822bc8e | 2021-04-02 09:25:35 +0900 | [diff] [blame] | 1749 | kfree(sess->Preauth_HashValue); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1750 | sess->Preauth_HashValue = NULL; |
| 1751 | } else if (conn->preferred_auth_mech == KSMBD_AUTH_NTLMSSP) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1752 | if (negblob->MessageType == NtLmNegotiate) { |
Marios Makassikis | 0d994cd | 2021-10-19 17:39:38 +0200 | [diff] [blame] | 1753 | rc = ntlm_negotiate(work, negblob, negblob_len); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1754 | if (rc) |
| 1755 | goto out_err; |
| 1756 | rsp->hdr.Status = |
| 1757 | STATUS_MORE_PROCESSING_REQUIRED; |
| 1758 | /* |
| 1759 | * Note: here total size -1 is done as an |
| 1760 | * adjustment for 0 size blob |
| 1761 | */ |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1762 | inc_rfc1001_len(work->response_buf, |
| 1763 | le16_to_cpu(rsp->SecurityBufferLength) - 1); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1764 | |
| 1765 | } else if (negblob->MessageType == NtLmAuthenticate) { |
| 1766 | rc = ntlm_authenticate(work); |
| 1767 | if (rc) |
| 1768 | goto out_err; |
| 1769 | |
| 1770 | ksmbd_conn_set_good(work); |
| 1771 | sess->state = SMB2_SESSION_VALID; |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1772 | if (conn->binding) { |
| 1773 | struct preauth_session *preauth_sess; |
| 1774 | |
| 1775 | preauth_sess = |
| 1776 | ksmbd_preauth_session_lookup(conn, sess->id); |
| 1777 | if (preauth_sess) { |
| 1778 | list_del(&preauth_sess->preauth_entry); |
| 1779 | kfree(preauth_sess); |
| 1780 | } |
| 1781 | } |
Muhammad Usama Anjum | 822bc8e | 2021-04-02 09:25:35 +0900 | [diff] [blame] | 1782 | kfree(sess->Preauth_HashValue); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1783 | sess->Preauth_HashValue = NULL; |
| 1784 | } |
| 1785 | } else { |
| 1786 | /* TODO: need one more negotiation */ |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 1787 | pr_err("Not support the preferred authentication\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1788 | rc = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1789 | } |
| 1790 | } else { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 1791 | pr_err("Not support authentication\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1792 | rc = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1793 | } |
| 1794 | |
| 1795 | out_err: |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1796 | if (rc == -EINVAL) |
| 1797 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 1798 | else if (rc == -ENOENT) |
| 1799 | rsp->hdr.Status = STATUS_USER_SESSION_DELETED; |
| 1800 | else if (rc == -EACCES) |
| 1801 | rsp->hdr.Status = STATUS_REQUEST_NOT_ACCEPTED; |
| 1802 | else if (rc == -EFAULT) |
| 1803 | rsp->hdr.Status = STATUS_NETWORK_SESSION_EXPIRED; |
Namjae Jeon | 58090b1 | 2021-07-16 14:52:09 +0900 | [diff] [blame] | 1804 | else if (rc == -ENOMEM) |
| 1805 | rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES; |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1806 | else if (rc) |
| 1807 | rsp->hdr.Status = STATUS_LOGON_FAILURE; |
| 1808 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1809 | if (conn->use_spnego && conn->mechToken) { |
| 1810 | kfree(conn->mechToken); |
| 1811 | conn->mechToken = NULL; |
| 1812 | } |
| 1813 | |
Namjae Jeon | 621be84 | 2021-10-13 17:28:31 +0900 | [diff] [blame] | 1814 | if (rc < 0) { |
| 1815 | /* |
| 1816 | * SecurityBufferOffset should be set to zero |
| 1817 | * in session setup error response. |
| 1818 | */ |
| 1819 | rsp->SecurityBufferOffset = 0; |
| 1820 | |
| 1821 | if (sess) { |
| 1822 | bool try_delay = false; |
| 1823 | |
| 1824 | /* |
| 1825 | * To avoid dictionary attacks (repeated session setups rapidly sent) to |
| 1826 | * connect to server, ksmbd make a delay of a 5 seconds on session setup |
| 1827 | * failure to make it harder to send enough random connection requests |
| 1828 | * to break into a server. |
| 1829 | */ |
| 1830 | if (sess->user && sess->user->flags & KSMBD_USER_FLAG_DELAY_SESSION) |
| 1831 | try_delay = true; |
| 1832 | |
| 1833 | ksmbd_session_destroy(sess); |
| 1834 | work->sess = NULL; |
| 1835 | if (try_delay) |
| 1836 | ssleep(5); |
| 1837 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1838 | } |
| 1839 | |
| 1840 | return rc; |
| 1841 | } |
| 1842 | |
| 1843 | /** |
| 1844 | * smb2_tree_connect() - handler for smb2 tree connect command |
| 1845 | * @work: smb work containing smb request buffer |
| 1846 | * |
| 1847 | * Return: 0 on success, otherwise error |
| 1848 | */ |
| 1849 | int smb2_tree_connect(struct ksmbd_work *work) |
| 1850 | { |
| 1851 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1852 | struct smb2_tree_connect_req *req = smb2_get_msg(work->request_buf); |
| 1853 | struct smb2_tree_connect_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1854 | struct ksmbd_session *sess = work->sess; |
| 1855 | char *treename = NULL, *name = NULL; |
| 1856 | struct ksmbd_tree_conn_status status; |
| 1857 | struct ksmbd_share_config *share; |
| 1858 | int rc = -EINVAL; |
| 1859 | |
| 1860 | treename = smb_strndup_from_utf16(req->Buffer, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1861 | le16_to_cpu(req->PathLength), true, |
| 1862 | conn->local_nls); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1863 | if (IS_ERR(treename)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 1864 | pr_err("treename is NULL\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1865 | status.ret = KSMBD_TREE_CONN_STATUS_ERROR; |
| 1866 | goto out_err1; |
| 1867 | } |
| 1868 | |
Stephen Rothwell | 36ba386 | 2021-03-17 17:01:15 +0900 | [diff] [blame] | 1869 | name = ksmbd_extract_sharename(treename); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1870 | if (IS_ERR(name)) { |
| 1871 | status.ret = KSMBD_TREE_CONN_STATUS_ERROR; |
| 1872 | goto out_err1; |
| 1873 | } |
| 1874 | |
| 1875 | ksmbd_debug(SMB, "tree connect request for tree %s treename %s\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1876 | name, treename); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1877 | |
| 1878 | status = ksmbd_tree_conn_connect(sess, name); |
| 1879 | if (status.ret == KSMBD_TREE_CONN_STATUS_OK) |
| 1880 | rsp->hdr.Id.SyncId.TreeId = cpu_to_le32(status.tree_conn->id); |
| 1881 | else |
| 1882 | goto out_err1; |
| 1883 | |
| 1884 | share = status.tree_conn->share_conf; |
| 1885 | if (test_share_config_flag(share, KSMBD_SHARE_FLAG_PIPE)) { |
| 1886 | ksmbd_debug(SMB, "IPC share path request\n"); |
| 1887 | rsp->ShareType = SMB2_SHARE_TYPE_PIPE; |
| 1888 | rsp->MaximalAccess = FILE_READ_DATA_LE | FILE_READ_EA_LE | |
| 1889 | FILE_EXECUTE_LE | FILE_READ_ATTRIBUTES_LE | |
| 1890 | FILE_DELETE_LE | FILE_READ_CONTROL_LE | |
| 1891 | FILE_WRITE_DAC_LE | FILE_WRITE_OWNER_LE | |
| 1892 | FILE_SYNCHRONIZE_LE; |
| 1893 | } else { |
| 1894 | rsp->ShareType = SMB2_SHARE_TYPE_DISK; |
| 1895 | rsp->MaximalAccess = FILE_READ_DATA_LE | FILE_READ_EA_LE | |
| 1896 | FILE_EXECUTE_LE | FILE_READ_ATTRIBUTES_LE; |
| 1897 | if (test_tree_conn_flag(status.tree_conn, |
| 1898 | KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
| 1899 | rsp->MaximalAccess |= FILE_WRITE_DATA_LE | |
| 1900 | FILE_APPEND_DATA_LE | FILE_WRITE_EA_LE | |
Wan Jiabing | 3aefd54 | 2021-06-07 12:54:32 +0800 | [diff] [blame] | 1901 | FILE_DELETE_LE | FILE_WRITE_ATTRIBUTES_LE | |
| 1902 | FILE_DELETE_CHILD_LE | FILE_READ_CONTROL_LE | |
| 1903 | FILE_WRITE_DAC_LE | FILE_WRITE_OWNER_LE | |
| 1904 | FILE_SYNCHRONIZE_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1905 | } |
| 1906 | } |
| 1907 | |
| 1908 | status.tree_conn->maximal_access = le32_to_cpu(rsp->MaximalAccess); |
| 1909 | if (conn->posix_ext_supported) |
| 1910 | status.tree_conn->posix_extensions = true; |
| 1911 | |
| 1912 | out_err1: |
| 1913 | rsp->StructureSize = cpu_to_le16(16); |
| 1914 | rsp->Capabilities = 0; |
| 1915 | rsp->Reserved = 0; |
| 1916 | /* default manual caching */ |
| 1917 | rsp->ShareFlags = SMB2_SHAREFLAG_MANUAL_CACHING; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 1918 | inc_rfc1001_len(work->response_buf, 16); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1919 | |
| 1920 | if (!IS_ERR(treename)) |
| 1921 | kfree(treename); |
| 1922 | if (!IS_ERR(name)) |
| 1923 | kfree(name); |
| 1924 | |
| 1925 | switch (status.ret) { |
| 1926 | case KSMBD_TREE_CONN_STATUS_OK: |
| 1927 | rsp->hdr.Status = STATUS_SUCCESS; |
| 1928 | rc = 0; |
| 1929 | break; |
| 1930 | case KSMBD_TREE_CONN_STATUS_NO_SHARE: |
| 1931 | rsp->hdr.Status = STATUS_BAD_NETWORK_PATH; |
| 1932 | break; |
| 1933 | case -ENOMEM: |
| 1934 | case KSMBD_TREE_CONN_STATUS_NOMEM: |
| 1935 | rsp->hdr.Status = STATUS_NO_MEMORY; |
| 1936 | break; |
| 1937 | case KSMBD_TREE_CONN_STATUS_ERROR: |
| 1938 | case KSMBD_TREE_CONN_STATUS_TOO_MANY_CONNS: |
| 1939 | case KSMBD_TREE_CONN_STATUS_TOO_MANY_SESSIONS: |
| 1940 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 1941 | break; |
| 1942 | case -EINVAL: |
| 1943 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 1944 | break; |
| 1945 | default: |
| 1946 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 1947 | } |
| 1948 | |
| 1949 | return rc; |
| 1950 | } |
| 1951 | |
| 1952 | /** |
| 1953 | * smb2_create_open_flags() - convert smb open flags to unix open flags |
| 1954 | * @file_present: is file already present |
| 1955 | * @access: file access flags |
| 1956 | * @disposition: file disposition flags |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 1957 | * @may_flags: set with MAY_ flags |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1958 | * |
| 1959 | * Return: file open flags |
| 1960 | */ |
| 1961 | static int smb2_create_open_flags(bool file_present, __le32 access, |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 1962 | __le32 disposition, |
| 1963 | int *may_flags) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1964 | { |
| 1965 | int oflags = O_NONBLOCK | O_LARGEFILE; |
| 1966 | |
| 1967 | if (access & FILE_READ_DESIRED_ACCESS_LE && |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 1968 | access & FILE_WRITE_DESIRE_ACCESS_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1969 | oflags |= O_RDWR; |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 1970 | *may_flags = MAY_OPEN | MAY_READ | MAY_WRITE; |
| 1971 | } else if (access & FILE_WRITE_DESIRE_ACCESS_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1972 | oflags |= O_WRONLY; |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 1973 | *may_flags = MAY_OPEN | MAY_WRITE; |
| 1974 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1975 | oflags |= O_RDONLY; |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 1976 | *may_flags = MAY_OPEN | MAY_READ; |
| 1977 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1978 | |
| 1979 | if (access == FILE_READ_ATTRIBUTES_LE) |
| 1980 | oflags |= O_PATH; |
| 1981 | |
| 1982 | if (file_present) { |
| 1983 | switch (disposition & FILE_CREATE_MASK_LE) { |
| 1984 | case FILE_OPEN_LE: |
| 1985 | case FILE_CREATE_LE: |
| 1986 | break; |
| 1987 | case FILE_SUPERSEDE_LE: |
| 1988 | case FILE_OVERWRITE_LE: |
| 1989 | case FILE_OVERWRITE_IF_LE: |
| 1990 | oflags |= O_TRUNC; |
| 1991 | break; |
| 1992 | default: |
| 1993 | break; |
| 1994 | } |
| 1995 | } else { |
| 1996 | switch (disposition & FILE_CREATE_MASK_LE) { |
| 1997 | case FILE_SUPERSEDE_LE: |
| 1998 | case FILE_CREATE_LE: |
| 1999 | case FILE_OPEN_IF_LE: |
| 2000 | case FILE_OVERWRITE_IF_LE: |
| 2001 | oflags |= O_CREAT; |
| 2002 | break; |
| 2003 | case FILE_OPEN_LE: |
| 2004 | case FILE_OVERWRITE_LE: |
| 2005 | oflags &= ~O_CREAT; |
| 2006 | break; |
| 2007 | default: |
| 2008 | break; |
| 2009 | } |
| 2010 | } |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 2011 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2012 | return oflags; |
| 2013 | } |
| 2014 | |
| 2015 | /** |
| 2016 | * smb2_tree_disconnect() - handler for smb tree connect request |
| 2017 | * @work: smb work containing request buffer |
| 2018 | * |
| 2019 | * Return: 0 |
| 2020 | */ |
| 2021 | int smb2_tree_disconnect(struct ksmbd_work *work) |
| 2022 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 2023 | struct smb2_tree_disconnect_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2024 | struct ksmbd_session *sess = work->sess; |
| 2025 | struct ksmbd_tree_connect *tcon = work->tcon; |
| 2026 | |
| 2027 | rsp->StructureSize = cpu_to_le16(4); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 2028 | inc_rfc1001_len(work->response_buf, 4); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2029 | |
| 2030 | ksmbd_debug(SMB, "request\n"); |
| 2031 | |
| 2032 | if (!tcon) { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 2033 | struct smb2_tree_disconnect_req *req = |
| 2034 | smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2035 | |
| 2036 | ksmbd_debug(SMB, "Invalid tid %d\n", req->hdr.Id.SyncId.TreeId); |
| 2037 | rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED; |
| 2038 | smb2_set_err_rsp(work); |
| 2039 | return 0; |
| 2040 | } |
| 2041 | |
| 2042 | ksmbd_close_tree_conn_fds(work); |
| 2043 | ksmbd_tree_conn_disconnect(sess, tcon); |
| 2044 | return 0; |
| 2045 | } |
| 2046 | |
| 2047 | /** |
| 2048 | * smb2_session_logoff() - handler for session log off request |
| 2049 | * @work: smb work containing request buffer |
| 2050 | * |
| 2051 | * Return: 0 |
| 2052 | */ |
| 2053 | int smb2_session_logoff(struct ksmbd_work *work) |
| 2054 | { |
| 2055 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 2056 | struct smb2_logoff_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2057 | struct ksmbd_session *sess = work->sess; |
| 2058 | |
| 2059 | rsp->StructureSize = cpu_to_le16(4); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 2060 | inc_rfc1001_len(work->response_buf, 4); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2061 | |
| 2062 | ksmbd_debug(SMB, "request\n"); |
| 2063 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2064 | /* setting CifsExiting here may race with start_tcp_sess */ |
| 2065 | ksmbd_conn_set_need_reconnect(work); |
| 2066 | ksmbd_close_session_fds(work); |
| 2067 | ksmbd_conn_wait_idle(conn); |
| 2068 | |
| 2069 | if (ksmbd_tree_conn_session_logoff(sess)) { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 2070 | struct smb2_logoff_req *req = smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2071 | |
| 2072 | ksmbd_debug(SMB, "Invalid tid %d\n", req->hdr.Id.SyncId.TreeId); |
| 2073 | rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED; |
| 2074 | smb2_set_err_rsp(work); |
| 2075 | return 0; |
| 2076 | } |
| 2077 | |
| 2078 | ksmbd_destroy_file_table(&sess->file_table); |
| 2079 | sess->state = SMB2_SESSION_EXPIRED; |
| 2080 | |
| 2081 | ksmbd_free_user(sess->user); |
| 2082 | sess->user = NULL; |
| 2083 | |
| 2084 | /* let start_tcp_sess free connection info now */ |
| 2085 | ksmbd_conn_set_need_negotiate(work); |
| 2086 | return 0; |
| 2087 | } |
| 2088 | |
| 2089 | /** |
| 2090 | * create_smb2_pipe() - create IPC pipe |
| 2091 | * @work: smb work containing request buffer |
| 2092 | * |
| 2093 | * Return: 0 on success, otherwise error |
| 2094 | */ |
| 2095 | static noinline int create_smb2_pipe(struct ksmbd_work *work) |
| 2096 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 2097 | struct smb2_create_rsp *rsp = smb2_get_msg(work->response_buf); |
| 2098 | struct smb2_create_req *req = smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2099 | int id; |
| 2100 | int err; |
| 2101 | char *name; |
| 2102 | |
| 2103 | name = smb_strndup_from_utf16(req->Buffer, le16_to_cpu(req->NameLength), |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2104 | 1, work->conn->local_nls); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2105 | if (IS_ERR(name)) { |
| 2106 | rsp->hdr.Status = STATUS_NO_MEMORY; |
| 2107 | err = PTR_ERR(name); |
| 2108 | goto out; |
| 2109 | } |
| 2110 | |
| 2111 | id = ksmbd_session_rpc_open(work->sess, name); |
Marios Makassikis | 79caa96 | 2021-05-06 11:38:35 +0900 | [diff] [blame] | 2112 | if (id < 0) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2113 | pr_err("Unable to open RPC pipe: %d\n", id); |
Marios Makassikis | 79caa96 | 2021-05-06 11:38:35 +0900 | [diff] [blame] | 2114 | err = id; |
| 2115 | goto out; |
| 2116 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2117 | |
Marios Makassikis | 79caa96 | 2021-05-06 11:38:35 +0900 | [diff] [blame] | 2118 | rsp->hdr.Status = STATUS_SUCCESS; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2119 | rsp->StructureSize = cpu_to_le16(89); |
| 2120 | rsp->OplockLevel = SMB2_OPLOCK_LEVEL_NONE; |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 2121 | rsp->Flags = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2122 | rsp->CreateAction = cpu_to_le32(FILE_OPENED); |
| 2123 | |
| 2124 | rsp->CreationTime = cpu_to_le64(0); |
| 2125 | rsp->LastAccessTime = cpu_to_le64(0); |
| 2126 | rsp->ChangeTime = cpu_to_le64(0); |
| 2127 | rsp->AllocationSize = cpu_to_le64(0); |
| 2128 | rsp->EndofFile = cpu_to_le64(0); |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 2129 | rsp->FileAttributes = FILE_ATTRIBUTE_NORMAL_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2130 | rsp->Reserved2 = 0; |
| 2131 | rsp->VolatileFileId = cpu_to_le64(id); |
| 2132 | rsp->PersistentFileId = 0; |
| 2133 | rsp->CreateContextsOffset = 0; |
| 2134 | rsp->CreateContextsLength = 0; |
| 2135 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 2136 | inc_rfc1001_len(work->response_buf, 88); /* StructureSize - 1*/ |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2137 | kfree(name); |
| 2138 | return 0; |
| 2139 | |
| 2140 | out: |
Marios Makassikis | 79caa96 | 2021-05-06 11:38:35 +0900 | [diff] [blame] | 2141 | switch (err) { |
| 2142 | case -EINVAL: |
| 2143 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 2144 | break; |
| 2145 | case -ENOSPC: |
| 2146 | case -ENOMEM: |
| 2147 | rsp->hdr.Status = STATUS_NO_MEMORY; |
| 2148 | break; |
| 2149 | } |
| 2150 | |
| 2151 | if (!IS_ERR(name)) |
| 2152 | kfree(name); |
| 2153 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2154 | smb2_set_err_rsp(work); |
| 2155 | return err; |
| 2156 | } |
| 2157 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2158 | /** |
| 2159 | * smb2_set_ea() - handler for setting extended attributes using set |
| 2160 | * info command |
| 2161 | * @eabuf: set info command buffer |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 2162 | * @buf_len: set info command buffer length |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2163 | * @path: dentry path for get ea |
| 2164 | * |
| 2165 | * Return: 0 on success, otherwise error |
| 2166 | */ |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 2167 | static int smb2_set_ea(struct smb2_ea_info *eabuf, unsigned int buf_len, |
| 2168 | struct path *path) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2169 | { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2170 | struct user_namespace *user_ns = mnt_user_ns(path->mnt); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2171 | char *attr_name = NULL, *value; |
| 2172 | int rc = 0; |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 2173 | unsigned int next = 0; |
| 2174 | |
| 2175 | if (buf_len < sizeof(struct smb2_ea_info) + eabuf->EaNameLength + |
| 2176 | le16_to_cpu(eabuf->EaValueLength)) |
| 2177 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2178 | |
| 2179 | attr_name = kmalloc(XATTR_NAME_MAX + 1, GFP_KERNEL); |
| 2180 | if (!attr_name) |
| 2181 | return -ENOMEM; |
| 2182 | |
| 2183 | do { |
| 2184 | if (!eabuf->EaNameLength) |
| 2185 | goto next; |
| 2186 | |
| 2187 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2188 | "name : <%s>, name_len : %u, value_len : %u, next : %u\n", |
| 2189 | eabuf->name, eabuf->EaNameLength, |
| 2190 | le16_to_cpu(eabuf->EaValueLength), |
| 2191 | le32_to_cpu(eabuf->NextEntryOffset)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2192 | |
| 2193 | if (eabuf->EaNameLength > |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2194 | (XATTR_NAME_MAX - XATTR_USER_PREFIX_LEN)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2195 | rc = -EINVAL; |
| 2196 | break; |
| 2197 | } |
| 2198 | |
| 2199 | memcpy(attr_name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN); |
| 2200 | memcpy(&attr_name[XATTR_USER_PREFIX_LEN], eabuf->name, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2201 | eabuf->EaNameLength); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2202 | attr_name[XATTR_USER_PREFIX_LEN + eabuf->EaNameLength] = '\0'; |
| 2203 | value = (char *)&eabuf->name + eabuf->EaNameLength + 1; |
| 2204 | |
| 2205 | if (!eabuf->EaValueLength) { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2206 | rc = ksmbd_vfs_casexattr_len(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2207 | path->dentry, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2208 | attr_name, |
| 2209 | XATTR_USER_PREFIX_LEN + |
| 2210 | eabuf->EaNameLength); |
| 2211 | |
| 2212 | /* delete the EA only when it exits */ |
| 2213 | if (rc > 0) { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2214 | rc = ksmbd_vfs_remove_xattr(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2215 | path->dentry, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2216 | attr_name); |
| 2217 | |
| 2218 | if (rc < 0) { |
| 2219 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2220 | "remove xattr failed(%d)\n", |
| 2221 | rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2222 | break; |
| 2223 | } |
| 2224 | } |
| 2225 | |
| 2226 | /* if the EA doesn't exist, just do nothing. */ |
| 2227 | rc = 0; |
| 2228 | } else { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2229 | rc = ksmbd_vfs_setxattr(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2230 | path->dentry, attr_name, value, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2231 | le16_to_cpu(eabuf->EaValueLength), 0); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2232 | if (rc < 0) { |
| 2233 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2234 | "ksmbd_vfs_setxattr is failed(%d)\n", |
| 2235 | rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2236 | break; |
| 2237 | } |
| 2238 | } |
| 2239 | |
| 2240 | next: |
| 2241 | next = le32_to_cpu(eabuf->NextEntryOffset); |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 2242 | if (next == 0 || buf_len < next) |
| 2243 | break; |
| 2244 | buf_len -= next; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2245 | eabuf = (struct smb2_ea_info *)((char *)eabuf + next); |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 2246 | if (next < (u32)eabuf->EaNameLength + le16_to_cpu(eabuf->EaValueLength)) |
| 2247 | break; |
| 2248 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2249 | } while (next != 0); |
| 2250 | |
| 2251 | kfree(attr_name); |
| 2252 | return rc; |
| 2253 | } |
| 2254 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2255 | static noinline int smb2_set_stream_name_xattr(struct path *path, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2256 | struct ksmbd_file *fp, |
| 2257 | char *stream_name, int s_type) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2258 | { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2259 | struct user_namespace *user_ns = mnt_user_ns(path->mnt); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2260 | size_t xattr_stream_size; |
| 2261 | char *xattr_stream_name; |
| 2262 | int rc; |
| 2263 | |
| 2264 | rc = ksmbd_vfs_xattr_stream_name(stream_name, |
| 2265 | &xattr_stream_name, |
| 2266 | &xattr_stream_size, |
| 2267 | s_type); |
| 2268 | if (rc) |
| 2269 | return rc; |
| 2270 | |
| 2271 | fp->stream.name = xattr_stream_name; |
| 2272 | fp->stream.size = xattr_stream_size; |
| 2273 | |
| 2274 | /* Check if there is stream prefix in xattr space */ |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2275 | rc = ksmbd_vfs_casexattr_len(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2276 | path->dentry, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2277 | xattr_stream_name, |
| 2278 | xattr_stream_size); |
| 2279 | if (rc >= 0) |
| 2280 | return 0; |
| 2281 | |
| 2282 | if (fp->cdoption == FILE_OPEN_LE) { |
| 2283 | ksmbd_debug(SMB, "XATTR stream name lookup failed: %d\n", rc); |
| 2284 | return -EBADF; |
| 2285 | } |
| 2286 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2287 | rc = ksmbd_vfs_setxattr(user_ns, path->dentry, |
| 2288 | xattr_stream_name, NULL, 0, 0); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2289 | if (rc < 0) |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2290 | pr_err("Failed to store XATTR stream name :%d\n", rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2291 | return 0; |
| 2292 | } |
| 2293 | |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 2294 | static int smb2_remove_smb_xattrs(struct path *path) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2295 | { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2296 | struct user_namespace *user_ns = mnt_user_ns(path->mnt); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2297 | char *name, *xattr_list = NULL; |
| 2298 | ssize_t xattr_list_len; |
| 2299 | int err = 0; |
| 2300 | |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 2301 | xattr_list_len = ksmbd_vfs_listxattr(path->dentry, &xattr_list); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2302 | if (xattr_list_len < 0) { |
| 2303 | goto out; |
| 2304 | } else if (!xattr_list_len) { |
| 2305 | ksmbd_debug(SMB, "empty xattr in the file\n"); |
| 2306 | goto out; |
| 2307 | } |
| 2308 | |
| 2309 | for (name = xattr_list; name - xattr_list < xattr_list_len; |
| 2310 | name += strlen(name) + 1) { |
| 2311 | ksmbd_debug(SMB, "%s, len %zd\n", name, strlen(name)); |
| 2312 | |
| 2313 | if (strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2314 | strncmp(&name[XATTR_USER_PREFIX_LEN], DOS_ATTRIBUTE_PREFIX, |
| 2315 | DOS_ATTRIBUTE_PREFIX_LEN) && |
| 2316 | strncmp(&name[XATTR_USER_PREFIX_LEN], STREAM_PREFIX, STREAM_PREFIX_LEN)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2317 | continue; |
| 2318 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2319 | err = ksmbd_vfs_remove_xattr(user_ns, path->dentry, name); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2320 | if (err) |
| 2321 | ksmbd_debug(SMB, "remove xattr failed : %s\n", name); |
| 2322 | } |
| 2323 | out: |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 2324 | kvfree(xattr_list); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2325 | return err; |
| 2326 | } |
| 2327 | |
| 2328 | static int smb2_create_truncate(struct path *path) |
| 2329 | { |
| 2330 | int rc = vfs_truncate(path, 0); |
| 2331 | |
| 2332 | if (rc) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2333 | pr_err("vfs_truncate failed, rc %d\n", rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2334 | return rc; |
| 2335 | } |
| 2336 | |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 2337 | rc = smb2_remove_smb_xattrs(path); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2338 | if (rc == -EOPNOTSUPP) |
| 2339 | rc = 0; |
| 2340 | if (rc) |
| 2341 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2342 | "ksmbd_truncate_stream_name_xattr failed, rc %d\n", |
| 2343 | rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2344 | return rc; |
| 2345 | } |
| 2346 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2347 | 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] | 2348 | struct ksmbd_file *fp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2349 | { |
| 2350 | struct xattr_dos_attrib da = {0}; |
| 2351 | int rc; |
| 2352 | |
| 2353 | if (!test_share_config_flag(tcon->share_conf, |
| 2354 | KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) |
| 2355 | return; |
| 2356 | |
| 2357 | da.version = 4; |
| 2358 | da.attr = le32_to_cpu(fp->f_ci->m_fattr); |
| 2359 | da.itime = da.create_time = fp->create_time; |
| 2360 | da.flags = XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME | |
| 2361 | XATTR_DOSINFO_ITIME; |
| 2362 | |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2363 | rc = ksmbd_vfs_set_dos_attrib_xattr(mnt_user_ns(path->mnt), |
| 2364 | path->dentry, &da); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2365 | if (rc) |
| 2366 | ksmbd_debug(SMB, "failed to store file attribute into xattr\n"); |
| 2367 | } |
| 2368 | |
| 2369 | static void smb2_update_xattrs(struct ksmbd_tree_connect *tcon, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2370 | struct path *path, struct ksmbd_file *fp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2371 | { |
| 2372 | struct xattr_dos_attrib da; |
| 2373 | int rc; |
| 2374 | |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 2375 | fp->f_ci->m_fattr &= ~(FILE_ATTRIBUTE_HIDDEN_LE | FILE_ATTRIBUTE_SYSTEM_LE); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2376 | |
| 2377 | /* get FileAttributes from XATTR_NAME_DOS_ATTRIBUTE */ |
| 2378 | if (!test_share_config_flag(tcon->share_conf, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2379 | KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2380 | return; |
| 2381 | |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2382 | rc = ksmbd_vfs_get_dos_attrib_xattr(mnt_user_ns(path->mnt), |
| 2383 | path->dentry, &da); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2384 | if (rc > 0) { |
| 2385 | fp->f_ci->m_fattr = cpu_to_le32(da.attr); |
| 2386 | fp->create_time = da.create_time; |
| 2387 | fp->itime = da.itime; |
| 2388 | } |
| 2389 | } |
| 2390 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2391 | 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] | 2392 | int open_flags, umode_t posix_mode, bool is_dir) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2393 | { |
| 2394 | struct ksmbd_tree_connect *tcon = work->tcon; |
| 2395 | struct ksmbd_share_config *share = tcon->share_conf; |
| 2396 | umode_t mode; |
| 2397 | int rc; |
| 2398 | |
| 2399 | if (!(open_flags & O_CREAT)) |
| 2400 | return -EBADF; |
| 2401 | |
| 2402 | ksmbd_debug(SMB, "file does not exist, so creating\n"); |
| 2403 | if (is_dir == true) { |
| 2404 | ksmbd_debug(SMB, "creating directory\n"); |
| 2405 | |
| 2406 | mode = share_config_directory_mode(share, posix_mode); |
| 2407 | rc = ksmbd_vfs_mkdir(work, name, mode); |
| 2408 | if (rc) |
| 2409 | return rc; |
| 2410 | } else { |
| 2411 | ksmbd_debug(SMB, "creating regular file\n"); |
| 2412 | |
| 2413 | mode = share_config_create_mode(share, posix_mode); |
| 2414 | rc = ksmbd_vfs_create(work, name, mode); |
| 2415 | if (rc) |
| 2416 | return rc; |
| 2417 | } |
| 2418 | |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 2419 | rc = ksmbd_vfs_kern_path(work, name, 0, path, 0); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2420 | if (rc) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2421 | pr_err("cannot get linux path (%s), err = %d\n", |
| 2422 | name, rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2423 | return rc; |
| 2424 | } |
| 2425 | return 0; |
| 2426 | } |
| 2427 | |
| 2428 | static int smb2_create_sd_buffer(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2429 | struct smb2_create_req *req, |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 2430 | struct path *path) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2431 | { |
| 2432 | struct create_context *context; |
Hyunchul Lee | 21dd1fd | 2021-07-09 17:06:34 +0900 | [diff] [blame] | 2433 | struct create_sd_buf_req *sd_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2434 | |
| 2435 | if (!req->CreateContextsOffset) |
Hyunchul Lee | 21dd1fd | 2021-07-09 17:06:34 +0900 | [diff] [blame] | 2436 | return -ENOENT; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2437 | |
| 2438 | /* Parse SD BUFFER create contexts */ |
| 2439 | context = smb2_find_context_vals(req, SMB2_CREATE_SD_BUFFER); |
Hyunchul Lee | 21dd1fd | 2021-07-09 17:06:34 +0900 | [diff] [blame] | 2440 | if (!context) |
| 2441 | return -ENOENT; |
| 2442 | else if (IS_ERR(context)) |
| 2443 | return PTR_ERR(context); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2444 | |
Hyunchul Lee | 21dd1fd | 2021-07-09 17:06:34 +0900 | [diff] [blame] | 2445 | ksmbd_debug(SMB, |
| 2446 | "Set ACLs using SMB2_CREATE_SD_BUFFER context\n"); |
| 2447 | sd_buf = (struct create_sd_buf_req *)context; |
Hyunchul Lee | 8f77150 | 2021-09-24 22:22:22 +0900 | [diff] [blame] | 2448 | if (le16_to_cpu(context->DataOffset) + |
| 2449 | le32_to_cpu(context->DataLength) < |
| 2450 | sizeof(struct create_sd_buf_req)) |
| 2451 | return -EINVAL; |
Hyunchul Lee | 21dd1fd | 2021-07-09 17:06:34 +0900 | [diff] [blame] | 2452 | return set_info_sec(work->conn, work->tcon, path, &sd_buf->ntsd, |
| 2453 | le32_to_cpu(sd_buf->ccontext.DataLength), true); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2454 | } |
| 2455 | |
Christian Brauner | 43205ca | 2021-08-23 17:13:50 +0200 | [diff] [blame] | 2456 | static void ksmbd_acls_fattr(struct smb_fattr *fattr, |
| 2457 | struct user_namespace *mnt_userns, |
| 2458 | struct inode *inode) |
Namjae Jeon | 3d47e54 | 2021-04-20 14:25:35 +0900 | [diff] [blame] | 2459 | { |
Christian Brauner | 43205ca | 2021-08-23 17:13:50 +0200 | [diff] [blame] | 2460 | fattr->cf_uid = i_uid_into_mnt(mnt_userns, inode); |
| 2461 | fattr->cf_gid = i_gid_into_mnt(mnt_userns, inode); |
Namjae Jeon | 3d47e54 | 2021-04-20 14:25:35 +0900 | [diff] [blame] | 2462 | fattr->cf_mode = inode->i_mode; |
Namjae Jeon | 777cad1 | 2021-08-13 08:15:33 +0900 | [diff] [blame] | 2463 | fattr->cf_acls = NULL; |
Namjae Jeon | 3d47e54 | 2021-04-20 14:25:35 +0900 | [diff] [blame] | 2464 | fattr->cf_dacls = NULL; |
| 2465 | |
Namjae Jeon | 777cad1 | 2021-08-13 08:15:33 +0900 | [diff] [blame] | 2466 | if (IS_ENABLED(CONFIG_FS_POSIX_ACL)) { |
| 2467 | fattr->cf_acls = get_acl(inode, ACL_TYPE_ACCESS); |
| 2468 | if (S_ISDIR(inode->i_mode)) |
| 2469 | fattr->cf_dacls = get_acl(inode, ACL_TYPE_DEFAULT); |
| 2470 | } |
Namjae Jeon | 3d47e54 | 2021-04-20 14:25:35 +0900 | [diff] [blame] | 2471 | } |
| 2472 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2473 | /** |
| 2474 | * smb2_open() - handler for smb file open request |
| 2475 | * @work: smb work containing request buffer |
| 2476 | * |
| 2477 | * Return: 0 on success, otherwise error |
| 2478 | */ |
| 2479 | int smb2_open(struct ksmbd_work *work) |
| 2480 | { |
| 2481 | struct ksmbd_conn *conn = work->conn; |
| 2482 | struct ksmbd_session *sess = work->sess; |
| 2483 | struct ksmbd_tree_connect *tcon = work->tcon; |
| 2484 | struct smb2_create_req *req; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 2485 | struct smb2_create_rsp *rsp; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2486 | struct path path; |
| 2487 | struct ksmbd_share_config *share = tcon->share_conf; |
| 2488 | struct ksmbd_file *fp = NULL; |
| 2489 | struct file *filp = NULL; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2490 | struct user_namespace *user_ns = NULL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2491 | struct kstat stat; |
| 2492 | struct create_context *context; |
| 2493 | struct lease_ctx_info *lc = NULL; |
| 2494 | struct create_ea_buf_req *ea_buf = NULL; |
| 2495 | struct oplock_info *opinfo; |
| 2496 | __le32 *next_ptr = NULL; |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 2497 | 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] | 2498 | int rc = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2499 | int contxt_cnt = 0, query_disk_id = 0; |
| 2500 | int maximal_access_ctxt = 0, posix_ctxt = 0; |
| 2501 | int s_type = 0; |
| 2502 | int next_off = 0; |
| 2503 | char *name = NULL; |
| 2504 | char *stream_name = NULL; |
| 2505 | bool file_present = false, created = false, already_permitted = false; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2506 | int share_ret, need_truncate = 0; |
| 2507 | u64 time; |
| 2508 | umode_t posix_mode = 0; |
| 2509 | __le32 daccess, maximal_access = 0; |
| 2510 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2511 | WORK_BUFFERS(work, req, rsp); |
| 2512 | |
| 2513 | if (req->hdr.NextCommand && !work->next_smb2_rcv_hdr_off && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2514 | (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2515 | ksmbd_debug(SMB, "invalid flag in chained command\n"); |
| 2516 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 2517 | smb2_set_err_rsp(work); |
| 2518 | return -EINVAL; |
| 2519 | } |
| 2520 | |
| 2521 | if (test_share_config_flag(share, KSMBD_SHARE_FLAG_PIPE)) { |
| 2522 | ksmbd_debug(SMB, "IPC pipe create request\n"); |
| 2523 | return create_smb2_pipe(work); |
| 2524 | } |
| 2525 | |
| 2526 | if (req->NameLength) { |
| 2527 | if ((req->CreateOptions & FILE_DIRECTORY_FILE_LE) && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2528 | *(char *)req->Buffer == '\\') { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2529 | pr_err("not allow directory name included leading slash\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2530 | rc = -EINVAL; |
| 2531 | goto err_out1; |
| 2532 | } |
| 2533 | |
Marios Makassikis | 80917f1 | 2021-12-01 21:41:19 +0100 | [diff] [blame] | 2534 | name = smb2_get_name(req->Buffer, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2535 | le16_to_cpu(req->NameLength), |
| 2536 | work->conn->local_nls); |
| 2537 | if (IS_ERR(name)) { |
| 2538 | rc = PTR_ERR(name); |
| 2539 | if (rc != -ENOMEM) |
| 2540 | rc = -ENOENT; |
Dan Carpenter | 8b99f35 | 2021-08-02 08:14:03 +0900 | [diff] [blame] | 2541 | name = NULL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2542 | goto err_out1; |
| 2543 | } |
| 2544 | |
| 2545 | ksmbd_debug(SMB, "converted name = %s\n", name); |
| 2546 | if (strchr(name, ':')) { |
| 2547 | if (!test_share_config_flag(work->tcon->share_conf, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2548 | KSMBD_SHARE_FLAG_STREAMS)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2549 | rc = -EBADF; |
| 2550 | goto err_out1; |
| 2551 | } |
| 2552 | rc = parse_stream_name(name, &stream_name, &s_type); |
| 2553 | if (rc < 0) |
| 2554 | goto err_out1; |
| 2555 | } |
| 2556 | |
| 2557 | rc = ksmbd_validate_filename(name); |
| 2558 | if (rc < 0) |
| 2559 | goto err_out1; |
| 2560 | |
| 2561 | if (ksmbd_share_veto_filename(share, name)) { |
| 2562 | rc = -ENOENT; |
| 2563 | ksmbd_debug(SMB, "Reject open(), vetoed file: %s\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2564 | name); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2565 | goto err_out1; |
| 2566 | } |
| 2567 | } else { |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 2568 | name = kstrdup("", GFP_KERNEL); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2569 | if (!name) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2570 | rc = -ENOMEM; |
| 2571 | goto err_out1; |
| 2572 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2573 | } |
| 2574 | |
| 2575 | req_op_level = req->RequestedOplockLevel; |
Namjae Jeon | 73f9dad | 2021-04-16 14:12:06 +0900 | [diff] [blame] | 2576 | if (req_op_level == SMB2_OPLOCK_LEVEL_LEASE) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2577 | lc = parse_lease_state(req); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2578 | |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 2579 | if (le32_to_cpu(req->ImpersonationLevel) > le32_to_cpu(IL_DELEGATE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2580 | pr_err("Invalid impersonationlevel : 0x%x\n", |
| 2581 | le32_to_cpu(req->ImpersonationLevel)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2582 | rc = -EIO; |
| 2583 | rsp->hdr.Status = STATUS_BAD_IMPERSONATION_LEVEL; |
| 2584 | goto err_out1; |
| 2585 | } |
| 2586 | |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 2587 | if (req->CreateOptions && !(req->CreateOptions & CREATE_OPTIONS_MASK_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2588 | pr_err("Invalid create options : 0x%x\n", |
| 2589 | le32_to_cpu(req->CreateOptions)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2590 | rc = -EINVAL; |
| 2591 | goto err_out1; |
| 2592 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2593 | if (req->CreateOptions & FILE_SEQUENTIAL_ONLY_LE && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2594 | req->CreateOptions & FILE_RANDOM_ACCESS_LE) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2595 | req->CreateOptions = ~(FILE_SEQUENTIAL_ONLY_LE); |
| 2596 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2597 | if (req->CreateOptions & |
| 2598 | (FILE_OPEN_BY_FILE_ID_LE | CREATE_TREE_CONNECTION | |
| 2599 | FILE_RESERVE_OPFILTER_LE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2600 | rc = -EOPNOTSUPP; |
| 2601 | goto err_out1; |
| 2602 | } |
| 2603 | |
| 2604 | if (req->CreateOptions & FILE_DIRECTORY_FILE_LE) { |
| 2605 | if (req->CreateOptions & FILE_NON_DIRECTORY_FILE_LE) { |
| 2606 | rc = -EINVAL; |
| 2607 | goto err_out1; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2608 | } else if (req->CreateOptions & FILE_NO_COMPRESSION_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2609 | req->CreateOptions = ~(FILE_NO_COMPRESSION_LE); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2610 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2611 | } |
| 2612 | } |
| 2613 | |
| 2614 | if (le32_to_cpu(req->CreateDisposition) > |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2615 | le32_to_cpu(FILE_OVERWRITE_IF_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2616 | pr_err("Invalid create disposition : 0x%x\n", |
| 2617 | le32_to_cpu(req->CreateDisposition)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2618 | rc = -EINVAL; |
| 2619 | goto err_out1; |
| 2620 | } |
| 2621 | |
| 2622 | if (!(req->DesiredAccess & DESIRED_ACCESS_MASK)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2623 | pr_err("Invalid desired access : 0x%x\n", |
| 2624 | le32_to_cpu(req->DesiredAccess)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2625 | rc = -EACCES; |
| 2626 | goto err_out1; |
| 2627 | } |
| 2628 | |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 2629 | if (req->FileAttributes && !(req->FileAttributes & FILE_ATTRIBUTE_MASK_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2630 | pr_err("Invalid file attribute : 0x%x\n", |
| 2631 | le32_to_cpu(req->FileAttributes)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2632 | rc = -EINVAL; |
| 2633 | goto err_out1; |
| 2634 | } |
| 2635 | |
| 2636 | if (req->CreateContextsOffset) { |
| 2637 | /* Parse non-durable handle create contexts */ |
| 2638 | context = smb2_find_context_vals(req, SMB2_CREATE_EA_BUFFER); |
Namjae Jeon | f19b396 | 2021-07-13 09:59:34 +0900 | [diff] [blame] | 2639 | if (IS_ERR(context)) { |
| 2640 | rc = PTR_ERR(context); |
| 2641 | goto err_out1; |
| 2642 | } else if (context) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2643 | ea_buf = (struct create_ea_buf_req *)context; |
Hyunchul Lee | 8f77150 | 2021-09-24 22:22:22 +0900 | [diff] [blame] | 2644 | if (le16_to_cpu(context->DataOffset) + |
| 2645 | le32_to_cpu(context->DataLength) < |
| 2646 | sizeof(struct create_ea_buf_req)) { |
| 2647 | rc = -EINVAL; |
| 2648 | goto err_out1; |
| 2649 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2650 | if (req->CreateOptions & FILE_NO_EA_KNOWLEDGE_LE) { |
| 2651 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 2652 | rc = -EACCES; |
| 2653 | goto err_out1; |
| 2654 | } |
| 2655 | } |
| 2656 | |
| 2657 | context = smb2_find_context_vals(req, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2658 | SMB2_CREATE_QUERY_MAXIMAL_ACCESS_REQUEST); |
Namjae Jeon | f19b396 | 2021-07-13 09:59:34 +0900 | [diff] [blame] | 2659 | if (IS_ERR(context)) { |
| 2660 | rc = PTR_ERR(context); |
| 2661 | goto err_out1; |
| 2662 | } else if (context) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2663 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2664 | "get query maximal access context\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2665 | maximal_access_ctxt = 1; |
| 2666 | } |
| 2667 | |
| 2668 | context = smb2_find_context_vals(req, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2669 | SMB2_CREATE_TIMEWARP_REQUEST); |
Namjae Jeon | f19b396 | 2021-07-13 09:59:34 +0900 | [diff] [blame] | 2670 | if (IS_ERR(context)) { |
| 2671 | rc = PTR_ERR(context); |
| 2672 | goto err_out1; |
| 2673 | } else if (context) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2674 | ksmbd_debug(SMB, "get timewarp context\n"); |
| 2675 | rc = -EBADF; |
| 2676 | goto err_out1; |
| 2677 | } |
| 2678 | |
| 2679 | if (tcon->posix_extensions) { |
| 2680 | context = smb2_find_context_vals(req, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2681 | SMB2_CREATE_TAG_POSIX); |
Namjae Jeon | f19b396 | 2021-07-13 09:59:34 +0900 | [diff] [blame] | 2682 | if (IS_ERR(context)) { |
| 2683 | rc = PTR_ERR(context); |
| 2684 | goto err_out1; |
| 2685 | } else if (context) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2686 | struct create_posix *posix = |
| 2687 | (struct create_posix *)context; |
Hyunchul Lee | 8f77150 | 2021-09-24 22:22:22 +0900 | [diff] [blame] | 2688 | if (le16_to_cpu(context->DataOffset) + |
| 2689 | le32_to_cpu(context->DataLength) < |
| 2690 | sizeof(struct create_posix)) { |
| 2691 | rc = -EINVAL; |
| 2692 | goto err_out1; |
| 2693 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2694 | ksmbd_debug(SMB, "get posix context\n"); |
| 2695 | |
| 2696 | posix_mode = le32_to_cpu(posix->Mode); |
| 2697 | posix_ctxt = 1; |
| 2698 | } |
| 2699 | } |
| 2700 | } |
| 2701 | |
| 2702 | if (ksmbd_override_fsids(work)) { |
| 2703 | rc = -ENOMEM; |
| 2704 | goto err_out1; |
| 2705 | } |
| 2706 | |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 2707 | rc = ksmbd_vfs_kern_path(work, name, LOOKUP_NO_SYMLINKS, &path, 1); |
Namjae Jeon | 4ea4779 | 2021-09-21 14:19:33 +0900 | [diff] [blame] | 2708 | if (!rc) { |
| 2709 | if (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2710 | /* |
| 2711 | * If file exists with under flags, return access |
| 2712 | * denied error. |
| 2713 | */ |
| 2714 | if (req->CreateDisposition == FILE_OVERWRITE_IF_LE || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2715 | req->CreateDisposition == FILE_OPEN_IF_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2716 | rc = -EACCES; |
| 2717 | path_put(&path); |
| 2718 | goto err_out; |
| 2719 | } |
| 2720 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2721 | if (!test_tree_conn_flag(tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2722 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2723 | "User does not have write permission\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2724 | rc = -EACCES; |
| 2725 | path_put(&path); |
| 2726 | goto err_out; |
| 2727 | } |
Namjae Jeon | 4ea4779 | 2021-09-21 14:19:33 +0900 | [diff] [blame] | 2728 | } else if (d_is_symlink(path.dentry)) { |
| 2729 | rc = -EACCES; |
| 2730 | path_put(&path); |
| 2731 | goto err_out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2732 | } |
| 2733 | } |
| 2734 | |
| 2735 | if (rc) { |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 2736 | if (rc != -ENOENT) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2737 | goto err_out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2738 | 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] | 2739 | name, rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2740 | rc = 0; |
| 2741 | } else { |
| 2742 | file_present = true; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2743 | user_ns = mnt_user_ns(path.mnt); |
| 2744 | generic_fillattr(user_ns, d_inode(path.dentry), &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2745 | } |
| 2746 | if (stream_name) { |
| 2747 | if (req->CreateOptions & FILE_DIRECTORY_FILE_LE) { |
| 2748 | if (s_type == DATA_STREAM) { |
| 2749 | rc = -EIO; |
| 2750 | rsp->hdr.Status = STATUS_NOT_A_DIRECTORY; |
| 2751 | } |
| 2752 | } else { |
| 2753 | if (S_ISDIR(stat.mode) && s_type == DATA_STREAM) { |
| 2754 | rc = -EIO; |
| 2755 | rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY; |
| 2756 | } |
| 2757 | } |
| 2758 | |
| 2759 | if (req->CreateOptions & FILE_DIRECTORY_FILE_LE && |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 2760 | req->FileAttributes & FILE_ATTRIBUTE_NORMAL_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2761 | rsp->hdr.Status = STATUS_NOT_A_DIRECTORY; |
| 2762 | rc = -EIO; |
| 2763 | } |
| 2764 | |
| 2765 | if (rc < 0) |
| 2766 | goto err_out; |
| 2767 | } |
| 2768 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2769 | if (file_present && req->CreateOptions & FILE_NON_DIRECTORY_FILE_LE && |
| 2770 | S_ISDIR(stat.mode) && !(req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2771 | ksmbd_debug(SMB, "open() argument is a directory: %s, %x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2772 | name, req->CreateOptions); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2773 | rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY; |
| 2774 | rc = -EIO; |
| 2775 | goto err_out; |
| 2776 | } |
| 2777 | |
| 2778 | if (file_present && (req->CreateOptions & FILE_DIRECTORY_FILE_LE) && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2779 | !(req->CreateDisposition == FILE_CREATE_LE) && |
| 2780 | !S_ISDIR(stat.mode)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2781 | rsp->hdr.Status = STATUS_NOT_A_DIRECTORY; |
| 2782 | rc = -EIO; |
| 2783 | goto err_out; |
| 2784 | } |
| 2785 | |
| 2786 | if (!stream_name && file_present && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2787 | req->CreateDisposition == FILE_CREATE_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2788 | rc = -EEXIST; |
| 2789 | goto err_out; |
| 2790 | } |
| 2791 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2792 | daccess = smb_map_generic_desired_access(req->DesiredAccess); |
| 2793 | |
| 2794 | if (file_present && !(req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) { |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 2795 | rc = smb_check_perm_dacl(conn, &path, &daccess, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2796 | sess->user->uid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2797 | if (rc) |
| 2798 | goto err_out; |
| 2799 | } |
| 2800 | |
| 2801 | if (daccess & FILE_MAXIMAL_ACCESS_LE) { |
| 2802 | if (!file_present) { |
| 2803 | daccess = cpu_to_le32(GENERIC_ALL_FLAGS); |
| 2804 | } else { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2805 | rc = ksmbd_vfs_query_maximal_access(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2806 | path.dentry, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2807 | &daccess); |
| 2808 | if (rc) |
| 2809 | goto err_out; |
| 2810 | already_permitted = true; |
| 2811 | } |
| 2812 | maximal_access = daccess; |
| 2813 | } |
| 2814 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2815 | open_flags = smb2_create_open_flags(file_present, daccess, |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 2816 | req->CreateDisposition, |
| 2817 | &may_flags); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2818 | |
| 2819 | if (!test_tree_conn_flag(tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
| 2820 | if (open_flags & O_CREAT) { |
| 2821 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2822 | "User does not have write permission\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2823 | rc = -EACCES; |
| 2824 | goto err_out; |
| 2825 | } |
| 2826 | } |
| 2827 | |
| 2828 | /*create file if not present */ |
| 2829 | if (!file_present) { |
| 2830 | rc = smb2_creat(work, &path, name, open_flags, posix_mode, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2831 | req->CreateOptions & FILE_DIRECTORY_FILE_LE); |
Marios Makassikis | d337a44 | 2021-07-27 09:24:51 +0900 | [diff] [blame] | 2832 | if (rc) { |
| 2833 | if (rc == -ENOENT) { |
| 2834 | rc = -EIO; |
| 2835 | rsp->hdr.Status = STATUS_OBJECT_PATH_NOT_FOUND; |
| 2836 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2837 | goto err_out; |
Marios Makassikis | d337a44 | 2021-07-27 09:24:51 +0900 | [diff] [blame] | 2838 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2839 | |
| 2840 | created = true; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2841 | user_ns = mnt_user_ns(path.mnt); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2842 | if (ea_buf) { |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 2843 | if (le32_to_cpu(ea_buf->ccontext.DataLength) < |
| 2844 | sizeof(struct smb2_ea_info)) { |
| 2845 | rc = -EINVAL; |
| 2846 | goto err_out; |
| 2847 | } |
| 2848 | |
| 2849 | rc = smb2_set_ea(&ea_buf->ea, |
| 2850 | le32_to_cpu(ea_buf->ccontext.DataLength), |
| 2851 | &path); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2852 | if (rc == -EOPNOTSUPP) |
| 2853 | rc = 0; |
| 2854 | else if (rc) |
| 2855 | goto err_out; |
| 2856 | } |
| 2857 | } else if (!already_permitted) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2858 | /* FILE_READ_ATTRIBUTE is allowed without inode_permission, |
| 2859 | * because execute(search) permission on a parent directory, |
| 2860 | * is already granted. |
| 2861 | */ |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2862 | if (daccess & ~(FILE_READ_ATTRIBUTES_LE | FILE_READ_CONTROL_LE)) { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2863 | rc = inode_permission(user_ns, |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 2864 | d_inode(path.dentry), |
| 2865 | may_flags); |
Namjae Jeon | ff1d572 | 2021-04-13 13:18:10 +0900 | [diff] [blame] | 2866 | if (rc) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2867 | goto err_out; |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 2868 | |
| 2869 | if ((daccess & FILE_DELETE_LE) || |
| 2870 | (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2871 | rc = ksmbd_vfs_may_delete(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2872 | path.dentry); |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 2873 | if (rc) |
| 2874 | goto err_out; |
| 2875 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2876 | } |
| 2877 | } |
| 2878 | |
| 2879 | rc = ksmbd_query_inode_status(d_inode(path.dentry->d_parent)); |
| 2880 | if (rc == KSMBD_INODE_STATUS_PENDING_DELETE) { |
| 2881 | rc = -EBUSY; |
| 2882 | goto err_out; |
| 2883 | } |
| 2884 | |
| 2885 | rc = 0; |
| 2886 | filp = dentry_open(&path, open_flags, current_cred()); |
| 2887 | if (IS_ERR(filp)) { |
| 2888 | rc = PTR_ERR(filp); |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2889 | pr_err("dentry open for dir failed, rc %d\n", rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2890 | goto err_out; |
| 2891 | } |
| 2892 | |
| 2893 | if (file_present) { |
| 2894 | if (!(open_flags & O_TRUNC)) |
| 2895 | file_info = FILE_OPENED; |
| 2896 | else |
| 2897 | file_info = FILE_OVERWRITTEN; |
| 2898 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2899 | if ((req->CreateDisposition & FILE_CREATE_MASK_LE) == |
| 2900 | FILE_SUPERSEDE_LE) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2901 | file_info = FILE_SUPERSEDED; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2902 | } else if (open_flags & O_CREAT) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2903 | file_info = FILE_CREATED; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2904 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2905 | |
| 2906 | ksmbd_vfs_set_fadvise(filp, req->CreateOptions); |
| 2907 | |
| 2908 | /* Obtain Volatile-ID */ |
| 2909 | fp = ksmbd_open_fd(work, filp); |
| 2910 | if (IS_ERR(fp)) { |
| 2911 | fput(filp); |
| 2912 | rc = PTR_ERR(fp); |
| 2913 | fp = NULL; |
| 2914 | goto err_out; |
| 2915 | } |
| 2916 | |
| 2917 | /* Get Persistent-ID */ |
| 2918 | ksmbd_open_durable_fd(fp); |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 2919 | if (!has_file_id(fp->persistent_id)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2920 | rc = -ENOMEM; |
| 2921 | goto err_out; |
| 2922 | } |
| 2923 | |
| 2924 | fp->filename = name; |
| 2925 | fp->cdoption = req->CreateDisposition; |
| 2926 | fp->daccess = daccess; |
| 2927 | fp->saccess = req->ShareAccess; |
| 2928 | fp->coption = req->CreateOptions; |
| 2929 | |
| 2930 | /* Set default windows and posix acls if creating new file */ |
| 2931 | if (created) { |
| 2932 | int posix_acl_rc; |
Namjae Jeon | fba08fa | 2021-04-15 10:29:39 +0900 | [diff] [blame] | 2933 | struct inode *inode = d_inode(path.dentry); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2934 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2935 | posix_acl_rc = ksmbd_vfs_inherit_posix_acl(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2936 | inode, |
| 2937 | d_inode(path.dentry->d_parent)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2938 | if (posix_acl_rc) |
| 2939 | ksmbd_debug(SMB, "inherit posix acl failed : %d\n", posix_acl_rc); |
| 2940 | |
| 2941 | if (test_share_config_flag(work->tcon->share_conf, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2942 | KSMBD_SHARE_FLAG_ACL_XATTR)) { |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 2943 | rc = smb_inherit_dacl(conn, &path, sess->user->uid, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2944 | sess->user->gid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2945 | } |
| 2946 | |
| 2947 | if (rc) { |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 2948 | rc = smb2_create_sd_buffer(work, req, &path); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2949 | if (rc) { |
| 2950 | if (posix_acl_rc) |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2951 | ksmbd_vfs_set_init_posix_acl(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2952 | inode); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2953 | |
| 2954 | if (test_share_config_flag(work->tcon->share_conf, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2955 | KSMBD_SHARE_FLAG_ACL_XATTR)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2956 | struct smb_fattr fattr; |
| 2957 | struct smb_ntsd *pntsd; |
Namjae Jeon | 3d47e54 | 2021-04-20 14:25:35 +0900 | [diff] [blame] | 2958 | int pntsd_size, ace_num = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2959 | |
Christian Brauner | 43205ca | 2021-08-23 17:13:50 +0200 | [diff] [blame] | 2960 | ksmbd_acls_fattr(&fattr, user_ns, inode); |
Marios Makassikis | e6b1059 | 2021-04-15 10:24:56 +0900 | [diff] [blame] | 2961 | if (fattr.cf_acls) |
| 2962 | ace_num = fattr.cf_acls->a_count; |
Namjae Jeon | 3d47e54 | 2021-04-20 14:25:35 +0900 | [diff] [blame] | 2963 | if (fattr.cf_dacls) |
| 2964 | ace_num += fattr.cf_dacls->a_count; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2965 | |
| 2966 | pntsd = kmalloc(sizeof(struct smb_ntsd) + |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2967 | sizeof(struct smb_sid) * 3 + |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2968 | sizeof(struct smb_acl) + |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2969 | sizeof(struct smb_ace) * ace_num * 2, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2970 | GFP_KERNEL); |
| 2971 | if (!pntsd) |
| 2972 | goto err_out; |
| 2973 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2974 | rc = build_sec_desc(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2975 | pntsd, NULL, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2976 | OWNER_SECINFO | |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2977 | GROUP_SECINFO | |
| 2978 | DACL_SECINFO, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2979 | &pntsd_size, &fattr); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2980 | posix_acl_release(fattr.cf_acls); |
| 2981 | posix_acl_release(fattr.cf_dacls); |
Namjae Jeon | f2e78af | 2021-12-01 10:12:39 +0900 | [diff] [blame] | 2982 | if (rc) { |
| 2983 | kfree(pntsd); |
| 2984 | goto err_out; |
| 2985 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2986 | |
| 2987 | rc = ksmbd_vfs_set_sd_xattr(conn, |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2988 | user_ns, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2989 | path.dentry, |
| 2990 | pntsd, |
| 2991 | pntsd_size); |
Namjae Jeon | 3d47e54 | 2021-04-20 14:25:35 +0900 | [diff] [blame] | 2992 | kfree(pntsd); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2993 | if (rc) |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2994 | pr_err("failed to store ntacl in xattr : %d\n", |
| 2995 | rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2996 | } |
| 2997 | } |
| 2998 | } |
| 2999 | rc = 0; |
| 3000 | } |
| 3001 | |
| 3002 | if (stream_name) { |
| 3003 | rc = smb2_set_stream_name_xattr(&path, |
| 3004 | fp, |
| 3005 | stream_name, |
| 3006 | s_type); |
| 3007 | if (rc) |
| 3008 | goto err_out; |
| 3009 | file_info = FILE_CREATED; |
| 3010 | } |
| 3011 | |
| 3012 | fp->attrib_only = !(req->DesiredAccess & ~(FILE_READ_ATTRIBUTES_LE | |
| 3013 | FILE_WRITE_ATTRIBUTES_LE | FILE_SYNCHRONIZE_LE)); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3014 | if (!S_ISDIR(file_inode(filp)->i_mode) && open_flags & O_TRUNC && |
| 3015 | !fp->attrib_only && !stream_name) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3016 | smb_break_all_oplock(work, fp); |
| 3017 | need_truncate = 1; |
| 3018 | } |
| 3019 | |
| 3020 | /* fp should be searchable through ksmbd_inode.m_fp_list |
| 3021 | * after daccess, saccess, attrib_only, and stream are |
| 3022 | * initialized. |
| 3023 | */ |
| 3024 | write_lock(&fp->f_ci->m_lock); |
| 3025 | list_add(&fp->node, &fp->f_ci->m_fp_list); |
| 3026 | write_unlock(&fp->f_ci->m_lock); |
| 3027 | |
| 3028 | rc = ksmbd_vfs_getattr(&path, &stat); |
| 3029 | if (rc) { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 3030 | generic_fillattr(user_ns, d_inode(path.dentry), &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3031 | rc = 0; |
| 3032 | } |
| 3033 | |
| 3034 | /* Check delete pending among previous fp before oplock break */ |
| 3035 | if (ksmbd_inode_pending_delete(fp)) { |
| 3036 | rc = -EBUSY; |
| 3037 | goto err_out; |
| 3038 | } |
| 3039 | |
| 3040 | share_ret = ksmbd_smb_check_shared_mode(fp->filp, fp); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3041 | if (!test_share_config_flag(work->tcon->share_conf, KSMBD_SHARE_FLAG_OPLOCKS) || |
| 3042 | (req_op_level == SMB2_OPLOCK_LEVEL_LEASE && |
| 3043 | !(conn->vals->capabilities & SMB2_GLOBAL_CAP_LEASING))) { |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 3044 | if (share_ret < 0 && !S_ISDIR(file_inode(fp->filp)->i_mode)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3045 | rc = share_ret; |
| 3046 | goto err_out; |
| 3047 | } |
| 3048 | } else { |
| 3049 | if (req_op_level == SMB2_OPLOCK_LEVEL_LEASE) { |
| 3050 | req_op_level = smb2_map_lease_to_oplock(lc->req_state); |
| 3051 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3052 | "lease req for(%s) req oplock state 0x%x, lease state 0x%x\n", |
| 3053 | name, req_op_level, lc->req_state); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3054 | rc = find_same_lease_key(sess, fp->f_ci, lc); |
| 3055 | if (rc) |
| 3056 | goto err_out; |
| 3057 | } else if (open_flags == O_RDONLY && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3058 | (req_op_level == SMB2_OPLOCK_LEVEL_BATCH || |
| 3059 | req_op_level == SMB2_OPLOCK_LEVEL_EXCLUSIVE)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3060 | req_op_level = SMB2_OPLOCK_LEVEL_II; |
| 3061 | |
| 3062 | rc = smb_grant_oplock(work, req_op_level, |
| 3063 | fp->persistent_id, fp, |
| 3064 | le32_to_cpu(req->hdr.Id.SyncId.TreeId), |
| 3065 | lc, share_ret); |
| 3066 | if (rc < 0) |
| 3067 | goto err_out; |
| 3068 | } |
| 3069 | |
| 3070 | if (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE) |
| 3071 | ksmbd_fd_set_delete_on_close(fp, file_info); |
| 3072 | |
| 3073 | if (need_truncate) { |
| 3074 | rc = smb2_create_truncate(&path); |
| 3075 | if (rc) |
| 3076 | goto err_out; |
| 3077 | } |
| 3078 | |
| 3079 | if (req->CreateContextsOffset) { |
| 3080 | struct create_alloc_size_req *az_req; |
| 3081 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3082 | az_req = (struct create_alloc_size_req *)smb2_find_context_vals(req, |
| 3083 | SMB2_CREATE_ALLOCATION_SIZE); |
Namjae Jeon | f19b396 | 2021-07-13 09:59:34 +0900 | [diff] [blame] | 3084 | if (IS_ERR(az_req)) { |
| 3085 | rc = PTR_ERR(az_req); |
| 3086 | goto err_out; |
| 3087 | } else if (az_req) { |
Hyunchul Lee | 8f77150 | 2021-09-24 22:22:22 +0900 | [diff] [blame] | 3088 | loff_t alloc_size; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3089 | int err; |
| 3090 | |
Hyunchul Lee | 8f77150 | 2021-09-24 22:22:22 +0900 | [diff] [blame] | 3091 | if (le16_to_cpu(az_req->ccontext.DataOffset) + |
| 3092 | le32_to_cpu(az_req->ccontext.DataLength) < |
| 3093 | sizeof(struct create_alloc_size_req)) { |
| 3094 | rc = -EINVAL; |
| 3095 | goto err_out; |
| 3096 | } |
| 3097 | alloc_size = le64_to_cpu(az_req->AllocationSize); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3098 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3099 | "request smb2 create allocate size : %llu\n", |
| 3100 | alloc_size); |
Namjae Jeon | e8c0619 | 2021-06-22 11:06:11 +0900 | [diff] [blame] | 3101 | smb_break_all_levII_oplock(work, fp, 1); |
| 3102 | err = vfs_fallocate(fp->filp, FALLOC_FL_KEEP_SIZE, 0, |
| 3103 | alloc_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3104 | if (err < 0) |
| 3105 | ksmbd_debug(SMB, |
Namjae Jeon | e8c0619 | 2021-06-22 11:06:11 +0900 | [diff] [blame] | 3106 | "vfs_fallocate is failed : %d\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3107 | err); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3108 | } |
| 3109 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3110 | context = smb2_find_context_vals(req, SMB2_CREATE_QUERY_ON_DISK_ID); |
Namjae Jeon | f19b396 | 2021-07-13 09:59:34 +0900 | [diff] [blame] | 3111 | if (IS_ERR(context)) { |
| 3112 | rc = PTR_ERR(context); |
| 3113 | goto err_out; |
| 3114 | } else if (context) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3115 | ksmbd_debug(SMB, "get query on disk id context\n"); |
| 3116 | query_disk_id = 1; |
| 3117 | } |
| 3118 | } |
| 3119 | |
| 3120 | if (stat.result_mask & STATX_BTIME) |
| 3121 | fp->create_time = ksmbd_UnixTimeToNT(stat.btime); |
| 3122 | else |
| 3123 | fp->create_time = ksmbd_UnixTimeToNT(stat.ctime); |
| 3124 | if (req->FileAttributes || fp->f_ci->m_fattr == 0) |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3125 | fp->f_ci->m_fattr = |
| 3126 | 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] | 3127 | |
| 3128 | if (!created) |
| 3129 | smb2_update_xattrs(tcon, &path, fp); |
| 3130 | else |
| 3131 | smb2_new_xattrs(tcon, &path, fp); |
| 3132 | |
| 3133 | memcpy(fp->client_guid, conn->ClientGUID, SMB2_CLIENT_GUID_SIZE); |
| 3134 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 3135 | generic_fillattr(user_ns, file_inode(fp->filp), |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 3136 | &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3137 | |
| 3138 | rsp->StructureSize = cpu_to_le16(89); |
| 3139 | rcu_read_lock(); |
| 3140 | opinfo = rcu_dereference(fp->f_opinfo); |
| 3141 | rsp->OplockLevel = opinfo != NULL ? opinfo->level : 0; |
| 3142 | rcu_read_unlock(); |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 3143 | rsp->Flags = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3144 | rsp->CreateAction = cpu_to_le32(file_info); |
| 3145 | rsp->CreationTime = cpu_to_le64(fp->create_time); |
| 3146 | time = ksmbd_UnixTimeToNT(stat.atime); |
| 3147 | rsp->LastAccessTime = cpu_to_le64(time); |
| 3148 | time = ksmbd_UnixTimeToNT(stat.mtime); |
| 3149 | rsp->LastWriteTime = cpu_to_le64(time); |
| 3150 | time = ksmbd_UnixTimeToNT(stat.ctime); |
| 3151 | rsp->ChangeTime = cpu_to_le64(time); |
| 3152 | rsp->AllocationSize = S_ISDIR(stat.mode) ? 0 : |
| 3153 | cpu_to_le64(stat.blocks << 9); |
| 3154 | rsp->EndofFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size); |
| 3155 | rsp->FileAttributes = fp->f_ci->m_fattr; |
| 3156 | |
| 3157 | rsp->Reserved2 = 0; |
| 3158 | |
| 3159 | rsp->PersistentFileId = cpu_to_le64(fp->persistent_id); |
| 3160 | rsp->VolatileFileId = cpu_to_le64(fp->volatile_id); |
| 3161 | |
| 3162 | rsp->CreateContextsOffset = 0; |
| 3163 | rsp->CreateContextsLength = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 3164 | inc_rfc1001_len(work->response_buf, 88); /* StructureSize - 1*/ |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3165 | |
| 3166 | /* If lease is request send lease context response */ |
| 3167 | if (opinfo && opinfo->is_lease) { |
| 3168 | struct create_context *lease_ccontext; |
| 3169 | |
| 3170 | ksmbd_debug(SMB, "lease granted on(%s) lease state 0x%x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3171 | name, opinfo->o_lease->state); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3172 | rsp->OplockLevel = SMB2_OPLOCK_LEVEL_LEASE; |
| 3173 | |
| 3174 | lease_ccontext = (struct create_context *)rsp->Buffer; |
| 3175 | contxt_cnt++; |
| 3176 | create_lease_buf(rsp->Buffer, opinfo->o_lease); |
| 3177 | le32_add_cpu(&rsp->CreateContextsLength, |
| 3178 | conn->vals->create_lease_size); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 3179 | inc_rfc1001_len(work->response_buf, |
| 3180 | conn->vals->create_lease_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3181 | next_ptr = &lease_ccontext->Next; |
| 3182 | next_off = conn->vals->create_lease_size; |
| 3183 | } |
| 3184 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3185 | if (maximal_access_ctxt) { |
| 3186 | struct create_context *mxac_ccontext; |
| 3187 | |
| 3188 | if (maximal_access == 0) |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 3189 | ksmbd_vfs_query_maximal_access(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 3190 | path.dentry, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3191 | &maximal_access); |
| 3192 | mxac_ccontext = (struct create_context *)(rsp->Buffer + |
| 3193 | le32_to_cpu(rsp->CreateContextsLength)); |
| 3194 | contxt_cnt++; |
| 3195 | create_mxac_rsp_buf(rsp->Buffer + |
| 3196 | le32_to_cpu(rsp->CreateContextsLength), |
| 3197 | le32_to_cpu(maximal_access)); |
| 3198 | le32_add_cpu(&rsp->CreateContextsLength, |
| 3199 | conn->vals->create_mxac_size); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 3200 | inc_rfc1001_len(work->response_buf, |
| 3201 | conn->vals->create_mxac_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3202 | if (next_ptr) |
| 3203 | *next_ptr = cpu_to_le32(next_off); |
| 3204 | next_ptr = &mxac_ccontext->Next; |
| 3205 | next_off = conn->vals->create_mxac_size; |
| 3206 | } |
| 3207 | |
| 3208 | if (query_disk_id) { |
| 3209 | struct create_context *disk_id_ccontext; |
| 3210 | |
| 3211 | disk_id_ccontext = (struct create_context *)(rsp->Buffer + |
| 3212 | le32_to_cpu(rsp->CreateContextsLength)); |
| 3213 | contxt_cnt++; |
| 3214 | create_disk_id_rsp_buf(rsp->Buffer + |
| 3215 | le32_to_cpu(rsp->CreateContextsLength), |
| 3216 | stat.ino, tcon->id); |
| 3217 | le32_add_cpu(&rsp->CreateContextsLength, |
| 3218 | conn->vals->create_disk_id_size); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 3219 | inc_rfc1001_len(work->response_buf, |
| 3220 | conn->vals->create_disk_id_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3221 | if (next_ptr) |
| 3222 | *next_ptr = cpu_to_le32(next_off); |
| 3223 | next_ptr = &disk_id_ccontext->Next; |
| 3224 | next_off = conn->vals->create_disk_id_size; |
| 3225 | } |
| 3226 | |
| 3227 | if (posix_ctxt) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3228 | contxt_cnt++; |
| 3229 | create_posix_rsp_buf(rsp->Buffer + |
| 3230 | le32_to_cpu(rsp->CreateContextsLength), |
| 3231 | fp); |
| 3232 | le32_add_cpu(&rsp->CreateContextsLength, |
| 3233 | conn->vals->create_posix_size); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 3234 | inc_rfc1001_len(work->response_buf, |
| 3235 | conn->vals->create_posix_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3236 | if (next_ptr) |
| 3237 | *next_ptr = cpu_to_le32(next_off); |
| 3238 | } |
| 3239 | |
| 3240 | if (contxt_cnt > 0) { |
| 3241 | rsp->CreateContextsOffset = |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 3242 | cpu_to_le32(offsetof(struct smb2_create_rsp, Buffer)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3243 | } |
| 3244 | |
| 3245 | err_out: |
| 3246 | if (file_present || created) |
| 3247 | path_put(&path); |
| 3248 | ksmbd_revert_fsids(work); |
| 3249 | err_out1: |
| 3250 | if (rc) { |
| 3251 | if (rc == -EINVAL) |
| 3252 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 3253 | else if (rc == -EOPNOTSUPP) |
| 3254 | rsp->hdr.Status = STATUS_NOT_SUPPORTED; |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 3255 | else if (rc == -EACCES || rc == -ESTALE || rc == -EXDEV) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3256 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 3257 | else if (rc == -ENOENT) |
| 3258 | rsp->hdr.Status = STATUS_OBJECT_NAME_INVALID; |
| 3259 | else if (rc == -EPERM) |
| 3260 | rsp->hdr.Status = STATUS_SHARING_VIOLATION; |
| 3261 | else if (rc == -EBUSY) |
| 3262 | rsp->hdr.Status = STATUS_DELETE_PENDING; |
| 3263 | else if (rc == -EBADF) |
| 3264 | rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND; |
| 3265 | else if (rc == -ENOEXEC) |
| 3266 | rsp->hdr.Status = STATUS_DUPLICATE_OBJECTID; |
| 3267 | else if (rc == -ENXIO) |
| 3268 | rsp->hdr.Status = STATUS_NO_SUCH_DEVICE; |
| 3269 | else if (rc == -EEXIST) |
| 3270 | rsp->hdr.Status = STATUS_OBJECT_NAME_COLLISION; |
| 3271 | else if (rc == -EMFILE) |
| 3272 | rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES; |
| 3273 | if (!rsp->hdr.Status) |
| 3274 | rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR; |
| 3275 | |
| 3276 | if (!fp || !fp->filename) |
| 3277 | kfree(name); |
| 3278 | if (fp) |
| 3279 | ksmbd_fd_put(work, fp); |
| 3280 | smb2_set_err_rsp(work); |
| 3281 | ksmbd_debug(SMB, "Error response: %x\n", rsp->hdr.Status); |
| 3282 | } |
| 3283 | |
| 3284 | kfree(lc); |
| 3285 | |
| 3286 | return 0; |
| 3287 | } |
| 3288 | |
| 3289 | static int readdir_info_level_struct_sz(int info_level) |
| 3290 | { |
| 3291 | switch (info_level) { |
| 3292 | case FILE_FULL_DIRECTORY_INFORMATION: |
| 3293 | return sizeof(struct file_full_directory_info); |
| 3294 | case FILE_BOTH_DIRECTORY_INFORMATION: |
| 3295 | return sizeof(struct file_both_directory_info); |
| 3296 | case FILE_DIRECTORY_INFORMATION: |
| 3297 | return sizeof(struct file_directory_info); |
| 3298 | case FILE_NAMES_INFORMATION: |
| 3299 | return sizeof(struct file_names_info); |
| 3300 | case FILEID_FULL_DIRECTORY_INFORMATION: |
| 3301 | return sizeof(struct file_id_full_dir_info); |
| 3302 | case FILEID_BOTH_DIRECTORY_INFORMATION: |
| 3303 | return sizeof(struct file_id_both_directory_info); |
| 3304 | case SMB_FIND_FILE_POSIX_INFO: |
| 3305 | return sizeof(struct smb2_posix_info); |
| 3306 | default: |
| 3307 | return -EOPNOTSUPP; |
| 3308 | } |
| 3309 | } |
| 3310 | |
| 3311 | static int dentry_name(struct ksmbd_dir_info *d_info, int info_level) |
| 3312 | { |
| 3313 | switch (info_level) { |
| 3314 | case FILE_FULL_DIRECTORY_INFORMATION: |
| 3315 | { |
| 3316 | struct file_full_directory_info *ffdinfo; |
| 3317 | |
| 3318 | ffdinfo = (struct file_full_directory_info *)d_info->rptr; |
| 3319 | d_info->rptr += le32_to_cpu(ffdinfo->NextEntryOffset); |
| 3320 | d_info->name = ffdinfo->FileName; |
| 3321 | d_info->name_len = le32_to_cpu(ffdinfo->FileNameLength); |
| 3322 | return 0; |
| 3323 | } |
| 3324 | case FILE_BOTH_DIRECTORY_INFORMATION: |
| 3325 | { |
| 3326 | struct file_both_directory_info *fbdinfo; |
| 3327 | |
| 3328 | fbdinfo = (struct file_both_directory_info *)d_info->rptr; |
| 3329 | d_info->rptr += le32_to_cpu(fbdinfo->NextEntryOffset); |
| 3330 | d_info->name = fbdinfo->FileName; |
| 3331 | d_info->name_len = le32_to_cpu(fbdinfo->FileNameLength); |
| 3332 | return 0; |
| 3333 | } |
| 3334 | case FILE_DIRECTORY_INFORMATION: |
| 3335 | { |
| 3336 | struct file_directory_info *fdinfo; |
| 3337 | |
| 3338 | fdinfo = (struct file_directory_info *)d_info->rptr; |
| 3339 | d_info->rptr += le32_to_cpu(fdinfo->NextEntryOffset); |
| 3340 | d_info->name = fdinfo->FileName; |
| 3341 | d_info->name_len = le32_to_cpu(fdinfo->FileNameLength); |
| 3342 | return 0; |
| 3343 | } |
| 3344 | case FILE_NAMES_INFORMATION: |
| 3345 | { |
| 3346 | struct file_names_info *fninfo; |
| 3347 | |
| 3348 | fninfo = (struct file_names_info *)d_info->rptr; |
| 3349 | d_info->rptr += le32_to_cpu(fninfo->NextEntryOffset); |
| 3350 | d_info->name = fninfo->FileName; |
| 3351 | d_info->name_len = le32_to_cpu(fninfo->FileNameLength); |
| 3352 | return 0; |
| 3353 | } |
| 3354 | case FILEID_FULL_DIRECTORY_INFORMATION: |
| 3355 | { |
| 3356 | struct file_id_full_dir_info *dinfo; |
| 3357 | |
| 3358 | dinfo = (struct file_id_full_dir_info *)d_info->rptr; |
| 3359 | d_info->rptr += le32_to_cpu(dinfo->NextEntryOffset); |
| 3360 | d_info->name = dinfo->FileName; |
| 3361 | d_info->name_len = le32_to_cpu(dinfo->FileNameLength); |
| 3362 | return 0; |
| 3363 | } |
| 3364 | case FILEID_BOTH_DIRECTORY_INFORMATION: |
| 3365 | { |
| 3366 | struct file_id_both_directory_info *fibdinfo; |
| 3367 | |
| 3368 | fibdinfo = (struct file_id_both_directory_info *)d_info->rptr; |
| 3369 | d_info->rptr += le32_to_cpu(fibdinfo->NextEntryOffset); |
| 3370 | d_info->name = fibdinfo->FileName; |
| 3371 | d_info->name_len = le32_to_cpu(fibdinfo->FileNameLength); |
| 3372 | return 0; |
| 3373 | } |
| 3374 | case SMB_FIND_FILE_POSIX_INFO: |
| 3375 | { |
| 3376 | struct smb2_posix_info *posix_info; |
| 3377 | |
| 3378 | posix_info = (struct smb2_posix_info *)d_info->rptr; |
| 3379 | d_info->rptr += le32_to_cpu(posix_info->NextEntryOffset); |
| 3380 | d_info->name = posix_info->name; |
| 3381 | d_info->name_len = le32_to_cpu(posix_info->name_len); |
| 3382 | return 0; |
| 3383 | } |
| 3384 | default: |
| 3385 | return -EINVAL; |
| 3386 | } |
| 3387 | } |
| 3388 | |
| 3389 | /** |
| 3390 | * smb2_populate_readdir_entry() - encode directory entry in smb2 response |
| 3391 | * buffer |
| 3392 | * @conn: connection instance |
| 3393 | * @info_level: smb information level |
| 3394 | * @d_info: structure included variables for query dir |
| 3395 | * @ksmbd_kstat: ksmbd wrapper of dirent stat information |
| 3396 | * |
| 3397 | * if directory has many entries, find first can't read it fully. |
| 3398 | * find next might be called multiple times to read remaining dir entries |
| 3399 | * |
| 3400 | * Return: 0 on success, otherwise error |
| 3401 | */ |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3402 | 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] | 3403 | struct ksmbd_dir_info *d_info, |
| 3404 | struct ksmbd_kstat *ksmbd_kstat) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3405 | { |
| 3406 | int next_entry_offset = 0; |
| 3407 | char *conv_name; |
| 3408 | int conv_len; |
| 3409 | void *kstat; |
Namjae Jeon | dac0ec6 | 2021-07-07 14:57:24 +0900 | [diff] [blame] | 3410 | int struct_sz, rc = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3411 | |
| 3412 | conv_name = ksmbd_convert_dir_info_name(d_info, |
| 3413 | conn->local_nls, |
| 3414 | &conv_len); |
| 3415 | if (!conv_name) |
| 3416 | return -ENOMEM; |
| 3417 | |
| 3418 | /* Somehow the name has only terminating NULL bytes */ |
| 3419 | if (conv_len < 0) { |
Namjae Jeon | dac0ec6 | 2021-07-07 14:57:24 +0900 | [diff] [blame] | 3420 | rc = -EINVAL; |
| 3421 | goto free_conv_name; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3422 | } |
| 3423 | |
| 3424 | struct_sz = readdir_info_level_struct_sz(info_level); |
| 3425 | next_entry_offset = ALIGN(struct_sz - 1 + conv_len, |
| 3426 | KSMBD_DIR_INFO_ALIGNMENT); |
| 3427 | |
| 3428 | if (next_entry_offset > d_info->out_buf_len) { |
| 3429 | d_info->out_buf_len = 0; |
Namjae Jeon | dac0ec6 | 2021-07-07 14:57:24 +0900 | [diff] [blame] | 3430 | rc = -ENOSPC; |
| 3431 | goto free_conv_name; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3432 | } |
| 3433 | |
| 3434 | kstat = d_info->wptr; |
| 3435 | if (info_level != FILE_NAMES_INFORMATION) |
| 3436 | kstat = ksmbd_vfs_init_kstat(&d_info->wptr, ksmbd_kstat); |
| 3437 | |
| 3438 | switch (info_level) { |
| 3439 | case FILE_FULL_DIRECTORY_INFORMATION: |
| 3440 | { |
| 3441 | struct file_full_directory_info *ffdinfo; |
| 3442 | |
| 3443 | ffdinfo = (struct file_full_directory_info *)kstat; |
| 3444 | ffdinfo->FileNameLength = cpu_to_le32(conv_len); |
| 3445 | ffdinfo->EaSize = |
| 3446 | smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode); |
| 3447 | if (ffdinfo->EaSize) |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 3448 | ffdinfo->ExtFileAttributes = FILE_ATTRIBUTE_REPARSE_POINT_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3449 | if (d_info->hide_dot_file && d_info->name[0] == '.') |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 3450 | ffdinfo->ExtFileAttributes |= FILE_ATTRIBUTE_HIDDEN_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3451 | memcpy(ffdinfo->FileName, conv_name, conv_len); |
| 3452 | ffdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3453 | break; |
| 3454 | } |
| 3455 | case FILE_BOTH_DIRECTORY_INFORMATION: |
| 3456 | { |
| 3457 | struct file_both_directory_info *fbdinfo; |
| 3458 | |
| 3459 | fbdinfo = (struct file_both_directory_info *)kstat; |
| 3460 | fbdinfo->FileNameLength = cpu_to_le32(conv_len); |
| 3461 | fbdinfo->EaSize = |
| 3462 | smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode); |
| 3463 | if (fbdinfo->EaSize) |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 3464 | fbdinfo->ExtFileAttributes = FILE_ATTRIBUTE_REPARSE_POINT_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3465 | fbdinfo->ShortNameLength = 0; |
| 3466 | fbdinfo->Reserved = 0; |
| 3467 | if (d_info->hide_dot_file && d_info->name[0] == '.') |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 3468 | fbdinfo->ExtFileAttributes |= FILE_ATTRIBUTE_HIDDEN_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3469 | memcpy(fbdinfo->FileName, conv_name, conv_len); |
| 3470 | fbdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3471 | break; |
| 3472 | } |
| 3473 | case FILE_DIRECTORY_INFORMATION: |
| 3474 | { |
| 3475 | struct file_directory_info *fdinfo; |
| 3476 | |
| 3477 | fdinfo = (struct file_directory_info *)kstat; |
| 3478 | fdinfo->FileNameLength = cpu_to_le32(conv_len); |
| 3479 | if (d_info->hide_dot_file && d_info->name[0] == '.') |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 3480 | fdinfo->ExtFileAttributes |= FILE_ATTRIBUTE_HIDDEN_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3481 | memcpy(fdinfo->FileName, conv_name, conv_len); |
| 3482 | fdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3483 | break; |
| 3484 | } |
| 3485 | case FILE_NAMES_INFORMATION: |
| 3486 | { |
| 3487 | struct file_names_info *fninfo; |
| 3488 | |
| 3489 | fninfo = (struct file_names_info *)kstat; |
| 3490 | fninfo->FileNameLength = cpu_to_le32(conv_len); |
| 3491 | memcpy(fninfo->FileName, conv_name, conv_len); |
| 3492 | fninfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3493 | break; |
| 3494 | } |
| 3495 | case FILEID_FULL_DIRECTORY_INFORMATION: |
| 3496 | { |
| 3497 | struct file_id_full_dir_info *dinfo; |
| 3498 | |
| 3499 | dinfo = (struct file_id_full_dir_info *)kstat; |
| 3500 | dinfo->FileNameLength = cpu_to_le32(conv_len); |
| 3501 | dinfo->EaSize = |
| 3502 | smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode); |
| 3503 | if (dinfo->EaSize) |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 3504 | dinfo->ExtFileAttributes = FILE_ATTRIBUTE_REPARSE_POINT_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3505 | dinfo->Reserved = 0; |
| 3506 | dinfo->UniqueId = cpu_to_le64(ksmbd_kstat->kstat->ino); |
| 3507 | if (d_info->hide_dot_file && d_info->name[0] == '.') |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 3508 | dinfo->ExtFileAttributes |= FILE_ATTRIBUTE_HIDDEN_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3509 | memcpy(dinfo->FileName, conv_name, conv_len); |
| 3510 | dinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3511 | break; |
| 3512 | } |
| 3513 | case FILEID_BOTH_DIRECTORY_INFORMATION: |
| 3514 | { |
| 3515 | struct file_id_both_directory_info *fibdinfo; |
| 3516 | |
| 3517 | fibdinfo = (struct file_id_both_directory_info *)kstat; |
| 3518 | fibdinfo->FileNameLength = cpu_to_le32(conv_len); |
| 3519 | fibdinfo->EaSize = |
| 3520 | smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode); |
| 3521 | if (fibdinfo->EaSize) |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 3522 | fibdinfo->ExtFileAttributes = FILE_ATTRIBUTE_REPARSE_POINT_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3523 | fibdinfo->UniqueId = cpu_to_le64(ksmbd_kstat->kstat->ino); |
| 3524 | fibdinfo->ShortNameLength = 0; |
| 3525 | fibdinfo->Reserved = 0; |
| 3526 | fibdinfo->Reserved2 = cpu_to_le16(0); |
| 3527 | if (d_info->hide_dot_file && d_info->name[0] == '.') |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 3528 | fibdinfo->ExtFileAttributes |= FILE_ATTRIBUTE_HIDDEN_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3529 | memcpy(fibdinfo->FileName, conv_name, conv_len); |
| 3530 | fibdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3531 | break; |
| 3532 | } |
| 3533 | case SMB_FIND_FILE_POSIX_INFO: |
| 3534 | { |
| 3535 | struct smb2_posix_info *posix_info; |
| 3536 | u64 time; |
| 3537 | |
| 3538 | posix_info = (struct smb2_posix_info *)kstat; |
| 3539 | posix_info->Ignored = 0; |
| 3540 | posix_info->CreationTime = cpu_to_le64(ksmbd_kstat->create_time); |
| 3541 | time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->ctime); |
| 3542 | posix_info->ChangeTime = cpu_to_le64(time); |
| 3543 | time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->atime); |
| 3544 | posix_info->LastAccessTime = cpu_to_le64(time); |
| 3545 | time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->mtime); |
| 3546 | posix_info->LastWriteTime = cpu_to_le64(time); |
| 3547 | posix_info->EndOfFile = cpu_to_le64(ksmbd_kstat->kstat->size); |
| 3548 | posix_info->AllocationSize = cpu_to_le64(ksmbd_kstat->kstat->blocks << 9); |
| 3549 | posix_info->DeviceId = cpu_to_le32(ksmbd_kstat->kstat->rdev); |
| 3550 | posix_info->HardLinks = cpu_to_le32(ksmbd_kstat->kstat->nlink); |
| 3551 | posix_info->Mode = cpu_to_le32(ksmbd_kstat->kstat->mode); |
| 3552 | posix_info->Inode = cpu_to_le64(ksmbd_kstat->kstat->ino); |
| 3553 | posix_info->DosAttributes = |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 3554 | S_ISDIR(ksmbd_kstat->kstat->mode) ? |
| 3555 | FILE_ATTRIBUTE_DIRECTORY_LE : FILE_ATTRIBUTE_ARCHIVE_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3556 | if (d_info->hide_dot_file && d_info->name[0] == '.') |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 3557 | posix_info->DosAttributes |= FILE_ATTRIBUTE_HIDDEN_LE; |
Christian Brauner | 475d6f9 | 2021-08-23 17:13:48 +0200 | [diff] [blame] | 3558 | 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] | 3559 | SIDNFS_USER, (struct smb_sid *)&posix_info->SidBuffer[0]); |
Christian Brauner | 475d6f9 | 2021-08-23 17:13:48 +0200 | [diff] [blame] | 3560 | 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] | 3561 | SIDNFS_GROUP, (struct smb_sid *)&posix_info->SidBuffer[20]); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3562 | memcpy(posix_info->name, conv_name, conv_len); |
| 3563 | posix_info->name_len = cpu_to_le32(conv_len); |
| 3564 | posix_info->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3565 | break; |
| 3566 | } |
| 3567 | |
| 3568 | } /* switch (info_level) */ |
| 3569 | |
| 3570 | d_info->last_entry_offset = d_info->data_count; |
| 3571 | d_info->data_count += next_entry_offset; |
Marios Makassikis | e7735c8 | 2021-05-06 11:40:02 +0900 | [diff] [blame] | 3572 | d_info->out_buf_len -= next_entry_offset; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3573 | d_info->wptr += next_entry_offset; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3574 | |
| 3575 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3576 | "info_level : %d, buf_len :%d, next_offset : %d, data_count : %d\n", |
| 3577 | info_level, d_info->out_buf_len, |
| 3578 | next_entry_offset, d_info->data_count); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3579 | |
Namjae Jeon | dac0ec6 | 2021-07-07 14:57:24 +0900 | [diff] [blame] | 3580 | free_conv_name: |
| 3581 | kfree(conv_name); |
| 3582 | return rc; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3583 | } |
| 3584 | |
| 3585 | struct smb2_query_dir_private { |
| 3586 | struct ksmbd_work *work; |
| 3587 | char *search_pattern; |
| 3588 | struct ksmbd_file *dir_fp; |
| 3589 | |
| 3590 | struct ksmbd_dir_info *d_info; |
| 3591 | int info_level; |
| 3592 | }; |
| 3593 | |
| 3594 | static void lock_dir(struct ksmbd_file *dir_fp) |
| 3595 | { |
| 3596 | struct dentry *dir = dir_fp->filp->f_path.dentry; |
| 3597 | |
| 3598 | inode_lock_nested(d_inode(dir), I_MUTEX_PARENT); |
| 3599 | } |
| 3600 | |
| 3601 | static void unlock_dir(struct ksmbd_file *dir_fp) |
| 3602 | { |
| 3603 | struct dentry *dir = dir_fp->filp->f_path.dentry; |
| 3604 | |
| 3605 | inode_unlock(d_inode(dir)); |
| 3606 | } |
| 3607 | |
| 3608 | static int process_query_dir_entries(struct smb2_query_dir_private *priv) |
| 3609 | { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 3610 | 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] | 3611 | struct kstat kstat; |
| 3612 | struct ksmbd_kstat ksmbd_kstat; |
| 3613 | int rc; |
| 3614 | int i; |
| 3615 | |
| 3616 | for (i = 0; i < priv->d_info->num_entry; i++) { |
| 3617 | struct dentry *dent; |
| 3618 | |
| 3619 | if (dentry_name(priv->d_info, priv->info_level)) |
| 3620 | return -EINVAL; |
| 3621 | |
| 3622 | lock_dir(priv->dir_fp); |
Christian Brauner | da1e7ad | 2021-08-23 17:13:47 +0200 | [diff] [blame] | 3623 | dent = lookup_one(user_ns, priv->d_info->name, |
| 3624 | priv->dir_fp->filp->f_path.dentry, |
| 3625 | priv->d_info->name_len); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3626 | unlock_dir(priv->dir_fp); |
| 3627 | |
| 3628 | if (IS_ERR(dent)) { |
| 3629 | ksmbd_debug(SMB, "Cannot lookup `%s' [%ld]\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3630 | priv->d_info->name, |
| 3631 | PTR_ERR(dent)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3632 | continue; |
| 3633 | } |
| 3634 | if (unlikely(d_is_negative(dent))) { |
| 3635 | dput(dent); |
| 3636 | ksmbd_debug(SMB, "Negative dentry `%s'\n", |
| 3637 | priv->d_info->name); |
| 3638 | continue; |
| 3639 | } |
| 3640 | |
| 3641 | ksmbd_kstat.kstat = &kstat; |
| 3642 | if (priv->info_level != FILE_NAMES_INFORMATION) |
| 3643 | ksmbd_vfs_fill_dentry_attrs(priv->work, |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 3644 | user_ns, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3645 | dent, |
| 3646 | &ksmbd_kstat); |
| 3647 | |
| 3648 | rc = smb2_populate_readdir_entry(priv->work->conn, |
| 3649 | priv->info_level, |
| 3650 | priv->d_info, |
| 3651 | &ksmbd_kstat); |
| 3652 | dput(dent); |
| 3653 | if (rc) |
| 3654 | return rc; |
| 3655 | } |
| 3656 | return 0; |
| 3657 | } |
| 3658 | |
| 3659 | static int reserve_populate_dentry(struct ksmbd_dir_info *d_info, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3660 | int info_level) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3661 | { |
| 3662 | int struct_sz; |
| 3663 | int conv_len; |
| 3664 | int next_entry_offset; |
| 3665 | |
| 3666 | struct_sz = readdir_info_level_struct_sz(info_level); |
| 3667 | if (struct_sz == -EOPNOTSUPP) |
| 3668 | return -EOPNOTSUPP; |
| 3669 | |
| 3670 | conv_len = (d_info->name_len + 1) * 2; |
| 3671 | next_entry_offset = ALIGN(struct_sz - 1 + conv_len, |
| 3672 | KSMBD_DIR_INFO_ALIGNMENT); |
| 3673 | |
| 3674 | if (next_entry_offset > d_info->out_buf_len) { |
| 3675 | d_info->out_buf_len = 0; |
| 3676 | return -ENOSPC; |
| 3677 | } |
| 3678 | |
| 3679 | switch (info_level) { |
| 3680 | case FILE_FULL_DIRECTORY_INFORMATION: |
| 3681 | { |
| 3682 | struct file_full_directory_info *ffdinfo; |
| 3683 | |
| 3684 | ffdinfo = (struct file_full_directory_info *)d_info->wptr; |
| 3685 | memcpy(ffdinfo->FileName, d_info->name, d_info->name_len); |
| 3686 | ffdinfo->FileName[d_info->name_len] = 0x00; |
| 3687 | ffdinfo->FileNameLength = cpu_to_le32(d_info->name_len); |
| 3688 | ffdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3689 | break; |
| 3690 | } |
| 3691 | case FILE_BOTH_DIRECTORY_INFORMATION: |
| 3692 | { |
| 3693 | struct file_both_directory_info *fbdinfo; |
| 3694 | |
| 3695 | fbdinfo = (struct file_both_directory_info *)d_info->wptr; |
| 3696 | memcpy(fbdinfo->FileName, d_info->name, d_info->name_len); |
| 3697 | fbdinfo->FileName[d_info->name_len] = 0x00; |
| 3698 | fbdinfo->FileNameLength = cpu_to_le32(d_info->name_len); |
| 3699 | fbdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3700 | break; |
| 3701 | } |
| 3702 | case FILE_DIRECTORY_INFORMATION: |
| 3703 | { |
| 3704 | struct file_directory_info *fdinfo; |
| 3705 | |
| 3706 | fdinfo = (struct file_directory_info *)d_info->wptr; |
| 3707 | memcpy(fdinfo->FileName, d_info->name, d_info->name_len); |
| 3708 | fdinfo->FileName[d_info->name_len] = 0x00; |
| 3709 | fdinfo->FileNameLength = cpu_to_le32(d_info->name_len); |
| 3710 | fdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3711 | break; |
| 3712 | } |
| 3713 | case FILE_NAMES_INFORMATION: |
| 3714 | { |
| 3715 | struct file_names_info *fninfo; |
| 3716 | |
| 3717 | fninfo = (struct file_names_info *)d_info->wptr; |
| 3718 | memcpy(fninfo->FileName, d_info->name, d_info->name_len); |
| 3719 | fninfo->FileName[d_info->name_len] = 0x00; |
| 3720 | fninfo->FileNameLength = cpu_to_le32(d_info->name_len); |
| 3721 | fninfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3722 | break; |
| 3723 | } |
| 3724 | case FILEID_FULL_DIRECTORY_INFORMATION: |
| 3725 | { |
| 3726 | struct file_id_full_dir_info *dinfo; |
| 3727 | |
| 3728 | dinfo = (struct file_id_full_dir_info *)d_info->wptr; |
| 3729 | memcpy(dinfo->FileName, d_info->name, d_info->name_len); |
| 3730 | dinfo->FileName[d_info->name_len] = 0x00; |
| 3731 | dinfo->FileNameLength = cpu_to_le32(d_info->name_len); |
| 3732 | dinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3733 | break; |
| 3734 | } |
| 3735 | case FILEID_BOTH_DIRECTORY_INFORMATION: |
| 3736 | { |
| 3737 | struct file_id_both_directory_info *fibdinfo; |
| 3738 | |
| 3739 | fibdinfo = (struct file_id_both_directory_info *)d_info->wptr; |
| 3740 | memcpy(fibdinfo->FileName, d_info->name, d_info->name_len); |
| 3741 | fibdinfo->FileName[d_info->name_len] = 0x00; |
| 3742 | fibdinfo->FileNameLength = cpu_to_le32(d_info->name_len); |
| 3743 | fibdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3744 | break; |
| 3745 | } |
| 3746 | case SMB_FIND_FILE_POSIX_INFO: |
| 3747 | { |
| 3748 | struct smb2_posix_info *posix_info; |
| 3749 | |
| 3750 | posix_info = (struct smb2_posix_info *)d_info->wptr; |
| 3751 | memcpy(posix_info->name, d_info->name, d_info->name_len); |
| 3752 | posix_info->name[d_info->name_len] = 0x00; |
| 3753 | posix_info->name_len = cpu_to_le32(d_info->name_len); |
| 3754 | posix_info->NextEntryOffset = |
| 3755 | cpu_to_le32(next_entry_offset); |
| 3756 | break; |
| 3757 | } |
| 3758 | } /* switch (info_level) */ |
| 3759 | |
| 3760 | d_info->num_entry++; |
| 3761 | d_info->out_buf_len -= next_entry_offset; |
| 3762 | d_info->wptr += next_entry_offset; |
| 3763 | return 0; |
| 3764 | } |
| 3765 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3766 | 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] | 3767 | loff_t offset, u64 ino, unsigned int d_type) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3768 | { |
| 3769 | struct ksmbd_readdir_data *buf; |
| 3770 | struct smb2_query_dir_private *priv; |
| 3771 | struct ksmbd_dir_info *d_info; |
| 3772 | int rc; |
| 3773 | |
| 3774 | buf = container_of(ctx, struct ksmbd_readdir_data, ctx); |
| 3775 | priv = buf->private; |
| 3776 | d_info = priv->d_info; |
| 3777 | |
| 3778 | /* dot and dotdot entries are already reserved */ |
| 3779 | if (!strcmp(".", name) || !strcmp("..", name)) |
| 3780 | return 0; |
| 3781 | if (ksmbd_share_veto_filename(priv->work->tcon->share_conf, name)) |
| 3782 | return 0; |
Namjae Jeon | b24c933 | 2021-03-21 17:32:19 +0900 | [diff] [blame] | 3783 | if (!match_pattern(name, namlen, priv->search_pattern)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3784 | return 0; |
| 3785 | |
| 3786 | d_info->name = name; |
| 3787 | d_info->name_len = namlen; |
| 3788 | rc = reserve_populate_dentry(d_info, priv->info_level); |
| 3789 | if (rc) |
| 3790 | return rc; |
| 3791 | if (d_info->flags & SMB2_RETURN_SINGLE_ENTRY) { |
| 3792 | d_info->out_buf_len = 0; |
| 3793 | return 0; |
| 3794 | } |
| 3795 | return 0; |
| 3796 | } |
| 3797 | |
| 3798 | static void restart_ctx(struct dir_context *ctx) |
| 3799 | { |
| 3800 | ctx->pos = 0; |
| 3801 | } |
| 3802 | |
| 3803 | static int verify_info_level(int info_level) |
| 3804 | { |
| 3805 | switch (info_level) { |
| 3806 | case FILE_FULL_DIRECTORY_INFORMATION: |
| 3807 | case FILE_BOTH_DIRECTORY_INFORMATION: |
| 3808 | case FILE_DIRECTORY_INFORMATION: |
| 3809 | case FILE_NAMES_INFORMATION: |
| 3810 | case FILEID_FULL_DIRECTORY_INFORMATION: |
| 3811 | case FILEID_BOTH_DIRECTORY_INFORMATION: |
| 3812 | case SMB_FIND_FILE_POSIX_INFO: |
| 3813 | break; |
| 3814 | default: |
| 3815 | return -EOPNOTSUPP; |
| 3816 | } |
| 3817 | |
| 3818 | return 0; |
| 3819 | } |
| 3820 | |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 3821 | static int smb2_calc_max_out_buf_len(struct ksmbd_work *work, |
| 3822 | unsigned short hdr2_len, |
| 3823 | unsigned int out_buf_len) |
| 3824 | { |
| 3825 | int free_len; |
| 3826 | |
| 3827 | if (out_buf_len > work->conn->vals->max_trans_size) |
| 3828 | return -EINVAL; |
| 3829 | |
| 3830 | free_len = (int)(work->response_sz - |
| 3831 | (get_rfc1002_len(work->response_buf) + 4)) - |
| 3832 | hdr2_len; |
| 3833 | if (free_len < 0) |
| 3834 | return -EINVAL; |
| 3835 | |
| 3836 | return min_t(int, out_buf_len, free_len); |
| 3837 | } |
| 3838 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3839 | int smb2_query_dir(struct ksmbd_work *work) |
| 3840 | { |
| 3841 | struct ksmbd_conn *conn = work->conn; |
| 3842 | struct smb2_query_directory_req *req; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 3843 | struct smb2_query_directory_rsp *rsp; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3844 | struct ksmbd_share_config *share = work->tcon->share_conf; |
| 3845 | struct ksmbd_file *dir_fp = NULL; |
| 3846 | struct ksmbd_dir_info d_info; |
| 3847 | int rc = 0; |
| 3848 | char *srch_ptr = NULL; |
| 3849 | unsigned char srch_flag; |
| 3850 | int buffer_sz; |
| 3851 | struct smb2_query_dir_private query_dir_private = {NULL, }; |
| 3852 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3853 | WORK_BUFFERS(work, req, rsp); |
| 3854 | |
| 3855 | if (ksmbd_override_fsids(work)) { |
| 3856 | rsp->hdr.Status = STATUS_NO_MEMORY; |
| 3857 | smb2_set_err_rsp(work); |
| 3858 | return -ENOMEM; |
| 3859 | } |
| 3860 | |
| 3861 | rc = verify_info_level(req->FileInformationClass); |
| 3862 | if (rc) { |
| 3863 | rc = -EFAULT; |
| 3864 | goto err_out2; |
| 3865 | } |
| 3866 | |
| 3867 | dir_fp = ksmbd_lookup_fd_slow(work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3868 | le64_to_cpu(req->VolatileFileId), |
| 3869 | le64_to_cpu(req->PersistentFileId)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3870 | if (!dir_fp) { |
| 3871 | rc = -EBADF; |
| 3872 | goto err_out2; |
| 3873 | } |
| 3874 | |
| 3875 | if (!(dir_fp->daccess & FILE_LIST_DIRECTORY_LE) || |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 3876 | inode_permission(file_mnt_user_ns(dir_fp->filp), |
| 3877 | file_inode(dir_fp->filp), |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3878 | MAY_READ | MAY_EXEC)) { |
Namjae Jeon | 493fa2f | 2021-06-29 09:22:16 +0900 | [diff] [blame] | 3879 | pr_err("no right to enumerate directory (%pd)\n", |
| 3880 | dir_fp->filp->f_path.dentry); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3881 | rc = -EACCES; |
| 3882 | goto err_out2; |
| 3883 | } |
| 3884 | |
| 3885 | if (!S_ISDIR(file_inode(dir_fp->filp)->i_mode)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 3886 | pr_err("can't do query dir for a file\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3887 | rc = -EINVAL; |
| 3888 | goto err_out2; |
| 3889 | } |
| 3890 | |
| 3891 | srch_flag = req->Flags; |
| 3892 | srch_ptr = smb_strndup_from_utf16(req->Buffer, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3893 | le16_to_cpu(req->FileNameLength), 1, |
| 3894 | conn->local_nls); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3895 | if (IS_ERR(srch_ptr)) { |
| 3896 | ksmbd_debug(SMB, "Search Pattern not found\n"); |
| 3897 | rc = -EINVAL; |
| 3898 | goto err_out2; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3899 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3900 | ksmbd_debug(SMB, "Search pattern is %s\n", srch_ptr); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3901 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3902 | |
| 3903 | ksmbd_debug(SMB, "Directory name is %s\n", dir_fp->filename); |
| 3904 | |
| 3905 | if (srch_flag & SMB2_REOPEN || srch_flag & SMB2_RESTART_SCANS) { |
| 3906 | ksmbd_debug(SMB, "Restart directory scan\n"); |
| 3907 | generic_file_llseek(dir_fp->filp, 0, SEEK_SET); |
| 3908 | restart_ctx(&dir_fp->readdir_data.ctx); |
| 3909 | } |
| 3910 | |
| 3911 | memset(&d_info, 0, sizeof(struct ksmbd_dir_info)); |
| 3912 | d_info.wptr = (char *)rsp->Buffer; |
| 3913 | d_info.rptr = (char *)rsp->Buffer; |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 3914 | d_info.out_buf_len = |
| 3915 | smb2_calc_max_out_buf_len(work, 8, |
| 3916 | le32_to_cpu(req->OutputBufferLength)); |
| 3917 | if (d_info.out_buf_len < 0) { |
| 3918 | rc = -EINVAL; |
| 3919 | goto err_out; |
| 3920 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3921 | d_info.flags = srch_flag; |
| 3922 | |
| 3923 | /* |
| 3924 | * reserve dot and dotdot entries in head of buffer |
| 3925 | * in first response |
| 3926 | */ |
| 3927 | rc = ksmbd_populate_dot_dotdot_entries(work, req->FileInformationClass, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3928 | dir_fp, &d_info, srch_ptr, |
| 3929 | smb2_populate_readdir_entry); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3930 | if (rc == -ENOSPC) |
| 3931 | rc = 0; |
| 3932 | else if (rc) |
| 3933 | goto err_out; |
| 3934 | |
| 3935 | if (test_share_config_flag(share, KSMBD_SHARE_FLAG_HIDE_DOT_FILES)) |
| 3936 | d_info.hide_dot_file = true; |
| 3937 | |
| 3938 | buffer_sz = d_info.out_buf_len; |
| 3939 | d_info.rptr = d_info.wptr; |
| 3940 | query_dir_private.work = work; |
| 3941 | query_dir_private.search_pattern = srch_ptr; |
| 3942 | query_dir_private.dir_fp = dir_fp; |
| 3943 | query_dir_private.d_info = &d_info; |
| 3944 | query_dir_private.info_level = req->FileInformationClass; |
| 3945 | dir_fp->readdir_data.private = &query_dir_private; |
| 3946 | set_ctx_actor(&dir_fp->readdir_data.ctx, __query_dir); |
| 3947 | |
Namjae Jeon | e8c0619 | 2021-06-22 11:06:11 +0900 | [diff] [blame] | 3948 | rc = iterate_dir(dir_fp->filp, &dir_fp->readdir_data.ctx); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3949 | if (rc == 0) |
| 3950 | restart_ctx(&dir_fp->readdir_data.ctx); |
| 3951 | if (rc == -ENOSPC) |
| 3952 | rc = 0; |
| 3953 | if (rc) |
| 3954 | goto err_out; |
| 3955 | |
| 3956 | d_info.wptr = d_info.rptr; |
| 3957 | d_info.out_buf_len = buffer_sz; |
| 3958 | rc = process_query_dir_entries(&query_dir_private); |
| 3959 | if (rc) |
| 3960 | goto err_out; |
| 3961 | |
| 3962 | if (!d_info.data_count && d_info.out_buf_len >= 0) { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3963 | if (srch_flag & SMB2_RETURN_SINGLE_ENTRY && !is_asterisk(srch_ptr)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3964 | rsp->hdr.Status = STATUS_NO_SUCH_FILE; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3965 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3966 | dir_fp->dot_dotdot[0] = dir_fp->dot_dotdot[1] = 0; |
| 3967 | rsp->hdr.Status = STATUS_NO_MORE_FILES; |
| 3968 | } |
| 3969 | rsp->StructureSize = cpu_to_le16(9); |
| 3970 | rsp->OutputBufferOffset = cpu_to_le16(0); |
| 3971 | rsp->OutputBufferLength = cpu_to_le32(0); |
| 3972 | rsp->Buffer[0] = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 3973 | inc_rfc1001_len(work->response_buf, 9); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3974 | } else { |
| 3975 | ((struct file_directory_info *) |
| 3976 | ((char *)rsp->Buffer + d_info.last_entry_offset)) |
| 3977 | ->NextEntryOffset = 0; |
| 3978 | |
| 3979 | rsp->StructureSize = cpu_to_le16(9); |
| 3980 | rsp->OutputBufferOffset = cpu_to_le16(72); |
| 3981 | rsp->OutputBufferLength = cpu_to_le32(d_info.data_count); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 3982 | inc_rfc1001_len(work->response_buf, 8 + d_info.data_count); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3983 | } |
| 3984 | |
| 3985 | kfree(srch_ptr); |
| 3986 | ksmbd_fd_put(work, dir_fp); |
| 3987 | ksmbd_revert_fsids(work); |
| 3988 | return 0; |
| 3989 | |
| 3990 | err_out: |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 3991 | pr_err("error while processing smb2 query dir rc = %d\n", rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3992 | kfree(srch_ptr); |
| 3993 | |
| 3994 | err_out2: |
| 3995 | if (rc == -EINVAL) |
| 3996 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 3997 | else if (rc == -EACCES) |
| 3998 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 3999 | else if (rc == -ENOENT) |
| 4000 | rsp->hdr.Status = STATUS_NO_SUCH_FILE; |
| 4001 | else if (rc == -EBADF) |
| 4002 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 4003 | else if (rc == -ENOMEM) |
| 4004 | rsp->hdr.Status = STATUS_NO_MEMORY; |
| 4005 | else if (rc == -EFAULT) |
| 4006 | rsp->hdr.Status = STATUS_INVALID_INFO_CLASS; |
| 4007 | if (!rsp->hdr.Status) |
| 4008 | rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR; |
| 4009 | |
| 4010 | smb2_set_err_rsp(work); |
| 4011 | ksmbd_fd_put(work, dir_fp); |
| 4012 | ksmbd_revert_fsids(work); |
| 4013 | return 0; |
| 4014 | } |
| 4015 | |
| 4016 | /** |
| 4017 | * buffer_check_err() - helper function to check buffer errors |
| 4018 | * @reqOutputBufferLength: max buffer length expected in command response |
| 4019 | * @rsp: query info response buffer contains output buffer length |
Yang Li | e230d01 | 2021-12-21 17:07:11 +0800 | [diff] [blame] | 4020 | * @rsp_org: base response buffer pointer in case of chained response |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4021 | * @infoclass_size: query info class response buffer size |
| 4022 | * |
| 4023 | * Return: 0 on success, otherwise error |
| 4024 | */ |
| 4025 | static int buffer_check_err(int reqOutputBufferLength, |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4026 | struct smb2_query_info_rsp *rsp, |
| 4027 | void *rsp_org, int infoclass_size) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4028 | { |
| 4029 | if (reqOutputBufferLength < le32_to_cpu(rsp->OutputBufferLength)) { |
| 4030 | if (reqOutputBufferLength < infoclass_size) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4031 | pr_err("Invalid Buffer Size Requested\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4032 | rsp->hdr.Status = STATUS_INFO_LENGTH_MISMATCH; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4033 | *(__be32 *)rsp_org = cpu_to_be32(sizeof(struct smb2_hdr)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4034 | return -EINVAL; |
| 4035 | } |
| 4036 | |
| 4037 | ksmbd_debug(SMB, "Buffer Overflow\n"); |
| 4038 | rsp->hdr.Status = STATUS_BUFFER_OVERFLOW; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4039 | *(__be32 *)rsp_org = cpu_to_be32(sizeof(struct smb2_hdr) + |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4040 | reqOutputBufferLength); |
| 4041 | rsp->OutputBufferLength = cpu_to_le32(reqOutputBufferLength); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4042 | } |
| 4043 | return 0; |
| 4044 | } |
| 4045 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4046 | static void get_standard_info_pipe(struct smb2_query_info_rsp *rsp, |
| 4047 | void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4048 | { |
| 4049 | struct smb2_file_standard_info *sinfo; |
| 4050 | |
| 4051 | sinfo = (struct smb2_file_standard_info *)rsp->Buffer; |
| 4052 | |
| 4053 | sinfo->AllocationSize = cpu_to_le64(4096); |
| 4054 | sinfo->EndOfFile = cpu_to_le64(0); |
| 4055 | sinfo->NumberOfLinks = cpu_to_le32(1); |
| 4056 | sinfo->DeletePending = 1; |
| 4057 | sinfo->Directory = 0; |
| 4058 | rsp->OutputBufferLength = |
| 4059 | cpu_to_le32(sizeof(struct smb2_file_standard_info)); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4060 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_standard_info)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4061 | } |
| 4062 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4063 | static void get_internal_info_pipe(struct smb2_query_info_rsp *rsp, u64 num, |
| 4064 | void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4065 | { |
| 4066 | struct smb2_file_internal_info *file_info; |
| 4067 | |
| 4068 | file_info = (struct smb2_file_internal_info *)rsp->Buffer; |
| 4069 | |
| 4070 | /* any unique number */ |
| 4071 | file_info->IndexNumber = cpu_to_le64(num | (1ULL << 63)); |
| 4072 | rsp->OutputBufferLength = |
| 4073 | cpu_to_le32(sizeof(struct smb2_file_internal_info)); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4074 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_internal_info)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4075 | } |
| 4076 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4077 | static int smb2_get_info_file_pipe(struct ksmbd_session *sess, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4078 | struct smb2_query_info_req *req, |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4079 | struct smb2_query_info_rsp *rsp, |
| 4080 | void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4081 | { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4082 | u64 id; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4083 | int rc; |
| 4084 | |
| 4085 | /* |
| 4086 | * Windows can sometime send query file info request on |
| 4087 | * pipe without opening it, checking error condition here |
| 4088 | */ |
| 4089 | id = le64_to_cpu(req->VolatileFileId); |
| 4090 | if (!ksmbd_session_rpc_method(sess, id)) |
| 4091 | return -ENOENT; |
| 4092 | |
| 4093 | ksmbd_debug(SMB, "FileInfoClass %u, FileId 0x%llx\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4094 | req->FileInfoClass, le64_to_cpu(req->VolatileFileId)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4095 | |
| 4096 | switch (req->FileInfoClass) { |
| 4097 | case FILE_STANDARD_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4098 | get_standard_info_pipe(rsp, rsp_org); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4099 | rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength), |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4100 | rsp, rsp_org, |
| 4101 | FILE_STANDARD_INFORMATION_SIZE); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4102 | break; |
| 4103 | case FILE_INTERNAL_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4104 | get_internal_info_pipe(rsp, id, rsp_org); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4105 | rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength), |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4106 | rsp, rsp_org, |
| 4107 | FILE_INTERNAL_INFORMATION_SIZE); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4108 | break; |
| 4109 | default: |
| 4110 | ksmbd_debug(SMB, "smb2_info_file_pipe for %u not supported\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4111 | req->FileInfoClass); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4112 | rc = -EOPNOTSUPP; |
| 4113 | } |
| 4114 | return rc; |
| 4115 | } |
| 4116 | |
| 4117 | /** |
| 4118 | * smb2_get_ea() - handler for smb2 get extended attribute command |
| 4119 | * @work: smb work containing query info command buffer |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 4120 | * @fp: ksmbd_file pointer |
| 4121 | * @req: get extended attribute request |
| 4122 | * @rsp: response buffer pointer |
| 4123 | * @rsp_org: base response buffer pointer in case of chained response |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4124 | * |
| 4125 | * Return: 0 on success, otherwise error |
| 4126 | */ |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4127 | 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] | 4128 | struct smb2_query_info_req *req, |
| 4129 | struct smb2_query_info_rsp *rsp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4130 | { |
| 4131 | struct smb2_ea_info *eainfo, *prev_eainfo; |
| 4132 | char *name, *ptr, *xattr_list = NULL, *buf; |
| 4133 | int rc, name_len, value_len, xattr_list_len, idx; |
| 4134 | ssize_t buf_free_len, alignment_bytes, next_offset, rsp_data_cnt = 0; |
| 4135 | struct smb2_ea_info_req *ea_req = NULL; |
| 4136 | struct path *path; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 4137 | struct user_namespace *user_ns = file_mnt_user_ns(fp->filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4138 | |
| 4139 | if (!(fp->daccess & FILE_READ_EA_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4140 | pr_err("Not permitted to read ext attr : 0x%x\n", |
| 4141 | fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4142 | return -EACCES; |
| 4143 | } |
| 4144 | |
| 4145 | path = &fp->filp->f_path; |
| 4146 | /* single EA entry is requested with given user.* name */ |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4147 | if (req->InputBufferLength) { |
Namjae Jeon | 6d56262 | 2021-09-18 18:45:12 +0900 | [diff] [blame] | 4148 | if (le32_to_cpu(req->InputBufferLength) < |
| 4149 | sizeof(struct smb2_ea_info_req)) |
| 4150 | return -EINVAL; |
| 4151 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4152 | ea_req = (struct smb2_ea_info_req *)req->Buffer; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4153 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4154 | /* need to send all EAs, if no specific EA is requested*/ |
| 4155 | if (le32_to_cpu(req->Flags) & SL_RETURN_SINGLE_ENTRY) |
| 4156 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4157 | "All EAs are requested but need to send single EA entry in rsp flags 0x%x\n", |
| 4158 | le32_to_cpu(req->Flags)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4159 | } |
| 4160 | |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 4161 | buf_free_len = |
| 4162 | smb2_calc_max_out_buf_len(work, 8, |
| 4163 | le32_to_cpu(req->OutputBufferLength)); |
| 4164 | if (buf_free_len < 0) |
| 4165 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4166 | |
| 4167 | rc = ksmbd_vfs_listxattr(path->dentry, &xattr_list); |
| 4168 | if (rc < 0) { |
| 4169 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 4170 | goto out; |
| 4171 | } else if (!rc) { /* there is no EA in the file */ |
| 4172 | ksmbd_debug(SMB, "no ea data in the file\n"); |
| 4173 | goto done; |
| 4174 | } |
| 4175 | xattr_list_len = rc; |
| 4176 | |
| 4177 | ptr = (char *)rsp->Buffer; |
| 4178 | eainfo = (struct smb2_ea_info *)ptr; |
| 4179 | prev_eainfo = eainfo; |
| 4180 | idx = 0; |
| 4181 | |
| 4182 | while (idx < xattr_list_len) { |
| 4183 | name = xattr_list + idx; |
| 4184 | name_len = strlen(name); |
| 4185 | |
| 4186 | ksmbd_debug(SMB, "%s, len %d\n", name, name_len); |
| 4187 | idx += name_len + 1; |
| 4188 | |
| 4189 | /* |
| 4190 | * CIFS does not support EA other than user.* namespace, |
| 4191 | * still keep the framework generic, to list other attrs |
| 4192 | * in future. |
| 4193 | */ |
| 4194 | if (strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) |
| 4195 | continue; |
| 4196 | |
| 4197 | if (!strncmp(&name[XATTR_USER_PREFIX_LEN], STREAM_PREFIX, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4198 | STREAM_PREFIX_LEN)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4199 | continue; |
| 4200 | |
| 4201 | if (req->InputBufferLength && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4202 | strncmp(&name[XATTR_USER_PREFIX_LEN], ea_req->name, |
| 4203 | ea_req->EaNameLength)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4204 | continue; |
| 4205 | |
| 4206 | if (!strncmp(&name[XATTR_USER_PREFIX_LEN], |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4207 | DOS_ATTRIBUTE_PREFIX, DOS_ATTRIBUTE_PREFIX_LEN)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4208 | continue; |
| 4209 | |
| 4210 | if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) |
| 4211 | name_len -= XATTR_USER_PREFIX_LEN; |
| 4212 | |
| 4213 | ptr = (char *)(&eainfo->name + name_len + 1); |
| 4214 | buf_free_len -= (offsetof(struct smb2_ea_info, name) + |
| 4215 | name_len + 1); |
| 4216 | /* bailout if xattr can't fit in buf_free_len */ |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 4217 | value_len = ksmbd_vfs_getxattr(user_ns, path->dentry, |
| 4218 | name, &buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4219 | if (value_len <= 0) { |
| 4220 | rc = -ENOENT; |
| 4221 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 4222 | goto out; |
| 4223 | } |
| 4224 | |
| 4225 | buf_free_len -= value_len; |
| 4226 | if (buf_free_len < 0) { |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 4227 | kfree(buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4228 | break; |
| 4229 | } |
| 4230 | |
| 4231 | memcpy(ptr, buf, value_len); |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 4232 | kfree(buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4233 | |
| 4234 | ptr += value_len; |
| 4235 | eainfo->Flags = 0; |
| 4236 | eainfo->EaNameLength = name_len; |
| 4237 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4238 | if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4239 | memcpy(eainfo->name, &name[XATTR_USER_PREFIX_LEN], |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4240 | name_len); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4241 | else |
| 4242 | memcpy(eainfo->name, name, name_len); |
| 4243 | |
| 4244 | eainfo->name[name_len] = '\0'; |
| 4245 | eainfo->EaValueLength = cpu_to_le16(value_len); |
| 4246 | next_offset = offsetof(struct smb2_ea_info, name) + |
| 4247 | name_len + 1 + value_len; |
| 4248 | |
| 4249 | /* align next xattr entry at 4 byte bundary */ |
| 4250 | alignment_bytes = ((next_offset + 3) & ~3) - next_offset; |
| 4251 | if (alignment_bytes) { |
| 4252 | memset(ptr, '\0', alignment_bytes); |
| 4253 | ptr += alignment_bytes; |
| 4254 | next_offset += alignment_bytes; |
| 4255 | buf_free_len -= alignment_bytes; |
| 4256 | } |
| 4257 | eainfo->NextEntryOffset = cpu_to_le32(next_offset); |
| 4258 | prev_eainfo = eainfo; |
| 4259 | eainfo = (struct smb2_ea_info *)ptr; |
| 4260 | rsp_data_cnt += next_offset; |
| 4261 | |
| 4262 | if (req->InputBufferLength) { |
| 4263 | ksmbd_debug(SMB, "single entry requested\n"); |
| 4264 | break; |
| 4265 | } |
| 4266 | } |
| 4267 | |
| 4268 | /* no more ea entries */ |
| 4269 | prev_eainfo->NextEntryOffset = 0; |
| 4270 | done: |
| 4271 | rc = 0; |
| 4272 | if (rsp_data_cnt == 0) |
| 4273 | rsp->hdr.Status = STATUS_NO_EAS_ON_FILE; |
| 4274 | rsp->OutputBufferLength = cpu_to_le32(rsp_data_cnt); |
| 4275 | inc_rfc1001_len(rsp_org, rsp_data_cnt); |
| 4276 | out: |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 4277 | kvfree(xattr_list); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4278 | return rc; |
| 4279 | } |
| 4280 | |
| 4281 | static void get_file_access_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4282 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4283 | { |
| 4284 | struct smb2_file_access_info *file_info; |
| 4285 | |
| 4286 | file_info = (struct smb2_file_access_info *)rsp->Buffer; |
| 4287 | file_info->AccessFlags = fp->daccess; |
| 4288 | rsp->OutputBufferLength = |
| 4289 | cpu_to_le32(sizeof(struct smb2_file_access_info)); |
| 4290 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_access_info)); |
| 4291 | } |
| 4292 | |
| 4293 | static int get_file_basic_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4294 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4295 | { |
Namjae Jeon | 88d3005 | 2021-09-29 15:37:18 +0900 | [diff] [blame] | 4296 | struct smb2_file_basic_info *basic_info; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4297 | struct kstat stat; |
| 4298 | u64 time; |
| 4299 | |
| 4300 | if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4301 | pr_err("no right to read the attributes : 0x%x\n", |
| 4302 | fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4303 | return -EACCES; |
| 4304 | } |
| 4305 | |
Namjae Jeon | 88d3005 | 2021-09-29 15:37:18 +0900 | [diff] [blame] | 4306 | basic_info = (struct smb2_file_basic_info *)rsp->Buffer; |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 4307 | generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp), |
| 4308 | &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4309 | basic_info->CreationTime = cpu_to_le64(fp->create_time); |
| 4310 | time = ksmbd_UnixTimeToNT(stat.atime); |
| 4311 | basic_info->LastAccessTime = cpu_to_le64(time); |
| 4312 | time = ksmbd_UnixTimeToNT(stat.mtime); |
| 4313 | basic_info->LastWriteTime = cpu_to_le64(time); |
| 4314 | time = ksmbd_UnixTimeToNT(stat.ctime); |
| 4315 | basic_info->ChangeTime = cpu_to_le64(time); |
| 4316 | basic_info->Attributes = fp->f_ci->m_fattr; |
| 4317 | basic_info->Pad1 = 0; |
| 4318 | rsp->OutputBufferLength = |
Namjae Jeon | 88d3005 | 2021-09-29 15:37:18 +0900 | [diff] [blame] | 4319 | cpu_to_le32(sizeof(struct smb2_file_basic_info)); |
| 4320 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_basic_info)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4321 | return 0; |
| 4322 | } |
| 4323 | |
| 4324 | static unsigned long long get_allocation_size(struct inode *inode, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4325 | struct kstat *stat) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4326 | { |
| 4327 | unsigned long long alloc_size = 0; |
| 4328 | |
| 4329 | if (!S_ISDIR(stat->mode)) { |
| 4330 | if ((inode->i_blocks << 9) <= stat->size) |
| 4331 | alloc_size = stat->size; |
| 4332 | else |
| 4333 | alloc_size = inode->i_blocks << 9; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4334 | } |
| 4335 | |
| 4336 | return alloc_size; |
| 4337 | } |
| 4338 | |
| 4339 | static void get_file_standard_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4340 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4341 | { |
| 4342 | struct smb2_file_standard_info *sinfo; |
| 4343 | unsigned int delete_pending; |
| 4344 | struct inode *inode; |
| 4345 | struct kstat stat; |
| 4346 | |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 4347 | inode = file_inode(fp->filp); |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 4348 | generic_fillattr(file_mnt_user_ns(fp->filp), inode, &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4349 | |
| 4350 | sinfo = (struct smb2_file_standard_info *)rsp->Buffer; |
| 4351 | delete_pending = ksmbd_inode_pending_delete(fp); |
| 4352 | |
| 4353 | sinfo->AllocationSize = cpu_to_le64(get_allocation_size(inode, &stat)); |
| 4354 | sinfo->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size); |
| 4355 | sinfo->NumberOfLinks = cpu_to_le32(get_nlink(&stat) - delete_pending); |
| 4356 | sinfo->DeletePending = delete_pending; |
| 4357 | sinfo->Directory = S_ISDIR(stat.mode) ? 1 : 0; |
| 4358 | rsp->OutputBufferLength = |
| 4359 | cpu_to_le32(sizeof(struct smb2_file_standard_info)); |
| 4360 | inc_rfc1001_len(rsp_org, |
| 4361 | sizeof(struct smb2_file_standard_info)); |
| 4362 | } |
| 4363 | |
| 4364 | static void get_file_alignment_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4365 | void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4366 | { |
| 4367 | struct smb2_file_alignment_info *file_info; |
| 4368 | |
| 4369 | file_info = (struct smb2_file_alignment_info *)rsp->Buffer; |
| 4370 | file_info->AlignmentRequirement = 0; |
| 4371 | rsp->OutputBufferLength = |
| 4372 | cpu_to_le32(sizeof(struct smb2_file_alignment_info)); |
| 4373 | inc_rfc1001_len(rsp_org, |
| 4374 | sizeof(struct smb2_file_alignment_info)); |
| 4375 | } |
| 4376 | |
| 4377 | static int get_file_all_info(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4378 | struct smb2_query_info_rsp *rsp, |
| 4379 | struct ksmbd_file *fp, |
| 4380 | void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4381 | { |
| 4382 | struct ksmbd_conn *conn = work->conn; |
| 4383 | struct smb2_file_all_info *file_info; |
| 4384 | unsigned int delete_pending; |
| 4385 | struct inode *inode; |
| 4386 | struct kstat stat; |
| 4387 | int conv_len; |
| 4388 | char *filename; |
| 4389 | u64 time; |
| 4390 | |
| 4391 | if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) { |
| 4392 | ksmbd_debug(SMB, "no right to read the attributes : 0x%x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4393 | fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4394 | return -EACCES; |
| 4395 | } |
| 4396 | |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 4397 | filename = convert_to_nt_pathname(fp->filename); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4398 | if (!filename) |
| 4399 | return -ENOMEM; |
| 4400 | |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 4401 | inode = file_inode(fp->filp); |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 4402 | generic_fillattr(file_mnt_user_ns(fp->filp), inode, &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4403 | |
| 4404 | ksmbd_debug(SMB, "filename = %s\n", filename); |
| 4405 | delete_pending = ksmbd_inode_pending_delete(fp); |
| 4406 | file_info = (struct smb2_file_all_info *)rsp->Buffer; |
| 4407 | |
| 4408 | file_info->CreationTime = cpu_to_le64(fp->create_time); |
| 4409 | time = ksmbd_UnixTimeToNT(stat.atime); |
| 4410 | file_info->LastAccessTime = cpu_to_le64(time); |
| 4411 | time = ksmbd_UnixTimeToNT(stat.mtime); |
| 4412 | file_info->LastWriteTime = cpu_to_le64(time); |
| 4413 | time = ksmbd_UnixTimeToNT(stat.ctime); |
| 4414 | file_info->ChangeTime = cpu_to_le64(time); |
| 4415 | file_info->Attributes = fp->f_ci->m_fattr; |
| 4416 | file_info->Pad1 = 0; |
| 4417 | file_info->AllocationSize = |
| 4418 | cpu_to_le64(get_allocation_size(inode, &stat)); |
| 4419 | file_info->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size); |
| 4420 | file_info->NumberOfLinks = |
| 4421 | cpu_to_le32(get_nlink(&stat) - delete_pending); |
| 4422 | file_info->DeletePending = delete_pending; |
| 4423 | file_info->Directory = S_ISDIR(stat.mode) ? 1 : 0; |
| 4424 | file_info->Pad2 = 0; |
| 4425 | file_info->IndexNumber = cpu_to_le64(stat.ino); |
| 4426 | file_info->EASize = 0; |
| 4427 | file_info->AccessFlags = fp->daccess; |
| 4428 | file_info->CurrentByteOffset = cpu_to_le64(fp->filp->f_pos); |
| 4429 | file_info->Mode = fp->coption; |
| 4430 | file_info->AlignmentRequirement = 0; |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4431 | conv_len = smbConvertToUTF16((__le16 *)file_info->FileName, filename, |
| 4432 | PATH_MAX, conn->local_nls, 0); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4433 | conv_len *= 2; |
| 4434 | file_info->FileNameLength = cpu_to_le32(conv_len); |
| 4435 | rsp->OutputBufferLength = |
| 4436 | cpu_to_le32(sizeof(struct smb2_file_all_info) + conv_len - 1); |
| 4437 | kfree(filename); |
| 4438 | inc_rfc1001_len(rsp_org, le32_to_cpu(rsp->OutputBufferLength)); |
| 4439 | return 0; |
| 4440 | } |
| 4441 | |
| 4442 | static void get_file_alternate_info(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4443 | struct smb2_query_info_rsp *rsp, |
| 4444 | struct ksmbd_file *fp, |
| 4445 | void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4446 | { |
| 4447 | struct ksmbd_conn *conn = work->conn; |
| 4448 | struct smb2_file_alt_name_info *file_info; |
Namjae Jeon | 493fa2f | 2021-06-29 09:22:16 +0900 | [diff] [blame] | 4449 | struct dentry *dentry = fp->filp->f_path.dentry; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4450 | int conv_len; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4451 | |
Namjae Jeon | 493fa2f | 2021-06-29 09:22:16 +0900 | [diff] [blame] | 4452 | spin_lock(&dentry->d_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4453 | file_info = (struct smb2_file_alt_name_info *)rsp->Buffer; |
| 4454 | conv_len = ksmbd_extract_shortname(conn, |
Namjae Jeon | 493fa2f | 2021-06-29 09:22:16 +0900 | [diff] [blame] | 4455 | dentry->d_name.name, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4456 | file_info->FileName); |
Namjae Jeon | 493fa2f | 2021-06-29 09:22:16 +0900 | [diff] [blame] | 4457 | spin_unlock(&dentry->d_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4458 | file_info->FileNameLength = cpu_to_le32(conv_len); |
| 4459 | rsp->OutputBufferLength = |
| 4460 | cpu_to_le32(sizeof(struct smb2_file_alt_name_info) + conv_len); |
| 4461 | inc_rfc1001_len(rsp_org, le32_to_cpu(rsp->OutputBufferLength)); |
| 4462 | } |
| 4463 | |
| 4464 | static void get_file_stream_info(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4465 | struct smb2_query_info_rsp *rsp, |
| 4466 | struct ksmbd_file *fp, |
| 4467 | void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4468 | { |
| 4469 | struct ksmbd_conn *conn = work->conn; |
| 4470 | struct smb2_file_stream_info *file_info; |
| 4471 | char *stream_name, *xattr_list = NULL, *stream_buf; |
| 4472 | struct kstat stat; |
| 4473 | struct path *path = &fp->filp->f_path; |
| 4474 | ssize_t xattr_list_len; |
| 4475 | int nbytes = 0, streamlen, stream_name_len, next, idx = 0; |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 4476 | int buf_free_len; |
| 4477 | struct smb2_query_info_req *req = ksmbd_req_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4478 | |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 4479 | generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp), |
| 4480 | &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4481 | file_info = (struct smb2_file_stream_info *)rsp->Buffer; |
| 4482 | |
Namjae Jeon | 1ec72153 | 2021-11-21 11:32:39 +0900 | [diff] [blame] | 4483 | buf_free_len = |
| 4484 | smb2_calc_max_out_buf_len(work, 8, |
| 4485 | le32_to_cpu(req->OutputBufferLength)); |
| 4486 | if (buf_free_len < 0) |
| 4487 | goto out; |
| 4488 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4489 | xattr_list_len = ksmbd_vfs_listxattr(path->dentry, &xattr_list); |
| 4490 | if (xattr_list_len < 0) { |
| 4491 | goto out; |
| 4492 | } else if (!xattr_list_len) { |
| 4493 | ksmbd_debug(SMB, "empty xattr in the file\n"); |
| 4494 | goto out; |
| 4495 | } |
| 4496 | |
| 4497 | while (idx < xattr_list_len) { |
| 4498 | stream_name = xattr_list + idx; |
| 4499 | streamlen = strlen(stream_name); |
| 4500 | idx += streamlen + 1; |
| 4501 | |
| 4502 | ksmbd_debug(SMB, "%s, len %d\n", stream_name, streamlen); |
| 4503 | |
| 4504 | if (strncmp(&stream_name[XATTR_USER_PREFIX_LEN], |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4505 | STREAM_PREFIX, STREAM_PREFIX_LEN)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4506 | continue; |
| 4507 | |
| 4508 | stream_name_len = streamlen - (XATTR_USER_PREFIX_LEN + |
| 4509 | STREAM_PREFIX_LEN); |
| 4510 | streamlen = stream_name_len; |
| 4511 | |
| 4512 | /* plus : size */ |
| 4513 | streamlen += 1; |
| 4514 | stream_buf = kmalloc(streamlen + 1, GFP_KERNEL); |
| 4515 | if (!stream_buf) |
| 4516 | break; |
| 4517 | |
| 4518 | streamlen = snprintf(stream_buf, streamlen + 1, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4519 | ":%s", &stream_name[XATTR_NAME_STREAM_LEN]); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4520 | |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 4521 | next = sizeof(struct smb2_file_stream_info) + streamlen * 2; |
Namjae Jeon | 178ca6f | 2021-11-24 10:23:02 +0900 | [diff] [blame] | 4522 | if (next > buf_free_len) { |
| 4523 | kfree(stream_buf); |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 4524 | break; |
Namjae Jeon | 178ca6f | 2021-11-24 10:23:02 +0900 | [diff] [blame] | 4525 | } |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 4526 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4527 | file_info = (struct smb2_file_stream_info *)&rsp->Buffer[nbytes]; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4528 | streamlen = smbConvertToUTF16((__le16 *)file_info->StreamName, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4529 | stream_buf, streamlen, |
| 4530 | conn->local_nls, 0); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4531 | streamlen *= 2; |
| 4532 | kfree(stream_buf); |
| 4533 | file_info->StreamNameLength = cpu_to_le32(streamlen); |
| 4534 | file_info->StreamSize = cpu_to_le64(stream_name_len); |
| 4535 | file_info->StreamAllocationSize = cpu_to_le64(stream_name_len); |
| 4536 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4537 | nbytes += next; |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 4538 | buf_free_len -= next; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4539 | file_info->NextEntryOffset = cpu_to_le32(next); |
| 4540 | } |
| 4541 | |
Namjae Jeon | 1ec72153 | 2021-11-21 11:32:39 +0900 | [diff] [blame] | 4542 | out: |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 4543 | if (!S_ISDIR(stat.mode) && |
| 4544 | buf_free_len >= sizeof(struct smb2_file_stream_info) + 7 * 2) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4545 | file_info = (struct smb2_file_stream_info *) |
| 4546 | &rsp->Buffer[nbytes]; |
| 4547 | streamlen = smbConvertToUTF16((__le16 *)file_info->StreamName, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4548 | "::$DATA", 7, conn->local_nls, 0); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4549 | streamlen *= 2; |
| 4550 | file_info->StreamNameLength = cpu_to_le32(streamlen); |
Namjae Jeon | 1ec72153 | 2021-11-21 11:32:39 +0900 | [diff] [blame] | 4551 | file_info->StreamSize = cpu_to_le64(stat.size); |
| 4552 | file_info->StreamAllocationSize = cpu_to_le64(stat.blocks << 9); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4553 | nbytes += sizeof(struct smb2_file_stream_info) + streamlen; |
| 4554 | } |
| 4555 | |
| 4556 | /* last entry offset should be 0 */ |
| 4557 | file_info->NextEntryOffset = 0; |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 4558 | kvfree(xattr_list); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4559 | |
| 4560 | rsp->OutputBufferLength = cpu_to_le32(nbytes); |
| 4561 | inc_rfc1001_len(rsp_org, nbytes); |
| 4562 | } |
| 4563 | |
| 4564 | static void get_file_internal_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4565 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4566 | { |
| 4567 | struct smb2_file_internal_info *file_info; |
| 4568 | struct kstat stat; |
| 4569 | |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 4570 | generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp), |
| 4571 | &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4572 | file_info = (struct smb2_file_internal_info *)rsp->Buffer; |
| 4573 | file_info->IndexNumber = cpu_to_le64(stat.ino); |
| 4574 | rsp->OutputBufferLength = |
| 4575 | cpu_to_le32(sizeof(struct smb2_file_internal_info)); |
| 4576 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_internal_info)); |
| 4577 | } |
| 4578 | |
| 4579 | 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] | 4580 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4581 | { |
| 4582 | struct smb2_file_ntwrk_info *file_info; |
| 4583 | struct inode *inode; |
| 4584 | struct kstat stat; |
| 4585 | u64 time; |
| 4586 | |
| 4587 | if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4588 | pr_err("no right to read the attributes : 0x%x\n", |
| 4589 | fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4590 | return -EACCES; |
| 4591 | } |
| 4592 | |
| 4593 | file_info = (struct smb2_file_ntwrk_info *)rsp->Buffer; |
| 4594 | |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 4595 | inode = file_inode(fp->filp); |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 4596 | generic_fillattr(file_mnt_user_ns(fp->filp), inode, &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4597 | |
| 4598 | file_info->CreationTime = cpu_to_le64(fp->create_time); |
| 4599 | time = ksmbd_UnixTimeToNT(stat.atime); |
| 4600 | file_info->LastAccessTime = cpu_to_le64(time); |
| 4601 | time = ksmbd_UnixTimeToNT(stat.mtime); |
| 4602 | file_info->LastWriteTime = cpu_to_le64(time); |
| 4603 | time = ksmbd_UnixTimeToNT(stat.ctime); |
| 4604 | file_info->ChangeTime = cpu_to_le64(time); |
| 4605 | file_info->Attributes = fp->f_ci->m_fattr; |
| 4606 | file_info->AllocationSize = |
| 4607 | cpu_to_le64(get_allocation_size(inode, &stat)); |
| 4608 | file_info->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size); |
| 4609 | file_info->Reserved = cpu_to_le32(0); |
| 4610 | rsp->OutputBufferLength = |
| 4611 | cpu_to_le32(sizeof(struct smb2_file_ntwrk_info)); |
| 4612 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_ntwrk_info)); |
| 4613 | return 0; |
| 4614 | } |
| 4615 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4616 | 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] | 4617 | { |
| 4618 | struct smb2_file_ea_info *file_info; |
| 4619 | |
| 4620 | file_info = (struct smb2_file_ea_info *)rsp->Buffer; |
| 4621 | file_info->EASize = 0; |
| 4622 | rsp->OutputBufferLength = |
| 4623 | cpu_to_le32(sizeof(struct smb2_file_ea_info)); |
| 4624 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_ea_info)); |
| 4625 | } |
| 4626 | |
| 4627 | static void get_file_position_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4628 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4629 | { |
| 4630 | struct smb2_file_pos_info *file_info; |
| 4631 | |
| 4632 | file_info = (struct smb2_file_pos_info *)rsp->Buffer; |
| 4633 | file_info->CurrentByteOffset = cpu_to_le64(fp->filp->f_pos); |
| 4634 | rsp->OutputBufferLength = |
| 4635 | cpu_to_le32(sizeof(struct smb2_file_pos_info)); |
| 4636 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_pos_info)); |
| 4637 | } |
| 4638 | |
| 4639 | static void get_file_mode_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4640 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4641 | { |
| 4642 | struct smb2_file_mode_info *file_info; |
| 4643 | |
| 4644 | file_info = (struct smb2_file_mode_info *)rsp->Buffer; |
| 4645 | file_info->Mode = fp->coption & FILE_MODE_INFO_MASK; |
| 4646 | rsp->OutputBufferLength = |
| 4647 | cpu_to_le32(sizeof(struct smb2_file_mode_info)); |
| 4648 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_mode_info)); |
| 4649 | } |
| 4650 | |
| 4651 | static void get_file_compression_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4652 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4653 | { |
| 4654 | struct smb2_file_comp_info *file_info; |
| 4655 | struct kstat stat; |
| 4656 | |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 4657 | generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp), |
| 4658 | &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4659 | |
| 4660 | file_info = (struct smb2_file_comp_info *)rsp->Buffer; |
| 4661 | file_info->CompressedFileSize = cpu_to_le64(stat.blocks << 9); |
| 4662 | file_info->CompressionFormat = COMPRESSION_FORMAT_NONE; |
| 4663 | file_info->CompressionUnitShift = 0; |
| 4664 | file_info->ChunkShift = 0; |
| 4665 | file_info->ClusterShift = 0; |
| 4666 | memset(&file_info->Reserved[0], 0, 3); |
| 4667 | |
| 4668 | rsp->OutputBufferLength = |
| 4669 | cpu_to_le32(sizeof(struct smb2_file_comp_info)); |
| 4670 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_comp_info)); |
| 4671 | } |
| 4672 | |
| 4673 | 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] | 4674 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4675 | { |
| 4676 | struct smb2_file_attr_tag_info *file_info; |
| 4677 | |
| 4678 | if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4679 | pr_err("no right to read the attributes : 0x%x\n", |
| 4680 | fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4681 | return -EACCES; |
| 4682 | } |
| 4683 | |
| 4684 | file_info = (struct smb2_file_attr_tag_info *)rsp->Buffer; |
| 4685 | file_info->FileAttributes = fp->f_ci->m_fattr; |
| 4686 | file_info->ReparseTag = 0; |
| 4687 | rsp->OutputBufferLength = |
| 4688 | cpu_to_le32(sizeof(struct smb2_file_attr_tag_info)); |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4689 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_attr_tag_info)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4690 | return 0; |
| 4691 | } |
| 4692 | |
| 4693 | static int find_file_posix_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4694 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4695 | { |
| 4696 | struct smb311_posix_qinfo *file_info; |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 4697 | struct inode *inode = file_inode(fp->filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4698 | u64 time; |
| 4699 | |
| 4700 | file_info = (struct smb311_posix_qinfo *)rsp->Buffer; |
| 4701 | file_info->CreationTime = cpu_to_le64(fp->create_time); |
| 4702 | time = ksmbd_UnixTimeToNT(inode->i_atime); |
| 4703 | file_info->LastAccessTime = cpu_to_le64(time); |
| 4704 | time = ksmbd_UnixTimeToNT(inode->i_mtime); |
| 4705 | file_info->LastWriteTime = cpu_to_le64(time); |
| 4706 | time = ksmbd_UnixTimeToNT(inode->i_ctime); |
| 4707 | file_info->ChangeTime = cpu_to_le64(time); |
| 4708 | file_info->DosAttributes = fp->f_ci->m_fattr; |
| 4709 | file_info->Inode = cpu_to_le64(inode->i_ino); |
| 4710 | file_info->EndOfFile = cpu_to_le64(inode->i_size); |
| 4711 | file_info->AllocationSize = cpu_to_le64(inode->i_blocks << 9); |
| 4712 | file_info->HardLinks = cpu_to_le32(inode->i_nlink); |
| 4713 | file_info->Mode = cpu_to_le32(inode->i_mode); |
| 4714 | file_info->DeviceId = cpu_to_le32(inode->i_rdev); |
| 4715 | rsp->OutputBufferLength = |
| 4716 | cpu_to_le32(sizeof(struct smb311_posix_qinfo)); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4717 | inc_rfc1001_len(rsp_org, sizeof(struct smb311_posix_qinfo)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4718 | return 0; |
| 4719 | } |
| 4720 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4721 | static int smb2_get_info_file(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4722 | struct smb2_query_info_req *req, |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4723 | struct smb2_query_info_rsp *rsp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4724 | { |
| 4725 | struct ksmbd_file *fp; |
| 4726 | int fileinfoclass = 0; |
| 4727 | int rc = 0; |
| 4728 | int file_infoclass_size; |
| 4729 | unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID; |
| 4730 | |
| 4731 | if (test_share_config_flag(work->tcon->share_conf, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4732 | KSMBD_SHARE_FLAG_PIPE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4733 | /* smb2 info file called for pipe */ |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4734 | return smb2_get_info_file_pipe(work->sess, req, rsp, |
| 4735 | work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4736 | } |
| 4737 | |
| 4738 | if (work->next_smb2_rcv_hdr_off) { |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 4739 | if (!has_file_id(le64_to_cpu(req->VolatileFileId))) { |
| 4740 | ksmbd_debug(SMB, "Compound request set FID = %llu\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4741 | work->compound_fid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4742 | id = work->compound_fid; |
| 4743 | pid = work->compound_pfid; |
| 4744 | } |
| 4745 | } |
| 4746 | |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 4747 | if (!has_file_id(id)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4748 | id = le64_to_cpu(req->VolatileFileId); |
| 4749 | pid = le64_to_cpu(req->PersistentFileId); |
| 4750 | } |
| 4751 | |
| 4752 | fp = ksmbd_lookup_fd_slow(work, id, pid); |
| 4753 | if (!fp) |
| 4754 | return -ENOENT; |
| 4755 | |
| 4756 | fileinfoclass = req->FileInfoClass; |
| 4757 | |
| 4758 | switch (fileinfoclass) { |
| 4759 | case FILE_ACCESS_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4760 | get_file_access_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4761 | file_infoclass_size = FILE_ACCESS_INFORMATION_SIZE; |
| 4762 | break; |
| 4763 | |
| 4764 | case FILE_BASIC_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4765 | rc = get_file_basic_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4766 | file_infoclass_size = FILE_BASIC_INFORMATION_SIZE; |
| 4767 | break; |
| 4768 | |
| 4769 | case FILE_STANDARD_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4770 | get_file_standard_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4771 | file_infoclass_size = FILE_STANDARD_INFORMATION_SIZE; |
| 4772 | break; |
| 4773 | |
| 4774 | case FILE_ALIGNMENT_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4775 | get_file_alignment_info(rsp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4776 | file_infoclass_size = FILE_ALIGNMENT_INFORMATION_SIZE; |
| 4777 | break; |
| 4778 | |
| 4779 | case FILE_ALL_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4780 | rc = get_file_all_info(work, rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4781 | file_infoclass_size = FILE_ALL_INFORMATION_SIZE; |
| 4782 | break; |
| 4783 | |
| 4784 | case FILE_ALTERNATE_NAME_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4785 | get_file_alternate_info(work, rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4786 | file_infoclass_size = FILE_ALTERNATE_NAME_INFORMATION_SIZE; |
| 4787 | break; |
| 4788 | |
| 4789 | case FILE_STREAM_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4790 | get_file_stream_info(work, rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4791 | file_infoclass_size = FILE_STREAM_INFORMATION_SIZE; |
| 4792 | break; |
| 4793 | |
| 4794 | case FILE_INTERNAL_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4795 | get_file_internal_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4796 | file_infoclass_size = FILE_INTERNAL_INFORMATION_SIZE; |
| 4797 | break; |
| 4798 | |
| 4799 | case FILE_NETWORK_OPEN_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4800 | rc = get_file_network_open_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4801 | file_infoclass_size = FILE_NETWORK_OPEN_INFORMATION_SIZE; |
| 4802 | break; |
| 4803 | |
| 4804 | case FILE_EA_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4805 | get_file_ea_info(rsp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4806 | file_infoclass_size = FILE_EA_INFORMATION_SIZE; |
| 4807 | break; |
| 4808 | |
| 4809 | case FILE_FULL_EA_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4810 | rc = smb2_get_ea(work, fp, req, rsp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4811 | file_infoclass_size = FILE_FULL_EA_INFORMATION_SIZE; |
| 4812 | break; |
| 4813 | |
| 4814 | case FILE_POSITION_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4815 | get_file_position_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4816 | file_infoclass_size = FILE_POSITION_INFORMATION_SIZE; |
| 4817 | break; |
| 4818 | |
| 4819 | case FILE_MODE_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4820 | get_file_mode_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4821 | file_infoclass_size = FILE_MODE_INFORMATION_SIZE; |
| 4822 | break; |
| 4823 | |
| 4824 | case FILE_COMPRESSION_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4825 | get_file_compression_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4826 | file_infoclass_size = FILE_COMPRESSION_INFORMATION_SIZE; |
| 4827 | break; |
| 4828 | |
| 4829 | case FILE_ATTRIBUTE_TAG_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4830 | rc = get_file_attribute_tag_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4831 | file_infoclass_size = FILE_ATTRIBUTE_TAG_INFORMATION_SIZE; |
| 4832 | break; |
| 4833 | case SMB_FIND_FILE_POSIX_INFO: |
| 4834 | if (!work->tcon->posix_extensions) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4835 | 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] | 4836 | rc = -EOPNOTSUPP; |
| 4837 | } else { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4838 | rc = find_file_posix_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4839 | file_infoclass_size = sizeof(struct smb311_posix_qinfo); |
| 4840 | } |
| 4841 | break; |
| 4842 | default: |
| 4843 | ksmbd_debug(SMB, "fileinfoclass %d not supported yet\n", |
| 4844 | fileinfoclass); |
| 4845 | rc = -EOPNOTSUPP; |
| 4846 | } |
| 4847 | if (!rc) |
| 4848 | rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength), |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4849 | rsp, work->response_buf, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4850 | file_infoclass_size); |
| 4851 | ksmbd_fd_put(work, fp); |
| 4852 | return rc; |
| 4853 | } |
| 4854 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4855 | static int smb2_get_info_filesystem(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4856 | struct smb2_query_info_req *req, |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4857 | struct smb2_query_info_rsp *rsp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4858 | { |
| 4859 | struct ksmbd_session *sess = work->sess; |
| 4860 | struct ksmbd_conn *conn = sess->conn; |
| 4861 | struct ksmbd_share_config *share = work->tcon->share_conf; |
| 4862 | int fsinfoclass = 0; |
| 4863 | struct kstatfs stfs; |
| 4864 | struct path path; |
| 4865 | int rc = 0, len; |
| 4866 | int fs_infoclass_size = 0; |
| 4867 | |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 4868 | rc = kern_path(share->path, LOOKUP_NO_SYMLINKS, &path); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4869 | if (rc) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4870 | pr_err("cannot create vfs path\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4871 | return -EIO; |
| 4872 | } |
| 4873 | |
| 4874 | rc = vfs_statfs(&path, &stfs); |
| 4875 | if (rc) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4876 | pr_err("cannot do stat of path %s\n", share->path); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4877 | path_put(&path); |
| 4878 | return -EIO; |
| 4879 | } |
| 4880 | |
| 4881 | fsinfoclass = req->FileInfoClass; |
| 4882 | |
| 4883 | switch (fsinfoclass) { |
| 4884 | case FS_DEVICE_INFORMATION: |
| 4885 | { |
| 4886 | struct filesystem_device_info *info; |
| 4887 | |
| 4888 | info = (struct filesystem_device_info *)rsp->Buffer; |
| 4889 | |
| 4890 | info->DeviceType = cpu_to_le32(stfs.f_type); |
| 4891 | info->DeviceCharacteristics = cpu_to_le32(0x00000020); |
| 4892 | rsp->OutputBufferLength = cpu_to_le32(8); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4893 | inc_rfc1001_len(work->response_buf, 8); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4894 | fs_infoclass_size = FS_DEVICE_INFORMATION_SIZE; |
| 4895 | break; |
| 4896 | } |
| 4897 | case FS_ATTRIBUTE_INFORMATION: |
| 4898 | { |
| 4899 | struct filesystem_attribute_info *info; |
| 4900 | size_t sz; |
| 4901 | |
| 4902 | info = (struct filesystem_attribute_info *)rsp->Buffer; |
| 4903 | info->Attributes = cpu_to_le32(FILE_SUPPORTS_OBJECT_IDS | |
| 4904 | FILE_PERSISTENT_ACLS | |
| 4905 | FILE_UNICODE_ON_DISK | |
| 4906 | FILE_CASE_PRESERVED_NAMES | |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 4907 | FILE_CASE_SENSITIVE_SEARCH | |
| 4908 | FILE_SUPPORTS_BLOCK_REFCOUNTING); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4909 | |
| 4910 | info->Attributes |= cpu_to_le32(server_conf.share_fake_fscaps); |
| 4911 | |
| 4912 | info->MaxPathNameComponentLength = cpu_to_le32(stfs.f_namelen); |
| 4913 | len = smbConvertToUTF16((__le16 *)info->FileSystemName, |
| 4914 | "NTFS", PATH_MAX, conn->local_nls, 0); |
| 4915 | len = len * 2; |
| 4916 | info->FileSystemNameLen = cpu_to_le32(len); |
| 4917 | sz = sizeof(struct filesystem_attribute_info) - 2 + len; |
| 4918 | rsp->OutputBufferLength = cpu_to_le32(sz); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4919 | inc_rfc1001_len(work->response_buf, sz); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4920 | fs_infoclass_size = FS_ATTRIBUTE_INFORMATION_SIZE; |
| 4921 | break; |
| 4922 | } |
| 4923 | case FS_VOLUME_INFORMATION: |
| 4924 | { |
| 4925 | struct filesystem_vol_info *info; |
| 4926 | size_t sz; |
Namjae Jeon | 5d2f0b1 | 2021-10-31 09:53:50 +0900 | [diff] [blame] | 4927 | unsigned int serial_crc = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4928 | |
| 4929 | info = (struct filesystem_vol_info *)(rsp->Buffer); |
| 4930 | info->VolumeCreationTime = 0; |
Namjae Jeon | 5d2f0b1 | 2021-10-31 09:53:50 +0900 | [diff] [blame] | 4931 | serial_crc = crc32_le(serial_crc, share->name, |
| 4932 | strlen(share->name)); |
| 4933 | serial_crc = crc32_le(serial_crc, share->path, |
| 4934 | strlen(share->path)); |
| 4935 | serial_crc = crc32_le(serial_crc, ksmbd_netbios_name(), |
| 4936 | strlen(ksmbd_netbios_name())); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4937 | /* Taking dummy value of serial number*/ |
Namjae Jeon | 5d2f0b1 | 2021-10-31 09:53:50 +0900 | [diff] [blame] | 4938 | info->SerialNumber = cpu_to_le32(serial_crc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4939 | len = smbConvertToUTF16((__le16 *)info->VolumeLabel, |
| 4940 | share->name, PATH_MAX, |
| 4941 | conn->local_nls, 0); |
| 4942 | len = len * 2; |
| 4943 | info->VolumeLabelSize = cpu_to_le32(len); |
| 4944 | info->Reserved = 0; |
| 4945 | sz = sizeof(struct filesystem_vol_info) - 2 + len; |
| 4946 | rsp->OutputBufferLength = cpu_to_le32(sz); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4947 | inc_rfc1001_len(work->response_buf, sz); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4948 | fs_infoclass_size = FS_VOLUME_INFORMATION_SIZE; |
| 4949 | break; |
| 4950 | } |
| 4951 | case FS_SIZE_INFORMATION: |
| 4952 | { |
| 4953 | struct filesystem_info *info; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4954 | |
| 4955 | info = (struct filesystem_info *)(rsp->Buffer); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4956 | info->TotalAllocationUnits = cpu_to_le64(stfs.f_blocks); |
| 4957 | info->FreeAllocationUnits = cpu_to_le64(stfs.f_bfree); |
Namjae Jeon | ee81cae | 2021-06-26 22:32:34 +0900 | [diff] [blame] | 4958 | info->SectorsPerAllocationUnit = cpu_to_le32(1); |
| 4959 | info->BytesPerSector = cpu_to_le32(stfs.f_bsize); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4960 | rsp->OutputBufferLength = cpu_to_le32(24); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4961 | inc_rfc1001_len(work->response_buf, 24); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4962 | fs_infoclass_size = FS_SIZE_INFORMATION_SIZE; |
| 4963 | break; |
| 4964 | } |
| 4965 | case FS_FULL_SIZE_INFORMATION: |
| 4966 | { |
| 4967 | struct smb2_fs_full_size_info *info; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4968 | |
| 4969 | info = (struct smb2_fs_full_size_info *)(rsp->Buffer); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4970 | info->TotalAllocationUnits = cpu_to_le64(stfs.f_blocks); |
| 4971 | info->CallerAvailableAllocationUnits = |
| 4972 | cpu_to_le64(stfs.f_bavail); |
| 4973 | info->ActualAvailableAllocationUnits = |
| 4974 | cpu_to_le64(stfs.f_bfree); |
Namjae Jeon | ee81cae | 2021-06-26 22:32:34 +0900 | [diff] [blame] | 4975 | info->SectorsPerAllocationUnit = cpu_to_le32(1); |
| 4976 | info->BytesPerSector = cpu_to_le32(stfs.f_bsize); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4977 | rsp->OutputBufferLength = cpu_to_le32(32); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4978 | inc_rfc1001_len(work->response_buf, 32); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4979 | fs_infoclass_size = FS_FULL_SIZE_INFORMATION_SIZE; |
| 4980 | break; |
| 4981 | } |
| 4982 | case FS_OBJECT_ID_INFORMATION: |
| 4983 | { |
| 4984 | struct object_id_info *info; |
| 4985 | |
| 4986 | info = (struct object_id_info *)(rsp->Buffer); |
| 4987 | |
| 4988 | if (!user_guest(sess->user)) |
| 4989 | memcpy(info->objid, user_passkey(sess->user), 16); |
| 4990 | else |
| 4991 | memset(info->objid, 0, 16); |
| 4992 | |
| 4993 | info->extended_info.magic = cpu_to_le32(EXTENDED_INFO_MAGIC); |
| 4994 | info->extended_info.version = cpu_to_le32(1); |
| 4995 | info->extended_info.release = cpu_to_le32(1); |
| 4996 | info->extended_info.rel_date = 0; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4997 | 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] | 4998 | rsp->OutputBufferLength = cpu_to_le32(64); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 4999 | inc_rfc1001_len(work->response_buf, 64); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5000 | fs_infoclass_size = FS_OBJECT_ID_INFORMATION_SIZE; |
| 5001 | break; |
| 5002 | } |
| 5003 | case FS_SECTOR_SIZE_INFORMATION: |
| 5004 | { |
| 5005 | struct smb3_fs_ss_info *info; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5006 | |
| 5007 | info = (struct smb3_fs_ss_info *)(rsp->Buffer); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5008 | |
Namjae Jeon | 131bac1 | 2021-06-22 16:20:47 +0900 | [diff] [blame] | 5009 | info->LogicalBytesPerSector = cpu_to_le32(stfs.f_bsize); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5010 | info->PhysicalBytesPerSectorForAtomicity = |
Namjae Jeon | 131bac1 | 2021-06-22 16:20:47 +0900 | [diff] [blame] | 5011 | cpu_to_le32(stfs.f_bsize); |
| 5012 | info->PhysicalBytesPerSectorForPerf = cpu_to_le32(stfs.f_bsize); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5013 | info->FSEffPhysicalBytesPerSectorForAtomicity = |
Namjae Jeon | 131bac1 | 2021-06-22 16:20:47 +0900 | [diff] [blame] | 5014 | cpu_to_le32(stfs.f_bsize); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5015 | info->Flags = cpu_to_le32(SSINFO_FLAGS_ALIGNED_DEVICE | |
| 5016 | SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE); |
| 5017 | info->ByteOffsetForSectorAlignment = 0; |
| 5018 | info->ByteOffsetForPartitionAlignment = 0; |
| 5019 | rsp->OutputBufferLength = cpu_to_le32(28); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5020 | inc_rfc1001_len(work->response_buf, 28); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5021 | fs_infoclass_size = FS_SECTOR_SIZE_INFORMATION_SIZE; |
| 5022 | break; |
| 5023 | } |
| 5024 | case FS_CONTROL_INFORMATION: |
| 5025 | { |
| 5026 | /* |
| 5027 | * TODO : The current implementation is based on |
| 5028 | * test result with win7(NTFS) server. It's need to |
| 5029 | * modify this to get valid Quota values |
| 5030 | * from Linux kernel |
| 5031 | */ |
| 5032 | struct smb2_fs_control_info *info; |
| 5033 | |
| 5034 | info = (struct smb2_fs_control_info *)(rsp->Buffer); |
| 5035 | info->FreeSpaceStartFiltering = 0; |
| 5036 | info->FreeSpaceThreshold = 0; |
| 5037 | info->FreeSpaceStopFiltering = 0; |
| 5038 | info->DefaultQuotaThreshold = cpu_to_le64(SMB2_NO_FID); |
| 5039 | info->DefaultQuotaLimit = cpu_to_le64(SMB2_NO_FID); |
| 5040 | info->Padding = 0; |
| 5041 | rsp->OutputBufferLength = cpu_to_le32(48); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5042 | inc_rfc1001_len(work->response_buf, 48); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5043 | fs_infoclass_size = FS_CONTROL_INFORMATION_SIZE; |
| 5044 | break; |
| 5045 | } |
| 5046 | case FS_POSIX_INFORMATION: |
| 5047 | { |
| 5048 | struct filesystem_posix_info *info; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5049 | |
| 5050 | if (!work->tcon->posix_extensions) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5051 | 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] | 5052 | rc = -EOPNOTSUPP; |
| 5053 | } else { |
| 5054 | info = (struct filesystem_posix_info *)(rsp->Buffer); |
Namjae Jeon | ee81cae | 2021-06-26 22:32:34 +0900 | [diff] [blame] | 5055 | info->OptimalTransferSize = cpu_to_le32(stfs.f_bsize); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5056 | info->BlockSize = cpu_to_le32(stfs.f_bsize); |
| 5057 | info->TotalBlocks = cpu_to_le64(stfs.f_blocks); |
| 5058 | info->BlocksAvail = cpu_to_le64(stfs.f_bfree); |
| 5059 | info->UserBlocksAvail = cpu_to_le64(stfs.f_bavail); |
| 5060 | info->TotalFileNodes = cpu_to_le64(stfs.f_files); |
| 5061 | info->FreeFileNodes = cpu_to_le64(stfs.f_ffree); |
| 5062 | rsp->OutputBufferLength = cpu_to_le32(56); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5063 | inc_rfc1001_len(work->response_buf, 56); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5064 | fs_infoclass_size = FS_POSIX_INFORMATION_SIZE; |
| 5065 | } |
| 5066 | break; |
| 5067 | } |
| 5068 | default: |
| 5069 | path_put(&path); |
| 5070 | return -EOPNOTSUPP; |
| 5071 | } |
| 5072 | rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength), |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5073 | rsp, work->response_buf, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5074 | fs_infoclass_size); |
| 5075 | path_put(&path); |
| 5076 | return rc; |
| 5077 | } |
| 5078 | |
| 5079 | static int smb2_get_info_sec(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5080 | struct smb2_query_info_req *req, |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5081 | struct smb2_query_info_rsp *rsp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5082 | { |
| 5083 | struct ksmbd_file *fp; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 5084 | struct user_namespace *user_ns; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5085 | struct smb_ntsd *pntsd = (struct smb_ntsd *)rsp->Buffer, *ppntsd = NULL; |
| 5086 | struct smb_fattr fattr = {{0}}; |
| 5087 | struct inode *inode; |
| 5088 | __u32 secdesclen; |
| 5089 | unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID; |
| 5090 | int addition_info = le32_to_cpu(req->AdditionalInformation); |
| 5091 | int rc; |
| 5092 | |
Namjae Jeon | e294f78 | 2021-06-28 15:26:37 +0900 | [diff] [blame] | 5093 | if (addition_info & ~(OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO | |
| 5094 | PROTECTED_DACL_SECINFO | |
| 5095 | UNPROTECTED_DACL_SECINFO)) { |
Namjae Jeon | 8e537d1 | 2021-11-21 07:48:45 +0900 | [diff] [blame] | 5096 | ksmbd_debug(SMB, "Unsupported addition info: 0x%x)\n", |
Namjae Jeon | e294f78 | 2021-06-28 15:26:37 +0900 | [diff] [blame] | 5097 | addition_info); |
Sebastian Gottschall | ced2b26 | 2021-04-27 15:33:54 +0900 | [diff] [blame] | 5098 | |
| 5099 | pntsd->revision = cpu_to_le16(1); |
| 5100 | pntsd->type = cpu_to_le16(SELF_RELATIVE | DACL_PROTECTED); |
| 5101 | pntsd->osidoffset = 0; |
| 5102 | pntsd->gsidoffset = 0; |
| 5103 | pntsd->sacloffset = 0; |
| 5104 | pntsd->dacloffset = 0; |
| 5105 | |
| 5106 | secdesclen = sizeof(struct smb_ntsd); |
| 5107 | rsp->OutputBufferLength = cpu_to_le32(secdesclen); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5108 | inc_rfc1001_len(work->response_buf, secdesclen); |
Sebastian Gottschall | ced2b26 | 2021-04-27 15:33:54 +0900 | [diff] [blame] | 5109 | |
| 5110 | return 0; |
| 5111 | } |
| 5112 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5113 | if (work->next_smb2_rcv_hdr_off) { |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 5114 | if (!has_file_id(le64_to_cpu(req->VolatileFileId))) { |
| 5115 | ksmbd_debug(SMB, "Compound request set FID = %llu\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5116 | work->compound_fid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5117 | id = work->compound_fid; |
| 5118 | pid = work->compound_pfid; |
| 5119 | } |
| 5120 | } |
| 5121 | |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 5122 | if (!has_file_id(id)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5123 | id = le64_to_cpu(req->VolatileFileId); |
| 5124 | pid = le64_to_cpu(req->PersistentFileId); |
| 5125 | } |
| 5126 | |
| 5127 | fp = ksmbd_lookup_fd_slow(work, id, pid); |
| 5128 | if (!fp) |
| 5129 | return -ENOENT; |
| 5130 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 5131 | user_ns = file_mnt_user_ns(fp->filp); |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 5132 | inode = file_inode(fp->filp); |
Christian Brauner | 43205ca | 2021-08-23 17:13:50 +0200 | [diff] [blame] | 5133 | ksmbd_acls_fattr(&fattr, user_ns, inode); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5134 | |
| 5135 | if (test_share_config_flag(work->tcon->share_conf, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5136 | KSMBD_SHARE_FLAG_ACL_XATTR)) |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 5137 | ksmbd_vfs_get_sd_xattr(work->conn, user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 5138 | fp->filp->f_path.dentry, &ppntsd); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5139 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 5140 | rc = build_sec_desc(user_ns, pntsd, ppntsd, addition_info, |
| 5141 | &secdesclen, &fattr); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5142 | posix_acl_release(fattr.cf_acls); |
| 5143 | posix_acl_release(fattr.cf_dacls); |
| 5144 | kfree(ppntsd); |
| 5145 | ksmbd_fd_put(work, fp); |
| 5146 | if (rc) |
| 5147 | return rc; |
| 5148 | |
| 5149 | rsp->OutputBufferLength = cpu_to_le32(secdesclen); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5150 | inc_rfc1001_len(work->response_buf, secdesclen); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5151 | return 0; |
| 5152 | } |
| 5153 | |
| 5154 | /** |
| 5155 | * smb2_query_info() - handler for smb2 query info command |
| 5156 | * @work: smb work containing query info request buffer |
| 5157 | * |
| 5158 | * Return: 0 on success, otherwise error |
| 5159 | */ |
| 5160 | int smb2_query_info(struct ksmbd_work *work) |
| 5161 | { |
| 5162 | struct smb2_query_info_req *req; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5163 | struct smb2_query_info_rsp *rsp; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5164 | int rc = 0; |
| 5165 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5166 | WORK_BUFFERS(work, req, rsp); |
| 5167 | |
| 5168 | ksmbd_debug(SMB, "GOT query info request\n"); |
| 5169 | |
| 5170 | switch (req->InfoType) { |
| 5171 | case SMB2_O_INFO_FILE: |
| 5172 | ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILE\n"); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5173 | rc = smb2_get_info_file(work, req, rsp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5174 | break; |
| 5175 | case SMB2_O_INFO_FILESYSTEM: |
| 5176 | ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILESYSTEM\n"); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5177 | rc = smb2_get_info_filesystem(work, req, rsp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5178 | break; |
| 5179 | case SMB2_O_INFO_SECURITY: |
| 5180 | ksmbd_debug(SMB, "GOT SMB2_O_INFO_SECURITY\n"); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5181 | rc = smb2_get_info_sec(work, req, rsp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5182 | break; |
| 5183 | default: |
| 5184 | ksmbd_debug(SMB, "InfoType %d not supported yet\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5185 | req->InfoType); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5186 | rc = -EOPNOTSUPP; |
| 5187 | } |
| 5188 | |
| 5189 | if (rc < 0) { |
| 5190 | if (rc == -EACCES) |
| 5191 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 5192 | else if (rc == -ENOENT) |
| 5193 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 5194 | else if (rc == -EIO) |
| 5195 | rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR; |
| 5196 | else if (rc == -EOPNOTSUPP || rsp->hdr.Status == 0) |
| 5197 | rsp->hdr.Status = STATUS_INVALID_INFO_CLASS; |
| 5198 | smb2_set_err_rsp(work); |
| 5199 | |
| 5200 | ksmbd_debug(SMB, "error while processing smb2 query rc = %d\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5201 | rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5202 | return rc; |
| 5203 | } |
| 5204 | rsp->StructureSize = cpu_to_le16(9); |
| 5205 | rsp->OutputBufferOffset = cpu_to_le16(72); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5206 | inc_rfc1001_len(work->response_buf, 8); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5207 | return 0; |
| 5208 | } |
| 5209 | |
| 5210 | /** |
| 5211 | * smb2_close_pipe() - handler for closing IPC pipe |
| 5212 | * @work: smb work containing close request buffer |
| 5213 | * |
| 5214 | * Return: 0 |
| 5215 | */ |
| 5216 | static noinline int smb2_close_pipe(struct ksmbd_work *work) |
| 5217 | { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5218 | u64 id; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5219 | struct smb2_close_req *req = smb2_get_msg(work->request_buf); |
| 5220 | struct smb2_close_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5221 | |
| 5222 | id = le64_to_cpu(req->VolatileFileId); |
| 5223 | ksmbd_session_rpc_close(work->sess, id); |
| 5224 | |
| 5225 | rsp->StructureSize = cpu_to_le16(60); |
| 5226 | rsp->Flags = 0; |
| 5227 | rsp->Reserved = 0; |
| 5228 | rsp->CreationTime = 0; |
| 5229 | rsp->LastAccessTime = 0; |
| 5230 | rsp->LastWriteTime = 0; |
| 5231 | rsp->ChangeTime = 0; |
| 5232 | rsp->AllocationSize = 0; |
| 5233 | rsp->EndOfFile = 0; |
| 5234 | rsp->Attributes = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5235 | inc_rfc1001_len(work->response_buf, 60); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5236 | return 0; |
| 5237 | } |
| 5238 | |
| 5239 | /** |
| 5240 | * smb2_close() - handler for smb2 close file command |
| 5241 | * @work: smb work containing close request buffer |
| 5242 | * |
| 5243 | * Return: 0 |
| 5244 | */ |
| 5245 | int smb2_close(struct ksmbd_work *work) |
| 5246 | { |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 5247 | u64 volatile_id = KSMBD_NO_FID; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5248 | u64 sess_id; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5249 | struct smb2_close_req *req; |
| 5250 | struct smb2_close_rsp *rsp; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5251 | struct ksmbd_conn *conn = work->conn; |
| 5252 | struct ksmbd_file *fp; |
| 5253 | struct inode *inode; |
| 5254 | u64 time; |
| 5255 | int err = 0; |
| 5256 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5257 | WORK_BUFFERS(work, req, rsp); |
| 5258 | |
| 5259 | if (test_share_config_flag(work->tcon->share_conf, |
| 5260 | KSMBD_SHARE_FLAG_PIPE)) { |
| 5261 | ksmbd_debug(SMB, "IPC pipe close request\n"); |
| 5262 | return smb2_close_pipe(work); |
| 5263 | } |
| 5264 | |
| 5265 | sess_id = le64_to_cpu(req->hdr.SessionId); |
| 5266 | if (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS) |
| 5267 | sess_id = work->compound_sid; |
| 5268 | |
| 5269 | work->compound_sid = 0; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5270 | if (check_session_id(conn, sess_id)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5271 | work->compound_sid = sess_id; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5272 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5273 | rsp->hdr.Status = STATUS_USER_SESSION_DELETED; |
| 5274 | if (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS) |
| 5275 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 5276 | err = -EBADF; |
| 5277 | goto out; |
| 5278 | } |
| 5279 | |
| 5280 | if (work->next_smb2_rcv_hdr_off && |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 5281 | !has_file_id(le64_to_cpu(req->VolatileFileId))) { |
| 5282 | if (!has_file_id(work->compound_fid)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5283 | /* file already closed, return FILE_CLOSED */ |
| 5284 | ksmbd_debug(SMB, "file already closed\n"); |
| 5285 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 5286 | err = -EBADF; |
| 5287 | goto out; |
| 5288 | } else { |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 5289 | ksmbd_debug(SMB, |
| 5290 | "Compound request set FID = %llu:%llu\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5291 | work->compound_fid, |
| 5292 | work->compound_pfid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5293 | volatile_id = work->compound_fid; |
| 5294 | |
| 5295 | /* file closed, stored id is not valid anymore */ |
| 5296 | work->compound_fid = KSMBD_NO_FID; |
| 5297 | work->compound_pfid = KSMBD_NO_FID; |
| 5298 | } |
| 5299 | } else { |
| 5300 | volatile_id = le64_to_cpu(req->VolatileFileId); |
| 5301 | } |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 5302 | ksmbd_debug(SMB, "volatile_id = %llu\n", volatile_id); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5303 | |
| 5304 | rsp->StructureSize = cpu_to_le16(60); |
| 5305 | rsp->Reserved = 0; |
| 5306 | |
| 5307 | if (req->Flags == SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB) { |
| 5308 | fp = ksmbd_lookup_fd_fast(work, volatile_id); |
| 5309 | if (!fp) { |
| 5310 | err = -ENOENT; |
| 5311 | goto out; |
| 5312 | } |
| 5313 | |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 5314 | inode = file_inode(fp->filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5315 | rsp->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB; |
| 5316 | rsp->AllocationSize = S_ISDIR(inode->i_mode) ? 0 : |
| 5317 | cpu_to_le64(inode->i_blocks << 9); |
| 5318 | rsp->EndOfFile = cpu_to_le64(inode->i_size); |
| 5319 | rsp->Attributes = fp->f_ci->m_fattr; |
| 5320 | rsp->CreationTime = cpu_to_le64(fp->create_time); |
| 5321 | time = ksmbd_UnixTimeToNT(inode->i_atime); |
| 5322 | rsp->LastAccessTime = cpu_to_le64(time); |
| 5323 | time = ksmbd_UnixTimeToNT(inode->i_mtime); |
| 5324 | rsp->LastWriteTime = cpu_to_le64(time); |
| 5325 | time = ksmbd_UnixTimeToNT(inode->i_ctime); |
| 5326 | rsp->ChangeTime = cpu_to_le64(time); |
| 5327 | ksmbd_fd_put(work, fp); |
| 5328 | } else { |
| 5329 | rsp->Flags = 0; |
| 5330 | rsp->AllocationSize = 0; |
| 5331 | rsp->EndOfFile = 0; |
| 5332 | rsp->Attributes = 0; |
| 5333 | rsp->CreationTime = 0; |
| 5334 | rsp->LastAccessTime = 0; |
| 5335 | rsp->LastWriteTime = 0; |
| 5336 | rsp->ChangeTime = 0; |
| 5337 | } |
| 5338 | |
| 5339 | err = ksmbd_close_fd(work, volatile_id); |
| 5340 | out: |
| 5341 | if (err) { |
| 5342 | if (rsp->hdr.Status == 0) |
| 5343 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 5344 | smb2_set_err_rsp(work); |
| 5345 | } else { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5346 | inc_rfc1001_len(work->response_buf, 60); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5347 | } |
| 5348 | |
| 5349 | return 0; |
| 5350 | } |
| 5351 | |
| 5352 | /** |
| 5353 | * smb2_echo() - handler for smb2 echo(ping) command |
| 5354 | * @work: smb work containing echo request buffer |
| 5355 | * |
| 5356 | * Return: 0 |
| 5357 | */ |
| 5358 | int smb2_echo(struct ksmbd_work *work) |
| 5359 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5360 | struct smb2_echo_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5361 | |
| 5362 | rsp->StructureSize = cpu_to_le16(4); |
| 5363 | rsp->Reserved = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5364 | inc_rfc1001_len(work->response_buf, 4); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5365 | return 0; |
| 5366 | } |
| 5367 | |
Christian Brauner | da1e7ad | 2021-08-23 17:13:47 +0200 | [diff] [blame] | 5368 | static int smb2_rename(struct ksmbd_work *work, |
| 5369 | struct ksmbd_file *fp, |
| 5370 | struct user_namespace *user_ns, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5371 | struct smb2_file_rename_info *file_info, |
| 5372 | struct nls_table *local_nls) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5373 | { |
| 5374 | struct ksmbd_share_config *share = fp->tcon->share_conf; |
| 5375 | char *new_name = NULL, *abs_oldname = NULL, *old_name = NULL; |
| 5376 | char *pathname = NULL; |
| 5377 | struct path path; |
| 5378 | bool file_present = true; |
| 5379 | int rc; |
| 5380 | |
| 5381 | ksmbd_debug(SMB, "setting FILE_RENAME_INFO\n"); |
| 5382 | pathname = kmalloc(PATH_MAX, GFP_KERNEL); |
| 5383 | if (!pathname) |
| 5384 | return -ENOMEM; |
| 5385 | |
| 5386 | abs_oldname = d_path(&fp->filp->f_path, pathname, PATH_MAX); |
| 5387 | if (IS_ERR(abs_oldname)) { |
| 5388 | rc = -EINVAL; |
| 5389 | goto out; |
| 5390 | } |
| 5391 | old_name = strrchr(abs_oldname, '/'); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5392 | if (old_name && old_name[1] != '\0') { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5393 | old_name++; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5394 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5395 | ksmbd_debug(SMB, "can't get last component in path %s\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5396 | abs_oldname); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5397 | rc = -ENOENT; |
| 5398 | goto out; |
| 5399 | } |
| 5400 | |
Marios Makassikis | 80917f1 | 2021-12-01 21:41:19 +0100 | [diff] [blame] | 5401 | new_name = smb2_get_name(file_info->FileName, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5402 | le32_to_cpu(file_info->FileNameLength), |
| 5403 | local_nls); |
| 5404 | if (IS_ERR(new_name)) { |
| 5405 | rc = PTR_ERR(new_name); |
| 5406 | goto out; |
| 5407 | } |
| 5408 | |
| 5409 | if (strchr(new_name, ':')) { |
| 5410 | int s_type; |
| 5411 | char *xattr_stream_name, *stream_name = NULL; |
| 5412 | size_t xattr_stream_size; |
| 5413 | int len; |
| 5414 | |
| 5415 | rc = parse_stream_name(new_name, &stream_name, &s_type); |
| 5416 | if (rc < 0) |
| 5417 | goto out; |
| 5418 | |
| 5419 | len = strlen(new_name); |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 5420 | if (len > 0 && new_name[len - 1] != '/') { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5421 | pr_err("not allow base filename in rename\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5422 | rc = -ESHARE; |
| 5423 | goto out; |
| 5424 | } |
| 5425 | |
| 5426 | rc = ksmbd_vfs_xattr_stream_name(stream_name, |
| 5427 | &xattr_stream_name, |
| 5428 | &xattr_stream_size, |
| 5429 | s_type); |
| 5430 | if (rc) |
| 5431 | goto out; |
| 5432 | |
Christian Brauner | da1e7ad | 2021-08-23 17:13:47 +0200 | [diff] [blame] | 5433 | rc = ksmbd_vfs_setxattr(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 5434 | fp->filp->f_path.dentry, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5435 | xattr_stream_name, |
| 5436 | NULL, 0, 0); |
| 5437 | if (rc < 0) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5438 | pr_err("failed to store stream name in xattr: %d\n", |
| 5439 | rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5440 | rc = -EINVAL; |
| 5441 | goto out; |
| 5442 | } |
| 5443 | |
| 5444 | goto out; |
| 5445 | } |
| 5446 | |
| 5447 | ksmbd_debug(SMB, "new name %s\n", new_name); |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 5448 | rc = ksmbd_vfs_kern_path(work, new_name, LOOKUP_NO_SYMLINKS, &path, 1); |
| 5449 | if (rc) { |
| 5450 | if (rc != -ENOENT) |
| 5451 | goto out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5452 | file_present = false; |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 5453 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5454 | path_put(&path); |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 5455 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5456 | |
| 5457 | if (ksmbd_share_veto_filename(share, new_name)) { |
| 5458 | rc = -ENOENT; |
| 5459 | ksmbd_debug(SMB, "Can't rename vetoed file: %s\n", new_name); |
| 5460 | goto out; |
| 5461 | } |
| 5462 | |
| 5463 | if (file_info->ReplaceIfExists) { |
| 5464 | if (file_present) { |
| 5465 | rc = ksmbd_vfs_remove_file(work, new_name); |
| 5466 | if (rc) { |
| 5467 | if (rc != -ENOTEMPTY) |
| 5468 | rc = -EINVAL; |
| 5469 | ksmbd_debug(SMB, "cannot delete %s, rc %d\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5470 | new_name, rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5471 | goto out; |
| 5472 | } |
| 5473 | } |
| 5474 | } else { |
| 5475 | if (file_present && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5476 | strncmp(old_name, path.dentry->d_name.name, strlen(old_name))) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5477 | rc = -EEXIST; |
| 5478 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5479 | "cannot rename already existing file\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5480 | goto out; |
| 5481 | } |
| 5482 | } |
| 5483 | |
| 5484 | rc = ksmbd_vfs_fp_rename(work, fp, new_name); |
| 5485 | out: |
| 5486 | kfree(pathname); |
| 5487 | if (!IS_ERR(new_name)) |
Marios Makassikis | 915f570 | 2021-04-13 13:25:57 +0900 | [diff] [blame] | 5488 | kfree(new_name); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5489 | return rc; |
| 5490 | } |
| 5491 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5492 | static int smb2_create_link(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5493 | struct ksmbd_share_config *share, |
| 5494 | struct smb2_file_link_info *file_info, |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5495 | unsigned int buf_len, struct file *filp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5496 | struct nls_table *local_nls) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5497 | { |
| 5498 | char *link_name = NULL, *target_name = NULL, *pathname = NULL; |
| 5499 | struct path path; |
| 5500 | bool file_present = true; |
| 5501 | int rc; |
| 5502 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5503 | if (buf_len < (u64)sizeof(struct smb2_file_link_info) + |
| 5504 | le32_to_cpu(file_info->FileNameLength)) |
| 5505 | return -EINVAL; |
| 5506 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5507 | ksmbd_debug(SMB, "setting FILE_LINK_INFORMATION\n"); |
| 5508 | pathname = kmalloc(PATH_MAX, GFP_KERNEL); |
| 5509 | if (!pathname) |
| 5510 | return -ENOMEM; |
| 5511 | |
Marios Makassikis | 80917f1 | 2021-12-01 21:41:19 +0100 | [diff] [blame] | 5512 | link_name = smb2_get_name(file_info->FileName, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5513 | le32_to_cpu(file_info->FileNameLength), |
| 5514 | local_nls); |
| 5515 | if (IS_ERR(link_name) || S_ISDIR(file_inode(filp)->i_mode)) { |
| 5516 | rc = -EINVAL; |
| 5517 | goto out; |
| 5518 | } |
| 5519 | |
| 5520 | ksmbd_debug(SMB, "link name is %s\n", link_name); |
| 5521 | target_name = d_path(&filp->f_path, pathname, PATH_MAX); |
| 5522 | if (IS_ERR(target_name)) { |
| 5523 | rc = -EINVAL; |
| 5524 | goto out; |
| 5525 | } |
| 5526 | |
| 5527 | ksmbd_debug(SMB, "target name is %s\n", target_name); |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 5528 | rc = ksmbd_vfs_kern_path(work, link_name, LOOKUP_NO_SYMLINKS, &path, 0); |
| 5529 | if (rc) { |
| 5530 | if (rc != -ENOENT) |
| 5531 | goto out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5532 | file_present = false; |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 5533 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5534 | path_put(&path); |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 5535 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5536 | |
| 5537 | if (file_info->ReplaceIfExists) { |
| 5538 | if (file_present) { |
| 5539 | rc = ksmbd_vfs_remove_file(work, link_name); |
| 5540 | if (rc) { |
| 5541 | rc = -EINVAL; |
| 5542 | ksmbd_debug(SMB, "cannot delete %s\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5543 | link_name); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5544 | goto out; |
| 5545 | } |
| 5546 | } |
| 5547 | } else { |
| 5548 | if (file_present) { |
| 5549 | rc = -EEXIST; |
| 5550 | ksmbd_debug(SMB, "link already exists\n"); |
| 5551 | goto out; |
| 5552 | } |
| 5553 | } |
| 5554 | |
| 5555 | rc = ksmbd_vfs_link(work, target_name, link_name); |
| 5556 | if (rc) |
| 5557 | rc = -EINVAL; |
| 5558 | out: |
| 5559 | if (!IS_ERR(link_name)) |
Marios Makassikis | 915f570 | 2021-04-13 13:25:57 +0900 | [diff] [blame] | 5560 | kfree(link_name); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5561 | kfree(pathname); |
| 5562 | return rc; |
| 5563 | } |
| 5564 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5565 | static int set_file_basic_info(struct ksmbd_file *fp, |
| 5566 | struct smb2_file_basic_info *file_info, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5567 | struct ksmbd_share_config *share) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5568 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5569 | struct iattr attrs; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5570 | struct file *filp; |
| 5571 | struct inode *inode; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 5572 | struct user_namespace *user_ns; |
Namjae Jeon | 4ffd526 | 2021-09-07 08:15:21 +0900 | [diff] [blame] | 5573 | int rc = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5574 | |
Marios Makassikis | 7adfd4f | 2021-04-27 15:30:22 +0900 | [diff] [blame] | 5575 | if (!(fp->daccess & FILE_WRITE_ATTRIBUTES_LE)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5576 | return -EACCES; |
| 5577 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5578 | attrs.ia_valid = 0; |
| 5579 | filp = fp->filp; |
| 5580 | inode = file_inode(filp); |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 5581 | user_ns = file_mnt_user_ns(filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5582 | |
| 5583 | if (file_info->CreationTime) |
| 5584 | fp->create_time = le64_to_cpu(file_info->CreationTime); |
| 5585 | |
| 5586 | if (file_info->LastAccessTime) { |
| 5587 | attrs.ia_atime = ksmbd_NTtimeToUnix(file_info->LastAccessTime); |
| 5588 | attrs.ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET); |
| 5589 | } |
| 5590 | |
Namjae Jeon | 64e7875 | 2021-10-03 13:19:00 +0900 | [diff] [blame] | 5591 | attrs.ia_valid |= ATTR_CTIME; |
| 5592 | if (file_info->ChangeTime) |
Christian Brauner | db7fb6f | 2021-08-26 10:07:05 +0900 | [diff] [blame] | 5593 | attrs.ia_ctime = ksmbd_NTtimeToUnix(file_info->ChangeTime); |
Namjae Jeon | 64e7875 | 2021-10-03 13:19:00 +0900 | [diff] [blame] | 5594 | else |
| 5595 | attrs.ia_ctime = inode->i_ctime; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5596 | |
| 5597 | if (file_info->LastWriteTime) { |
| 5598 | attrs.ia_mtime = ksmbd_NTtimeToUnix(file_info->LastWriteTime); |
| 5599 | attrs.ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET); |
| 5600 | } |
| 5601 | |
| 5602 | if (file_info->Attributes) { |
| 5603 | if (!S_ISDIR(inode->i_mode) && |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 5604 | file_info->Attributes & FILE_ATTRIBUTE_DIRECTORY_LE) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5605 | pr_err("can't change a file to a directory\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5606 | return -EINVAL; |
| 5607 | } |
| 5608 | |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 5609 | if (!(S_ISDIR(inode->i_mode) && file_info->Attributes == FILE_ATTRIBUTE_NORMAL_LE)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5610 | fp->f_ci->m_fattr = file_info->Attributes | |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 5611 | (fp->f_ci->m_fattr & FILE_ATTRIBUTE_DIRECTORY_LE); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5612 | } |
| 5613 | |
| 5614 | if (test_share_config_flag(share, KSMBD_SHARE_FLAG_STORE_DOS_ATTRS) && |
| 5615 | (file_info->CreationTime || file_info->Attributes)) { |
| 5616 | struct xattr_dos_attrib da = {0}; |
| 5617 | |
| 5618 | da.version = 4; |
| 5619 | da.itime = fp->itime; |
| 5620 | da.create_time = fp->create_time; |
| 5621 | da.attr = le32_to_cpu(fp->f_ci->m_fattr); |
| 5622 | da.flags = XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME | |
| 5623 | XATTR_DOSINFO_ITIME; |
| 5624 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 5625 | rc = ksmbd_vfs_set_dos_attrib_xattr(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 5626 | filp->f_path.dentry, &da); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5627 | if (rc) |
| 5628 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5629 | "failed to restore file attribute in EA\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5630 | rc = 0; |
| 5631 | } |
| 5632 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5633 | if (attrs.ia_valid) { |
| 5634 | struct dentry *dentry = filp->f_path.dentry; |
| 5635 | struct inode *inode = d_inode(dentry); |
| 5636 | |
| 5637 | if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) |
| 5638 | return -EACCES; |
| 5639 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5640 | inode_lock(inode); |
Namjae Jeon | 64e7875 | 2021-10-03 13:19:00 +0900 | [diff] [blame] | 5641 | inode->i_ctime = attrs.ia_ctime; |
| 5642 | attrs.ia_valid &= ~ATTR_CTIME; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 5643 | rc = notify_change(user_ns, dentry, &attrs, NULL); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5644 | inode_unlock(inode); |
| 5645 | } |
Christian Brauner | eb5784f | 2021-08-23 17:13:55 +0200 | [diff] [blame] | 5646 | return rc; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5647 | } |
| 5648 | |
| 5649 | static int set_file_allocation_info(struct ksmbd_work *work, |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5650 | struct ksmbd_file *fp, |
| 5651 | struct smb2_file_alloc_info *file_alloc_info) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5652 | { |
| 5653 | /* |
| 5654 | * TODO : It's working fine only when store dos attributes |
| 5655 | * is not yes. need to implement a logic which works |
| 5656 | * properly with any smb.conf option |
| 5657 | */ |
| 5658 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5659 | loff_t alloc_blks; |
| 5660 | struct inode *inode; |
| 5661 | int rc; |
| 5662 | |
Marios Makassikis | a299669 | 2021-04-27 15:29:01 +0900 | [diff] [blame] | 5663 | if (!(fp->daccess & FILE_WRITE_DATA_LE)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5664 | return -EACCES; |
| 5665 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5666 | alloc_blks = (le64_to_cpu(file_alloc_info->AllocationSize) + 511) >> 9; |
| 5667 | inode = file_inode(fp->filp); |
| 5668 | |
| 5669 | if (alloc_blks > inode->i_blocks) { |
Namjae Jeon | e8c0619 | 2021-06-22 11:06:11 +0900 | [diff] [blame] | 5670 | smb_break_all_levII_oplock(work, fp, 1); |
| 5671 | rc = vfs_fallocate(fp->filp, FALLOC_FL_KEEP_SIZE, 0, |
| 5672 | alloc_blks * 512); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5673 | if (rc && rc != -EOPNOTSUPP) { |
Namjae Jeon | e8c0619 | 2021-06-22 11:06:11 +0900 | [diff] [blame] | 5674 | pr_err("vfs_fallocate is failed : %d\n", rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5675 | return rc; |
| 5676 | } |
| 5677 | } else if (alloc_blks < inode->i_blocks) { |
| 5678 | loff_t size; |
| 5679 | |
| 5680 | /* |
| 5681 | * Allocation size could be smaller than original one |
| 5682 | * which means allocated blocks in file should be |
| 5683 | * deallocated. use truncate to cut out it, but inode |
| 5684 | * size is also updated with truncate offset. |
| 5685 | * inode size is retained by backup inode size. |
| 5686 | */ |
| 5687 | size = i_size_read(inode); |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 5688 | rc = ksmbd_vfs_truncate(work, fp, alloc_blks * 512); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5689 | if (rc) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5690 | pr_err("truncate failed! filename : %s, err %d\n", |
| 5691 | fp->filename, rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5692 | return rc; |
| 5693 | } |
| 5694 | if (size < alloc_blks * 512) |
| 5695 | i_size_write(inode, size); |
| 5696 | } |
| 5697 | return 0; |
| 5698 | } |
| 5699 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5700 | 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] | 5701 | struct smb2_file_eof_info *file_eof_info) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5702 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5703 | loff_t newsize; |
| 5704 | struct inode *inode; |
| 5705 | int rc; |
| 5706 | |
Marios Makassikis | a299669 | 2021-04-27 15:29:01 +0900 | [diff] [blame] | 5707 | if (!(fp->daccess & FILE_WRITE_DATA_LE)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5708 | return -EACCES; |
| 5709 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5710 | newsize = le64_to_cpu(file_eof_info->EndOfFile); |
| 5711 | inode = file_inode(fp->filp); |
| 5712 | |
| 5713 | /* |
| 5714 | * If FILE_END_OF_FILE_INFORMATION of set_info_file is called |
| 5715 | * on FAT32 shared device, truncate execution time is too long |
| 5716 | * and network error could cause from windows client. because |
| 5717 | * truncate of some filesystem like FAT32 fill zero data in |
| 5718 | * truncated range. |
| 5719 | */ |
| 5720 | if (inode->i_sb->s_magic != MSDOS_SUPER_MAGIC) { |
| 5721 | ksmbd_debug(SMB, "filename : %s truncated to newsize %lld\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5722 | fp->filename, newsize); |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 5723 | rc = ksmbd_vfs_truncate(work, fp, newsize); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5724 | if (rc) { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5725 | ksmbd_debug(SMB, "truncate failed! filename : %s err %d\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5726 | fp->filename, rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5727 | if (rc != -EAGAIN) |
| 5728 | rc = -EBADF; |
| 5729 | return rc; |
| 5730 | } |
| 5731 | } |
| 5732 | return 0; |
| 5733 | } |
| 5734 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5735 | 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] | 5736 | struct smb2_file_rename_info *rename_info, |
| 5737 | unsigned int buf_len) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5738 | { |
Christian Brauner | da1e7ad | 2021-08-23 17:13:47 +0200 | [diff] [blame] | 5739 | struct user_namespace *user_ns; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5740 | struct ksmbd_file *parent_fp; |
Namjae Jeon | 12202c0 | 2021-06-29 09:23:56 +0900 | [diff] [blame] | 5741 | struct dentry *parent; |
| 5742 | struct dentry *dentry = fp->filp->f_path.dentry; |
| 5743 | int ret; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5744 | |
| 5745 | if (!(fp->daccess & FILE_DELETE_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5746 | pr_err("no right to delete : 0x%x\n", fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5747 | return -EACCES; |
| 5748 | } |
| 5749 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5750 | if (buf_len < (u64)sizeof(struct smb2_file_rename_info) + |
| 5751 | le32_to_cpu(rename_info->FileNameLength)) |
| 5752 | return -EINVAL; |
| 5753 | |
Christian Brauner | da1e7ad | 2021-08-23 17:13:47 +0200 | [diff] [blame] | 5754 | user_ns = file_mnt_user_ns(fp->filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5755 | if (ksmbd_stream_fd(fp)) |
| 5756 | goto next; |
| 5757 | |
Namjae Jeon | 12202c0 | 2021-06-29 09:23:56 +0900 | [diff] [blame] | 5758 | parent = dget_parent(dentry); |
Christian Brauner | da1e7ad | 2021-08-23 17:13:47 +0200 | [diff] [blame] | 5759 | ret = ksmbd_vfs_lock_parent(user_ns, parent, dentry); |
Namjae Jeon | 12202c0 | 2021-06-29 09:23:56 +0900 | [diff] [blame] | 5760 | if (ret) { |
| 5761 | dput(parent); |
| 5762 | return ret; |
| 5763 | } |
| 5764 | |
| 5765 | parent_fp = ksmbd_lookup_fd_inode(d_inode(parent)); |
| 5766 | inode_unlock(d_inode(parent)); |
| 5767 | dput(parent); |
| 5768 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5769 | if (parent_fp) { |
| 5770 | if (parent_fp->daccess & FILE_DELETE_LE) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5771 | pr_err("parent dir is opened with delete access\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5772 | return -ESHARE; |
| 5773 | } |
| 5774 | } |
| 5775 | next: |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5776 | return smb2_rename(work, fp, user_ns, rename_info, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5777 | work->sess->conn->local_nls); |
| 5778 | } |
| 5779 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5780 | static int set_file_disposition_info(struct ksmbd_file *fp, |
| 5781 | struct smb2_file_disposition_info *file_info) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5782 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5783 | struct inode *inode; |
| 5784 | |
| 5785 | if (!(fp->daccess & FILE_DELETE_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5786 | pr_err("no right to delete : 0x%x\n", fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5787 | return -EACCES; |
| 5788 | } |
| 5789 | |
| 5790 | inode = file_inode(fp->filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5791 | if (file_info->DeletePending) { |
| 5792 | if (S_ISDIR(inode->i_mode) && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5793 | ksmbd_vfs_empty_dir(fp) == -ENOTEMPTY) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5794 | return -EBUSY; |
| 5795 | ksmbd_set_inode_pending_delete(fp); |
| 5796 | } else { |
| 5797 | ksmbd_clear_inode_pending_delete(fp); |
| 5798 | } |
| 5799 | return 0; |
| 5800 | } |
| 5801 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5802 | static int set_file_position_info(struct ksmbd_file *fp, |
| 5803 | struct smb2_file_pos_info *file_info) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5804 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5805 | loff_t current_byte_offset; |
Namjae Jeon | ee81cae | 2021-06-26 22:32:34 +0900 | [diff] [blame] | 5806 | unsigned long sector_size; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5807 | struct inode *inode; |
| 5808 | |
| 5809 | inode = file_inode(fp->filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5810 | current_byte_offset = le64_to_cpu(file_info->CurrentByteOffset); |
Namjae Jeon | ee81cae | 2021-06-26 22:32:34 +0900 | [diff] [blame] | 5811 | sector_size = inode->i_sb->s_blocksize; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5812 | |
| 5813 | if (current_byte_offset < 0 || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5814 | (fp->coption == FILE_NO_INTERMEDIATE_BUFFERING_LE && |
| 5815 | current_byte_offset & (sector_size - 1))) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5816 | pr_err("CurrentByteOffset is not valid : %llu\n", |
| 5817 | current_byte_offset); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5818 | return -EINVAL; |
| 5819 | } |
| 5820 | |
| 5821 | fp->filp->f_pos = current_byte_offset; |
| 5822 | return 0; |
| 5823 | } |
| 5824 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5825 | static int set_file_mode_info(struct ksmbd_file *fp, |
| 5826 | struct smb2_file_mode_info *file_info) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5827 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5828 | __le32 mode; |
| 5829 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5830 | mode = file_info->Mode; |
| 5831 | |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 5832 | if ((mode & ~FILE_MODE_INFO_MASK)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5833 | 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] | 5834 | return -EINVAL; |
| 5835 | } |
| 5836 | |
| 5837 | /* |
| 5838 | * TODO : need to implement consideration for |
| 5839 | * FILE_SYNCHRONOUS_IO_ALERT and FILE_SYNCHRONOUS_IO_NONALERT |
| 5840 | */ |
| 5841 | ksmbd_vfs_set_fadvise(fp->filp, mode); |
| 5842 | fp->coption = mode; |
| 5843 | return 0; |
| 5844 | } |
| 5845 | |
| 5846 | /** |
| 5847 | * smb2_set_info_file() - handler for smb2 set info command |
| 5848 | * @work: smb work containing set info command buffer |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 5849 | * @fp: ksmbd_file pointer |
Yang Li | 4bfd9ee | 2021-12-21 17:07:12 +0800 | [diff] [blame] | 5850 | * @req: request buffer pointer |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 5851 | * @share: ksmbd_share_config pointer |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5852 | * |
| 5853 | * Return: 0 on success, otherwise error |
| 5854 | * TODO: need to implement an error handling for STATUS_INFO_LENGTH_MISMATCH |
| 5855 | */ |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5856 | 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] | 5857 | struct smb2_set_info_req *req, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5858 | struct ksmbd_share_config *share) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5859 | { |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5860 | unsigned int buf_len = le32_to_cpu(req->BufferLength); |
| 5861 | |
| 5862 | switch (req->FileInfoClass) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5863 | case FILE_BASIC_INFORMATION: |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5864 | { |
| 5865 | if (buf_len < sizeof(struct smb2_file_basic_info)) |
| 5866 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5867 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5868 | return set_file_basic_info(fp, (struct smb2_file_basic_info *)req->Buffer, share); |
| 5869 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5870 | case FILE_ALLOCATION_INFORMATION: |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5871 | { |
| 5872 | if (buf_len < sizeof(struct smb2_file_alloc_info)) |
| 5873 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5874 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5875 | return set_file_allocation_info(work, fp, |
| 5876 | (struct smb2_file_alloc_info *)req->Buffer); |
| 5877 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5878 | case FILE_END_OF_FILE_INFORMATION: |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5879 | { |
| 5880 | if (buf_len < sizeof(struct smb2_file_eof_info)) |
| 5881 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5882 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5883 | return set_end_of_file_info(work, fp, |
| 5884 | (struct smb2_file_eof_info *)req->Buffer); |
| 5885 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5886 | case FILE_RENAME_INFORMATION: |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5887 | { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5888 | if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5889 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5890 | "User does not have write permission\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5891 | return -EACCES; |
| 5892 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5893 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5894 | if (buf_len < sizeof(struct smb2_file_rename_info)) |
| 5895 | return -EINVAL; |
| 5896 | |
| 5897 | return set_rename_info(work, fp, |
| 5898 | (struct smb2_file_rename_info *)req->Buffer, |
| 5899 | buf_len); |
| 5900 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5901 | case FILE_LINK_INFORMATION: |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5902 | { |
| 5903 | if (buf_len < sizeof(struct smb2_file_link_info)) |
| 5904 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5905 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5906 | return smb2_create_link(work, work->tcon->share_conf, |
| 5907 | (struct smb2_file_link_info *)req->Buffer, |
| 5908 | buf_len, fp->filp, |
| 5909 | work->sess->conn->local_nls); |
| 5910 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5911 | case FILE_DISPOSITION_INFORMATION: |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5912 | { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5913 | if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5914 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5915 | "User does not have write permission\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5916 | return -EACCES; |
| 5917 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5918 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5919 | if (buf_len < sizeof(struct smb2_file_disposition_info)) |
| 5920 | return -EINVAL; |
| 5921 | |
| 5922 | return set_file_disposition_info(fp, |
| 5923 | (struct smb2_file_disposition_info *)req->Buffer); |
| 5924 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5925 | case FILE_FULL_EA_INFORMATION: |
| 5926 | { |
| 5927 | if (!(fp->daccess & FILE_WRITE_EA_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5928 | pr_err("Not permitted to write ext attr: 0x%x\n", |
| 5929 | fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5930 | return -EACCES; |
| 5931 | } |
| 5932 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5933 | if (buf_len < sizeof(struct smb2_ea_info)) |
| 5934 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5935 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5936 | return smb2_set_ea((struct smb2_ea_info *)req->Buffer, |
| 5937 | buf_len, &fp->filp->f_path); |
| 5938 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5939 | case FILE_POSITION_INFORMATION: |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5940 | { |
| 5941 | if (buf_len < sizeof(struct smb2_file_pos_info)) |
| 5942 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5943 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5944 | return set_file_position_info(fp, (struct smb2_file_pos_info *)req->Buffer); |
| 5945 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5946 | case FILE_MODE_INFORMATION: |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5947 | { |
| 5948 | if (buf_len < sizeof(struct smb2_file_mode_info)) |
| 5949 | return -EINVAL; |
| 5950 | |
| 5951 | return set_file_mode_info(fp, (struct smb2_file_mode_info *)req->Buffer); |
| 5952 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5953 | } |
| 5954 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5955 | pr_err("Unimplemented Fileinfoclass :%d\n", req->FileInfoClass); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5956 | return -EOPNOTSUPP; |
| 5957 | } |
| 5958 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5959 | 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] | 5960 | char *buffer, int buf_len) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5961 | { |
| 5962 | struct smb_ntsd *pntsd = (struct smb_ntsd *)buffer; |
| 5963 | |
| 5964 | fp->saccess |= FILE_SHARE_DELETE_LE; |
| 5965 | |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 5966 | 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] | 5967 | buf_len, false); |
| 5968 | } |
| 5969 | |
| 5970 | /** |
| 5971 | * smb2_set_info() - handler for smb2 set info command handler |
| 5972 | * @work: smb work containing set info request buffer |
| 5973 | * |
| 5974 | * Return: 0 on success, otherwise error |
| 5975 | */ |
| 5976 | int smb2_set_info(struct ksmbd_work *work) |
| 5977 | { |
| 5978 | struct smb2_set_info_req *req; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5979 | struct smb2_set_info_rsp *rsp; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5980 | struct ksmbd_file *fp; |
| 5981 | int rc = 0; |
| 5982 | unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID; |
| 5983 | |
| 5984 | ksmbd_debug(SMB, "Received set info request\n"); |
| 5985 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5986 | if (work->next_smb2_rcv_hdr_off) { |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 5987 | req = ksmbd_req_buf_next(work); |
| 5988 | rsp = ksmbd_resp_buf_next(work); |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 5989 | if (!has_file_id(le64_to_cpu(req->VolatileFileId))) { |
| 5990 | ksmbd_debug(SMB, "Compound request set FID = %llu\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5991 | work->compound_fid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5992 | id = work->compound_fid; |
| 5993 | pid = work->compound_pfid; |
| 5994 | } |
| 5995 | } else { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 5996 | req = smb2_get_msg(work->request_buf); |
| 5997 | rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5998 | } |
| 5999 | |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 6000 | if (!has_file_id(id)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6001 | id = le64_to_cpu(req->VolatileFileId); |
| 6002 | pid = le64_to_cpu(req->PersistentFileId); |
| 6003 | } |
| 6004 | |
| 6005 | fp = ksmbd_lookup_fd_slow(work, id, pid); |
| 6006 | if (!fp) { |
| 6007 | ksmbd_debug(SMB, "Invalid id for close: %u\n", id); |
| 6008 | rc = -ENOENT; |
| 6009 | goto err_out; |
| 6010 | } |
| 6011 | |
| 6012 | switch (req->InfoType) { |
| 6013 | case SMB2_O_INFO_FILE: |
| 6014 | ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILE\n"); |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 6015 | rc = smb2_set_info_file(work, fp, req, work->tcon->share_conf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6016 | break; |
| 6017 | case SMB2_O_INFO_SECURITY: |
| 6018 | ksmbd_debug(SMB, "GOT SMB2_O_INFO_SECURITY\n"); |
Namjae Jeon | e70e392 | 2021-08-21 23:26:01 +0900 | [diff] [blame] | 6019 | if (ksmbd_override_fsids(work)) { |
| 6020 | rc = -ENOMEM; |
| 6021 | goto err_out; |
| 6022 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6023 | rc = smb2_set_info_sec(fp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6024 | le32_to_cpu(req->AdditionalInformation), |
| 6025 | req->Buffer, |
| 6026 | le32_to_cpu(req->BufferLength)); |
Namjae Jeon | e70e392 | 2021-08-21 23:26:01 +0900 | [diff] [blame] | 6027 | ksmbd_revert_fsids(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6028 | break; |
| 6029 | default: |
| 6030 | rc = -EOPNOTSUPP; |
| 6031 | } |
| 6032 | |
| 6033 | if (rc < 0) |
| 6034 | goto err_out; |
| 6035 | |
| 6036 | rsp->StructureSize = cpu_to_le16(2); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6037 | inc_rfc1001_len(work->response_buf, 2); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6038 | ksmbd_fd_put(work, fp); |
| 6039 | return 0; |
| 6040 | |
| 6041 | err_out: |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 6042 | if (rc == -EACCES || rc == -EPERM || rc == -EXDEV) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6043 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 6044 | else if (rc == -EINVAL) |
| 6045 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 6046 | else if (rc == -ESHARE) |
| 6047 | rsp->hdr.Status = STATUS_SHARING_VIOLATION; |
| 6048 | else if (rc == -ENOENT) |
| 6049 | rsp->hdr.Status = STATUS_OBJECT_NAME_INVALID; |
| 6050 | else if (rc == -EBUSY || rc == -ENOTEMPTY) |
| 6051 | rsp->hdr.Status = STATUS_DIRECTORY_NOT_EMPTY; |
| 6052 | else if (rc == -EAGAIN) |
| 6053 | rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT; |
Namjae Jeon | ff1d572 | 2021-04-13 13:18:10 +0900 | [diff] [blame] | 6054 | else if (rc == -EBADF || rc == -ESTALE) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6055 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 6056 | else if (rc == -EEXIST) |
| 6057 | rsp->hdr.Status = STATUS_OBJECT_NAME_COLLISION; |
| 6058 | else if (rsp->hdr.Status == 0 || rc == -EOPNOTSUPP) |
| 6059 | rsp->hdr.Status = STATUS_INVALID_INFO_CLASS; |
| 6060 | smb2_set_err_rsp(work); |
| 6061 | ksmbd_fd_put(work, fp); |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6062 | ksmbd_debug(SMB, "error while processing smb2 query rc = %d\n", rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6063 | return rc; |
| 6064 | } |
| 6065 | |
| 6066 | /** |
| 6067 | * smb2_read_pipe() - handler for smb2 read from IPC pipe |
| 6068 | * @work: smb work containing read IPC pipe command buffer |
| 6069 | * |
| 6070 | * Return: 0 on success, otherwise error |
| 6071 | */ |
| 6072 | static noinline int smb2_read_pipe(struct ksmbd_work *work) |
| 6073 | { |
| 6074 | int nbytes = 0, err; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6075 | u64 id; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6076 | struct ksmbd_rpc_command *rpc_resp; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6077 | struct smb2_read_req *req = smb2_get_msg(work->request_buf); |
| 6078 | struct smb2_read_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6079 | |
| 6080 | id = le64_to_cpu(req->VolatileFileId); |
| 6081 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6082 | inc_rfc1001_len(work->response_buf, 16); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6083 | rpc_resp = ksmbd_rpc_read(work->sess, id); |
| 6084 | if (rpc_resp) { |
| 6085 | if (rpc_resp->flags != KSMBD_RPC_OK) { |
| 6086 | err = -EINVAL; |
| 6087 | goto out; |
| 6088 | } |
| 6089 | |
| 6090 | work->aux_payload_buf = |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6091 | kvmalloc(rpc_resp->payload_sz, GFP_KERNEL | __GFP_ZERO); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6092 | if (!work->aux_payload_buf) { |
| 6093 | err = -ENOMEM; |
| 6094 | goto out; |
| 6095 | } |
| 6096 | |
| 6097 | memcpy(work->aux_payload_buf, rpc_resp->payload, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6098 | rpc_resp->payload_sz); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6099 | |
| 6100 | nbytes = rpc_resp->payload_sz; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6101 | work->resp_hdr_sz = get_rfc1002_len(work->response_buf) + 4; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6102 | work->aux_payload_sz = nbytes; |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6103 | kvfree(rpc_resp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6104 | } |
| 6105 | |
| 6106 | rsp->StructureSize = cpu_to_le16(17); |
| 6107 | rsp->DataOffset = 80; |
| 6108 | rsp->Reserved = 0; |
| 6109 | rsp->DataLength = cpu_to_le32(nbytes); |
| 6110 | rsp->DataRemaining = 0; |
Ronnie Sahlberg | 699230f | 2021-09-09 12:26:12 +0900 | [diff] [blame] | 6111 | rsp->Flags = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6112 | inc_rfc1001_len(work->response_buf, nbytes); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6113 | return 0; |
| 6114 | |
| 6115 | out: |
| 6116 | rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR; |
| 6117 | smb2_set_err_rsp(work); |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6118 | kvfree(rpc_resp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6119 | return err; |
| 6120 | } |
| 6121 | |
| 6122 | static ssize_t smb2_read_rdma_channel(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6123 | struct smb2_read_req *req, void *data_buf, |
| 6124 | size_t length) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6125 | { |
| 6126 | struct smb2_buffer_desc_v1 *desc = |
| 6127 | (struct smb2_buffer_desc_v1 *)&req->Buffer[0]; |
| 6128 | int err; |
| 6129 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6130 | if (work->conn->dialect == SMB30_PROT_ID && |
| 6131 | req->Channel != SMB2_CHANNEL_RDMA_V1) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6132 | return -EINVAL; |
| 6133 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6134 | if (req->ReadChannelInfoOffset == 0 || |
| 6135 | le16_to_cpu(req->ReadChannelInfoLength) < sizeof(*desc)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6136 | return -EINVAL; |
| 6137 | |
| 6138 | work->need_invalidate_rkey = |
| 6139 | (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE); |
| 6140 | work->remote_key = le32_to_cpu(desc->token); |
| 6141 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6142 | err = ksmbd_conn_rdma_write(work->conn, data_buf, length, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6143 | le32_to_cpu(desc->token), |
| 6144 | le64_to_cpu(desc->offset), |
| 6145 | le32_to_cpu(desc->length)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6146 | if (err) |
| 6147 | return err; |
| 6148 | |
| 6149 | return length; |
| 6150 | } |
| 6151 | |
| 6152 | /** |
| 6153 | * smb2_read() - handler for smb2 read from file |
| 6154 | * @work: smb work containing read command buffer |
| 6155 | * |
| 6156 | * Return: 0 on success, otherwise error |
| 6157 | */ |
| 6158 | int smb2_read(struct ksmbd_work *work) |
| 6159 | { |
| 6160 | struct ksmbd_conn *conn = work->conn; |
| 6161 | struct smb2_read_req *req; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6162 | struct smb2_read_rsp *rsp; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6163 | struct ksmbd_file *fp; |
| 6164 | loff_t offset; |
| 6165 | size_t length, mincount; |
| 6166 | ssize_t nbytes = 0, remain_bytes = 0; |
| 6167 | int err = 0; |
| 6168 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6169 | WORK_BUFFERS(work, req, rsp); |
| 6170 | |
| 6171 | if (test_share_config_flag(work->tcon->share_conf, |
| 6172 | KSMBD_SHARE_FLAG_PIPE)) { |
| 6173 | ksmbd_debug(SMB, "IPC pipe read request\n"); |
| 6174 | return smb2_read_pipe(work); |
| 6175 | } |
| 6176 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6177 | fp = ksmbd_lookup_fd_slow(work, le64_to_cpu(req->VolatileFileId), |
| 6178 | le64_to_cpu(req->PersistentFileId)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6179 | if (!fp) { |
Marios Makassikis | a4382db | 2021-05-06 11:34:52 +0900 | [diff] [blame] | 6180 | err = -ENOENT; |
| 6181 | goto out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6182 | } |
| 6183 | |
| 6184 | if (!(fp->daccess & (FILE_READ_DATA_LE | FILE_READ_ATTRIBUTES_LE))) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 6185 | pr_err("Not permitted to read : 0x%x\n", fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6186 | err = -EACCES; |
| 6187 | goto out; |
| 6188 | } |
| 6189 | |
| 6190 | offset = le64_to_cpu(req->Offset); |
| 6191 | length = le32_to_cpu(req->Length); |
| 6192 | mincount = le32_to_cpu(req->MinimumCount); |
| 6193 | |
| 6194 | if (length > conn->vals->max_read_size) { |
| 6195 | ksmbd_debug(SMB, "limiting read size to max size(%u)\n", |
| 6196 | conn->vals->max_read_size); |
| 6197 | err = -EINVAL; |
| 6198 | goto out; |
| 6199 | } |
| 6200 | |
Namjae Jeon | 493fa2f | 2021-06-29 09:22:16 +0900 | [diff] [blame] | 6201 | ksmbd_debug(SMB, "filename %pd, offset %lld, len %zu\n", |
| 6202 | fp->filp->f_path.dentry, offset, length); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6203 | |
Namjae Jeon | c30f4eb | 2021-06-18 10:17:37 +0900 | [diff] [blame] | 6204 | work->aux_payload_buf = kvmalloc(length, GFP_KERNEL | __GFP_ZERO); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6205 | if (!work->aux_payload_buf) { |
Dan Carpenter | c1ea111 | 2021-03-22 17:50:11 +0300 | [diff] [blame] | 6206 | err = -ENOMEM; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6207 | goto out; |
| 6208 | } |
| 6209 | |
| 6210 | nbytes = ksmbd_vfs_read(work, fp, length, &offset); |
| 6211 | if (nbytes < 0) { |
| 6212 | err = nbytes; |
| 6213 | goto out; |
| 6214 | } |
| 6215 | |
| 6216 | if ((nbytes == 0 && length != 0) || nbytes < mincount) { |
Namjae Jeon | c30f4eb | 2021-06-18 10:17:37 +0900 | [diff] [blame] | 6217 | kvfree(work->aux_payload_buf); |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 6218 | work->aux_payload_buf = NULL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6219 | rsp->hdr.Status = STATUS_END_OF_FILE; |
| 6220 | smb2_set_err_rsp(work); |
| 6221 | ksmbd_fd_put(work, fp); |
| 6222 | return 0; |
| 6223 | } |
| 6224 | |
| 6225 | ksmbd_debug(SMB, "nbytes %zu, offset %lld mincount %zu\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6226 | nbytes, offset, mincount); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6227 | |
| 6228 | if (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6229 | req->Channel == SMB2_CHANNEL_RDMA_V1) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6230 | /* write data to the client using rdma channel */ |
| 6231 | remain_bytes = smb2_read_rdma_channel(work, req, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6232 | work->aux_payload_buf, |
| 6233 | nbytes); |
Namjae Jeon | c30f4eb | 2021-06-18 10:17:37 +0900 | [diff] [blame] | 6234 | kvfree(work->aux_payload_buf); |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 6235 | work->aux_payload_buf = NULL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6236 | |
| 6237 | nbytes = 0; |
| 6238 | if (remain_bytes < 0) { |
| 6239 | err = (int)remain_bytes; |
| 6240 | goto out; |
| 6241 | } |
| 6242 | } |
| 6243 | |
| 6244 | rsp->StructureSize = cpu_to_le16(17); |
| 6245 | rsp->DataOffset = 80; |
| 6246 | rsp->Reserved = 0; |
| 6247 | rsp->DataLength = cpu_to_le32(nbytes); |
| 6248 | rsp->DataRemaining = cpu_to_le32(remain_bytes); |
Ronnie Sahlberg | 699230f | 2021-09-09 12:26:12 +0900 | [diff] [blame] | 6249 | rsp->Flags = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6250 | inc_rfc1001_len(work->response_buf, 16); |
| 6251 | work->resp_hdr_sz = get_rfc1002_len(work->response_buf) + 4; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6252 | work->aux_payload_sz = nbytes; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6253 | inc_rfc1001_len(work->response_buf, nbytes); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6254 | ksmbd_fd_put(work, fp); |
| 6255 | return 0; |
| 6256 | |
| 6257 | out: |
| 6258 | if (err) { |
| 6259 | if (err == -EISDIR) |
| 6260 | rsp->hdr.Status = STATUS_INVALID_DEVICE_REQUEST; |
| 6261 | else if (err == -EAGAIN) |
| 6262 | rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT; |
| 6263 | else if (err == -ENOENT) |
| 6264 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 6265 | else if (err == -EACCES) |
| 6266 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 6267 | else if (err == -ESHARE) |
| 6268 | rsp->hdr.Status = STATUS_SHARING_VIOLATION; |
| 6269 | else if (err == -EINVAL) |
| 6270 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 6271 | else |
| 6272 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 6273 | |
| 6274 | smb2_set_err_rsp(work); |
| 6275 | } |
| 6276 | ksmbd_fd_put(work, fp); |
| 6277 | return err; |
| 6278 | } |
| 6279 | |
| 6280 | /** |
| 6281 | * smb2_write_pipe() - handler for smb2 write on IPC pipe |
| 6282 | * @work: smb work containing write IPC pipe command buffer |
| 6283 | * |
| 6284 | * Return: 0 on success, otherwise error |
| 6285 | */ |
| 6286 | static noinline int smb2_write_pipe(struct ksmbd_work *work) |
| 6287 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6288 | struct smb2_write_req *req = smb2_get_msg(work->request_buf); |
| 6289 | struct smb2_write_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6290 | struct ksmbd_rpc_command *rpc_resp; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6291 | u64 id = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6292 | int err = 0, ret = 0; |
| 6293 | char *data_buf; |
| 6294 | size_t length; |
| 6295 | |
| 6296 | length = le32_to_cpu(req->Length); |
| 6297 | id = le64_to_cpu(req->VolatileFileId); |
| 6298 | |
| 6299 | if (le16_to_cpu(req->DataOffset) == |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6300 | offsetof(struct smb2_write_req, Buffer)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6301 | data_buf = (char *)&req->Buffer[0]; |
| 6302 | } else { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6303 | if ((u64)le16_to_cpu(req->DataOffset) + length > |
| 6304 | get_rfc1002_len(work->request_buf)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 6305 | pr_err("invalid write data offset %u, smb_len %u\n", |
| 6306 | le16_to_cpu(req->DataOffset), |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6307 | get_rfc1002_len(work->request_buf)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6308 | err = -EINVAL; |
| 6309 | goto out; |
| 6310 | } |
| 6311 | |
| 6312 | data_buf = (char *)(((char *)&req->hdr.ProtocolId) + |
| 6313 | le16_to_cpu(req->DataOffset)); |
| 6314 | } |
| 6315 | |
| 6316 | rpc_resp = ksmbd_rpc_write(work->sess, id, data_buf, length); |
| 6317 | if (rpc_resp) { |
| 6318 | if (rpc_resp->flags == KSMBD_RPC_ENOTIMPLEMENTED) { |
| 6319 | rsp->hdr.Status = STATUS_NOT_SUPPORTED; |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6320 | kvfree(rpc_resp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6321 | smb2_set_err_rsp(work); |
| 6322 | return -EOPNOTSUPP; |
| 6323 | } |
| 6324 | if (rpc_resp->flags != KSMBD_RPC_OK) { |
| 6325 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 6326 | smb2_set_err_rsp(work); |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6327 | kvfree(rpc_resp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6328 | return ret; |
| 6329 | } |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6330 | kvfree(rpc_resp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6331 | } |
| 6332 | |
| 6333 | rsp->StructureSize = cpu_to_le16(17); |
| 6334 | rsp->DataOffset = 0; |
| 6335 | rsp->Reserved = 0; |
| 6336 | rsp->DataLength = cpu_to_le32(length); |
| 6337 | rsp->DataRemaining = 0; |
| 6338 | rsp->Reserved2 = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6339 | inc_rfc1001_len(work->response_buf, 16); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6340 | return 0; |
| 6341 | out: |
| 6342 | if (err) { |
| 6343 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 6344 | smb2_set_err_rsp(work); |
| 6345 | } |
| 6346 | |
| 6347 | return err; |
| 6348 | } |
| 6349 | |
| 6350 | static ssize_t smb2_write_rdma_channel(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6351 | struct smb2_write_req *req, |
| 6352 | struct ksmbd_file *fp, |
| 6353 | loff_t offset, size_t length, bool sync) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6354 | { |
| 6355 | struct smb2_buffer_desc_v1 *desc; |
| 6356 | char *data_buf; |
| 6357 | int ret; |
| 6358 | ssize_t nbytes; |
| 6359 | |
| 6360 | desc = (struct smb2_buffer_desc_v1 *)&req->Buffer[0]; |
| 6361 | |
| 6362 | if (work->conn->dialect == SMB30_PROT_ID && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6363 | req->Channel != SMB2_CHANNEL_RDMA_V1) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6364 | return -EINVAL; |
| 6365 | |
| 6366 | if (req->Length != 0 || req->DataOffset != 0) |
| 6367 | return -EINVAL; |
| 6368 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6369 | if (req->WriteChannelInfoOffset == 0 || |
| 6370 | le16_to_cpu(req->WriteChannelInfoLength) < sizeof(*desc)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6371 | return -EINVAL; |
| 6372 | |
| 6373 | work->need_invalidate_rkey = |
| 6374 | (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE); |
| 6375 | work->remote_key = le32_to_cpu(desc->token); |
| 6376 | |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6377 | data_buf = kvmalloc(length, GFP_KERNEL | __GFP_ZERO); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6378 | if (!data_buf) |
| 6379 | return -ENOMEM; |
| 6380 | |
| 6381 | ret = ksmbd_conn_rdma_read(work->conn, data_buf, length, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6382 | le32_to_cpu(desc->token), |
| 6383 | le64_to_cpu(desc->offset), |
| 6384 | le32_to_cpu(desc->length)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6385 | if (ret < 0) { |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6386 | kvfree(data_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6387 | return ret; |
| 6388 | } |
| 6389 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6390 | ret = ksmbd_vfs_write(work, fp, data_buf, length, &offset, sync, &nbytes); |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6391 | kvfree(data_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6392 | if (ret < 0) |
| 6393 | return ret; |
| 6394 | |
| 6395 | return nbytes; |
| 6396 | } |
| 6397 | |
| 6398 | /** |
| 6399 | * smb2_write() - handler for smb2 write from file |
| 6400 | * @work: smb work containing write command buffer |
| 6401 | * |
| 6402 | * Return: 0 on success, otherwise error |
| 6403 | */ |
| 6404 | int smb2_write(struct ksmbd_work *work) |
| 6405 | { |
| 6406 | struct smb2_write_req *req; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6407 | struct smb2_write_rsp *rsp; |
Namjae Jeon | bcd62a3 | 2021-05-10 09:08:19 +0900 | [diff] [blame] | 6408 | struct ksmbd_file *fp = NULL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6409 | loff_t offset; |
| 6410 | size_t length; |
| 6411 | ssize_t nbytes; |
| 6412 | char *data_buf; |
| 6413 | bool writethrough = false; |
| 6414 | int err = 0; |
| 6415 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6416 | WORK_BUFFERS(work, req, rsp); |
| 6417 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6418 | 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] | 6419 | ksmbd_debug(SMB, "IPC pipe write request\n"); |
| 6420 | return smb2_write_pipe(work); |
| 6421 | } |
| 6422 | |
| 6423 | if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
| 6424 | ksmbd_debug(SMB, "User does not have write permission\n"); |
| 6425 | err = -EACCES; |
| 6426 | goto out; |
| 6427 | } |
| 6428 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6429 | fp = ksmbd_lookup_fd_slow(work, le64_to_cpu(req->VolatileFileId), |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6430 | le64_to_cpu(req->PersistentFileId)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6431 | if (!fp) { |
Marios Makassikis | a4382db | 2021-05-06 11:34:52 +0900 | [diff] [blame] | 6432 | err = -ENOENT; |
| 6433 | goto out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6434 | } |
| 6435 | |
| 6436 | if (!(fp->daccess & (FILE_WRITE_DATA_LE | FILE_READ_ATTRIBUTES_LE))) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 6437 | pr_err("Not permitted to write : 0x%x\n", fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6438 | err = -EACCES; |
| 6439 | goto out; |
| 6440 | } |
| 6441 | |
| 6442 | offset = le64_to_cpu(req->Offset); |
| 6443 | length = le32_to_cpu(req->Length); |
| 6444 | |
| 6445 | if (length > work->conn->vals->max_write_size) { |
| 6446 | ksmbd_debug(SMB, "limiting write size to max size(%u)\n", |
| 6447 | work->conn->vals->max_write_size); |
| 6448 | err = -EINVAL; |
| 6449 | goto out; |
| 6450 | } |
| 6451 | |
| 6452 | if (le32_to_cpu(req->Flags) & SMB2_WRITEFLAG_WRITE_THROUGH) |
| 6453 | writethrough = true; |
| 6454 | |
| 6455 | if (req->Channel != SMB2_CHANNEL_RDMA_V1 && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6456 | req->Channel != SMB2_CHANNEL_RDMA_V1_INVALIDATE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6457 | if (le16_to_cpu(req->DataOffset) == |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6458 | offsetof(struct smb2_write_req, Buffer)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6459 | data_buf = (char *)&req->Buffer[0]; |
| 6460 | } else { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6461 | if ((u64)le16_to_cpu(req->DataOffset) + length > |
| 6462 | get_rfc1002_len(work->request_buf)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 6463 | pr_err("invalid write data offset %u, smb_len %u\n", |
| 6464 | le16_to_cpu(req->DataOffset), |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6465 | get_rfc1002_len(work->request_buf)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6466 | err = -EINVAL; |
| 6467 | goto out; |
| 6468 | } |
| 6469 | |
| 6470 | data_buf = (char *)(((char *)&req->hdr.ProtocolId) + |
| 6471 | le16_to_cpu(req->DataOffset)); |
| 6472 | } |
| 6473 | |
| 6474 | ksmbd_debug(SMB, "flags %u\n", le32_to_cpu(req->Flags)); |
| 6475 | if (le32_to_cpu(req->Flags) & SMB2_WRITEFLAG_WRITE_THROUGH) |
| 6476 | writethrough = true; |
| 6477 | |
Namjae Jeon | 493fa2f | 2021-06-29 09:22:16 +0900 | [diff] [blame] | 6478 | ksmbd_debug(SMB, "filename %pd, offset %lld, len %zu\n", |
| 6479 | fp->filp->f_path.dentry, offset, length); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6480 | err = ksmbd_vfs_write(work, fp, data_buf, length, &offset, |
| 6481 | writethrough, &nbytes); |
| 6482 | if (err < 0) |
| 6483 | goto out; |
| 6484 | } else { |
| 6485 | /* read data from the client using rdma channel, and |
| 6486 | * write the data. |
| 6487 | */ |
| 6488 | nbytes = smb2_write_rdma_channel(work, req, fp, offset, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6489 | le32_to_cpu(req->RemainingBytes), |
| 6490 | writethrough); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6491 | if (nbytes < 0) { |
| 6492 | err = (int)nbytes; |
| 6493 | goto out; |
| 6494 | } |
| 6495 | } |
| 6496 | |
| 6497 | rsp->StructureSize = cpu_to_le16(17); |
| 6498 | rsp->DataOffset = 0; |
| 6499 | rsp->Reserved = 0; |
| 6500 | rsp->DataLength = cpu_to_le32(nbytes); |
| 6501 | rsp->DataRemaining = 0; |
| 6502 | rsp->Reserved2 = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6503 | inc_rfc1001_len(work->response_buf, 16); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6504 | ksmbd_fd_put(work, fp); |
| 6505 | return 0; |
| 6506 | |
| 6507 | out: |
| 6508 | if (err == -EAGAIN) |
| 6509 | rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT; |
| 6510 | else if (err == -ENOSPC || err == -EFBIG) |
| 6511 | rsp->hdr.Status = STATUS_DISK_FULL; |
| 6512 | else if (err == -ENOENT) |
| 6513 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 6514 | else if (err == -EACCES) |
| 6515 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 6516 | else if (err == -ESHARE) |
| 6517 | rsp->hdr.Status = STATUS_SHARING_VIOLATION; |
| 6518 | else if (err == -EINVAL) |
| 6519 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 6520 | else |
| 6521 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 6522 | |
| 6523 | smb2_set_err_rsp(work); |
| 6524 | ksmbd_fd_put(work, fp); |
| 6525 | return err; |
| 6526 | } |
| 6527 | |
| 6528 | /** |
| 6529 | * smb2_flush() - handler for smb2 flush file - fsync |
| 6530 | * @work: smb work containing flush command buffer |
| 6531 | * |
| 6532 | * Return: 0 on success, otherwise error |
| 6533 | */ |
| 6534 | int smb2_flush(struct ksmbd_work *work) |
| 6535 | { |
| 6536 | struct smb2_flush_req *req; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6537 | struct smb2_flush_rsp *rsp; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6538 | int err; |
| 6539 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6540 | WORK_BUFFERS(work, req, rsp); |
| 6541 | |
| 6542 | ksmbd_debug(SMB, "SMB2_FLUSH called for fid %llu\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6543 | le64_to_cpu(req->VolatileFileId)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6544 | |
| 6545 | err = ksmbd_vfs_fsync(work, |
| 6546 | le64_to_cpu(req->VolatileFileId), |
| 6547 | le64_to_cpu(req->PersistentFileId)); |
| 6548 | if (err) |
| 6549 | goto out; |
| 6550 | |
| 6551 | rsp->StructureSize = cpu_to_le16(4); |
| 6552 | rsp->Reserved = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6553 | inc_rfc1001_len(work->response_buf, 4); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6554 | return 0; |
| 6555 | |
| 6556 | out: |
| 6557 | if (err) { |
| 6558 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 6559 | smb2_set_err_rsp(work); |
| 6560 | } |
| 6561 | |
| 6562 | return err; |
| 6563 | } |
| 6564 | |
| 6565 | /** |
| 6566 | * smb2_cancel() - handler for smb2 cancel command |
| 6567 | * @work: smb work containing cancel command buffer |
| 6568 | * |
| 6569 | * Return: 0 on success, otherwise error |
| 6570 | */ |
| 6571 | int smb2_cancel(struct ksmbd_work *work) |
| 6572 | { |
| 6573 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6574 | struct smb2_hdr *hdr = smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6575 | struct smb2_hdr *chdr; |
| 6576 | struct ksmbd_work *cancel_work = NULL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6577 | int canceled = 0; |
| 6578 | struct list_head *command_list; |
| 6579 | |
| 6580 | 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] | 6581 | hdr->MessageId, hdr->Flags); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6582 | |
| 6583 | if (hdr->Flags & SMB2_FLAGS_ASYNC_COMMAND) { |
| 6584 | command_list = &conn->async_requests; |
| 6585 | |
| 6586 | spin_lock(&conn->request_lock); |
Namjae Jeon | 6f3d5ee | 2021-06-18 10:28:52 +0900 | [diff] [blame] | 6587 | list_for_each_entry(cancel_work, command_list, |
| 6588 | async_request_entry) { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6589 | chdr = smb2_get_msg(cancel_work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6590 | |
| 6591 | if (cancel_work->async_id != |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6592 | le64_to_cpu(hdr->Id.AsyncId)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6593 | continue; |
| 6594 | |
| 6595 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6596 | "smb2 with AsyncId %llu cancelled command = 0x%x\n", |
| 6597 | le64_to_cpu(hdr->Id.AsyncId), |
| 6598 | le16_to_cpu(chdr->Command)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6599 | canceled = 1; |
| 6600 | break; |
| 6601 | } |
| 6602 | spin_unlock(&conn->request_lock); |
| 6603 | } else { |
| 6604 | command_list = &conn->requests; |
| 6605 | |
| 6606 | spin_lock(&conn->request_lock); |
Namjae Jeon | 6f3d5ee | 2021-06-18 10:28:52 +0900 | [diff] [blame] | 6607 | list_for_each_entry(cancel_work, command_list, request_entry) { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6608 | chdr = smb2_get_msg(cancel_work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6609 | |
| 6610 | if (chdr->MessageId != hdr->MessageId || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6611 | cancel_work == work) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6612 | continue; |
| 6613 | |
| 6614 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6615 | "smb2 with mid %llu cancelled command = 0x%x\n", |
| 6616 | le64_to_cpu(hdr->MessageId), |
| 6617 | le16_to_cpu(chdr->Command)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6618 | canceled = 1; |
| 6619 | break; |
| 6620 | } |
| 6621 | spin_unlock(&conn->request_lock); |
| 6622 | } |
| 6623 | |
| 6624 | if (canceled) { |
| 6625 | cancel_work->state = KSMBD_WORK_CANCELLED; |
| 6626 | if (cancel_work->cancel_fn) |
| 6627 | cancel_work->cancel_fn(cancel_work->cancel_argv); |
| 6628 | } |
| 6629 | |
| 6630 | /* For SMB2_CANCEL command itself send no response*/ |
| 6631 | work->send_no_response = 1; |
| 6632 | return 0; |
| 6633 | } |
| 6634 | |
| 6635 | struct file_lock *smb_flock_init(struct file *f) |
| 6636 | { |
| 6637 | struct file_lock *fl; |
| 6638 | |
| 6639 | fl = locks_alloc_lock(); |
| 6640 | if (!fl) |
| 6641 | goto out; |
| 6642 | |
| 6643 | locks_init_lock(fl); |
| 6644 | |
| 6645 | fl->fl_owner = f; |
| 6646 | fl->fl_pid = current->tgid; |
| 6647 | fl->fl_file = f; |
| 6648 | fl->fl_flags = FL_POSIX; |
| 6649 | fl->fl_ops = NULL; |
| 6650 | fl->fl_lmops = NULL; |
| 6651 | |
| 6652 | out: |
| 6653 | return fl; |
| 6654 | } |
| 6655 | |
| 6656 | static int smb2_set_flock_flags(struct file_lock *flock, int flags) |
| 6657 | { |
| 6658 | int cmd = -EINVAL; |
| 6659 | |
| 6660 | /* Checking for wrong flag combination during lock request*/ |
| 6661 | switch (flags) { |
| 6662 | case SMB2_LOCKFLAG_SHARED: |
| 6663 | ksmbd_debug(SMB, "received shared request\n"); |
| 6664 | cmd = F_SETLKW; |
| 6665 | flock->fl_type = F_RDLCK; |
| 6666 | flock->fl_flags |= FL_SLEEP; |
| 6667 | break; |
| 6668 | case SMB2_LOCKFLAG_EXCLUSIVE: |
| 6669 | ksmbd_debug(SMB, "received exclusive request\n"); |
| 6670 | cmd = F_SETLKW; |
| 6671 | flock->fl_type = F_WRLCK; |
| 6672 | flock->fl_flags |= FL_SLEEP; |
| 6673 | break; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6674 | case SMB2_LOCKFLAG_SHARED | SMB2_LOCKFLAG_FAIL_IMMEDIATELY: |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6675 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6676 | "received shared & fail immediately request\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6677 | cmd = F_SETLK; |
| 6678 | flock->fl_type = F_RDLCK; |
| 6679 | break; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6680 | case SMB2_LOCKFLAG_EXCLUSIVE | SMB2_LOCKFLAG_FAIL_IMMEDIATELY: |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6681 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6682 | "received exclusive & fail immediately request\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6683 | cmd = F_SETLK; |
| 6684 | flock->fl_type = F_WRLCK; |
| 6685 | break; |
| 6686 | case SMB2_LOCKFLAG_UNLOCK: |
| 6687 | ksmbd_debug(SMB, "received unlock request\n"); |
| 6688 | flock->fl_type = F_UNLCK; |
| 6689 | cmd = 0; |
| 6690 | break; |
| 6691 | } |
| 6692 | |
| 6693 | return cmd; |
| 6694 | } |
| 6695 | |
| 6696 | static struct ksmbd_lock *smb2_lock_init(struct file_lock *flock, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6697 | unsigned int cmd, int flags, |
| 6698 | struct list_head *lock_list) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6699 | { |
| 6700 | struct ksmbd_lock *lock; |
| 6701 | |
| 6702 | lock = kzalloc(sizeof(struct ksmbd_lock), GFP_KERNEL); |
| 6703 | if (!lock) |
| 6704 | return NULL; |
| 6705 | |
| 6706 | lock->cmd = cmd; |
| 6707 | lock->fl = flock; |
| 6708 | lock->start = flock->fl_start; |
| 6709 | lock->end = flock->fl_end; |
| 6710 | lock->flags = flags; |
| 6711 | if (lock->start == lock->end) |
| 6712 | lock->zero_len = 1; |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6713 | INIT_LIST_HEAD(&lock->clist); |
| 6714 | INIT_LIST_HEAD(&lock->flist); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6715 | INIT_LIST_HEAD(&lock->llist); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6716 | list_add_tail(&lock->llist, lock_list); |
| 6717 | |
| 6718 | return lock; |
| 6719 | } |
| 6720 | |
| 6721 | static void smb2_remove_blocked_lock(void **argv) |
| 6722 | { |
| 6723 | struct file_lock *flock = (struct file_lock *)argv[0]; |
| 6724 | |
| 6725 | ksmbd_vfs_posix_lock_unblock(flock); |
| 6726 | wake_up(&flock->fl_wait); |
| 6727 | } |
| 6728 | |
| 6729 | static inline bool lock_defer_pending(struct file_lock *fl) |
| 6730 | { |
| 6731 | /* check pending lock waiters */ |
| 6732 | return waitqueue_active(&fl->fl_wait); |
| 6733 | } |
| 6734 | |
| 6735 | /** |
| 6736 | * smb2_lock() - handler for smb2 file lock command |
| 6737 | * @work: smb work containing lock command buffer |
| 6738 | * |
| 6739 | * Return: 0 on success, otherwise error |
| 6740 | */ |
| 6741 | int smb2_lock(struct ksmbd_work *work) |
| 6742 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 6743 | struct smb2_lock_req *req = smb2_get_msg(work->request_buf); |
| 6744 | struct smb2_lock_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6745 | struct smb2_lock_element *lock_ele; |
| 6746 | struct ksmbd_file *fp = NULL; |
| 6747 | struct file_lock *flock = NULL; |
| 6748 | struct file *filp = NULL; |
| 6749 | int lock_count; |
| 6750 | int flags = 0; |
| 6751 | int cmd = 0; |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6752 | int err = -EIO, i, rc = 0; |
Namjae Jeon | 50bf80a | 2021-05-14 12:20:07 +0900 | [diff] [blame] | 6753 | u64 lock_start, lock_length; |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6754 | struct ksmbd_lock *smb_lock = NULL, *cmp_lock, *tmp, *tmp2; |
| 6755 | struct ksmbd_conn *conn; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6756 | int nolock = 0; |
| 6757 | LIST_HEAD(lock_list); |
| 6758 | LIST_HEAD(rollback_list); |
| 6759 | int prior_lock = 0; |
| 6760 | |
| 6761 | ksmbd_debug(SMB, "Received lock request\n"); |
| 6762 | fp = ksmbd_lookup_fd_slow(work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6763 | le64_to_cpu(req->VolatileFileId), |
| 6764 | le64_to_cpu(req->PersistentFileId)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6765 | if (!fp) { |
| 6766 | ksmbd_debug(SMB, "Invalid file id for lock : %llu\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6767 | le64_to_cpu(req->VolatileFileId)); |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6768 | err = -ENOENT; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6769 | goto out2; |
| 6770 | } |
| 6771 | |
| 6772 | filp = fp->filp; |
| 6773 | lock_count = le16_to_cpu(req->LockCount); |
| 6774 | lock_ele = req->locks; |
| 6775 | |
| 6776 | ksmbd_debug(SMB, "lock count is %d\n", lock_count); |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6777 | if (!lock_count) { |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6778 | err = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6779 | goto out2; |
| 6780 | } |
| 6781 | |
| 6782 | for (i = 0; i < lock_count; i++) { |
| 6783 | flags = le32_to_cpu(lock_ele[i].Flags); |
| 6784 | |
| 6785 | flock = smb_flock_init(filp); |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6786 | if (!flock) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6787 | goto out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6788 | |
| 6789 | cmd = smb2_set_flock_flags(flock, flags); |
| 6790 | |
Namjae Jeon | 50bf80a | 2021-05-14 12:20:07 +0900 | [diff] [blame] | 6791 | lock_start = le64_to_cpu(lock_ele[i].Offset); |
| 6792 | lock_length = le64_to_cpu(lock_ele[i].Length); |
| 6793 | if (lock_start > U64_MAX - lock_length) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 6794 | pr_err("Invalid lock range requested\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6795 | rsp->hdr.Status = STATUS_INVALID_LOCK_RANGE; |
| 6796 | goto out; |
| 6797 | } |
| 6798 | |
Namjae Jeon | 50bf80a | 2021-05-14 12:20:07 +0900 | [diff] [blame] | 6799 | if (lock_start > OFFSET_MAX) |
| 6800 | flock->fl_start = OFFSET_MAX; |
| 6801 | else |
| 6802 | flock->fl_start = lock_start; |
| 6803 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6804 | lock_length = le64_to_cpu(lock_ele[i].Length); |
Namjae Jeon | 50bf80a | 2021-05-14 12:20:07 +0900 | [diff] [blame] | 6805 | if (lock_length > OFFSET_MAX - flock->fl_start) |
| 6806 | lock_length = OFFSET_MAX - flock->fl_start; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6807 | |
| 6808 | flock->fl_end = flock->fl_start + lock_length; |
| 6809 | |
| 6810 | if (flock->fl_end < flock->fl_start) { |
| 6811 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6812 | "the end offset(%llx) is smaller than the start offset(%llx)\n", |
| 6813 | flock->fl_end, flock->fl_start); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6814 | rsp->hdr.Status = STATUS_INVALID_LOCK_RANGE; |
| 6815 | goto out; |
| 6816 | } |
| 6817 | |
| 6818 | /* Check conflict locks in one request */ |
| 6819 | list_for_each_entry(cmp_lock, &lock_list, llist) { |
| 6820 | if (cmp_lock->fl->fl_start <= flock->fl_start && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6821 | cmp_lock->fl->fl_end >= flock->fl_end) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6822 | if (cmp_lock->fl->fl_type != F_UNLCK && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6823 | flock->fl_type != F_UNLCK) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 6824 | pr_err("conflict two locks in one request\n"); |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6825 | err = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6826 | goto out; |
| 6827 | } |
| 6828 | } |
| 6829 | } |
| 6830 | |
| 6831 | smb_lock = smb2_lock_init(flock, cmd, flags, &lock_list); |
| 6832 | if (!smb_lock) { |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6833 | err = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6834 | goto out; |
| 6835 | } |
| 6836 | } |
| 6837 | |
| 6838 | list_for_each_entry_safe(smb_lock, tmp, &lock_list, llist) { |
| 6839 | if (smb_lock->cmd < 0) { |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6840 | err = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6841 | goto out; |
| 6842 | } |
| 6843 | |
| 6844 | if (!(smb_lock->flags & SMB2_LOCKFLAG_MASK)) { |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6845 | err = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6846 | goto out; |
| 6847 | } |
| 6848 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6849 | if ((prior_lock & (SMB2_LOCKFLAG_EXCLUSIVE | SMB2_LOCKFLAG_SHARED) && |
| 6850 | smb_lock->flags & SMB2_LOCKFLAG_UNLOCK) || |
| 6851 | (prior_lock == SMB2_LOCKFLAG_UNLOCK && |
| 6852 | !(smb_lock->flags & SMB2_LOCKFLAG_UNLOCK))) { |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6853 | err = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6854 | goto out; |
| 6855 | } |
| 6856 | |
| 6857 | prior_lock = smb_lock->flags; |
| 6858 | |
| 6859 | if (!(smb_lock->flags & SMB2_LOCKFLAG_UNLOCK) && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6860 | !(smb_lock->flags & SMB2_LOCKFLAG_FAIL_IMMEDIATELY)) |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6861 | goto no_check_cl; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6862 | |
| 6863 | nolock = 1; |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6864 | /* check locks in connection list */ |
| 6865 | read_lock(&conn_list_lock); |
| 6866 | list_for_each_entry(conn, &conn_list, conns_list) { |
| 6867 | spin_lock(&conn->llist_lock); |
| 6868 | list_for_each_entry_safe(cmp_lock, tmp2, &conn->lock_list, clist) { |
| 6869 | if (file_inode(cmp_lock->fl->fl_file) != |
| 6870 | file_inode(smb_lock->fl->fl_file)) |
| 6871 | continue; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6872 | |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6873 | if (smb_lock->fl->fl_type == F_UNLCK) { |
| 6874 | if (cmp_lock->fl->fl_file == smb_lock->fl->fl_file && |
| 6875 | cmp_lock->start == smb_lock->start && |
| 6876 | cmp_lock->end == smb_lock->end && |
| 6877 | !lock_defer_pending(cmp_lock->fl)) { |
| 6878 | nolock = 0; |
| 6879 | list_del(&cmp_lock->flist); |
| 6880 | list_del(&cmp_lock->clist); |
| 6881 | spin_unlock(&conn->llist_lock); |
| 6882 | read_unlock(&conn_list_lock); |
| 6883 | |
| 6884 | locks_free_lock(cmp_lock->fl); |
| 6885 | kfree(cmp_lock); |
| 6886 | goto out_check_cl; |
| 6887 | } |
| 6888 | continue; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6889 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6890 | |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6891 | if (cmp_lock->fl->fl_file == smb_lock->fl->fl_file) { |
| 6892 | if (smb_lock->flags & SMB2_LOCKFLAG_SHARED) |
| 6893 | continue; |
| 6894 | } else { |
| 6895 | if (cmp_lock->flags & SMB2_LOCKFLAG_SHARED) |
| 6896 | continue; |
| 6897 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6898 | |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6899 | /* check zero byte lock range */ |
| 6900 | if (cmp_lock->zero_len && !smb_lock->zero_len && |
| 6901 | cmp_lock->start > smb_lock->start && |
| 6902 | cmp_lock->start < smb_lock->end) { |
| 6903 | spin_unlock(&conn->llist_lock); |
| 6904 | read_unlock(&conn_list_lock); |
| 6905 | pr_err("previous lock conflict with zero byte lock range\n"); |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6906 | goto out; |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6907 | } |
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 | if (smb_lock->zero_len && !cmp_lock->zero_len && |
| 6910 | smb_lock->start > cmp_lock->start && |
| 6911 | smb_lock->start < cmp_lock->end) { |
| 6912 | spin_unlock(&conn->llist_lock); |
| 6913 | read_unlock(&conn_list_lock); |
| 6914 | pr_err("current lock conflict with zero byte lock range\n"); |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6915 | goto out; |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6916 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6917 | |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6918 | if (((cmp_lock->start <= smb_lock->start && |
| 6919 | cmp_lock->end > smb_lock->start) || |
| 6920 | (cmp_lock->start < smb_lock->end && |
| 6921 | cmp_lock->end >= smb_lock->end)) && |
| 6922 | !cmp_lock->zero_len && !smb_lock->zero_len) { |
| 6923 | spin_unlock(&conn->llist_lock); |
| 6924 | read_unlock(&conn_list_lock); |
| 6925 | pr_err("Not allow lock operation on exclusive lock range\n"); |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6926 | goto out; |
| 6927 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6928 | } |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6929 | spin_unlock(&conn->llist_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6930 | } |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6931 | read_unlock(&conn_list_lock); |
| 6932 | out_check_cl: |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6933 | if (smb_lock->fl->fl_type == F_UNLCK && nolock) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 6934 | pr_err("Try to unlock nolocked range\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6935 | rsp->hdr.Status = STATUS_RANGE_NOT_LOCKED; |
| 6936 | goto out; |
| 6937 | } |
| 6938 | |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6939 | no_check_cl: |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6940 | if (smb_lock->zero_len) { |
| 6941 | err = 0; |
| 6942 | goto skip; |
| 6943 | } |
| 6944 | |
| 6945 | flock = smb_lock->fl; |
| 6946 | list_del(&smb_lock->llist); |
| 6947 | retry: |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6948 | rc = vfs_lock_file(filp, smb_lock->cmd, flock, NULL); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6949 | skip: |
| 6950 | if (flags & SMB2_LOCKFLAG_UNLOCK) { |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6951 | if (!rc) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6952 | ksmbd_debug(SMB, "File unlocked\n"); |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6953 | } else if (rc == -ENOENT) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6954 | rsp->hdr.Status = STATUS_NOT_LOCKED; |
| 6955 | goto out; |
| 6956 | } |
| 6957 | locks_free_lock(flock); |
| 6958 | kfree(smb_lock); |
| 6959 | } else { |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6960 | if (rc == FILE_LOCK_DEFERRED) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6961 | void **argv; |
| 6962 | |
| 6963 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6964 | "would have to wait for getting lock\n"); |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6965 | spin_lock(&work->conn->llist_lock); |
| 6966 | list_add_tail(&smb_lock->clist, |
| 6967 | &work->conn->lock_list); |
| 6968 | spin_unlock(&work->conn->llist_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6969 | list_add(&smb_lock->llist, &rollback_list); |
| 6970 | |
| 6971 | argv = kmalloc(sizeof(void *), GFP_KERNEL); |
| 6972 | if (!argv) { |
| 6973 | err = -ENOMEM; |
| 6974 | goto out; |
| 6975 | } |
| 6976 | argv[0] = flock; |
| 6977 | |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6978 | rc = setup_async_work(work, |
| 6979 | smb2_remove_blocked_lock, |
| 6980 | argv); |
| 6981 | if (rc) { |
| 6982 | err = -ENOMEM; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6983 | goto out; |
| 6984 | } |
| 6985 | spin_lock(&fp->f_lock); |
| 6986 | list_add(&work->fp_entry, &fp->blocked_works); |
| 6987 | spin_unlock(&fp->f_lock); |
| 6988 | |
| 6989 | smb2_send_interim_resp(work, STATUS_PENDING); |
| 6990 | |
Hyunchul Lee | 45a64e8 | 2021-07-10 09:34:20 +0900 | [diff] [blame] | 6991 | ksmbd_vfs_posix_lock_wait(flock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6992 | |
Hyunchul Lee | d4075ab | 2021-06-25 07:02:10 +0900 | [diff] [blame] | 6993 | if (work->state != KSMBD_WORK_ACTIVE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6994 | list_del(&smb_lock->llist); |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6995 | spin_lock(&work->conn->llist_lock); |
| 6996 | list_del(&smb_lock->clist); |
| 6997 | spin_unlock(&work->conn->llist_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6998 | locks_free_lock(flock); |
| 6999 | |
Hyunchul Lee | d4075ab | 2021-06-25 07:02:10 +0900 | [diff] [blame] | 7000 | if (work->state == KSMBD_WORK_CANCELLED) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7001 | spin_lock(&fp->f_lock); |
| 7002 | list_del(&work->fp_entry); |
| 7003 | spin_unlock(&fp->f_lock); |
| 7004 | rsp->hdr.Status = |
| 7005 | STATUS_CANCELLED; |
| 7006 | kfree(smb_lock); |
| 7007 | smb2_send_interim_resp(work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7008 | STATUS_CANCELLED); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7009 | work->send_no_response = 1; |
| 7010 | goto out; |
| 7011 | } |
| 7012 | init_smb2_rsp_hdr(work); |
| 7013 | smb2_set_err_rsp(work); |
| 7014 | rsp->hdr.Status = |
| 7015 | STATUS_RANGE_NOT_LOCKED; |
| 7016 | kfree(smb_lock); |
| 7017 | goto out2; |
| 7018 | } |
| 7019 | |
| 7020 | list_del(&smb_lock->llist); |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 7021 | spin_lock(&work->conn->llist_lock); |
| 7022 | list_del(&smb_lock->clist); |
| 7023 | spin_unlock(&work->conn->llist_lock); |
| 7024 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7025 | spin_lock(&fp->f_lock); |
| 7026 | list_del(&work->fp_entry); |
| 7027 | spin_unlock(&fp->f_lock); |
| 7028 | goto retry; |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 7029 | } else if (!rc) { |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 7030 | spin_lock(&work->conn->llist_lock); |
| 7031 | list_add_tail(&smb_lock->clist, |
| 7032 | &work->conn->lock_list); |
| 7033 | list_add_tail(&smb_lock->flist, |
| 7034 | &fp->lock_list); |
| 7035 | spin_unlock(&work->conn->llist_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7036 | list_add(&smb_lock->llist, &rollback_list); |
| 7037 | ksmbd_debug(SMB, "successful in taking lock\n"); |
| 7038 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7039 | goto out; |
| 7040 | } |
| 7041 | } |
| 7042 | } |
| 7043 | |
| 7044 | if (atomic_read(&fp->f_ci->op_count) > 1) |
| 7045 | smb_break_all_oplock(work, fp); |
| 7046 | |
| 7047 | rsp->StructureSize = cpu_to_le16(4); |
| 7048 | ksmbd_debug(SMB, "successful in taking lock\n"); |
| 7049 | rsp->hdr.Status = STATUS_SUCCESS; |
| 7050 | rsp->Reserved = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 7051 | inc_rfc1001_len(work->response_buf, 4); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7052 | ksmbd_fd_put(work, fp); |
Namjae Jeon | 96ad4ec | 2021-07-13 17:17:28 +0900 | [diff] [blame] | 7053 | return 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7054 | |
| 7055 | out: |
| 7056 | list_for_each_entry_safe(smb_lock, tmp, &lock_list, llist) { |
| 7057 | locks_free_lock(smb_lock->fl); |
| 7058 | list_del(&smb_lock->llist); |
| 7059 | kfree(smb_lock); |
| 7060 | } |
| 7061 | |
| 7062 | list_for_each_entry_safe(smb_lock, tmp, &rollback_list, llist) { |
| 7063 | struct file_lock *rlock = NULL; |
| 7064 | |
| 7065 | rlock = smb_flock_init(filp); |
| 7066 | rlock->fl_type = F_UNLCK; |
| 7067 | rlock->fl_start = smb_lock->start; |
| 7068 | rlock->fl_end = smb_lock->end; |
| 7069 | |
Namjae Jeon | 96ad4ec | 2021-07-13 17:17:28 +0900 | [diff] [blame] | 7070 | rc = vfs_lock_file(filp, 0, rlock, NULL); |
| 7071 | if (rc) |
| 7072 | pr_err("rollback unlock fail : %d\n", rc); |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 7073 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7074 | list_del(&smb_lock->llist); |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 7075 | spin_lock(&work->conn->llist_lock); |
| 7076 | if (!list_empty(&smb_lock->flist)) |
| 7077 | list_del(&smb_lock->flist); |
| 7078 | list_del(&smb_lock->clist); |
| 7079 | spin_unlock(&work->conn->llist_lock); |
| 7080 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7081 | locks_free_lock(smb_lock->fl); |
| 7082 | locks_free_lock(rlock); |
| 7083 | kfree(smb_lock); |
| 7084 | } |
| 7085 | out2: |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 7086 | ksmbd_debug(SMB, "failed in taking lock(flags : %x), err : %d\n", flags, err); |
| 7087 | |
| 7088 | if (!rsp->hdr.Status) { |
| 7089 | if (err == -EINVAL) |
| 7090 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 7091 | else if (err == -ENOMEM) |
| 7092 | rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES; |
| 7093 | else if (err == -ENOENT) |
| 7094 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 7095 | else |
| 7096 | rsp->hdr.Status = STATUS_LOCK_NOT_GRANTED; |
| 7097 | } |
| 7098 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7099 | smb2_set_err_rsp(work); |
| 7100 | ksmbd_fd_put(work, fp); |
Namjae Jeon | 96ad4ec | 2021-07-13 17:17:28 +0900 | [diff] [blame] | 7101 | return err; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7102 | } |
| 7103 | |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7104 | static int fsctl_copychunk(struct ksmbd_work *work, |
| 7105 | struct copychunk_ioctl_req *ci_req, |
| 7106 | unsigned int cnt_code, |
| 7107 | unsigned int input_count, |
| 7108 | unsigned long long volatile_id, |
| 7109 | unsigned long long persistent_id, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7110 | struct smb2_ioctl_rsp *rsp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7111 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7112 | struct copychunk_ioctl_rsp *ci_rsp; |
| 7113 | struct ksmbd_file *src_fp = NULL, *dst_fp = NULL; |
| 7114 | struct srv_copychunk *chunks; |
| 7115 | unsigned int i, chunk_count, chunk_count_written = 0; |
| 7116 | unsigned int chunk_size_written = 0; |
| 7117 | loff_t total_size_written = 0; |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7118 | int ret = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7119 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7120 | ci_rsp = (struct copychunk_ioctl_rsp *)&rsp->Buffer[0]; |
| 7121 | |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7122 | rsp->VolatileFileId = cpu_to_le64(volatile_id); |
| 7123 | rsp->PersistentFileId = cpu_to_le64(persistent_id); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7124 | ci_rsp->ChunksWritten = |
| 7125 | cpu_to_le32(ksmbd_server_side_copy_max_chunk_count()); |
| 7126 | ci_rsp->ChunkBytesWritten = |
| 7127 | cpu_to_le32(ksmbd_server_side_copy_max_chunk_size()); |
| 7128 | ci_rsp->TotalBytesWritten = |
| 7129 | cpu_to_le32(ksmbd_server_side_copy_max_total_size()); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7130 | |
| 7131 | chunks = (struct srv_copychunk *)&ci_req->Chunks[0]; |
| 7132 | chunk_count = le32_to_cpu(ci_req->ChunkCount); |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7133 | if (chunk_count == 0) |
| 7134 | goto out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7135 | total_size_written = 0; |
| 7136 | |
| 7137 | /* verify the SRV_COPYCHUNK_COPY packet */ |
| 7138 | if (chunk_count > ksmbd_server_side_copy_max_chunk_count() || |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7139 | input_count < offsetof(struct copychunk_ioctl_req, Chunks) + |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7140 | chunk_count * sizeof(struct srv_copychunk)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7141 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 7142 | return -EINVAL; |
| 7143 | } |
| 7144 | |
| 7145 | for (i = 0; i < chunk_count; i++) { |
| 7146 | if (le32_to_cpu(chunks[i].Length) == 0 || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7147 | 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] | 7148 | break; |
| 7149 | total_size_written += le32_to_cpu(chunks[i].Length); |
| 7150 | } |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7151 | |
| 7152 | if (i < chunk_count || |
| 7153 | total_size_written > ksmbd_server_side_copy_max_total_size()) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7154 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 7155 | return -EINVAL; |
| 7156 | } |
| 7157 | |
| 7158 | src_fp = ksmbd_lookup_foreign_fd(work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7159 | le64_to_cpu(ci_req->ResumeKey[0])); |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7160 | dst_fp = ksmbd_lookup_fd_slow(work, volatile_id, persistent_id); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7161 | ret = -EINVAL; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7162 | if (!src_fp || |
| 7163 | src_fp->persistent_id != le64_to_cpu(ci_req->ResumeKey[1])) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7164 | rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND; |
| 7165 | goto out; |
| 7166 | } |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7167 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7168 | if (!dst_fp) { |
| 7169 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 7170 | goto out; |
| 7171 | } |
| 7172 | |
| 7173 | /* |
| 7174 | * FILE_READ_DATA should only be included in |
| 7175 | * the FSCTL_COPYCHUNK case |
| 7176 | */ |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7177 | if (cnt_code == FSCTL_COPYCHUNK && |
| 7178 | !(dst_fp->daccess & (FILE_READ_DATA_LE | FILE_GENERIC_READ_LE))) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7179 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 7180 | goto out; |
| 7181 | } |
| 7182 | |
| 7183 | ret = ksmbd_vfs_copy_file_ranges(work, src_fp, dst_fp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7184 | chunks, chunk_count, |
| 7185 | &chunk_count_written, |
| 7186 | &chunk_size_written, |
| 7187 | &total_size_written); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7188 | if (ret < 0) { |
| 7189 | if (ret == -EACCES) |
| 7190 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 7191 | if (ret == -EAGAIN) |
| 7192 | rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT; |
| 7193 | else if (ret == -EBADF) |
| 7194 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 7195 | else if (ret == -EFBIG || ret == -ENOSPC) |
| 7196 | rsp->hdr.Status = STATUS_DISK_FULL; |
| 7197 | else if (ret == -EINVAL) |
| 7198 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 7199 | else if (ret == -EISDIR) |
| 7200 | rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY; |
| 7201 | else if (ret == -E2BIG) |
| 7202 | rsp->hdr.Status = STATUS_INVALID_VIEW_SIZE; |
| 7203 | else |
| 7204 | rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR; |
| 7205 | } |
| 7206 | |
| 7207 | ci_rsp->ChunksWritten = cpu_to_le32(chunk_count_written); |
| 7208 | ci_rsp->ChunkBytesWritten = cpu_to_le32(chunk_size_written); |
| 7209 | ci_rsp->TotalBytesWritten = cpu_to_le32(total_size_written); |
| 7210 | out: |
| 7211 | ksmbd_fd_put(work, src_fp); |
| 7212 | ksmbd_fd_put(work, dst_fp); |
| 7213 | return ret; |
| 7214 | } |
| 7215 | |
| 7216 | static __be32 idev_ipv4_address(struct in_device *idev) |
| 7217 | { |
| 7218 | __be32 addr = 0; |
| 7219 | |
| 7220 | struct in_ifaddr *ifa; |
| 7221 | |
| 7222 | rcu_read_lock(); |
| 7223 | in_dev_for_each_ifa_rcu(ifa, idev) { |
| 7224 | if (ifa->ifa_flags & IFA_F_SECONDARY) |
| 7225 | continue; |
| 7226 | |
| 7227 | addr = ifa->ifa_address; |
| 7228 | break; |
| 7229 | } |
| 7230 | rcu_read_unlock(); |
| 7231 | return addr; |
| 7232 | } |
| 7233 | |
| 7234 | static int fsctl_query_iface_info_ioctl(struct ksmbd_conn *conn, |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7235 | struct smb2_ioctl_rsp *rsp, |
| 7236 | unsigned int out_buf_len) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7237 | { |
| 7238 | struct network_interface_info_ioctl_rsp *nii_rsp = NULL; |
| 7239 | int nbytes = 0; |
| 7240 | struct net_device *netdev; |
| 7241 | struct sockaddr_storage_rsp *sockaddr_storage; |
| 7242 | unsigned int flags; |
| 7243 | unsigned long long speed; |
| 7244 | |
| 7245 | rtnl_lock(); |
| 7246 | for_each_netdev(&init_net, netdev) { |
Namjae Jeon | 71cd9cb | 2021-12-16 10:31:44 +0900 | [diff] [blame] | 7247 | bool ipv4_set = false; |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7248 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7249 | if (netdev->type == ARPHRD_LOOPBACK) |
| 7250 | continue; |
| 7251 | |
| 7252 | flags = dev_get_flags(netdev); |
| 7253 | if (!(flags & IFF_RUNNING)) |
| 7254 | continue; |
Namjae Jeon | 71cd9cb | 2021-12-16 10:31:44 +0900 | [diff] [blame] | 7255 | ipv6_retry: |
| 7256 | if (out_buf_len < |
| 7257 | nbytes + sizeof(struct network_interface_info_ioctl_rsp)) { |
| 7258 | rtnl_unlock(); |
| 7259 | return -ENOSPC; |
| 7260 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7261 | |
| 7262 | nii_rsp = (struct network_interface_info_ioctl_rsp *) |
| 7263 | &rsp->Buffer[nbytes]; |
| 7264 | nii_rsp->IfIndex = cpu_to_le32(netdev->ifindex); |
| 7265 | |
Hyunchul Lee | 03d8d4f | 2021-07-13 16:09:34 +0900 | [diff] [blame] | 7266 | nii_rsp->Capability = 0; |
Namjae Jeon | a58b45a | 2021-12-16 10:26:43 +0900 | [diff] [blame] | 7267 | if (netdev->real_num_tx_queues > 1) |
| 7268 | nii_rsp->Capability |= cpu_to_le32(RSS_CAPABLE); |
Hyunchul Lee | 03d8d4f | 2021-07-13 16:09:34 +0900 | [diff] [blame] | 7269 | if (ksmbd_rdma_capable_netdev(netdev)) |
| 7270 | nii_rsp->Capability |= cpu_to_le32(RDMA_CAPABLE); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7271 | |
| 7272 | nii_rsp->Next = cpu_to_le32(152); |
| 7273 | nii_rsp->Reserved = 0; |
| 7274 | |
| 7275 | if (netdev->ethtool_ops->get_link_ksettings) { |
| 7276 | struct ethtool_link_ksettings cmd; |
| 7277 | |
| 7278 | netdev->ethtool_ops->get_link_ksettings(netdev, &cmd); |
| 7279 | speed = cmd.base.speed; |
| 7280 | } else { |
Per Forlin | d475866 | 2021-08-30 13:23:04 +0900 | [diff] [blame] | 7281 | ksmbd_debug(SMB, "%s %s\n", netdev->name, |
| 7282 | "speed is unknown, defaulting to 1Gb/sec"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7283 | speed = SPEED_1000; |
| 7284 | } |
| 7285 | |
| 7286 | speed *= 1000000; |
| 7287 | nii_rsp->LinkSpeed = cpu_to_le64(speed); |
| 7288 | |
| 7289 | sockaddr_storage = (struct sockaddr_storage_rsp *) |
| 7290 | nii_rsp->SockAddr_Storage; |
| 7291 | memset(sockaddr_storage, 0, 128); |
| 7292 | |
Namjae Jeon | 71cd9cb | 2021-12-16 10:31:44 +0900 | [diff] [blame] | 7293 | if (!ipv4_set) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7294 | struct in_device *idev; |
| 7295 | |
| 7296 | sockaddr_storage->Family = cpu_to_le16(INTERNETWORK); |
| 7297 | sockaddr_storage->addr4.Port = 0; |
| 7298 | |
| 7299 | idev = __in_dev_get_rtnl(netdev); |
| 7300 | if (!idev) |
| 7301 | continue; |
| 7302 | sockaddr_storage->addr4.IPv4address = |
| 7303 | idev_ipv4_address(idev); |
Namjae Jeon | 71cd9cb | 2021-12-16 10:31:44 +0900 | [diff] [blame] | 7304 | nbytes += sizeof(struct network_interface_info_ioctl_rsp); |
| 7305 | ipv4_set = true; |
| 7306 | goto ipv6_retry; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7307 | } else { |
| 7308 | struct inet6_dev *idev6; |
| 7309 | struct inet6_ifaddr *ifa; |
| 7310 | __u8 *ipv6_addr = sockaddr_storage->addr6.IPv6address; |
| 7311 | |
| 7312 | sockaddr_storage->Family = cpu_to_le16(INTERNETWORKV6); |
| 7313 | sockaddr_storage->addr6.Port = 0; |
| 7314 | sockaddr_storage->addr6.FlowInfo = 0; |
| 7315 | |
| 7316 | idev6 = __in6_dev_get(netdev); |
| 7317 | if (!idev6) |
| 7318 | continue; |
| 7319 | |
| 7320 | list_for_each_entry(ifa, &idev6->addr_list, if_list) { |
| 7321 | if (ifa->flags & (IFA_F_TENTATIVE | |
| 7322 | IFA_F_DEPRECATED)) |
| 7323 | continue; |
| 7324 | memcpy(ipv6_addr, ifa->addr.s6_addr, 16); |
| 7325 | break; |
| 7326 | } |
| 7327 | sockaddr_storage->addr6.ScopeId = 0; |
Namjae Jeon | 71cd9cb | 2021-12-16 10:31:44 +0900 | [diff] [blame] | 7328 | nbytes += sizeof(struct network_interface_info_ioctl_rsp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7329 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7330 | } |
| 7331 | rtnl_unlock(); |
| 7332 | |
| 7333 | /* zero if this is last one */ |
| 7334 | if (nii_rsp) |
| 7335 | nii_rsp->Next = 0; |
| 7336 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7337 | rsp->PersistentFileId = cpu_to_le64(SMB2_NO_FID); |
| 7338 | rsp->VolatileFileId = cpu_to_le64(SMB2_NO_FID); |
| 7339 | return nbytes; |
| 7340 | } |
| 7341 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7342 | static int fsctl_validate_negotiate_info(struct ksmbd_conn *conn, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7343 | struct validate_negotiate_info_req *neg_req, |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7344 | struct validate_negotiate_info_rsp *neg_rsp, |
| 7345 | unsigned int in_buf_len) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7346 | { |
| 7347 | int ret = 0; |
| 7348 | int dialect; |
| 7349 | |
Marios Makassikis | 78f1688 | 2021-10-28 21:01:27 +0200 | [diff] [blame] | 7350 | if (in_buf_len < offsetof(struct validate_negotiate_info_req, Dialects) + |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7351 | le16_to_cpu(neg_req->DialectCount) * sizeof(__le16)) |
| 7352 | return -EINVAL; |
| 7353 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7354 | dialect = ksmbd_lookup_dialect_by_id(neg_req->Dialects, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7355 | neg_req->DialectCount); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7356 | if (dialect == BAD_PROT_ID || dialect != conn->dialect) { |
| 7357 | ret = -EINVAL; |
| 7358 | goto err_out; |
| 7359 | } |
| 7360 | |
| 7361 | if (strncmp(neg_req->Guid, conn->ClientGUID, SMB2_CLIENT_GUID_SIZE)) { |
| 7362 | ret = -EINVAL; |
| 7363 | goto err_out; |
| 7364 | } |
| 7365 | |
| 7366 | if (le16_to_cpu(neg_req->SecurityMode) != conn->cli_sec_mode) { |
| 7367 | ret = -EINVAL; |
| 7368 | goto err_out; |
| 7369 | } |
| 7370 | |
| 7371 | if (le32_to_cpu(neg_req->Capabilities) != conn->cli_cap) { |
| 7372 | ret = -EINVAL; |
| 7373 | goto err_out; |
| 7374 | } |
| 7375 | |
| 7376 | neg_rsp->Capabilities = cpu_to_le32(conn->vals->capabilities); |
| 7377 | memset(neg_rsp->Guid, 0, SMB2_CLIENT_GUID_SIZE); |
| 7378 | neg_rsp->SecurityMode = cpu_to_le16(conn->srv_sec_mode); |
| 7379 | neg_rsp->Dialect = cpu_to_le16(conn->dialect); |
| 7380 | err_out: |
| 7381 | return ret; |
| 7382 | } |
| 7383 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7384 | static int fsctl_query_allocated_ranges(struct ksmbd_work *work, u64 id, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7385 | struct file_allocated_range_buffer *qar_req, |
| 7386 | struct file_allocated_range_buffer *qar_rsp, |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7387 | unsigned int in_count, unsigned int *out_count) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7388 | { |
| 7389 | struct ksmbd_file *fp; |
| 7390 | loff_t start, length; |
| 7391 | int ret = 0; |
| 7392 | |
| 7393 | *out_count = 0; |
| 7394 | if (in_count == 0) |
| 7395 | return -EINVAL; |
| 7396 | |
| 7397 | fp = ksmbd_lookup_fd_fast(work, id); |
| 7398 | if (!fp) |
| 7399 | return -ENOENT; |
| 7400 | |
| 7401 | start = le64_to_cpu(qar_req->file_offset); |
| 7402 | length = le64_to_cpu(qar_req->length); |
| 7403 | |
| 7404 | ret = ksmbd_vfs_fqar_lseek(fp, start, length, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7405 | qar_rsp, in_count, out_count); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7406 | if (ret && ret != -E2BIG) |
| 7407 | *out_count = 0; |
| 7408 | |
| 7409 | ksmbd_fd_put(work, fp); |
| 7410 | return ret; |
| 7411 | } |
| 7412 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7413 | static int fsctl_pipe_transceive(struct ksmbd_work *work, u64 id, |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7414 | unsigned int out_buf_len, |
| 7415 | struct smb2_ioctl_req *req, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7416 | struct smb2_ioctl_rsp *rsp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7417 | { |
| 7418 | struct ksmbd_rpc_command *rpc_resp; |
| 7419 | char *data_buf = (char *)&req->Buffer[0]; |
| 7420 | int nbytes = 0; |
| 7421 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7422 | rpc_resp = ksmbd_rpc_ioctl(work->sess, id, data_buf, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7423 | le32_to_cpu(req->InputCount)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7424 | if (rpc_resp) { |
| 7425 | if (rpc_resp->flags == KSMBD_RPC_SOME_NOT_MAPPED) { |
| 7426 | /* |
| 7427 | * set STATUS_SOME_NOT_MAPPED response |
| 7428 | * for unknown domain sid. |
| 7429 | */ |
| 7430 | rsp->hdr.Status = STATUS_SOME_NOT_MAPPED; |
| 7431 | } else if (rpc_resp->flags == KSMBD_RPC_ENOTIMPLEMENTED) { |
| 7432 | rsp->hdr.Status = STATUS_NOT_SUPPORTED; |
| 7433 | goto out; |
| 7434 | } else if (rpc_resp->flags != KSMBD_RPC_OK) { |
| 7435 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 7436 | goto out; |
| 7437 | } |
| 7438 | |
| 7439 | nbytes = rpc_resp->payload_sz; |
| 7440 | if (rpc_resp->payload_sz > out_buf_len) { |
| 7441 | rsp->hdr.Status = STATUS_BUFFER_OVERFLOW; |
| 7442 | nbytes = out_buf_len; |
| 7443 | } |
| 7444 | |
| 7445 | if (!rpc_resp->payload_sz) { |
| 7446 | rsp->hdr.Status = |
| 7447 | STATUS_UNEXPECTED_IO_ERROR; |
| 7448 | goto out; |
| 7449 | } |
| 7450 | |
| 7451 | memcpy((char *)rsp->Buffer, rpc_resp->payload, nbytes); |
| 7452 | } |
| 7453 | out: |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 7454 | kvfree(rpc_resp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7455 | return nbytes; |
| 7456 | } |
| 7457 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7458 | static inline int fsctl_set_sparse(struct ksmbd_work *work, u64 id, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7459 | struct file_sparse *sparse) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7460 | { |
| 7461 | struct ksmbd_file *fp; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 7462 | struct user_namespace *user_ns; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7463 | int ret = 0; |
| 7464 | __le32 old_fattr; |
| 7465 | |
| 7466 | fp = ksmbd_lookup_fd_fast(work, id); |
| 7467 | if (!fp) |
| 7468 | return -ENOENT; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 7469 | user_ns = file_mnt_user_ns(fp->filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7470 | |
| 7471 | old_fattr = fp->f_ci->m_fattr; |
| 7472 | if (sparse->SetSparse) |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 7473 | fp->f_ci->m_fattr |= FILE_ATTRIBUTE_SPARSE_FILE_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7474 | else |
Ronnie Sahlberg | 26a2787 | 2021-11-03 08:45:52 +0900 | [diff] [blame] | 7475 | fp->f_ci->m_fattr &= ~FILE_ATTRIBUTE_SPARSE_FILE_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7476 | |
| 7477 | if (fp->f_ci->m_fattr != old_fattr && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7478 | test_share_config_flag(work->tcon->share_conf, |
| 7479 | KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7480 | struct xattr_dos_attrib da; |
| 7481 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 7482 | ret = ksmbd_vfs_get_dos_attrib_xattr(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 7483 | fp->filp->f_path.dentry, &da); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7484 | if (ret <= 0) |
| 7485 | goto out; |
| 7486 | |
| 7487 | da.attr = le32_to_cpu(fp->f_ci->m_fattr); |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 7488 | ret = ksmbd_vfs_set_dos_attrib_xattr(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 7489 | fp->filp->f_path.dentry, &da); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7490 | if (ret) |
| 7491 | fp->f_ci->m_fattr = old_fattr; |
| 7492 | } |
| 7493 | |
| 7494 | out: |
| 7495 | ksmbd_fd_put(work, fp); |
| 7496 | return ret; |
| 7497 | } |
| 7498 | |
| 7499 | static int fsctl_request_resume_key(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7500 | struct smb2_ioctl_req *req, |
| 7501 | struct resume_key_ioctl_rsp *key_rsp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7502 | { |
| 7503 | struct ksmbd_file *fp; |
| 7504 | |
| 7505 | fp = ksmbd_lookup_fd_slow(work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7506 | le64_to_cpu(req->VolatileFileId), |
| 7507 | le64_to_cpu(req->PersistentFileId)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7508 | if (!fp) |
| 7509 | return -ENOENT; |
| 7510 | |
| 7511 | memset(key_rsp, 0, sizeof(*key_rsp)); |
| 7512 | key_rsp->ResumeKey[0] = req->VolatileFileId; |
| 7513 | key_rsp->ResumeKey[1] = req->PersistentFileId; |
| 7514 | ksmbd_fd_put(work, fp); |
| 7515 | |
| 7516 | return 0; |
| 7517 | } |
| 7518 | |
| 7519 | /** |
| 7520 | * smb2_ioctl() - handler for smb2 ioctl command |
| 7521 | * @work: smb work containing ioctl command buffer |
| 7522 | * |
| 7523 | * Return: 0 on success, otherwise error |
| 7524 | */ |
| 7525 | int smb2_ioctl(struct ksmbd_work *work) |
| 7526 | { |
| 7527 | struct smb2_ioctl_req *req; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 7528 | struct smb2_ioctl_rsp *rsp; |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7529 | unsigned int cnt_code, nbytes = 0, out_buf_len, in_buf_len; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7530 | u64 id = KSMBD_NO_FID; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7531 | struct ksmbd_conn *conn = work->conn; |
| 7532 | int ret = 0; |
| 7533 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7534 | if (work->next_smb2_rcv_hdr_off) { |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 7535 | req = ksmbd_req_buf_next(work); |
| 7536 | rsp = ksmbd_resp_buf_next(work); |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 7537 | if (!has_file_id(le64_to_cpu(req->VolatileFileId))) { |
| 7538 | ksmbd_debug(SMB, "Compound request set FID = %llu\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7539 | work->compound_fid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7540 | id = work->compound_fid; |
| 7541 | } |
| 7542 | } else { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 7543 | req = smb2_get_msg(work->request_buf); |
| 7544 | rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7545 | } |
| 7546 | |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 7547 | if (!has_file_id(id)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7548 | id = le64_to_cpu(req->VolatileFileId); |
| 7549 | |
| 7550 | if (req->Flags != cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL)) { |
| 7551 | rsp->hdr.Status = STATUS_NOT_SUPPORTED; |
| 7552 | goto out; |
| 7553 | } |
| 7554 | |
| 7555 | cnt_code = le32_to_cpu(req->CntCode); |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 7556 | ret = smb2_calc_max_out_buf_len(work, 48, |
| 7557 | le32_to_cpu(req->MaxOutputResponse)); |
| 7558 | if (ret < 0) { |
| 7559 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 7560 | goto out; |
| 7561 | } |
| 7562 | out_buf_len = (unsigned int)ret; |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7563 | in_buf_len = le32_to_cpu(req->InputCount); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7564 | |
| 7565 | switch (cnt_code) { |
| 7566 | case FSCTL_DFS_GET_REFERRALS: |
| 7567 | case FSCTL_DFS_GET_REFERRALS_EX: |
| 7568 | /* Not support DFS yet */ |
| 7569 | rsp->hdr.Status = STATUS_FS_DRIVER_REQUIRED; |
| 7570 | goto out; |
| 7571 | case FSCTL_CREATE_OR_GET_OBJECT_ID: |
| 7572 | { |
| 7573 | struct file_object_buf_type1_ioctl_rsp *obj_buf; |
| 7574 | |
| 7575 | nbytes = sizeof(struct file_object_buf_type1_ioctl_rsp); |
| 7576 | obj_buf = (struct file_object_buf_type1_ioctl_rsp *) |
| 7577 | &rsp->Buffer[0]; |
| 7578 | |
| 7579 | /* |
| 7580 | * TODO: This is dummy implementation to pass smbtorture |
| 7581 | * Need to check correct response later |
| 7582 | */ |
| 7583 | memset(obj_buf->ObjectId, 0x0, 16); |
| 7584 | memset(obj_buf->BirthVolumeId, 0x0, 16); |
| 7585 | memset(obj_buf->BirthObjectId, 0x0, 16); |
| 7586 | memset(obj_buf->DomainId, 0x0, 16); |
| 7587 | |
| 7588 | break; |
| 7589 | } |
| 7590 | case FSCTL_PIPE_TRANSCEIVE: |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7591 | 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] | 7592 | nbytes = fsctl_pipe_transceive(work, id, out_buf_len, req, rsp); |
| 7593 | break; |
| 7594 | case FSCTL_VALIDATE_NEGOTIATE_INFO: |
| 7595 | if (conn->dialect < SMB30_PROT_ID) { |
| 7596 | ret = -EOPNOTSUPP; |
| 7597 | goto out; |
| 7598 | } |
| 7599 | |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7600 | if (in_buf_len < sizeof(struct validate_negotiate_info_req)) |
| 7601 | return -EINVAL; |
| 7602 | |
| 7603 | if (out_buf_len < sizeof(struct validate_negotiate_info_rsp)) |
| 7604 | return -EINVAL; |
| 7605 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7606 | ret = fsctl_validate_negotiate_info(conn, |
| 7607 | (struct validate_negotiate_info_req *)&req->Buffer[0], |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7608 | (struct validate_negotiate_info_rsp *)&rsp->Buffer[0], |
| 7609 | in_buf_len); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7610 | if (ret < 0) |
| 7611 | goto out; |
| 7612 | |
| 7613 | nbytes = sizeof(struct validate_negotiate_info_rsp); |
| 7614 | rsp->PersistentFileId = cpu_to_le64(SMB2_NO_FID); |
| 7615 | rsp->VolatileFileId = cpu_to_le64(SMB2_NO_FID); |
| 7616 | break; |
| 7617 | case FSCTL_QUERY_NETWORK_INTERFACE_INFO: |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7618 | ret = fsctl_query_iface_info_ioctl(conn, rsp, out_buf_len); |
| 7619 | if (ret < 0) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7620 | goto out; |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7621 | nbytes = ret; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7622 | break; |
| 7623 | case FSCTL_REQUEST_RESUME_KEY: |
| 7624 | if (out_buf_len < sizeof(struct resume_key_ioctl_rsp)) { |
| 7625 | ret = -EINVAL; |
| 7626 | goto out; |
| 7627 | } |
| 7628 | |
| 7629 | ret = fsctl_request_resume_key(work, req, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7630 | (struct resume_key_ioctl_rsp *)&rsp->Buffer[0]); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7631 | if (ret < 0) |
| 7632 | goto out; |
| 7633 | rsp->PersistentFileId = req->PersistentFileId; |
| 7634 | rsp->VolatileFileId = req->VolatileFileId; |
| 7635 | nbytes = sizeof(struct resume_key_ioctl_rsp); |
| 7636 | break; |
| 7637 | case FSCTL_COPYCHUNK: |
| 7638 | case FSCTL_COPYCHUNK_WRITE: |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7639 | if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7640 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7641 | "User does not have write permission\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7642 | ret = -EACCES; |
| 7643 | goto out; |
| 7644 | } |
| 7645 | |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7646 | if (in_buf_len < sizeof(struct copychunk_ioctl_req)) { |
| 7647 | ret = -EINVAL; |
| 7648 | goto out; |
| 7649 | } |
| 7650 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7651 | if (out_buf_len < sizeof(struct copychunk_ioctl_rsp)) { |
| 7652 | ret = -EINVAL; |
| 7653 | goto out; |
| 7654 | } |
| 7655 | |
| 7656 | nbytes = sizeof(struct copychunk_ioctl_rsp); |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7657 | rsp->VolatileFileId = req->VolatileFileId; |
| 7658 | rsp->PersistentFileId = req->PersistentFileId; |
| 7659 | fsctl_copychunk(work, |
| 7660 | (struct copychunk_ioctl_req *)&req->Buffer[0], |
| 7661 | le32_to_cpu(req->CntCode), |
| 7662 | le32_to_cpu(req->InputCount), |
| 7663 | le64_to_cpu(req->VolatileFileId), |
| 7664 | le64_to_cpu(req->PersistentFileId), |
| 7665 | rsp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7666 | break; |
| 7667 | case FSCTL_SET_SPARSE: |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7668 | if (in_buf_len < sizeof(struct file_sparse)) { |
| 7669 | ret = -EINVAL; |
| 7670 | goto out; |
| 7671 | } |
| 7672 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7673 | ret = fsctl_set_sparse(work, id, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7674 | (struct file_sparse *)&req->Buffer[0]); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7675 | if (ret < 0) |
| 7676 | goto out; |
| 7677 | break; |
| 7678 | case FSCTL_SET_ZERO_DATA: |
| 7679 | { |
| 7680 | struct file_zero_data_information *zero_data; |
| 7681 | struct ksmbd_file *fp; |
| 7682 | loff_t off, len; |
| 7683 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7684 | if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7685 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7686 | "User does not have write permission\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7687 | ret = -EACCES; |
| 7688 | goto out; |
| 7689 | } |
| 7690 | |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7691 | if (in_buf_len < sizeof(struct file_zero_data_information)) { |
| 7692 | ret = -EINVAL; |
| 7693 | goto out; |
| 7694 | } |
| 7695 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7696 | zero_data = |
| 7697 | (struct file_zero_data_information *)&req->Buffer[0]; |
| 7698 | |
| 7699 | fp = ksmbd_lookup_fd_fast(work, id); |
| 7700 | if (!fp) { |
| 7701 | ret = -ENOENT; |
| 7702 | goto out; |
| 7703 | } |
| 7704 | |
| 7705 | off = le64_to_cpu(zero_data->FileOffset); |
| 7706 | len = le64_to_cpu(zero_data->BeyondFinalZero) - off; |
| 7707 | |
| 7708 | ret = ksmbd_vfs_zero_data(work, fp, off, len); |
| 7709 | ksmbd_fd_put(work, fp); |
| 7710 | if (ret < 0) |
| 7711 | goto out; |
| 7712 | break; |
| 7713 | } |
| 7714 | case FSCTL_QUERY_ALLOCATED_RANGES: |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7715 | if (in_buf_len < sizeof(struct file_allocated_range_buffer)) { |
| 7716 | ret = -EINVAL; |
| 7717 | goto out; |
| 7718 | } |
| 7719 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7720 | ret = fsctl_query_allocated_ranges(work, id, |
| 7721 | (struct file_allocated_range_buffer *)&req->Buffer[0], |
| 7722 | (struct file_allocated_range_buffer *)&rsp->Buffer[0], |
| 7723 | out_buf_len / |
| 7724 | sizeof(struct file_allocated_range_buffer), &nbytes); |
| 7725 | if (ret == -E2BIG) { |
| 7726 | rsp->hdr.Status = STATUS_BUFFER_OVERFLOW; |
| 7727 | } else if (ret < 0) { |
| 7728 | nbytes = 0; |
| 7729 | goto out; |
| 7730 | } |
| 7731 | |
| 7732 | nbytes *= sizeof(struct file_allocated_range_buffer); |
| 7733 | break; |
| 7734 | case FSCTL_GET_REPARSE_POINT: |
| 7735 | { |
| 7736 | struct reparse_data_buffer *reparse_ptr; |
| 7737 | struct ksmbd_file *fp; |
| 7738 | |
| 7739 | reparse_ptr = (struct reparse_data_buffer *)&rsp->Buffer[0]; |
| 7740 | fp = ksmbd_lookup_fd_fast(work, id); |
| 7741 | if (!fp) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 7742 | pr_err("not found fp!!\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7743 | ret = -ENOENT; |
| 7744 | goto out; |
| 7745 | } |
| 7746 | |
| 7747 | reparse_ptr->ReparseTag = |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 7748 | smb2_get_reparse_tag_special_file(file_inode(fp->filp)->i_mode); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7749 | reparse_ptr->ReparseDataLength = 0; |
| 7750 | ksmbd_fd_put(work, fp); |
| 7751 | nbytes = sizeof(struct reparse_data_buffer); |
| 7752 | break; |
| 7753 | } |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 7754 | case FSCTL_DUPLICATE_EXTENTS_TO_FILE: |
| 7755 | { |
| 7756 | struct ksmbd_file *fp_in, *fp_out = NULL; |
| 7757 | struct duplicate_extents_to_file *dup_ext; |
| 7758 | loff_t src_off, dst_off, length, cloned; |
| 7759 | |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7760 | if (in_buf_len < sizeof(struct duplicate_extents_to_file)) { |
| 7761 | ret = -EINVAL; |
| 7762 | goto out; |
| 7763 | } |
| 7764 | |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 7765 | dup_ext = (struct duplicate_extents_to_file *)&req->Buffer[0]; |
| 7766 | |
| 7767 | fp_in = ksmbd_lookup_fd_slow(work, dup_ext->VolatileFileHandle, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7768 | dup_ext->PersistentFileHandle); |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 7769 | if (!fp_in) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 7770 | pr_err("not found file handle in duplicate extent to file\n"); |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 7771 | ret = -ENOENT; |
| 7772 | goto out; |
| 7773 | } |
| 7774 | |
| 7775 | fp_out = ksmbd_lookup_fd_fast(work, id); |
| 7776 | if (!fp_out) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 7777 | pr_err("not found fp\n"); |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 7778 | ret = -ENOENT; |
| 7779 | goto dup_ext_out; |
| 7780 | } |
| 7781 | |
| 7782 | src_off = le64_to_cpu(dup_ext->SourceFileOffset); |
| 7783 | dst_off = le64_to_cpu(dup_ext->TargetFileOffset); |
| 7784 | length = le64_to_cpu(dup_ext->ByteCount); |
| 7785 | 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] | 7786 | dst_off, length, 0); |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 7787 | if (cloned == -EXDEV || cloned == -EOPNOTSUPP) { |
| 7788 | ret = -EOPNOTSUPP; |
| 7789 | goto dup_ext_out; |
| 7790 | } else if (cloned != length) { |
Namjae Jeon | f852477 | 2021-06-18 10:28:00 +0900 | [diff] [blame] | 7791 | cloned = vfs_copy_file_range(fp_in->filp, src_off, |
| 7792 | fp_out->filp, dst_off, length, 0); |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 7793 | if (cloned != length) { |
| 7794 | if (cloned < 0) |
| 7795 | ret = cloned; |
| 7796 | else |
| 7797 | ret = -EINVAL; |
| 7798 | } |
| 7799 | } |
| 7800 | |
| 7801 | dup_ext_out: |
| 7802 | ksmbd_fd_put(work, fp_in); |
| 7803 | ksmbd_fd_put(work, fp_out); |
| 7804 | if (ret < 0) |
| 7805 | goto out; |
| 7806 | break; |
| 7807 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7808 | default: |
| 7809 | ksmbd_debug(SMB, "not implemented yet ioctl command 0x%x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7810 | cnt_code); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7811 | ret = -EOPNOTSUPP; |
| 7812 | goto out; |
| 7813 | } |
| 7814 | |
| 7815 | rsp->CntCode = cpu_to_le32(cnt_code); |
| 7816 | rsp->InputCount = cpu_to_le32(0); |
| 7817 | rsp->InputOffset = cpu_to_le32(112); |
| 7818 | rsp->OutputOffset = cpu_to_le32(112); |
| 7819 | rsp->OutputCount = cpu_to_le32(nbytes); |
| 7820 | rsp->StructureSize = cpu_to_le16(49); |
| 7821 | rsp->Reserved = cpu_to_le16(0); |
| 7822 | rsp->Flags = cpu_to_le32(0); |
| 7823 | rsp->Reserved2 = cpu_to_le32(0); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 7824 | inc_rfc1001_len(work->response_buf, 48 + nbytes); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7825 | |
| 7826 | return 0; |
| 7827 | |
| 7828 | out: |
| 7829 | if (ret == -EACCES) |
| 7830 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 7831 | else if (ret == -ENOENT) |
| 7832 | rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND; |
| 7833 | else if (ret == -EOPNOTSUPP) |
| 7834 | rsp->hdr.Status = STATUS_NOT_SUPPORTED; |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7835 | else if (ret == -ENOSPC) |
| 7836 | rsp->hdr.Status = STATUS_BUFFER_TOO_SMALL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7837 | else if (ret < 0 || rsp->hdr.Status == 0) |
| 7838 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 7839 | smb2_set_err_rsp(work); |
| 7840 | return 0; |
| 7841 | } |
| 7842 | |
| 7843 | /** |
| 7844 | * smb20_oplock_break_ack() - handler for smb2.0 oplock break command |
| 7845 | * @work: smb work containing oplock break command buffer |
| 7846 | * |
| 7847 | * Return: 0 |
| 7848 | */ |
| 7849 | static void smb20_oplock_break_ack(struct ksmbd_work *work) |
| 7850 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 7851 | struct smb2_oplock_break *req = smb2_get_msg(work->request_buf); |
| 7852 | struct smb2_oplock_break *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7853 | struct ksmbd_file *fp; |
| 7854 | struct oplock_info *opinfo = NULL; |
| 7855 | __le32 err = 0; |
| 7856 | int ret = 0; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7857 | u64 volatile_id, persistent_id; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7858 | char req_oplevel = 0, rsp_oplevel = 0; |
| 7859 | unsigned int oplock_change_type; |
| 7860 | |
| 7861 | volatile_id = le64_to_cpu(req->VolatileFid); |
| 7862 | persistent_id = le64_to_cpu(req->PersistentFid); |
| 7863 | req_oplevel = req->OplockLevel; |
| 7864 | ksmbd_debug(OPLOCK, "v_id %llu, p_id %llu request oplock level %d\n", |
| 7865 | volatile_id, persistent_id, req_oplevel); |
| 7866 | |
| 7867 | fp = ksmbd_lookup_fd_slow(work, volatile_id, persistent_id); |
| 7868 | if (!fp) { |
| 7869 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 7870 | smb2_set_err_rsp(work); |
| 7871 | return; |
| 7872 | } |
| 7873 | |
| 7874 | opinfo = opinfo_get(fp); |
| 7875 | if (!opinfo) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 7876 | pr_err("unexpected null oplock_info\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7877 | rsp->hdr.Status = STATUS_INVALID_OPLOCK_PROTOCOL; |
| 7878 | smb2_set_err_rsp(work); |
| 7879 | ksmbd_fd_put(work, fp); |
| 7880 | return; |
| 7881 | } |
| 7882 | |
| 7883 | if (opinfo->level == SMB2_OPLOCK_LEVEL_NONE) { |
| 7884 | rsp->hdr.Status = STATUS_INVALID_OPLOCK_PROTOCOL; |
| 7885 | goto err_out; |
| 7886 | } |
| 7887 | |
| 7888 | if (opinfo->op_state == OPLOCK_STATE_NONE) { |
| 7889 | ksmbd_debug(SMB, "unexpected oplock state 0x%x\n", opinfo->op_state); |
| 7890 | rsp->hdr.Status = STATUS_UNSUCCESSFUL; |
| 7891 | goto err_out; |
| 7892 | } |
| 7893 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7894 | if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE || |
| 7895 | opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) && |
| 7896 | (req_oplevel != SMB2_OPLOCK_LEVEL_II && |
| 7897 | req_oplevel != SMB2_OPLOCK_LEVEL_NONE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7898 | err = STATUS_INVALID_OPLOCK_PROTOCOL; |
| 7899 | oplock_change_type = OPLOCK_WRITE_TO_NONE; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7900 | } else if (opinfo->level == SMB2_OPLOCK_LEVEL_II && |
| 7901 | req_oplevel != SMB2_OPLOCK_LEVEL_NONE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7902 | err = STATUS_INVALID_OPLOCK_PROTOCOL; |
| 7903 | oplock_change_type = OPLOCK_READ_TO_NONE; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7904 | } else if (req_oplevel == SMB2_OPLOCK_LEVEL_II || |
| 7905 | req_oplevel == SMB2_OPLOCK_LEVEL_NONE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7906 | err = STATUS_INVALID_DEVICE_STATE; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7907 | if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE || |
| 7908 | opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) && |
| 7909 | req_oplevel == SMB2_OPLOCK_LEVEL_II) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7910 | oplock_change_type = OPLOCK_WRITE_TO_READ; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7911 | } else if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE || |
| 7912 | opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) && |
| 7913 | req_oplevel == SMB2_OPLOCK_LEVEL_NONE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7914 | oplock_change_type = OPLOCK_WRITE_TO_NONE; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7915 | } else if (opinfo->level == SMB2_OPLOCK_LEVEL_II && |
| 7916 | req_oplevel == SMB2_OPLOCK_LEVEL_NONE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7917 | oplock_change_type = OPLOCK_READ_TO_NONE; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7918 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7919 | oplock_change_type = 0; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7920 | } |
| 7921 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7922 | oplock_change_type = 0; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7923 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7924 | |
| 7925 | switch (oplock_change_type) { |
| 7926 | case OPLOCK_WRITE_TO_READ: |
| 7927 | ret = opinfo_write_to_read(opinfo); |
| 7928 | rsp_oplevel = SMB2_OPLOCK_LEVEL_II; |
| 7929 | break; |
| 7930 | case OPLOCK_WRITE_TO_NONE: |
| 7931 | ret = opinfo_write_to_none(opinfo); |
| 7932 | rsp_oplevel = SMB2_OPLOCK_LEVEL_NONE; |
| 7933 | break; |
| 7934 | case OPLOCK_READ_TO_NONE: |
| 7935 | ret = opinfo_read_to_none(opinfo); |
| 7936 | rsp_oplevel = SMB2_OPLOCK_LEVEL_NONE; |
| 7937 | break; |
| 7938 | default: |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 7939 | pr_err("unknown oplock change 0x%x -> 0x%x\n", |
| 7940 | opinfo->level, rsp_oplevel); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7941 | } |
| 7942 | |
| 7943 | if (ret < 0) { |
| 7944 | rsp->hdr.Status = err; |
| 7945 | goto err_out; |
| 7946 | } |
| 7947 | |
| 7948 | opinfo_put(opinfo); |
| 7949 | ksmbd_fd_put(work, fp); |
| 7950 | opinfo->op_state = OPLOCK_STATE_NONE; |
| 7951 | wake_up_interruptible_all(&opinfo->oplock_q); |
| 7952 | |
| 7953 | rsp->StructureSize = cpu_to_le16(24); |
| 7954 | rsp->OplockLevel = rsp_oplevel; |
| 7955 | rsp->Reserved = 0; |
| 7956 | rsp->Reserved2 = 0; |
| 7957 | rsp->VolatileFid = cpu_to_le64(volatile_id); |
| 7958 | rsp->PersistentFid = cpu_to_le64(persistent_id); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 7959 | inc_rfc1001_len(work->response_buf, 24); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7960 | return; |
| 7961 | |
| 7962 | err_out: |
| 7963 | opinfo->op_state = OPLOCK_STATE_NONE; |
| 7964 | wake_up_interruptible_all(&opinfo->oplock_q); |
| 7965 | |
| 7966 | opinfo_put(opinfo); |
| 7967 | ksmbd_fd_put(work, fp); |
| 7968 | smb2_set_err_rsp(work); |
| 7969 | } |
| 7970 | |
| 7971 | static int check_lease_state(struct lease *lease, __le32 req_state) |
| 7972 | { |
| 7973 | if ((lease->new_state == |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7974 | (SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_HANDLE_CACHING_LE)) && |
| 7975 | !(req_state & SMB2_LEASE_WRITE_CACHING_LE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7976 | lease->new_state = req_state; |
| 7977 | return 0; |
| 7978 | } |
| 7979 | |
| 7980 | if (lease->new_state == req_state) |
| 7981 | return 0; |
| 7982 | |
| 7983 | return 1; |
| 7984 | } |
| 7985 | |
| 7986 | /** |
| 7987 | * smb21_lease_break_ack() - handler for smb2.1 lease break command |
| 7988 | * @work: smb work containing lease break command buffer |
| 7989 | * |
| 7990 | * Return: 0 |
| 7991 | */ |
| 7992 | static void smb21_lease_break_ack(struct ksmbd_work *work) |
| 7993 | { |
| 7994 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 7995 | struct smb2_lease_ack *req = smb2_get_msg(work->request_buf); |
| 7996 | struct smb2_lease_ack *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7997 | struct oplock_info *opinfo; |
| 7998 | __le32 err = 0; |
| 7999 | int ret = 0; |
| 8000 | unsigned int lease_change_type; |
| 8001 | __le32 lease_state; |
| 8002 | struct lease *lease; |
| 8003 | |
| 8004 | ksmbd_debug(OPLOCK, "smb21 lease break, lease state(0x%x)\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 8005 | le32_to_cpu(req->LeaseState)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8006 | opinfo = lookup_lease_in_table(conn, req->LeaseKey); |
| 8007 | if (!opinfo) { |
| 8008 | ksmbd_debug(OPLOCK, "file not opened\n"); |
| 8009 | smb2_set_err_rsp(work); |
| 8010 | rsp->hdr.Status = STATUS_UNSUCCESSFUL; |
| 8011 | return; |
| 8012 | } |
| 8013 | lease = opinfo->o_lease; |
| 8014 | |
| 8015 | if (opinfo->op_state == OPLOCK_STATE_NONE) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 8016 | pr_err("unexpected lease break state 0x%x\n", |
| 8017 | opinfo->op_state); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8018 | rsp->hdr.Status = STATUS_UNSUCCESSFUL; |
| 8019 | goto err_out; |
| 8020 | } |
| 8021 | |
| 8022 | if (check_lease_state(lease, req->LeaseState)) { |
| 8023 | rsp->hdr.Status = STATUS_REQUEST_NOT_ACCEPTED; |
| 8024 | ksmbd_debug(OPLOCK, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 8025 | "req lease state: 0x%x, expected state: 0x%x\n", |
| 8026 | req->LeaseState, lease->new_state); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8027 | goto err_out; |
| 8028 | } |
| 8029 | |
| 8030 | if (!atomic_read(&opinfo->breaking_cnt)) { |
| 8031 | rsp->hdr.Status = STATUS_UNSUCCESSFUL; |
| 8032 | goto err_out; |
| 8033 | } |
| 8034 | |
| 8035 | /* check for bad lease state */ |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 8036 | if (req->LeaseState & |
| 8037 | (~(SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_HANDLE_CACHING_LE))) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8038 | err = STATUS_INVALID_OPLOCK_PROTOCOL; |
| 8039 | if (lease->state & SMB2_LEASE_WRITE_CACHING_LE) |
| 8040 | lease_change_type = OPLOCK_WRITE_TO_NONE; |
| 8041 | else |
| 8042 | lease_change_type = OPLOCK_READ_TO_NONE; |
| 8043 | ksmbd_debug(OPLOCK, "handle bad lease state 0x%x -> 0x%x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 8044 | le32_to_cpu(lease->state), |
| 8045 | le32_to_cpu(req->LeaseState)); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 8046 | } else if (lease->state == SMB2_LEASE_READ_CACHING_LE && |
| 8047 | req->LeaseState != SMB2_LEASE_NONE_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8048 | err = STATUS_INVALID_OPLOCK_PROTOCOL; |
| 8049 | lease_change_type = OPLOCK_READ_TO_NONE; |
| 8050 | ksmbd_debug(OPLOCK, "handle bad lease state 0x%x -> 0x%x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 8051 | le32_to_cpu(lease->state), |
| 8052 | le32_to_cpu(req->LeaseState)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8053 | } else { |
| 8054 | /* valid lease state changes */ |
| 8055 | err = STATUS_INVALID_DEVICE_STATE; |
| 8056 | if (req->LeaseState == SMB2_LEASE_NONE_LE) { |
| 8057 | if (lease->state & SMB2_LEASE_WRITE_CACHING_LE) |
| 8058 | lease_change_type = OPLOCK_WRITE_TO_NONE; |
| 8059 | else |
| 8060 | lease_change_type = OPLOCK_READ_TO_NONE; |
| 8061 | } else if (req->LeaseState & SMB2_LEASE_READ_CACHING_LE) { |
| 8062 | if (lease->state & SMB2_LEASE_WRITE_CACHING_LE) |
| 8063 | lease_change_type = OPLOCK_WRITE_TO_READ; |
| 8064 | else |
| 8065 | lease_change_type = OPLOCK_READ_HANDLE_TO_READ; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 8066 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8067 | lease_change_type = 0; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 8068 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8069 | } |
| 8070 | |
| 8071 | switch (lease_change_type) { |
| 8072 | case OPLOCK_WRITE_TO_READ: |
| 8073 | ret = opinfo_write_to_read(opinfo); |
| 8074 | break; |
| 8075 | case OPLOCK_READ_HANDLE_TO_READ: |
| 8076 | ret = opinfo_read_handle_to_read(opinfo); |
| 8077 | break; |
| 8078 | case OPLOCK_WRITE_TO_NONE: |
| 8079 | ret = opinfo_write_to_none(opinfo); |
| 8080 | break; |
| 8081 | case OPLOCK_READ_TO_NONE: |
| 8082 | ret = opinfo_read_to_none(opinfo); |
| 8083 | break; |
| 8084 | default: |
| 8085 | ksmbd_debug(OPLOCK, "unknown lease change 0x%x -> 0x%x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 8086 | le32_to_cpu(lease->state), |
| 8087 | le32_to_cpu(req->LeaseState)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8088 | } |
| 8089 | |
| 8090 | lease_state = lease->state; |
| 8091 | opinfo->op_state = OPLOCK_STATE_NONE; |
| 8092 | wake_up_interruptible_all(&opinfo->oplock_q); |
| 8093 | atomic_dec(&opinfo->breaking_cnt); |
| 8094 | wake_up_interruptible_all(&opinfo->oplock_brk); |
| 8095 | opinfo_put(opinfo); |
| 8096 | |
| 8097 | if (ret < 0) { |
| 8098 | rsp->hdr.Status = err; |
| 8099 | goto err_out; |
| 8100 | } |
| 8101 | |
| 8102 | rsp->StructureSize = cpu_to_le16(36); |
| 8103 | rsp->Reserved = 0; |
| 8104 | rsp->Flags = 0; |
| 8105 | memcpy(rsp->LeaseKey, req->LeaseKey, 16); |
| 8106 | rsp->LeaseState = lease_state; |
| 8107 | rsp->LeaseDuration = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8108 | inc_rfc1001_len(work->response_buf, 36); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8109 | return; |
| 8110 | |
| 8111 | err_out: |
| 8112 | opinfo->op_state = OPLOCK_STATE_NONE; |
| 8113 | wake_up_interruptible_all(&opinfo->oplock_q); |
| 8114 | atomic_dec(&opinfo->breaking_cnt); |
| 8115 | wake_up_interruptible_all(&opinfo->oplock_brk); |
| 8116 | |
| 8117 | opinfo_put(opinfo); |
| 8118 | smb2_set_err_rsp(work); |
| 8119 | } |
| 8120 | |
| 8121 | /** |
| 8122 | * smb2_oplock_break() - dispatcher for smb2.0 and 2.1 oplock/lease break |
| 8123 | * @work: smb work containing oplock/lease break command buffer |
| 8124 | * |
| 8125 | * Return: 0 |
| 8126 | */ |
| 8127 | int smb2_oplock_break(struct ksmbd_work *work) |
| 8128 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8129 | struct smb2_oplock_break *req = smb2_get_msg(work->request_buf); |
| 8130 | struct smb2_oplock_break *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8131 | |
| 8132 | switch (le16_to_cpu(req->StructureSize)) { |
| 8133 | case OP_BREAK_STRUCT_SIZE_20: |
| 8134 | smb20_oplock_break_ack(work); |
| 8135 | break; |
| 8136 | case OP_BREAK_STRUCT_SIZE_21: |
| 8137 | smb21_lease_break_ack(work); |
| 8138 | break; |
| 8139 | default: |
| 8140 | ksmbd_debug(OPLOCK, "invalid break cmd %d\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 8141 | le16_to_cpu(req->StructureSize)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8142 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 8143 | smb2_set_err_rsp(work); |
| 8144 | } |
| 8145 | |
| 8146 | return 0; |
| 8147 | } |
| 8148 | |
| 8149 | /** |
| 8150 | * smb2_notify() - handler for smb2 notify request |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 8151 | * @work: smb work containing notify command buffer |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8152 | * |
| 8153 | * Return: 0 |
| 8154 | */ |
| 8155 | int smb2_notify(struct ksmbd_work *work) |
| 8156 | { |
Ronnie Sahlberg | 699230f | 2021-09-09 12:26:12 +0900 | [diff] [blame] | 8157 | struct smb2_change_notify_req *req; |
| 8158 | struct smb2_change_notify_rsp *rsp; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8159 | |
| 8160 | WORK_BUFFERS(work, req, rsp); |
| 8161 | |
| 8162 | if (work->next_smb2_rcv_hdr_off && req->hdr.NextCommand) { |
| 8163 | rsp->hdr.Status = STATUS_INTERNAL_ERROR; |
| 8164 | smb2_set_err_rsp(work); |
| 8165 | return 0; |
| 8166 | } |
| 8167 | |
| 8168 | smb2_set_err_rsp(work); |
| 8169 | rsp->hdr.Status = STATUS_NOT_IMPLEMENTED; |
| 8170 | return 0; |
| 8171 | } |
| 8172 | |
| 8173 | /** |
| 8174 | * smb2_is_sign_req() - handler for checking packet signing status |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 8175 | * @work: smb work containing notify command buffer |
| 8176 | * @command: SMB2 command id |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8177 | * |
| 8178 | * Return: true if packed is signed, false otherwise |
| 8179 | */ |
| 8180 | bool smb2_is_sign_req(struct ksmbd_work *work, unsigned int command) |
| 8181 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8182 | struct smb2_hdr *rcv_hdr2 = smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8183 | |
| 8184 | if ((rcv_hdr2->Flags & SMB2_FLAGS_SIGNED) && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 8185 | command != SMB2_NEGOTIATE_HE && |
| 8186 | command != SMB2_SESSION_SETUP_HE && |
| 8187 | command != SMB2_OPLOCK_BREAK_HE) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8188 | return true; |
| 8189 | |
kernel test robot | 5616015 | 2021-05-12 09:24:37 +0900 | [diff] [blame] | 8190 | return false; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8191 | } |
| 8192 | |
| 8193 | /** |
| 8194 | * smb2_check_sign_req() - handler for req packet sign processing |
| 8195 | * @work: smb work containing notify command buffer |
| 8196 | * |
| 8197 | * Return: 1 on success, 0 otherwise |
| 8198 | */ |
| 8199 | int smb2_check_sign_req(struct ksmbd_work *work) |
| 8200 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8201 | struct smb2_hdr *hdr; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8202 | char signature_req[SMB2_SIGNATURE_SIZE]; |
| 8203 | char signature[SMB2_HMACSHA256_SIZE]; |
| 8204 | struct kvec iov[1]; |
| 8205 | size_t len; |
| 8206 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8207 | hdr = smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8208 | if (work->next_smb2_rcv_hdr_off) |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 8209 | hdr = ksmbd_req_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8210 | |
| 8211 | if (!hdr->NextCommand && !work->next_smb2_rcv_hdr_off) |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8212 | len = get_rfc1002_len(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8213 | else if (hdr->NextCommand) |
| 8214 | len = le32_to_cpu(hdr->NextCommand); |
| 8215 | else |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8216 | len = get_rfc1002_len(work->request_buf) - |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8217 | work->next_smb2_rcv_hdr_off; |
| 8218 | |
| 8219 | memcpy(signature_req, hdr->Signature, SMB2_SIGNATURE_SIZE); |
| 8220 | memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE); |
| 8221 | |
| 8222 | iov[0].iov_base = (char *)&hdr->ProtocolId; |
| 8223 | iov[0].iov_len = len; |
| 8224 | |
| 8225 | 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] | 8226 | signature)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8227 | return 0; |
| 8228 | |
| 8229 | if (memcmp(signature, signature_req, SMB2_SIGNATURE_SIZE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 8230 | pr_err("bad smb2 signature\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8231 | return 0; |
| 8232 | } |
| 8233 | |
| 8234 | return 1; |
| 8235 | } |
| 8236 | |
| 8237 | /** |
| 8238 | * smb2_set_sign_rsp() - handler for rsp packet sign processing |
| 8239 | * @work: smb work containing notify command buffer |
| 8240 | * |
| 8241 | */ |
| 8242 | void smb2_set_sign_rsp(struct ksmbd_work *work) |
| 8243 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8244 | struct smb2_hdr *hdr; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8245 | struct smb2_hdr *req_hdr; |
| 8246 | char signature[SMB2_HMACSHA256_SIZE]; |
| 8247 | struct kvec iov[2]; |
| 8248 | size_t len; |
| 8249 | int n_vec = 1; |
| 8250 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8251 | hdr = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8252 | if (work->next_smb2_rsp_hdr_off) |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 8253 | hdr = ksmbd_resp_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8254 | |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 8255 | req_hdr = ksmbd_req_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8256 | |
| 8257 | if (!work->next_smb2_rsp_hdr_off) { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8258 | len = get_rfc1002_len(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8259 | if (req_hdr->NextCommand) |
| 8260 | len = ALIGN(len, 8); |
| 8261 | } else { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8262 | len = get_rfc1002_len(work->response_buf) - |
| 8263 | work->next_smb2_rsp_hdr_off; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8264 | len = ALIGN(len, 8); |
| 8265 | } |
| 8266 | |
| 8267 | if (req_hdr->NextCommand) |
| 8268 | hdr->NextCommand = cpu_to_le32(len); |
| 8269 | |
| 8270 | hdr->Flags |= SMB2_FLAGS_SIGNED; |
| 8271 | memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE); |
| 8272 | |
| 8273 | iov[0].iov_base = (char *)&hdr->ProtocolId; |
| 8274 | iov[0].iov_len = len; |
| 8275 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8276 | if (work->aux_payload_sz) { |
| 8277 | iov[0].iov_len -= work->aux_payload_sz; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8278 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8279 | iov[1].iov_base = work->aux_payload_buf; |
| 8280 | iov[1].iov_len = work->aux_payload_sz; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8281 | n_vec++; |
| 8282 | } |
| 8283 | |
| 8284 | 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] | 8285 | signature)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8286 | memcpy(hdr->Signature, signature, SMB2_SIGNATURE_SIZE); |
| 8287 | } |
| 8288 | |
| 8289 | /** |
| 8290 | * smb3_check_sign_req() - handler for req packet sign processing |
| 8291 | * @work: smb work containing notify command buffer |
| 8292 | * |
| 8293 | * Return: 1 on success, 0 otherwise |
| 8294 | */ |
| 8295 | int smb3_check_sign_req(struct ksmbd_work *work) |
| 8296 | { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 8297 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8298 | char *signing_key; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8299 | struct smb2_hdr *hdr; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8300 | struct channel *chann; |
| 8301 | char signature_req[SMB2_SIGNATURE_SIZE]; |
| 8302 | char signature[SMB2_CMACAES_SIZE]; |
| 8303 | struct kvec iov[1]; |
| 8304 | size_t len; |
| 8305 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8306 | hdr = smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8307 | if (work->next_smb2_rcv_hdr_off) |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 8308 | hdr = ksmbd_req_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8309 | |
| 8310 | if (!hdr->NextCommand && !work->next_smb2_rcv_hdr_off) |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8311 | len = get_rfc1002_len(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8312 | else if (hdr->NextCommand) |
| 8313 | len = le32_to_cpu(hdr->NextCommand); |
| 8314 | else |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8315 | len = get_rfc1002_len(work->request_buf) - |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8316 | work->next_smb2_rcv_hdr_off; |
| 8317 | |
| 8318 | if (le16_to_cpu(hdr->Command) == SMB2_SESSION_SETUP_HE) { |
| 8319 | signing_key = work->sess->smb3signingkey; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8320 | } else { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 8321 | chann = lookup_chann_list(work->sess, conn); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8322 | if (!chann) |
| 8323 | return 0; |
| 8324 | signing_key = chann->smb3signingkey; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8325 | } |
| 8326 | |
| 8327 | if (!signing_key) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 8328 | pr_err("SMB3 signing key is not generated\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8329 | return 0; |
| 8330 | } |
| 8331 | |
| 8332 | memcpy(signature_req, hdr->Signature, SMB2_SIGNATURE_SIZE); |
| 8333 | memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE); |
| 8334 | iov[0].iov_base = (char *)&hdr->ProtocolId; |
| 8335 | iov[0].iov_len = len; |
| 8336 | |
| 8337 | if (ksmbd_sign_smb3_pdu(conn, signing_key, iov, 1, signature)) |
| 8338 | return 0; |
| 8339 | |
| 8340 | if (memcmp(signature, signature_req, SMB2_SIGNATURE_SIZE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 8341 | pr_err("bad smb2 signature\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8342 | return 0; |
| 8343 | } |
| 8344 | |
| 8345 | return 1; |
| 8346 | } |
| 8347 | |
| 8348 | /** |
| 8349 | * smb3_set_sign_rsp() - handler for rsp packet sign processing |
| 8350 | * @work: smb work containing notify command buffer |
| 8351 | * |
| 8352 | */ |
| 8353 | void smb3_set_sign_rsp(struct ksmbd_work *work) |
| 8354 | { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 8355 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8356 | struct smb2_hdr *req_hdr, *hdr; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8357 | struct channel *chann; |
| 8358 | char signature[SMB2_CMACAES_SIZE]; |
| 8359 | struct kvec iov[2]; |
| 8360 | int n_vec = 1; |
| 8361 | size_t len; |
| 8362 | char *signing_key; |
| 8363 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8364 | hdr = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8365 | if (work->next_smb2_rsp_hdr_off) |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 8366 | hdr = ksmbd_resp_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8367 | |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 8368 | req_hdr = ksmbd_req_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8369 | |
| 8370 | if (!work->next_smb2_rsp_hdr_off) { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8371 | len = get_rfc1002_len(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8372 | if (req_hdr->NextCommand) |
| 8373 | len = ALIGN(len, 8); |
| 8374 | } else { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8375 | len = get_rfc1002_len(work->response_buf) - |
| 8376 | work->next_smb2_rsp_hdr_off; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8377 | len = ALIGN(len, 8); |
| 8378 | } |
| 8379 | |
Namjae Jeon | 08bdbc6 | 2021-07-27 09:30:29 +0900 | [diff] [blame] | 8380 | if (conn->binding == false && |
| 8381 | le16_to_cpu(hdr->Command) == SMB2_SESSION_SETUP_HE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8382 | signing_key = work->sess->smb3signingkey; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8383 | } else { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 8384 | chann = lookup_chann_list(work->sess, work->conn); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8385 | if (!chann) |
| 8386 | return; |
| 8387 | signing_key = chann->smb3signingkey; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8388 | } |
| 8389 | |
| 8390 | if (!signing_key) |
| 8391 | return; |
| 8392 | |
| 8393 | if (req_hdr->NextCommand) |
| 8394 | hdr->NextCommand = cpu_to_le32(len); |
| 8395 | |
| 8396 | hdr->Flags |= SMB2_FLAGS_SIGNED; |
| 8397 | memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE); |
| 8398 | iov[0].iov_base = (char *)&hdr->ProtocolId; |
| 8399 | iov[0].iov_len = len; |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8400 | if (work->aux_payload_sz) { |
| 8401 | iov[0].iov_len -= work->aux_payload_sz; |
| 8402 | iov[1].iov_base = work->aux_payload_buf; |
| 8403 | iov[1].iov_len = work->aux_payload_sz; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8404 | n_vec++; |
| 8405 | } |
| 8406 | |
| 8407 | if (!ksmbd_sign_smb3_pdu(conn, signing_key, iov, n_vec, signature)) |
| 8408 | memcpy(hdr->Signature, signature, SMB2_SIGNATURE_SIZE); |
| 8409 | } |
| 8410 | |
| 8411 | /** |
| 8412 | * smb3_preauth_hash_rsp() - handler for computing preauth hash on response |
| 8413 | * @work: smb work containing response buffer |
| 8414 | * |
| 8415 | */ |
| 8416 | void smb3_preauth_hash_rsp(struct ksmbd_work *work) |
| 8417 | { |
| 8418 | struct ksmbd_conn *conn = work->conn; |
| 8419 | struct ksmbd_session *sess = work->sess; |
| 8420 | struct smb2_hdr *req, *rsp; |
| 8421 | |
| 8422 | if (conn->dialect != SMB311_PROT_ID) |
| 8423 | return; |
| 8424 | |
| 8425 | WORK_BUFFERS(work, req, rsp); |
| 8426 | |
Namjae Jeon | 442ff9e | 2021-09-29 15:44:32 +0900 | [diff] [blame] | 8427 | if (le16_to_cpu(req->Command) == SMB2_NEGOTIATE_HE && |
| 8428 | conn->preauth_info) |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8429 | ksmbd_gen_preauth_integrity_hash(conn, work->response_buf, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 8430 | conn->preauth_info->Preauth_HashValue); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8431 | |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 8432 | if (le16_to_cpu(rsp->Command) == SMB2_SESSION_SETUP_HE && sess) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8433 | __u8 *hash_value; |
| 8434 | |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 8435 | if (conn->binding) { |
| 8436 | struct preauth_session *preauth_sess; |
| 8437 | |
| 8438 | preauth_sess = ksmbd_preauth_session_lookup(conn, sess->id); |
| 8439 | if (!preauth_sess) |
| 8440 | return; |
| 8441 | hash_value = preauth_sess->Preauth_HashValue; |
| 8442 | } else { |
| 8443 | hash_value = sess->Preauth_HashValue; |
| 8444 | if (!hash_value) |
| 8445 | return; |
| 8446 | } |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8447 | ksmbd_gen_preauth_integrity_hash(conn, work->response_buf, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 8448 | hash_value); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8449 | } |
| 8450 | } |
| 8451 | |
Namjae Jeon | 2dd9129 | 2021-11-03 08:25:54 +0900 | [diff] [blame] | 8452 | 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] | 8453 | { |
Namjae Jeon | 2dd9129 | 2021-11-03 08:25:54 +0900 | [diff] [blame] | 8454 | struct smb2_transform_hdr *tr_hdr = tr_buf + 4; |
| 8455 | struct smb2_hdr *hdr = smb2_get_msg(old_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8456 | unsigned int orig_len = get_rfc1002_len(old_buf); |
| 8457 | |
Namjae Jeon | 2dd9129 | 2021-11-03 08:25:54 +0900 | [diff] [blame] | 8458 | memset(tr_buf, 0, sizeof(struct smb2_transform_hdr) + 4); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8459 | tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM; |
| 8460 | tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len); |
Ronnie Sahlberg | 4355a8f | 2021-11-03 08:43:42 +0900 | [diff] [blame] | 8461 | tr_hdr->Flags = cpu_to_le16(TRANSFORM_FLAG_ENCRYPTED); |
Namjae Jeon | 5a0ca77 | 2021-05-06 11:43:37 +0900 | [diff] [blame] | 8462 | if (cipher_type == SMB2_ENCRYPTION_AES128_GCM || |
| 8463 | cipher_type == SMB2_ENCRYPTION_AES256_GCM) |
| 8464 | get_random_bytes(&tr_hdr->Nonce, SMB3_AES_GCM_NONCE); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8465 | else |
Namjae Jeon | 5a0ca77 | 2021-05-06 11:43:37 +0900 | [diff] [blame] | 8466 | get_random_bytes(&tr_hdr->Nonce, SMB3_AES_CCM_NONCE); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8467 | memcpy(&tr_hdr->SessionId, &hdr->SessionId, 8); |
Namjae Jeon | 2dd9129 | 2021-11-03 08:25:54 +0900 | [diff] [blame] | 8468 | inc_rfc1001_len(tr_buf, sizeof(struct smb2_transform_hdr)); |
| 8469 | inc_rfc1001_len(tr_buf, orig_len); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8470 | } |
| 8471 | |
| 8472 | int smb3_encrypt_resp(struct ksmbd_work *work) |
| 8473 | { |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8474 | char *buf = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8475 | struct kvec iov[3]; |
| 8476 | int rc = -ENOMEM; |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8477 | 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] | 8478 | |
| 8479 | if (ARRAY_SIZE(iov) < rq_nvec) |
| 8480 | return -ENOMEM; |
| 8481 | |
Namjae Jeon | 2dd9129 | 2021-11-03 08:25:54 +0900 | [diff] [blame] | 8482 | work->tr_buf = kzalloc(sizeof(struct smb2_transform_hdr) + 4, GFP_KERNEL); |
| 8483 | if (!work->tr_buf) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8484 | return rc; |
| 8485 | |
| 8486 | /* fill transform header */ |
Namjae Jeon | 2dd9129 | 2021-11-03 08:25:54 +0900 | [diff] [blame] | 8487 | fill_transform_hdr(work->tr_buf, buf, work->conn->cipher_type); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8488 | |
Namjae Jeon | 2dd9129 | 2021-11-03 08:25:54 +0900 | [diff] [blame] | 8489 | iov[0].iov_base = work->tr_buf; |
| 8490 | iov[0].iov_len = sizeof(struct smb2_transform_hdr) + 4; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8491 | buf_size += iov[0].iov_len - 4; |
| 8492 | |
| 8493 | iov[1].iov_base = buf + 4; |
| 8494 | iov[1].iov_len = get_rfc1002_len(buf); |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8495 | if (work->aux_payload_sz) { |
| 8496 | iov[1].iov_len = work->resp_hdr_sz - 4; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8497 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8498 | iov[2].iov_base = work->aux_payload_buf; |
| 8499 | iov[2].iov_len = work->aux_payload_sz; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8500 | buf_size += iov[2].iov_len; |
| 8501 | } |
| 8502 | buf_size += iov[1].iov_len; |
| 8503 | work->resp_hdr_sz = iov[1].iov_len; |
| 8504 | |
| 8505 | rc = ksmbd_crypt_message(work->conn, iov, rq_nvec, 1); |
| 8506 | if (rc) |
| 8507 | return rc; |
| 8508 | |
| 8509 | memmove(buf, iov[1].iov_base, iov[1].iov_len); |
Namjae Jeon | 2dd9129 | 2021-11-03 08:25:54 +0900 | [diff] [blame] | 8510 | *(__be32 *)work->tr_buf = cpu_to_be32(buf_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8511 | |
| 8512 | return rc; |
| 8513 | } |
| 8514 | |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 8515 | bool smb3_is_transform_hdr(void *buf) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8516 | { |
Namjae Jeon | 2dd9129 | 2021-11-03 08:25:54 +0900 | [diff] [blame] | 8517 | struct smb2_transform_hdr *trhdr = smb2_get_msg(buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8518 | |
| 8519 | return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM; |
| 8520 | } |
| 8521 | |
| 8522 | int smb3_decrypt_req(struct ksmbd_work *work) |
| 8523 | { |
| 8524 | struct ksmbd_conn *conn = work->conn; |
| 8525 | struct ksmbd_session *sess; |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8526 | char *buf = work->request_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8527 | unsigned int pdu_length = get_rfc1002_len(buf); |
| 8528 | struct kvec iov[2]; |
Namjae Jeon | 2dd9129 | 2021-11-03 08:25:54 +0900 | [diff] [blame] | 8529 | int buf_data_size = pdu_length - sizeof(struct smb2_transform_hdr); |
| 8530 | struct smb2_transform_hdr *tr_hdr = smb2_get_msg(buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8531 | int rc = 0; |
| 8532 | |
Namjae Jeon | c7705ee | 2021-10-04 20:44:52 +0900 | [diff] [blame] | 8533 | if (buf_data_size < sizeof(struct smb2_hdr)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 8534 | pr_err("Transform message is too small (%u)\n", |
| 8535 | pdu_length); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8536 | return -ECONNABORTED; |
| 8537 | } |
| 8538 | |
Namjae Jeon | c7705ee | 2021-10-04 20:44:52 +0900 | [diff] [blame] | 8539 | if (buf_data_size < le32_to_cpu(tr_hdr->OriginalMessageSize)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 8540 | pr_err("Transform message is broken\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8541 | return -ECONNABORTED; |
| 8542 | } |
| 8543 | |
Namjae Jeon | 4227f81 | 2021-09-29 19:52:51 +0900 | [diff] [blame] | 8544 | sess = ksmbd_session_lookup_all(conn, le64_to_cpu(tr_hdr->SessionId)); |
| 8545 | if (!sess) { |
| 8546 | pr_err("invalid session id(%llx) in transform header\n", |
| 8547 | le64_to_cpu(tr_hdr->SessionId)); |
| 8548 | return -ECONNABORTED; |
| 8549 | } |
| 8550 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8551 | iov[0].iov_base = buf; |
Namjae Jeon | 2dd9129 | 2021-11-03 08:25:54 +0900 | [diff] [blame] | 8552 | iov[0].iov_len = sizeof(struct smb2_transform_hdr) + 4; |
| 8553 | iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr) + 4; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8554 | iov[1].iov_len = buf_data_size; |
| 8555 | rc = ksmbd_crypt_message(conn, iov, 2, 0); |
| 8556 | if (rc) |
| 8557 | return rc; |
| 8558 | |
| 8559 | memmove(buf + 4, iov[1].iov_base, buf_data_size); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8560 | *(__be32 *)buf = cpu_to_be32(buf_data_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8561 | |
| 8562 | return rc; |
| 8563 | } |
| 8564 | |
| 8565 | bool smb3_11_final_sess_setup_resp(struct ksmbd_work *work) |
| 8566 | { |
| 8567 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 8568 | struct smb2_hdr *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8569 | |
| 8570 | if (conn->dialect < SMB30_PROT_ID) |
| 8571 | return false; |
| 8572 | |
| 8573 | if (work->next_smb2_rcv_hdr_off) |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 8574 | rsp = ksmbd_resp_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8575 | |
| 8576 | if (le16_to_cpu(rsp->Command) == SMB2_SESSION_SETUP_HE && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 8577 | rsp->Status == STATUS_SUCCESS) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8578 | return true; |
| 8579 | return false; |
| 8580 | } |