Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Copyright (C) 2016 Namjae Jeon <linkinjeon@kernel.org> |
| 4 | * Copyright (C) 2018 Samsung Electronics Co., Ltd. |
| 5 | */ |
| 6 | |
| 7 | #include <linux/inetdevice.h> |
| 8 | #include <net/addrconf.h> |
| 9 | #include <linux/syscalls.h> |
| 10 | #include <linux/namei.h> |
| 11 | #include <linux/statfs.h> |
| 12 | #include <linux/ethtool.h> |
Namjae Jeon | e8c0619 | 2021-06-22 11:06:11 +0900 | [diff] [blame] | 13 | #include <linux/falloc.h> |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 14 | |
| 15 | #include "glob.h" |
| 16 | #include "smb2pdu.h" |
| 17 | #include "smbfsctl.h" |
| 18 | #include "oplock.h" |
| 19 | #include "smbacl.h" |
| 20 | |
| 21 | #include "auth.h" |
| 22 | #include "asn1.h" |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 23 | #include "connection.h" |
| 24 | #include "transport_ipc.h" |
Hyunchul Lee | 03d8d4f | 2021-07-13 16:09:34 +0900 | [diff] [blame] | 25 | #include "transport_rdma.h" |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 26 | #include "vfs.h" |
| 27 | #include "vfs_cache.h" |
| 28 | #include "misc.h" |
| 29 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 30 | #include "server.h" |
| 31 | #include "smb_common.h" |
| 32 | #include "smbstatus.h" |
| 33 | #include "ksmbd_work.h" |
| 34 | #include "mgmt/user_config.h" |
| 35 | #include "mgmt/share_config.h" |
| 36 | #include "mgmt/tree_connect.h" |
| 37 | #include "mgmt/user_session.h" |
| 38 | #include "mgmt/ksmbd_ida.h" |
| 39 | #include "ndr.h" |
| 40 | |
| 41 | static void __wbuf(struct ksmbd_work *work, void **req, void **rsp) |
| 42 | { |
| 43 | if (work->next_smb2_rcv_hdr_off) { |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 44 | *req = ksmbd_req_buf_next(work); |
| 45 | *rsp = ksmbd_resp_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 46 | } else { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 47 | *req = smb2_get_msg(work->request_buf); |
| 48 | *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
| 52 | #define WORK_BUFFERS(w, rq, rs) __wbuf((w), (void **)&(rq), (void **)&(rs)) |
| 53 | |
| 54 | /** |
| 55 | * check_session_id() - check for valid session id in smb header |
| 56 | * @conn: connection instance |
| 57 | * @id: session id from smb header |
| 58 | * |
| 59 | * Return: 1 if valid session id, otherwise 0 |
| 60 | */ |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 61 | static inline bool check_session_id(struct ksmbd_conn *conn, u64 id) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 62 | { |
| 63 | struct ksmbd_session *sess; |
| 64 | |
| 65 | if (id == 0 || id == -1) |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 66 | return false; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 67 | |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 68 | sess = ksmbd_session_lookup_all(conn, id); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 69 | if (sess) |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 70 | return true; |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 71 | pr_err("Invalid user session id: %llu\n", id); |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 72 | return false; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 73 | } |
| 74 | |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 75 | struct channel *lookup_chann_list(struct ksmbd_session *sess, struct ksmbd_conn *conn) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 76 | { |
| 77 | struct channel *chann; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 78 | |
Namjae Jeon | 6f3d5ee | 2021-06-18 10:28:52 +0900 | [diff] [blame] | 79 | list_for_each_entry(chann, &sess->ksmbd_chann_list, chann_list) { |
Namjae Jeon | 560ac05 | 2021-06-22 16:16:45 +0900 | [diff] [blame] | 80 | if (chann->conn == conn) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 81 | return chann; |
| 82 | } |
| 83 | |
| 84 | return NULL; |
| 85 | } |
| 86 | |
| 87 | /** |
Namjae Jeon | 5ec3df8e | 2021-08-12 10:17:39 +0900 | [diff] [blame] | 88 | * smb2_get_ksmbd_tcon() - get tree connection information using a tree id. |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 89 | * @work: smb work |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 90 | * |
Namjae Jeon | 5ec3df8e | 2021-08-12 10:17:39 +0900 | [diff] [blame] | 91 | * Return: 0 if there is a tree connection matched or these are |
| 92 | * skipable commands, otherwise error |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 93 | */ |
| 94 | int smb2_get_ksmbd_tcon(struct ksmbd_work *work) |
| 95 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 96 | struct smb2_hdr *req_hdr = smb2_get_msg(work->request_buf); |
Ralph Boehme | 341b160 | 2021-10-05 07:03:40 +0200 | [diff] [blame] | 97 | unsigned int cmd = le16_to_cpu(req_hdr->Command); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 98 | int tree_id; |
| 99 | |
| 100 | work->tcon = NULL; |
Ralph Boehme | 341b160 | 2021-10-05 07:03:40 +0200 | [diff] [blame] | 101 | if (cmd == SMB2_TREE_CONNECT_HE || |
| 102 | cmd == SMB2_CANCEL_HE || |
| 103 | cmd == SMB2_LOGOFF_HE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 104 | ksmbd_debug(SMB, "skip to check tree connect request\n"); |
| 105 | return 0; |
| 106 | } |
| 107 | |
Namjae Jeon | 02b68b2 | 2021-04-01 17:45:33 +0900 | [diff] [blame] | 108 | if (xa_empty(&work->sess->tree_conns)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 109 | ksmbd_debug(SMB, "NO tree connected\n"); |
Namjae Jeon | c6ce2b5 | 2021-08-12 10:18:18 +0900 | [diff] [blame] | 110 | return -ENOENT; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | tree_id = le32_to_cpu(req_hdr->Id.SyncId.TreeId); |
| 114 | work->tcon = ksmbd_tree_conn_lookup(work->sess, tree_id); |
| 115 | if (!work->tcon) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 116 | pr_err("Invalid tid %d\n", tree_id); |
Namjae Jeon | c6ce2b5 | 2021-08-12 10:18:18 +0900 | [diff] [blame] | 117 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | return 1; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * smb2_set_err_rsp() - set error response code on smb response |
| 125 | * @work: smb work containing response buffer |
| 126 | */ |
| 127 | void smb2_set_err_rsp(struct ksmbd_work *work) |
| 128 | { |
| 129 | struct smb2_err_rsp *err_rsp; |
| 130 | |
| 131 | if (work->next_smb2_rcv_hdr_off) |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 132 | err_rsp = ksmbd_resp_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 133 | else |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 134 | err_rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 135 | |
| 136 | if (err_rsp->hdr.Status != STATUS_STOPPED_ON_SYMLINK) { |
| 137 | err_rsp->StructureSize = SMB2_ERROR_STRUCTURE_SIZE2_LE; |
| 138 | err_rsp->ErrorContextCount = 0; |
| 139 | err_rsp->Reserved = 0; |
| 140 | err_rsp->ByteCount = 0; |
| 141 | err_rsp->ErrorData[0] = 0; |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 142 | inc_rfc1001_len(work->response_buf, SMB2_ERROR_STRUCTURE_SIZE2); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * is_smb2_neg_cmd() - is it smb2 negotiation command |
| 148 | * @work: smb work containing smb header |
| 149 | * |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 150 | * Return: true if smb2 negotiation command, otherwise false |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 151 | */ |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 152 | bool is_smb2_neg_cmd(struct ksmbd_work *work) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 153 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 154 | struct smb2_hdr *hdr = smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 155 | |
| 156 | /* is it SMB2 header ? */ |
| 157 | if (hdr->ProtocolId != SMB2_PROTO_NUMBER) |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 158 | return false; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 159 | |
| 160 | /* make sure it is request not response message */ |
| 161 | if (hdr->Flags & SMB2_FLAGS_SERVER_TO_REDIR) |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 162 | return false; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 163 | |
| 164 | if (hdr->Command != SMB2_NEGOTIATE) |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 165 | return false; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 166 | |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 167 | return true; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /** |
| 171 | * is_smb2_rsp() - is it smb2 response |
| 172 | * @work: smb work containing smb response buffer |
| 173 | * |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 174 | * Return: true if smb2 response, otherwise false |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 175 | */ |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 176 | bool is_smb2_rsp(struct ksmbd_work *work) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 177 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 178 | struct smb2_hdr *hdr = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 179 | |
| 180 | /* is it SMB2 header ? */ |
| 181 | if (hdr->ProtocolId != SMB2_PROTO_NUMBER) |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 182 | return false; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 183 | |
| 184 | /* make sure it is response not request message */ |
| 185 | if (!(hdr->Flags & SMB2_FLAGS_SERVER_TO_REDIR)) |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 186 | return false; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 187 | |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 188 | return true; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /** |
| 192 | * get_smb2_cmd_val() - get smb command code from smb header |
| 193 | * @work: smb work containing smb request buffer |
| 194 | * |
| 195 | * Return: smb2 request command value |
| 196 | */ |
Namjae Jeon | fc2d1b5 | 2021-05-26 18:01:08 +0900 | [diff] [blame] | 197 | u16 get_smb2_cmd_val(struct ksmbd_work *work) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 198 | { |
| 199 | struct smb2_hdr *rcv_hdr; |
| 200 | |
| 201 | if (work->next_smb2_rcv_hdr_off) |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 202 | rcv_hdr = ksmbd_req_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 203 | else |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 204 | rcv_hdr = smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 205 | return le16_to_cpu(rcv_hdr->Command); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * set_smb2_rsp_status() - set error response code on smb2 header |
| 210 | * @work: smb work containing response buffer |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 211 | * @err: error response code |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 212 | */ |
| 213 | void set_smb2_rsp_status(struct ksmbd_work *work, __le32 err) |
| 214 | { |
| 215 | struct smb2_hdr *rsp_hdr; |
| 216 | |
| 217 | if (work->next_smb2_rcv_hdr_off) |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 218 | rsp_hdr = ksmbd_resp_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 219 | else |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 220 | rsp_hdr = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 221 | rsp_hdr->Status = err; |
| 222 | smb2_set_err_rsp(work); |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * init_smb2_neg_rsp() - initialize smb2 response for negotiate command |
| 227 | * @work: smb work containing smb request buffer |
| 228 | * |
| 229 | * smb2 negotiate response is sent in reply of smb1 negotiate command for |
| 230 | * dialect auto-negotiation. |
| 231 | */ |
| 232 | int init_smb2_neg_rsp(struct ksmbd_work *work) |
| 233 | { |
| 234 | struct smb2_hdr *rsp_hdr; |
| 235 | struct smb2_negotiate_rsp *rsp; |
| 236 | struct ksmbd_conn *conn = work->conn; |
| 237 | |
| 238 | if (conn->need_neg == false) |
| 239 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 240 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 241 | *(__be32 *)work->response_buf = |
| 242 | cpu_to_be32(conn->vals->header_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 243 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 244 | rsp_hdr = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 245 | memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 246 | rsp_hdr->ProtocolId = SMB2_PROTO_NUMBER; |
| 247 | rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE; |
| 248 | rsp_hdr->CreditRequest = cpu_to_le16(2); |
| 249 | rsp_hdr->Command = SMB2_NEGOTIATE; |
| 250 | rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR); |
| 251 | rsp_hdr->NextCommand = 0; |
| 252 | rsp_hdr->MessageId = 0; |
| 253 | rsp_hdr->Id.SyncId.ProcessId = 0; |
| 254 | rsp_hdr->Id.SyncId.TreeId = 0; |
| 255 | rsp_hdr->SessionId = 0; |
| 256 | memset(rsp_hdr->Signature, 0, 16); |
| 257 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 258 | rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 259 | |
| 260 | WARN_ON(ksmbd_conn_good(work)); |
| 261 | |
| 262 | rsp->StructureSize = cpu_to_le16(65); |
| 263 | ksmbd_debug(SMB, "conn->dialect 0x%x\n", conn->dialect); |
| 264 | rsp->DialectRevision = cpu_to_le16(conn->dialect); |
| 265 | /* Not setting conn guid rsp->ServerGUID, as it |
| 266 | * not used by client for identifying connection |
| 267 | */ |
| 268 | rsp->Capabilities = cpu_to_le32(conn->vals->capabilities); |
| 269 | /* Default Max Message Size till SMB2.0, 64K*/ |
| 270 | rsp->MaxTransactSize = cpu_to_le32(conn->vals->max_trans_size); |
| 271 | rsp->MaxReadSize = cpu_to_le32(conn->vals->max_read_size); |
| 272 | rsp->MaxWriteSize = cpu_to_le32(conn->vals->max_write_size); |
| 273 | |
| 274 | rsp->SystemTime = cpu_to_le64(ksmbd_systime()); |
| 275 | rsp->ServerStartTime = 0; |
| 276 | |
| 277 | rsp->SecurityBufferOffset = cpu_to_le16(128); |
| 278 | rsp->SecurityBufferLength = cpu_to_le16(AUTH_GSS_LENGTH); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 279 | ksmbd_copy_gss_neg_header((char *)(&rsp->hdr) + |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 280 | le16_to_cpu(rsp->SecurityBufferOffset)); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 281 | inc_rfc1001_len(work->response_buf, |
| 282 | sizeof(struct smb2_negotiate_rsp) - |
| 283 | sizeof(struct smb2_hdr) - sizeof(rsp->Buffer) + |
| 284 | AUTH_GSS_LENGTH); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 285 | rsp->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED_LE; |
| 286 | if (server_conf.signing == KSMBD_CONFIG_OPT_MANDATORY) |
| 287 | rsp->SecurityMode |= SMB2_NEGOTIATE_SIGNING_REQUIRED_LE; |
| 288 | conn->use_spnego = true; |
| 289 | |
| 290 | ksmbd_conn_set_need_negotiate(work); |
| 291 | return 0; |
| 292 | } |
| 293 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 294 | /** |
| 295 | * smb2_set_rsp_credits() - set number of credits in response buffer |
| 296 | * @work: smb work containing smb response buffer |
| 297 | */ |
| 298 | int smb2_set_rsp_credits(struct ksmbd_work *work) |
| 299 | { |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 300 | struct smb2_hdr *req_hdr = ksmbd_req_buf_next(work); |
| 301 | struct smb2_hdr *hdr = ksmbd_resp_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 302 | struct ksmbd_conn *conn = work->conn; |
Hyunchul Lee | bf8acc9 | 2021-10-07 16:26:58 +0900 | [diff] [blame] | 303 | unsigned short credits_requested; |
| 304 | unsigned short credit_charge, credits_granted = 0; |
| 305 | unsigned short aux_max, aux_credits; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 306 | |
Hyunchul Lee | bf8acc9 | 2021-10-07 16:26:58 +0900 | [diff] [blame] | 307 | if (work->send_no_response) |
| 308 | return 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 309 | |
Hyunchul Lee | bf8acc9 | 2021-10-07 16:26:58 +0900 | [diff] [blame] | 310 | hdr->CreditCharge = req_hdr->CreditCharge; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 311 | |
Hyunchul Lee | bf8acc9 | 2021-10-07 16:26:58 +0900 | [diff] [blame] | 312 | if (conn->total_credits > conn->max_credits) { |
| 313 | hdr->CreditRequest = 0; |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 314 | pr_err("Total credits overflow: %d\n", conn->total_credits); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 315 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 316 | } |
| 317 | |
Hyunchul Lee | bf8acc9 | 2021-10-07 16:26:58 +0900 | [diff] [blame] | 318 | credit_charge = max_t(unsigned short, |
| 319 | le16_to_cpu(req_hdr->CreditCharge), 1); |
| 320 | credits_requested = max_t(unsigned short, |
| 321 | le16_to_cpu(req_hdr->CreditRequest), 1); |
| 322 | |
| 323 | /* according to smb2.credits smbtorture, Windows server |
| 324 | * 2016 or later grant up to 8192 credits at once. |
| 325 | * |
| 326 | * TODO: Need to adjuct CreditRequest value according to |
| 327 | * current cpu load |
| 328 | */ |
| 329 | aux_credits = credits_requested - 1; |
| 330 | if (hdr->Command == SMB2_NEGOTIATE) |
| 331 | aux_max = 0; |
| 332 | else |
| 333 | aux_max = conn->max_credits - credit_charge; |
| 334 | aux_credits = min_t(unsigned short, aux_credits, aux_max); |
| 335 | credits_granted = credit_charge + aux_credits; |
| 336 | |
| 337 | if (conn->max_credits - conn->total_credits < credits_granted) |
| 338 | credits_granted = conn->max_credits - |
| 339 | conn->total_credits; |
| 340 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 341 | conn->total_credits += credits_granted; |
| 342 | work->credits_granted += credits_granted; |
| 343 | |
| 344 | if (!req_hdr->NextCommand) { |
| 345 | /* Update CreditRequest in last request */ |
| 346 | hdr->CreditRequest = cpu_to_le16(work->credits_granted); |
| 347 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 348 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 349 | "credits: requested[%d] granted[%d] total_granted[%d]\n", |
| 350 | credits_requested, credits_granted, |
| 351 | conn->total_credits); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 352 | return 0; |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * init_chained_smb2_rsp() - initialize smb2 chained response |
| 357 | * @work: smb work containing smb response buffer |
| 358 | */ |
| 359 | static void init_chained_smb2_rsp(struct ksmbd_work *work) |
| 360 | { |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 361 | struct smb2_hdr *req = ksmbd_req_buf_next(work); |
| 362 | struct smb2_hdr *rsp = ksmbd_resp_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 363 | struct smb2_hdr *rsp_hdr; |
| 364 | struct smb2_hdr *rcv_hdr; |
| 365 | int next_hdr_offset = 0; |
| 366 | int len, new_len; |
| 367 | |
| 368 | /* Len of this response = updated RFC len - offset of previous cmd |
| 369 | * in the compound rsp |
| 370 | */ |
| 371 | |
| 372 | /* Storing the current local FID which may be needed by subsequent |
| 373 | * command in the compound request |
| 374 | */ |
| 375 | if (req->Command == SMB2_CREATE && rsp->Status == STATUS_SUCCESS) { |
| 376 | work->compound_fid = |
| 377 | le64_to_cpu(((struct smb2_create_rsp *)rsp)-> |
| 378 | VolatileFileId); |
| 379 | work->compound_pfid = |
| 380 | le64_to_cpu(((struct smb2_create_rsp *)rsp)-> |
| 381 | PersistentFileId); |
| 382 | work->compound_sid = le64_to_cpu(rsp->SessionId); |
| 383 | } |
| 384 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 385 | 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] | 386 | next_hdr_offset = le32_to_cpu(req->NextCommand); |
| 387 | |
| 388 | new_len = ALIGN(len, 8); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 389 | inc_rfc1001_len(work->response_buf, |
| 390 | sizeof(struct smb2_hdr) + new_len - len); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 391 | rsp->NextCommand = cpu_to_le32(new_len); |
| 392 | |
| 393 | work->next_smb2_rcv_hdr_off += next_hdr_offset; |
| 394 | work->next_smb2_rsp_hdr_off += new_len; |
| 395 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 396 | "Compound req new_len = %d rcv off = %d rsp off = %d\n", |
| 397 | new_len, work->next_smb2_rcv_hdr_off, |
| 398 | work->next_smb2_rsp_hdr_off); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 399 | |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 400 | rsp_hdr = ksmbd_resp_buf_next(work); |
| 401 | rcv_hdr = ksmbd_req_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 402 | |
| 403 | if (!(rcv_hdr->Flags & SMB2_FLAGS_RELATED_OPERATIONS)) { |
| 404 | ksmbd_debug(SMB, "related flag should be set\n"); |
| 405 | work->compound_fid = KSMBD_NO_FID; |
| 406 | work->compound_pfid = KSMBD_NO_FID; |
| 407 | } |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 408 | memset((char *)rsp_hdr, 0, sizeof(struct smb2_hdr) + 2); |
Namjae Jeon | 18a015b | 2021-09-22 21:00:57 +0900 | [diff] [blame] | 409 | rsp_hdr->ProtocolId = SMB2_PROTO_NUMBER; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 410 | rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE; |
| 411 | rsp_hdr->Command = rcv_hdr->Command; |
| 412 | |
| 413 | /* |
| 414 | * Message is response. We don't grant oplock yet. |
| 415 | */ |
| 416 | rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR | |
| 417 | SMB2_FLAGS_RELATED_OPERATIONS); |
| 418 | rsp_hdr->NextCommand = 0; |
| 419 | rsp_hdr->MessageId = rcv_hdr->MessageId; |
| 420 | rsp_hdr->Id.SyncId.ProcessId = rcv_hdr->Id.SyncId.ProcessId; |
| 421 | rsp_hdr->Id.SyncId.TreeId = rcv_hdr->Id.SyncId.TreeId; |
| 422 | rsp_hdr->SessionId = rcv_hdr->SessionId; |
| 423 | memcpy(rsp_hdr->Signature, rcv_hdr->Signature, 16); |
| 424 | } |
| 425 | |
| 426 | /** |
| 427 | * is_chained_smb2_message() - check for chained command |
| 428 | * @work: smb work containing smb request buffer |
| 429 | * |
| 430 | * Return: true if chained request, otherwise false |
| 431 | */ |
| 432 | bool is_chained_smb2_message(struct ksmbd_work *work) |
| 433 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 434 | struct smb2_hdr *hdr = smb2_get_msg(work->request_buf); |
Namjae Jeon | d72a9c1 | 2021-09-24 09:24:08 +0900 | [diff] [blame] | 435 | unsigned int len, next_cmd; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 436 | |
| 437 | if (hdr->ProtocolId != SMB2_PROTO_NUMBER) |
| 438 | return false; |
| 439 | |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 440 | hdr = ksmbd_req_buf_next(work); |
Namjae Jeon | d72a9c1 | 2021-09-24 09:24:08 +0900 | [diff] [blame] | 441 | next_cmd = le32_to_cpu(hdr->NextCommand); |
| 442 | if (next_cmd > 0) { |
| 443 | if ((u64)work->next_smb2_rcv_hdr_off + next_cmd + |
| 444 | __SMB2_HEADER_STRUCTURE_SIZE > |
| 445 | get_rfc1002_len(work->request_buf)) { |
| 446 | pr_err("next command(%u) offset exceeds smb msg size\n", |
| 447 | next_cmd); |
| 448 | return false; |
| 449 | } |
| 450 | |
Namjae Jeon | dbad630 | 2021-10-11 19:15:25 +0900 | [diff] [blame] | 451 | if ((u64)get_rfc1002_len(work->response_buf) + MAX_CIFS_SMALL_BUFFER_SIZE > |
| 452 | work->response_sz) { |
| 453 | pr_err("next response offset exceeds response buffer size\n"); |
| 454 | return false; |
| 455 | } |
| 456 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 457 | ksmbd_debug(SMB, "got SMB2 chained command\n"); |
| 458 | init_chained_smb2_rsp(work); |
| 459 | return true; |
| 460 | } else if (work->next_smb2_rcv_hdr_off) { |
| 461 | /* |
| 462 | * This is last request in chained command, |
| 463 | * align response to 8 byte |
| 464 | */ |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 465 | len = ALIGN(get_rfc1002_len(work->response_buf), 8); |
| 466 | len = len - get_rfc1002_len(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 467 | if (len) { |
| 468 | ksmbd_debug(SMB, "padding len %u\n", len); |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 469 | inc_rfc1001_len(work->response_buf, len); |
| 470 | if (work->aux_payload_sz) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 471 | work->aux_payload_sz += len; |
| 472 | } |
| 473 | } |
| 474 | return false; |
| 475 | } |
| 476 | |
| 477 | /** |
| 478 | * init_smb2_rsp_hdr() - initialize smb2 response |
| 479 | * @work: smb work containing smb request buffer |
| 480 | * |
| 481 | * Return: 0 |
| 482 | */ |
| 483 | int init_smb2_rsp_hdr(struct ksmbd_work *work) |
| 484 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 485 | struct smb2_hdr *rsp_hdr = smb2_get_msg(work->response_buf); |
| 486 | struct smb2_hdr *rcv_hdr = smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 487 | struct ksmbd_conn *conn = work->conn; |
| 488 | |
| 489 | memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 490 | *(__be32 *)work->response_buf = |
| 491 | cpu_to_be32(conn->vals->header_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 492 | rsp_hdr->ProtocolId = rcv_hdr->ProtocolId; |
| 493 | rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE; |
| 494 | rsp_hdr->Command = rcv_hdr->Command; |
| 495 | |
| 496 | /* |
| 497 | * Message is response. We don't grant oplock yet. |
| 498 | */ |
| 499 | rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR); |
| 500 | rsp_hdr->NextCommand = 0; |
| 501 | rsp_hdr->MessageId = rcv_hdr->MessageId; |
| 502 | rsp_hdr->Id.SyncId.ProcessId = rcv_hdr->Id.SyncId.ProcessId; |
| 503 | rsp_hdr->Id.SyncId.TreeId = rcv_hdr->Id.SyncId.TreeId; |
| 504 | rsp_hdr->SessionId = rcv_hdr->SessionId; |
| 505 | memcpy(rsp_hdr->Signature, rcv_hdr->Signature, 16); |
| 506 | |
| 507 | work->syncronous = true; |
| 508 | if (work->async_id) { |
Namjae Jeon | d40012a | 2021-04-13 13:06:30 +0900 | [diff] [blame] | 509 | ksmbd_release_id(&conn->async_ida, work->async_id); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 510 | work->async_id = 0; |
| 511 | } |
| 512 | |
| 513 | return 0; |
| 514 | } |
| 515 | |
| 516 | /** |
| 517 | * smb2_allocate_rsp_buf() - allocate smb2 response buffer |
| 518 | * @work: smb work containing smb request buffer |
| 519 | * |
| 520 | * Return: 0 on success, otherwise -ENOMEM |
| 521 | */ |
| 522 | int smb2_allocate_rsp_buf(struct ksmbd_work *work) |
| 523 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 524 | struct smb2_hdr *hdr = smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 525 | size_t small_sz = MAX_CIFS_SMALL_BUFFER_SIZE; |
Namjae Jeon | 4bc5947 | 2021-10-15 17:14:02 +0900 | [diff] [blame] | 526 | size_t large_sz = small_sz + work->conn->vals->max_trans_size; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 527 | size_t sz = small_sz; |
| 528 | int cmd = le16_to_cpu(hdr->Command); |
| 529 | |
Namjae Jeon | c30f4eb | 2021-06-18 10:17:37 +0900 | [diff] [blame] | 530 | if (cmd == SMB2_IOCTL_HE || cmd == SMB2_QUERY_DIRECTORY_HE) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 531 | sz = large_sz; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 532 | |
| 533 | if (cmd == SMB2_QUERY_INFO_HE) { |
| 534 | struct smb2_query_info_req *req; |
| 535 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 536 | req = smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 537 | if (req->InfoType == SMB2_O_INFO_FILE && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 538 | (req->FileInfoClass == FILE_FULL_EA_INFORMATION || |
Namjae Jeon | c30f4eb | 2021-06-18 10:17:37 +0900 | [diff] [blame] | 539 | req->FileInfoClass == FILE_ALL_INFORMATION)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 540 | sz = large_sz; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | /* allocate large response buf for chained commands */ |
| 544 | if (le32_to_cpu(hdr->NextCommand) > 0) |
| 545 | sz = large_sz; |
| 546 | |
Namjae Jeon | c30f4eb | 2021-06-18 10:17:37 +0900 | [diff] [blame] | 547 | work->response_buf = kvmalloc(sz, GFP_KERNEL | __GFP_ZERO); |
Namjae Jeon | 63c454f | 2021-04-20 14:24:28 +0900 | [diff] [blame] | 548 | if (!work->response_buf) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 549 | return -ENOMEM; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 550 | |
| 551 | work->response_sz = sz; |
| 552 | return 0; |
| 553 | } |
| 554 | |
| 555 | /** |
| 556 | * smb2_check_user_session() - check for valid session for a user |
| 557 | * @work: smb work containing smb request buffer |
| 558 | * |
| 559 | * Return: 0 on success, otherwise error |
| 560 | */ |
| 561 | int smb2_check_user_session(struct ksmbd_work *work) |
| 562 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 563 | struct smb2_hdr *req_hdr = smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 564 | struct ksmbd_conn *conn = work->conn; |
| 565 | unsigned int cmd = conn->ops->get_cmd_val(work); |
| 566 | unsigned long long sess_id; |
| 567 | |
| 568 | work->sess = NULL; |
| 569 | /* |
| 570 | * SMB2_ECHO, SMB2_NEGOTIATE, SMB2_SESSION_SETUP command do not |
| 571 | * require a session id, so no need to validate user session's for |
| 572 | * these commands. |
| 573 | */ |
| 574 | if (cmd == SMB2_ECHO_HE || cmd == SMB2_NEGOTIATE_HE || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 575 | cmd == SMB2_SESSION_SETUP_HE) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 576 | return 0; |
| 577 | |
| 578 | if (!ksmbd_conn_good(work)) |
| 579 | return -EINVAL; |
| 580 | |
| 581 | sess_id = le64_to_cpu(req_hdr->SessionId); |
| 582 | /* Check for validity of user session */ |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 583 | work->sess = ksmbd_session_lookup_all(conn, sess_id); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 584 | if (work->sess) |
| 585 | return 1; |
| 586 | ksmbd_debug(SMB, "Invalid user session, Uid %llu\n", sess_id); |
| 587 | return -EINVAL; |
| 588 | } |
| 589 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 590 | static void destroy_previous_session(struct ksmbd_user *user, u64 id) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 591 | { |
| 592 | struct ksmbd_session *prev_sess = ksmbd_session_lookup_slowpath(id); |
| 593 | struct ksmbd_user *prev_user; |
| 594 | |
| 595 | if (!prev_sess) |
| 596 | return; |
| 597 | |
| 598 | prev_user = prev_sess->user; |
| 599 | |
Marios Makassikis | 1fca803 | 2021-05-06 11:41:54 +0900 | [diff] [blame] | 600 | if (!prev_user || |
| 601 | strcmp(user->name, prev_user->name) || |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 602 | user->passkey_sz != prev_user->passkey_sz || |
| 603 | memcmp(user->passkey, prev_user->passkey, user->passkey_sz)) { |
| 604 | put_session(prev_sess); |
| 605 | return; |
| 606 | } |
| 607 | |
| 608 | put_session(prev_sess); |
| 609 | ksmbd_session_destroy(prev_sess); |
| 610 | } |
| 611 | |
| 612 | /** |
| 613 | * smb2_get_name() - get filename string from on the wire smb format |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 614 | * @share: ksmbd_share_config pointer |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 615 | * @src: source buffer |
| 616 | * @maxlen: maxlen of source string |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 617 | * @nls_table: nls_table pointer |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 618 | * |
| 619 | * Return: matching converted filename on success, otherwise error ptr |
| 620 | */ |
| 621 | static char * |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 622 | smb2_get_name(struct ksmbd_share_config *share, const char *src, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 623 | const int maxlen, struct nls_table *local_nls) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 624 | { |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 625 | char *name; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 626 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 627 | name = smb_strndup_from_utf16(src, maxlen, 1, local_nls); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 628 | if (IS_ERR(name)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 629 | pr_err("failed to get name %ld\n", PTR_ERR(name)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 630 | return name; |
| 631 | } |
| 632 | |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 633 | ksmbd_conv_path_to_unix(name); |
| 634 | ksmbd_strip_last_slash(name); |
| 635 | return name; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 636 | } |
| 637 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 638 | int setup_async_work(struct ksmbd_work *work, void (*fn)(void **), void **arg) |
| 639 | { |
| 640 | struct smb2_hdr *rsp_hdr; |
| 641 | struct ksmbd_conn *conn = work->conn; |
| 642 | int id; |
| 643 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 644 | rsp_hdr = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 645 | rsp_hdr->Flags |= SMB2_FLAGS_ASYNC_COMMAND; |
| 646 | |
Namjae Jeon | d40012a | 2021-04-13 13:06:30 +0900 | [diff] [blame] | 647 | id = ksmbd_acquire_async_msg_id(&conn->async_ida); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 648 | if (id < 0) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 649 | pr_err("Failed to alloc async message id\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 650 | return id; |
| 651 | } |
| 652 | work->syncronous = false; |
| 653 | work->async_id = id; |
| 654 | rsp_hdr->Id.AsyncId = cpu_to_le64(id); |
| 655 | |
| 656 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 657 | "Send interim Response to inform async request id : %d\n", |
| 658 | work->async_id); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 659 | |
| 660 | work->cancel_fn = fn; |
| 661 | work->cancel_argv = arg; |
| 662 | |
Namjae Jeon | 6c4e675 | 2021-06-07 09:08:45 +0900 | [diff] [blame] | 663 | if (list_empty(&work->async_request_entry)) { |
| 664 | spin_lock(&conn->request_lock); |
| 665 | list_add_tail(&work->async_request_entry, &conn->async_requests); |
| 666 | spin_unlock(&conn->request_lock); |
| 667 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 668 | |
| 669 | return 0; |
| 670 | } |
| 671 | |
| 672 | void smb2_send_interim_resp(struct ksmbd_work *work, __le32 status) |
| 673 | { |
| 674 | struct smb2_hdr *rsp_hdr; |
| 675 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 676 | rsp_hdr = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 677 | smb2_set_err_rsp(work); |
| 678 | rsp_hdr->Status = status; |
| 679 | |
| 680 | work->multiRsp = 1; |
| 681 | ksmbd_conn_write(work); |
| 682 | rsp_hdr->Status = 0; |
| 683 | work->multiRsp = 0; |
| 684 | } |
| 685 | |
| 686 | static __le32 smb2_get_reparse_tag_special_file(umode_t mode) |
| 687 | { |
| 688 | if (S_ISDIR(mode) || S_ISREG(mode)) |
| 689 | return 0; |
| 690 | |
| 691 | if (S_ISLNK(mode)) |
| 692 | return IO_REPARSE_TAG_LX_SYMLINK_LE; |
| 693 | else if (S_ISFIFO(mode)) |
| 694 | return IO_REPARSE_TAG_LX_FIFO_LE; |
| 695 | else if (S_ISSOCK(mode)) |
| 696 | return IO_REPARSE_TAG_AF_UNIX_LE; |
| 697 | else if (S_ISCHR(mode)) |
| 698 | return IO_REPARSE_TAG_LX_CHR_LE; |
| 699 | else if (S_ISBLK(mode)) |
| 700 | return IO_REPARSE_TAG_LX_BLK_LE; |
| 701 | |
| 702 | return 0; |
| 703 | } |
| 704 | |
| 705 | /** |
| 706 | * smb2_get_dos_mode() - get file mode in dos format from unix mode |
| 707 | * @stat: kstat containing file mode |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 708 | * @attribute: attribute flags |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 709 | * |
| 710 | * Return: converted dos mode |
| 711 | */ |
| 712 | static int smb2_get_dos_mode(struct kstat *stat, int attribute) |
| 713 | { |
| 714 | int attr = 0; |
| 715 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 716 | if (S_ISDIR(stat->mode)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 717 | attr = ATTR_DIRECTORY | |
| 718 | (attribute & (ATTR_HIDDEN | ATTR_SYSTEM)); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 719 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 720 | attr = (attribute & 0x00005137) | ATTR_ARCHIVE; |
| 721 | attr &= ~(ATTR_DIRECTORY); |
| 722 | if (S_ISREG(stat->mode) && (server_conf.share_fake_fscaps & |
| 723 | FILE_SUPPORTS_SPARSE_FILES)) |
| 724 | attr |= ATTR_SPARSE; |
| 725 | |
| 726 | if (smb2_get_reparse_tag_special_file(stat->mode)) |
| 727 | attr |= ATTR_REPARSE; |
| 728 | } |
| 729 | |
| 730 | return attr; |
| 731 | } |
| 732 | |
| 733 | static void build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 734 | __le16 hash_id) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 735 | { |
| 736 | pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES; |
| 737 | pneg_ctxt->DataLength = cpu_to_le16(38); |
| 738 | pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1); |
| 739 | pneg_ctxt->Reserved = cpu_to_le32(0); |
| 740 | pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE); |
| 741 | get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE); |
| 742 | pneg_ctxt->HashAlgorithms = hash_id; |
| 743 | } |
| 744 | |
| 745 | static void build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 746 | __le16 cipher_type) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 747 | { |
| 748 | pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES; |
| 749 | pneg_ctxt->DataLength = cpu_to_le16(4); |
| 750 | pneg_ctxt->Reserved = cpu_to_le32(0); |
| 751 | pneg_ctxt->CipherCount = cpu_to_le16(1); |
| 752 | pneg_ctxt->Ciphers[0] = cipher_type; |
| 753 | } |
| 754 | |
| 755 | static void build_compression_ctxt(struct smb2_compression_ctx *pneg_ctxt, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 756 | __le16 comp_algo) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 757 | { |
| 758 | pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES; |
| 759 | pneg_ctxt->DataLength = |
| 760 | cpu_to_le16(sizeof(struct smb2_compression_ctx) |
| 761 | - sizeof(struct smb2_neg_context)); |
| 762 | pneg_ctxt->Reserved = cpu_to_le32(0); |
| 763 | pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(1); |
| 764 | pneg_ctxt->Reserved1 = cpu_to_le32(0); |
| 765 | pneg_ctxt->CompressionAlgorithms[0] = comp_algo; |
| 766 | } |
| 767 | |
Namjae Jeon | 378087c | 2021-07-21 10:05:53 +0900 | [diff] [blame] | 768 | static void build_sign_cap_ctxt(struct smb2_signing_capabilities *pneg_ctxt, |
| 769 | __le16 sign_algo) |
| 770 | { |
| 771 | pneg_ctxt->ContextType = SMB2_SIGNING_CAPABILITIES; |
| 772 | pneg_ctxt->DataLength = |
| 773 | cpu_to_le16((sizeof(struct smb2_signing_capabilities) + 2) |
| 774 | - sizeof(struct smb2_neg_context)); |
| 775 | pneg_ctxt->Reserved = cpu_to_le32(0); |
| 776 | pneg_ctxt->SigningAlgorithmCount = cpu_to_le16(1); |
| 777 | pneg_ctxt->SigningAlgorithms[0] = sign_algo; |
| 778 | } |
| 779 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 780 | static void build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 781 | { |
| 782 | pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE; |
| 783 | pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN); |
| 784 | /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */ |
| 785 | pneg_ctxt->Name[0] = 0x93; |
| 786 | pneg_ctxt->Name[1] = 0xAD; |
| 787 | pneg_ctxt->Name[2] = 0x25; |
| 788 | pneg_ctxt->Name[3] = 0x50; |
| 789 | pneg_ctxt->Name[4] = 0x9C; |
| 790 | pneg_ctxt->Name[5] = 0xB4; |
| 791 | pneg_ctxt->Name[6] = 0x11; |
| 792 | pneg_ctxt->Name[7] = 0xE7; |
| 793 | pneg_ctxt->Name[8] = 0xB4; |
| 794 | pneg_ctxt->Name[9] = 0x23; |
| 795 | pneg_ctxt->Name[10] = 0x83; |
| 796 | pneg_ctxt->Name[11] = 0xDE; |
| 797 | pneg_ctxt->Name[12] = 0x96; |
| 798 | pneg_ctxt->Name[13] = 0x8B; |
| 799 | pneg_ctxt->Name[14] = 0xCD; |
| 800 | pneg_ctxt->Name[15] = 0x7C; |
| 801 | } |
| 802 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 803 | static void assemble_neg_contexts(struct ksmbd_conn *conn, |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 804 | struct smb2_negotiate_rsp *rsp, |
| 805 | void *smb2_buf_len) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 806 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 807 | char *pneg_ctxt = (char *)rsp + |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 808 | le32_to_cpu(rsp->NegotiateContextOffset); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 809 | int neg_ctxt_cnt = 1; |
| 810 | int ctxt_size; |
| 811 | |
| 812 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 813 | "assemble SMB2_PREAUTH_INTEGRITY_CAPABILITIES context\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 814 | build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 815 | conn->preauth_info->Preauth_HashId); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 816 | rsp->NegotiateContextCount = cpu_to_le16(neg_ctxt_cnt); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 817 | inc_rfc1001_len(smb2_buf_len, AUTH_GSS_PADDING); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 818 | ctxt_size = sizeof(struct smb2_preauth_neg_context); |
| 819 | /* Round to 8 byte boundary */ |
| 820 | pneg_ctxt += round_up(sizeof(struct smb2_preauth_neg_context), 8); |
| 821 | |
| 822 | if (conn->cipher_type) { |
| 823 | ctxt_size = round_up(ctxt_size, 8); |
| 824 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 825 | "assemble SMB2_ENCRYPTION_CAPABILITIES context\n"); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 826 | build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 827 | conn->cipher_type); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 828 | rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt); |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 829 | ctxt_size += sizeof(struct smb2_encryption_neg_context) + 2; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 830 | /* Round to 8 byte boundary */ |
| 831 | pneg_ctxt += |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 832 | round_up(sizeof(struct smb2_encryption_neg_context) + 2, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 833 | 8); |
| 834 | } |
| 835 | |
| 836 | if (conn->compress_algorithm) { |
| 837 | ctxt_size = round_up(ctxt_size, 8); |
| 838 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 839 | "assemble SMB2_COMPRESSION_CAPABILITIES context\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 840 | /* Temporarily set to SMB3_COMPRESS_NONE */ |
| 841 | build_compression_ctxt((struct smb2_compression_ctx *)pneg_ctxt, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 842 | conn->compress_algorithm); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 843 | rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt); |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 844 | ctxt_size += sizeof(struct smb2_compression_ctx) + 2; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 845 | /* Round to 8 byte boundary */ |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 846 | pneg_ctxt += round_up(sizeof(struct smb2_compression_ctx) + 2, |
| 847 | 8); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | if (conn->posix_ext_supported) { |
| 851 | ctxt_size = round_up(ctxt_size, 8); |
| 852 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 853 | "assemble SMB2_POSIX_EXTENSIONS_AVAILABLE context\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 854 | build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt); |
| 855 | rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt); |
| 856 | ctxt_size += sizeof(struct smb2_posix_neg_context); |
Namjae Jeon | 378087c | 2021-07-21 10:05:53 +0900 | [diff] [blame] | 857 | /* Round to 8 byte boundary */ |
| 858 | pneg_ctxt += round_up(sizeof(struct smb2_posix_neg_context), 8); |
| 859 | } |
| 860 | |
| 861 | if (conn->signing_negotiated) { |
| 862 | ctxt_size = round_up(ctxt_size, 8); |
| 863 | ksmbd_debug(SMB, |
| 864 | "assemble SMB2_SIGNING_CAPABILITIES context\n"); |
| 865 | build_sign_cap_ctxt((struct smb2_signing_capabilities *)pneg_ctxt, |
| 866 | conn->signing_algorithm); |
| 867 | rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt); |
| 868 | ctxt_size += sizeof(struct smb2_signing_capabilities) + 2; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 869 | } |
| 870 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 871 | inc_rfc1001_len(smb2_buf_len, ctxt_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 872 | } |
| 873 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 874 | static __le32 decode_preauth_ctxt(struct ksmbd_conn *conn, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 875 | struct smb2_preauth_neg_context *pneg_ctxt) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 876 | { |
| 877 | __le32 err = STATUS_NO_PREAUTH_INTEGRITY_HASH_OVERLAP; |
| 878 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 879 | if (pneg_ctxt->HashAlgorithms == SMB2_PREAUTH_INTEGRITY_SHA512) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 880 | conn->preauth_info->Preauth_HashId = |
| 881 | SMB2_PREAUTH_INTEGRITY_SHA512; |
| 882 | err = STATUS_SUCCESS; |
| 883 | } |
| 884 | |
| 885 | return err; |
| 886 | } |
| 887 | |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 888 | static void decode_encrypt_ctxt(struct ksmbd_conn *conn, |
| 889 | struct smb2_encryption_neg_context *pneg_ctxt, |
| 890 | int len_of_ctxts) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 891 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 892 | int cph_cnt = le16_to_cpu(pneg_ctxt->CipherCount); |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 893 | int i, cphs_size = cph_cnt * sizeof(__le16); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 894 | |
| 895 | conn->cipher_type = 0; |
| 896 | |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 897 | if (sizeof(struct smb2_encryption_neg_context) + cphs_size > |
| 898 | len_of_ctxts) { |
| 899 | pr_err("Invalid cipher count(%d)\n", cph_cnt); |
| 900 | return; |
| 901 | } |
| 902 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 903 | if (!(server_conf.flags & KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION)) |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 904 | return; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 905 | |
| 906 | for (i = 0; i < cph_cnt; i++) { |
| 907 | if (pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES128_GCM || |
Namjae Jeon | 5a0ca77 | 2021-05-06 11:43:37 +0900 | [diff] [blame] | 908 | pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES128_CCM || |
| 909 | pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES256_CCM || |
| 910 | pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES256_GCM) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 911 | ksmbd_debug(SMB, "Cipher ID = 0x%x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 912 | pneg_ctxt->Ciphers[i]); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 913 | conn->cipher_type = pneg_ctxt->Ciphers[i]; |
| 914 | break; |
| 915 | } |
| 916 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 917 | } |
| 918 | |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 919 | static void decode_compress_ctxt(struct ksmbd_conn *conn, |
| 920 | struct smb2_compression_ctx *pneg_ctxt) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 921 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 922 | conn->compress_algorithm = SMB3_COMPRESS_NONE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 923 | } |
| 924 | |
Namjae Jeon | 378087c | 2021-07-21 10:05:53 +0900 | [diff] [blame] | 925 | static void decode_sign_cap_ctxt(struct ksmbd_conn *conn, |
| 926 | struct smb2_signing_capabilities *pneg_ctxt, |
| 927 | int len_of_ctxts) |
| 928 | { |
| 929 | int sign_algo_cnt = le16_to_cpu(pneg_ctxt->SigningAlgorithmCount); |
| 930 | int i, sign_alos_size = sign_algo_cnt * sizeof(__le16); |
| 931 | |
| 932 | conn->signing_negotiated = false; |
| 933 | |
| 934 | if (sizeof(struct smb2_signing_capabilities) + sign_alos_size > |
| 935 | len_of_ctxts) { |
| 936 | pr_err("Invalid signing algorithm count(%d)\n", sign_algo_cnt); |
| 937 | return; |
| 938 | } |
| 939 | |
| 940 | for (i = 0; i < sign_algo_cnt; i++) { |
| 941 | if (pneg_ctxt->SigningAlgorithms[i] == SIGNING_ALG_HMAC_SHA256 || |
| 942 | pneg_ctxt->SigningAlgorithms[i] == SIGNING_ALG_AES_CMAC) { |
| 943 | ksmbd_debug(SMB, "Signing Algorithm ID = 0x%x\n", |
| 944 | pneg_ctxt->SigningAlgorithms[i]); |
| 945 | conn->signing_negotiated = true; |
| 946 | conn->signing_algorithm = |
| 947 | pneg_ctxt->SigningAlgorithms[i]; |
| 948 | break; |
| 949 | } |
| 950 | } |
| 951 | } |
| 952 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 953 | static __le32 deassemble_neg_contexts(struct ksmbd_conn *conn, |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 954 | struct smb2_negotiate_req *req, |
| 955 | int len_of_smb) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 956 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 957 | /* +4 is to account for the RFC1001 len field */ |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 958 | struct smb2_neg_context *pctx = (struct smb2_neg_context *)req; |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 959 | int i = 0, len_of_ctxts; |
| 960 | int offset = le32_to_cpu(req->NegotiateContextOffset); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 961 | int neg_ctxt_cnt = le16_to_cpu(req->NegotiateContextCount); |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 962 | __le32 status = STATUS_INVALID_PARAMETER; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 963 | |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 964 | ksmbd_debug(SMB, "decoding %d negotiate contexts\n", neg_ctxt_cnt); |
| 965 | if (len_of_smb <= offset) { |
| 966 | ksmbd_debug(SMB, "Invalid response: negotiate context offset\n"); |
| 967 | return status; |
| 968 | } |
| 969 | |
| 970 | len_of_ctxts = len_of_smb - offset; |
| 971 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 972 | while (i++ < neg_ctxt_cnt) { |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 973 | int clen; |
| 974 | |
| 975 | /* check that offset is not beyond end of SMB */ |
| 976 | if (len_of_ctxts == 0) |
| 977 | break; |
| 978 | |
| 979 | if (len_of_ctxts < sizeof(struct smb2_neg_context)) |
| 980 | break; |
| 981 | |
| 982 | pctx = (struct smb2_neg_context *)((char *)pctx + offset); |
| 983 | clen = le16_to_cpu(pctx->DataLength); |
| 984 | if (clen + sizeof(struct smb2_neg_context) > len_of_ctxts) |
| 985 | break; |
| 986 | |
| 987 | if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 988 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 989 | "deassemble SMB2_PREAUTH_INTEGRITY_CAPABILITIES context\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 990 | if (conn->preauth_info->Preauth_HashId) |
| 991 | break; |
| 992 | |
| 993 | status = decode_preauth_ctxt(conn, |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 994 | (struct smb2_preauth_neg_context *)pctx); |
| 995 | if (status != STATUS_SUCCESS) |
| 996 | break; |
| 997 | } else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 998 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 999 | "deassemble SMB2_ENCRYPTION_CAPABILITIES context\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1000 | if (conn->cipher_type) |
| 1001 | break; |
| 1002 | |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 1003 | decode_encrypt_ctxt(conn, |
| 1004 | (struct smb2_encryption_neg_context *)pctx, |
| 1005 | len_of_ctxts); |
| 1006 | } else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1007 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1008 | "deassemble SMB2_COMPRESSION_CAPABILITIES context\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1009 | if (conn->compress_algorithm) |
| 1010 | break; |
| 1011 | |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 1012 | decode_compress_ctxt(conn, |
| 1013 | (struct smb2_compression_ctx *)pctx); |
| 1014 | } else if (pctx->ContextType == SMB2_NETNAME_NEGOTIATE_CONTEXT_ID) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1015 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1016 | "deassemble SMB2_NETNAME_NEGOTIATE_CONTEXT_ID context\n"); |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 1017 | } else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1018 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1019 | "deassemble SMB2_POSIX_EXTENSIONS_AVAILABLE context\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1020 | conn->posix_ext_supported = true; |
Namjae Jeon | 378087c | 2021-07-21 10:05:53 +0900 | [diff] [blame] | 1021 | } else if (pctx->ContextType == SMB2_SIGNING_CAPABILITIES) { |
| 1022 | ksmbd_debug(SMB, |
| 1023 | "deassemble SMB2_SIGNING_CAPABILITIES context\n"); |
| 1024 | decode_sign_cap_ctxt(conn, |
| 1025 | (struct smb2_signing_capabilities *)pctx, |
| 1026 | len_of_ctxts); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1027 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1028 | |
Namjae Jeon | af320a7 | 2021-07-21 10:03:19 +0900 | [diff] [blame] | 1029 | /* offsets must be 8 byte aligned */ |
| 1030 | clen = (clen + 7) & ~0x7; |
| 1031 | offset = clen + sizeof(struct smb2_neg_context); |
| 1032 | len_of_ctxts -= clen + sizeof(struct smb2_neg_context); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1033 | } |
| 1034 | return status; |
| 1035 | } |
| 1036 | |
| 1037 | /** |
| 1038 | * smb2_handle_negotiate() - handler for smb2 negotiate command |
| 1039 | * @work: smb work containing smb request buffer |
| 1040 | * |
| 1041 | * Return: 0 |
| 1042 | */ |
| 1043 | int smb2_handle_negotiate(struct ksmbd_work *work) |
| 1044 | { |
| 1045 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 1046 | struct smb2_negotiate_req *req = smb2_get_msg(work->request_buf); |
| 1047 | struct smb2_negotiate_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1048 | int rc = 0; |
Namjae Jeon | 442ff9e | 2021-09-29 15:44:32 +0900 | [diff] [blame] | 1049 | unsigned int smb2_buf_len, smb2_neg_size; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1050 | __le32 status; |
| 1051 | |
| 1052 | ksmbd_debug(SMB, "Received negotiate request\n"); |
| 1053 | conn->need_neg = false; |
| 1054 | if (ksmbd_conn_good(work)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 1055 | pr_err("conn->tcp_status is already in CifsGood State\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1056 | work->send_no_response = 1; |
| 1057 | return rc; |
| 1058 | } |
| 1059 | |
| 1060 | if (req->DialectCount == 0) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 1061 | pr_err("malformed packet\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1062 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 1063 | rc = -EINVAL; |
| 1064 | goto err_out; |
| 1065 | } |
| 1066 | |
Namjae Jeon | 442ff9e | 2021-09-29 15:44:32 +0900 | [diff] [blame] | 1067 | smb2_buf_len = get_rfc1002_len(work->request_buf); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 1068 | smb2_neg_size = offsetof(struct smb2_negotiate_req, Dialects); |
Namjae Jeon | 442ff9e | 2021-09-29 15:44:32 +0900 | [diff] [blame] | 1069 | if (smb2_neg_size > smb2_buf_len) { |
| 1070 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 1071 | rc = -EINVAL; |
| 1072 | goto err_out; |
| 1073 | } |
| 1074 | |
| 1075 | if (conn->dialect == SMB311_PROT_ID) { |
| 1076 | unsigned int nego_ctxt_off = le32_to_cpu(req->NegotiateContextOffset); |
| 1077 | |
| 1078 | if (smb2_buf_len < nego_ctxt_off) { |
| 1079 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 1080 | rc = -EINVAL; |
| 1081 | goto err_out; |
| 1082 | } |
| 1083 | |
| 1084 | if (smb2_neg_size > nego_ctxt_off) { |
| 1085 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 1086 | rc = -EINVAL; |
| 1087 | goto err_out; |
| 1088 | } |
| 1089 | |
| 1090 | if (smb2_neg_size + le16_to_cpu(req->DialectCount) * sizeof(__le16) > |
| 1091 | nego_ctxt_off) { |
| 1092 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 1093 | rc = -EINVAL; |
| 1094 | goto err_out; |
| 1095 | } |
| 1096 | } else { |
| 1097 | if (smb2_neg_size + le16_to_cpu(req->DialectCount) * sizeof(__le16) > |
| 1098 | smb2_buf_len) { |
| 1099 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 1100 | rc = -EINVAL; |
| 1101 | goto err_out; |
| 1102 | } |
| 1103 | } |
| 1104 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1105 | conn->cli_cap = le32_to_cpu(req->Capabilities); |
| 1106 | switch (conn->dialect) { |
| 1107 | case SMB311_PROT_ID: |
| 1108 | conn->preauth_info = |
| 1109 | kzalloc(sizeof(struct preauth_integrity_info), |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1110 | GFP_KERNEL); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1111 | if (!conn->preauth_info) { |
| 1112 | rc = -ENOMEM; |
| 1113 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 1114 | goto err_out; |
| 1115 | } |
| 1116 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 1117 | status = deassemble_neg_contexts(conn, req, |
| 1118 | get_rfc1002_len(work->request_buf)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1119 | if (status != STATUS_SUCCESS) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 1120 | pr_err("deassemble_neg_contexts error(0x%x)\n", |
| 1121 | status); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1122 | rsp->hdr.Status = status; |
| 1123 | rc = -EINVAL; |
| 1124 | goto err_out; |
| 1125 | } |
| 1126 | |
| 1127 | rc = init_smb3_11_server(conn); |
| 1128 | if (rc < 0) { |
| 1129 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 1130 | goto err_out; |
| 1131 | } |
| 1132 | |
| 1133 | ksmbd_gen_preauth_integrity_hash(conn, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1134 | work->request_buf, |
| 1135 | conn->preauth_info->Preauth_HashValue); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1136 | rsp->NegotiateContextOffset = |
| 1137 | cpu_to_le32(OFFSET_OF_NEG_CONTEXT); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 1138 | assemble_neg_contexts(conn, rsp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1139 | break; |
| 1140 | case SMB302_PROT_ID: |
| 1141 | init_smb3_02_server(conn); |
| 1142 | break; |
| 1143 | case SMB30_PROT_ID: |
| 1144 | init_smb3_0_server(conn); |
| 1145 | break; |
| 1146 | case SMB21_PROT_ID: |
| 1147 | init_smb2_1_server(conn); |
| 1148 | break; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1149 | case SMB2X_PROT_ID: |
| 1150 | case BAD_PROT_ID: |
| 1151 | default: |
| 1152 | ksmbd_debug(SMB, "Server dialect :0x%x not supported\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1153 | conn->dialect); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1154 | rsp->hdr.Status = STATUS_NOT_SUPPORTED; |
| 1155 | rc = -EINVAL; |
| 1156 | goto err_out; |
| 1157 | } |
| 1158 | rsp->Capabilities = cpu_to_le32(conn->vals->capabilities); |
| 1159 | |
| 1160 | /* For stats */ |
| 1161 | conn->connection_type = conn->dialect; |
| 1162 | |
| 1163 | rsp->MaxTransactSize = cpu_to_le32(conn->vals->max_trans_size); |
| 1164 | rsp->MaxReadSize = cpu_to_le32(conn->vals->max_read_size); |
| 1165 | rsp->MaxWriteSize = cpu_to_le32(conn->vals->max_write_size); |
| 1166 | |
Namjae Jeon | 51a1387 | 2021-09-29 13:09:24 +0900 | [diff] [blame] | 1167 | memcpy(conn->ClientGUID, req->ClientGUID, |
| 1168 | SMB2_CLIENT_GUID_SIZE); |
| 1169 | conn->cli_sec_mode = le16_to_cpu(req->SecurityMode); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1170 | |
| 1171 | rsp->StructureSize = cpu_to_le16(65); |
| 1172 | rsp->DialectRevision = cpu_to_le16(conn->dialect); |
| 1173 | /* Not setting conn guid rsp->ServerGUID, as it |
| 1174 | * not used by client for identifying server |
| 1175 | */ |
| 1176 | memset(rsp->ServerGUID, 0, SMB2_CLIENT_GUID_SIZE); |
| 1177 | |
| 1178 | rsp->SystemTime = cpu_to_le64(ksmbd_systime()); |
| 1179 | rsp->ServerStartTime = 0; |
| 1180 | ksmbd_debug(SMB, "negotiate context offset %d, count %d\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1181 | le32_to_cpu(rsp->NegotiateContextOffset), |
| 1182 | le16_to_cpu(rsp->NegotiateContextCount)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1183 | |
| 1184 | rsp->SecurityBufferOffset = cpu_to_le16(128); |
| 1185 | rsp->SecurityBufferLength = cpu_to_le16(AUTH_GSS_LENGTH); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 1186 | ksmbd_copy_gss_neg_header((char *)(&rsp->hdr) + |
| 1187 | le16_to_cpu(rsp->SecurityBufferOffset)); |
| 1188 | inc_rfc1001_len(work->response_buf, sizeof(struct smb2_negotiate_rsp) - |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1189 | sizeof(struct smb2_hdr) - sizeof(rsp->Buffer) + |
| 1190 | AUTH_GSS_LENGTH); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1191 | rsp->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED_LE; |
| 1192 | conn->use_spnego = true; |
| 1193 | |
| 1194 | if ((server_conf.signing == KSMBD_CONFIG_OPT_AUTO || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1195 | server_conf.signing == KSMBD_CONFIG_OPT_DISABLED) && |
| 1196 | req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED_LE) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1197 | conn->sign = true; |
| 1198 | else if (server_conf.signing == KSMBD_CONFIG_OPT_MANDATORY) { |
| 1199 | server_conf.enforced_signing = true; |
| 1200 | rsp->SecurityMode |= SMB2_NEGOTIATE_SIGNING_REQUIRED_LE; |
| 1201 | conn->sign = true; |
| 1202 | } |
| 1203 | |
| 1204 | conn->srv_sec_mode = le16_to_cpu(rsp->SecurityMode); |
| 1205 | ksmbd_conn_set_need_negotiate(work); |
| 1206 | |
| 1207 | err_out: |
| 1208 | if (rc < 0) |
| 1209 | smb2_set_err_rsp(work); |
| 1210 | |
| 1211 | return rc; |
| 1212 | } |
| 1213 | |
| 1214 | static int alloc_preauth_hash(struct ksmbd_session *sess, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1215 | struct ksmbd_conn *conn) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1216 | { |
| 1217 | if (sess->Preauth_HashValue) |
| 1218 | return 0; |
| 1219 | |
kernel test robot | 86f5297 | 2021-04-02 12:17:24 +0900 | [diff] [blame] | 1220 | sess->Preauth_HashValue = kmemdup(conn->preauth_info->Preauth_HashValue, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1221 | PREAUTH_HASHVALUE_SIZE, GFP_KERNEL); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1222 | if (!sess->Preauth_HashValue) |
| 1223 | return -ENOMEM; |
| 1224 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1225 | return 0; |
| 1226 | } |
| 1227 | |
| 1228 | static int generate_preauth_hash(struct ksmbd_work *work) |
| 1229 | { |
| 1230 | struct ksmbd_conn *conn = work->conn; |
| 1231 | struct ksmbd_session *sess = work->sess; |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1232 | u8 *preauth_hash; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1233 | |
| 1234 | if (conn->dialect != SMB311_PROT_ID) |
| 1235 | return 0; |
| 1236 | |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1237 | if (conn->binding) { |
| 1238 | struct preauth_session *preauth_sess; |
| 1239 | |
| 1240 | preauth_sess = ksmbd_preauth_session_lookup(conn, sess->id); |
| 1241 | if (!preauth_sess) { |
| 1242 | preauth_sess = ksmbd_preauth_session_alloc(conn, sess->id); |
| 1243 | if (!preauth_sess) |
| 1244 | return -ENOMEM; |
| 1245 | } |
| 1246 | |
| 1247 | preauth_hash = preauth_sess->Preauth_HashValue; |
| 1248 | } else { |
| 1249 | if (!sess->Preauth_HashValue) |
| 1250 | if (alloc_preauth_hash(sess, conn)) |
| 1251 | return -ENOMEM; |
| 1252 | preauth_hash = sess->Preauth_HashValue; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1253 | } |
| 1254 | |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1255 | ksmbd_gen_preauth_integrity_hash(conn, work->request_buf, preauth_hash); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1256 | return 0; |
| 1257 | } |
| 1258 | |
Marios Makassikis | 0d994cd | 2021-10-19 17:39:38 +0200 | [diff] [blame] | 1259 | static int decode_negotiation_token(struct ksmbd_conn *conn, |
| 1260 | struct negotiate_message *negblob, |
| 1261 | size_t sz) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1262 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1263 | if (!conn->use_spnego) |
| 1264 | return -EINVAL; |
| 1265 | |
Hyunchul Lee | fad4161 | 2021-04-19 17:26:15 +0900 | [diff] [blame] | 1266 | if (ksmbd_decode_negTokenInit((char *)negblob, sz, conn)) { |
| 1267 | if (ksmbd_decode_negTokenTarg((char *)negblob, sz, conn)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1268 | conn->auth_mechs |= KSMBD_AUTH_NTLMSSP; |
| 1269 | conn->preferred_auth_mech = KSMBD_AUTH_NTLMSSP; |
| 1270 | conn->use_spnego = false; |
| 1271 | } |
| 1272 | } |
| 1273 | return 0; |
| 1274 | } |
| 1275 | |
| 1276 | static int ntlm_negotiate(struct ksmbd_work *work, |
Marios Makassikis | 0d994cd | 2021-10-19 17:39:38 +0200 | [diff] [blame] | 1277 | struct negotiate_message *negblob, |
| 1278 | size_t negblob_len) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1279 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 1280 | struct smb2_sess_setup_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1281 | struct challenge_message *chgblob; |
| 1282 | unsigned char *spnego_blob = NULL; |
| 1283 | u16 spnego_blob_len; |
| 1284 | char *neg_blob; |
| 1285 | int sz, rc; |
| 1286 | |
| 1287 | ksmbd_debug(SMB, "negotiate phase\n"); |
Marios Makassikis | 0d994cd | 2021-10-19 17:39:38 +0200 | [diff] [blame] | 1288 | rc = ksmbd_decode_ntlmssp_neg_blob(negblob, negblob_len, work->sess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1289 | if (rc) |
| 1290 | return rc; |
| 1291 | |
| 1292 | sz = le16_to_cpu(rsp->SecurityBufferOffset); |
| 1293 | chgblob = |
| 1294 | (struct challenge_message *)((char *)&rsp->hdr.ProtocolId + sz); |
| 1295 | memset(chgblob, 0, sizeof(struct challenge_message)); |
| 1296 | |
| 1297 | if (!work->conn->use_spnego) { |
| 1298 | sz = ksmbd_build_ntlmssp_challenge_blob(chgblob, work->sess); |
| 1299 | if (sz < 0) |
| 1300 | return -ENOMEM; |
| 1301 | |
| 1302 | rsp->SecurityBufferLength = cpu_to_le16(sz); |
| 1303 | return 0; |
| 1304 | } |
| 1305 | |
| 1306 | sz = sizeof(struct challenge_message); |
| 1307 | sz += (strlen(ksmbd_netbios_name()) * 2 + 1 + 4) * 6; |
| 1308 | |
| 1309 | neg_blob = kzalloc(sz, GFP_KERNEL); |
| 1310 | if (!neg_blob) |
| 1311 | return -ENOMEM; |
| 1312 | |
| 1313 | chgblob = (struct challenge_message *)neg_blob; |
| 1314 | sz = ksmbd_build_ntlmssp_challenge_blob(chgblob, work->sess); |
| 1315 | if (sz < 0) { |
| 1316 | rc = -ENOMEM; |
| 1317 | goto out; |
| 1318 | } |
| 1319 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1320 | rc = build_spnego_ntlmssp_neg_blob(&spnego_blob, &spnego_blob_len, |
| 1321 | neg_blob, sz); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1322 | if (rc) { |
| 1323 | rc = -ENOMEM; |
| 1324 | goto out; |
| 1325 | } |
| 1326 | |
| 1327 | sz = le16_to_cpu(rsp->SecurityBufferOffset); |
| 1328 | memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len); |
| 1329 | rsp->SecurityBufferLength = cpu_to_le16(spnego_blob_len); |
| 1330 | |
| 1331 | out: |
| 1332 | kfree(spnego_blob); |
| 1333 | kfree(neg_blob); |
| 1334 | return rc; |
| 1335 | } |
| 1336 | |
| 1337 | static struct authenticate_message *user_authblob(struct ksmbd_conn *conn, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1338 | struct smb2_sess_setup_req *req) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1339 | { |
| 1340 | int sz; |
| 1341 | |
| 1342 | if (conn->use_spnego && conn->mechToken) |
| 1343 | return (struct authenticate_message *)conn->mechToken; |
| 1344 | |
| 1345 | sz = le16_to_cpu(req->SecurityBufferOffset); |
| 1346 | return (struct authenticate_message *)((char *)&req->hdr.ProtocolId |
| 1347 | + sz); |
| 1348 | } |
| 1349 | |
| 1350 | static struct ksmbd_user *session_user(struct ksmbd_conn *conn, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1351 | struct smb2_sess_setup_req *req) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1352 | { |
| 1353 | struct authenticate_message *authblob; |
| 1354 | struct ksmbd_user *user; |
| 1355 | char *name; |
Marios Makassikis | 0d994cd | 2021-10-19 17:39:38 +0200 | [diff] [blame] | 1356 | unsigned int auth_msg_len, name_off, name_len, secbuf_len; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1357 | |
Marios Makassikis | 0d994cd | 2021-10-19 17:39:38 +0200 | [diff] [blame] | 1358 | secbuf_len = le16_to_cpu(req->SecurityBufferLength); |
| 1359 | if (secbuf_len < sizeof(struct authenticate_message)) { |
| 1360 | ksmbd_debug(SMB, "blob len %d too small\n", secbuf_len); |
| 1361 | return NULL; |
| 1362 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1363 | authblob = user_authblob(conn, req); |
Marios Makassikis | 0d994cd | 2021-10-19 17:39:38 +0200 | [diff] [blame] | 1364 | name_off = le32_to_cpu(authblob->UserName.BufferOffset); |
| 1365 | name_len = le16_to_cpu(authblob->UserName.Length); |
| 1366 | auth_msg_len = le16_to_cpu(req->SecurityBufferOffset) + secbuf_len; |
| 1367 | |
| 1368 | if (auth_msg_len < (u64)name_off + name_len) |
| 1369 | return NULL; |
| 1370 | |
| 1371 | name = smb_strndup_from_utf16((const char *)authblob + name_off, |
| 1372 | name_len, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1373 | true, |
| 1374 | conn->local_nls); |
| 1375 | if (IS_ERR(name)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 1376 | pr_err("cannot allocate memory\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1377 | return NULL; |
| 1378 | } |
| 1379 | |
| 1380 | ksmbd_debug(SMB, "session setup request for user %s\n", name); |
| 1381 | user = ksmbd_login_user(name); |
| 1382 | kfree(name); |
| 1383 | return user; |
| 1384 | } |
| 1385 | |
| 1386 | static int ntlm_authenticate(struct ksmbd_work *work) |
| 1387 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 1388 | struct smb2_sess_setup_req *req = smb2_get_msg(work->request_buf); |
| 1389 | struct smb2_sess_setup_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1390 | struct ksmbd_conn *conn = work->conn; |
| 1391 | struct ksmbd_session *sess = work->sess; |
| 1392 | struct channel *chann = NULL; |
| 1393 | struct ksmbd_user *user; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1394 | u64 prev_id; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1395 | int sz, rc; |
| 1396 | |
| 1397 | ksmbd_debug(SMB, "authenticate phase\n"); |
| 1398 | if (conn->use_spnego) { |
| 1399 | unsigned char *spnego_blob; |
| 1400 | u16 spnego_blob_len; |
| 1401 | |
| 1402 | rc = build_spnego_ntlmssp_auth_blob(&spnego_blob, |
| 1403 | &spnego_blob_len, |
| 1404 | 0); |
| 1405 | if (rc) |
| 1406 | return -ENOMEM; |
| 1407 | |
| 1408 | sz = le16_to_cpu(rsp->SecurityBufferOffset); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1409 | memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1410 | rsp->SecurityBufferLength = cpu_to_le16(spnego_blob_len); |
| 1411 | kfree(spnego_blob); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 1412 | inc_rfc1001_len(work->response_buf, spnego_blob_len - 1); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1413 | } |
| 1414 | |
| 1415 | user = session_user(conn, req); |
| 1416 | if (!user) { |
| 1417 | ksmbd_debug(SMB, "Unknown user name or an error\n"); |
Namjae Jeon | 58090b1 | 2021-07-16 14:52:09 +0900 | [diff] [blame] | 1418 | return -EPERM; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1419 | } |
| 1420 | |
| 1421 | /* Check for previous session */ |
| 1422 | prev_id = le64_to_cpu(req->PreviousSessionId); |
| 1423 | if (prev_id && prev_id != sess->id) |
| 1424 | destroy_previous_session(user, prev_id); |
| 1425 | |
| 1426 | if (sess->state == SMB2_SESSION_VALID) { |
| 1427 | /* |
| 1428 | * Reuse session if anonymous try to connect |
| 1429 | * on reauthetication. |
| 1430 | */ |
| 1431 | if (ksmbd_anonymous_user(user)) { |
| 1432 | ksmbd_free_user(user); |
| 1433 | return 0; |
| 1434 | } |
| 1435 | ksmbd_free_user(sess->user); |
| 1436 | } |
| 1437 | |
| 1438 | sess->user = user; |
| 1439 | if (user_guest(sess->user)) { |
| 1440 | if (conn->sign) { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1441 | ksmbd_debug(SMB, "Guest login not allowed when signing enabled\n"); |
Namjae Jeon | 58090b1 | 2021-07-16 14:52:09 +0900 | [diff] [blame] | 1442 | return -EPERM; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1443 | } |
| 1444 | |
| 1445 | rsp->SessionFlags = SMB2_SESSION_FLAG_IS_GUEST_LE; |
| 1446 | } else { |
| 1447 | struct authenticate_message *authblob; |
| 1448 | |
| 1449 | authblob = user_authblob(conn, req); |
| 1450 | sz = le16_to_cpu(req->SecurityBufferLength); |
| 1451 | rc = ksmbd_decode_ntlmssp_auth_blob(authblob, sz, sess); |
| 1452 | if (rc) { |
| 1453 | set_user_flag(sess->user, KSMBD_USER_FLAG_BAD_PASSWORD); |
| 1454 | ksmbd_debug(SMB, "authentication failed\n"); |
Namjae Jeon | 58090b1 | 2021-07-16 14:52:09 +0900 | [diff] [blame] | 1455 | return -EPERM; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1456 | } |
| 1457 | |
| 1458 | /* |
| 1459 | * If session state is SMB2_SESSION_VALID, We can assume |
| 1460 | * that it is reauthentication. And the user/password |
| 1461 | * has been verified, so return it here. |
| 1462 | */ |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1463 | if (sess->state == SMB2_SESSION_VALID) { |
| 1464 | if (conn->binding) |
| 1465 | goto binding_session; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1466 | return 0; |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1467 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1468 | |
| 1469 | if ((conn->sign || server_conf.enforced_signing) || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1470 | (req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1471 | sess->sign = true; |
| 1472 | |
| 1473 | if (conn->vals->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION && |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1474 | conn->ops->generate_encryptionkey && |
| 1475 | !(req->Flags & SMB2_SESSION_REQ_FLAG_BINDING)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1476 | rc = conn->ops->generate_encryptionkey(sess); |
| 1477 | if (rc) { |
| 1478 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1479 | "SMB3 encryption key generation failed\n"); |
Namjae Jeon | 58090b1 | 2021-07-16 14:52:09 +0900 | [diff] [blame] | 1480 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1481 | } |
| 1482 | sess->enc = true; |
| 1483 | rsp->SessionFlags = SMB2_SESSION_FLAG_ENCRYPT_DATA_LE; |
| 1484 | /* |
| 1485 | * signing is disable if encryption is enable |
| 1486 | * on this session |
| 1487 | */ |
| 1488 | sess->sign = false; |
| 1489 | } |
| 1490 | } |
| 1491 | |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1492 | binding_session: |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1493 | if (conn->dialect >= SMB30_PROT_ID) { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1494 | chann = lookup_chann_list(sess, conn); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1495 | if (!chann) { |
| 1496 | chann = kmalloc(sizeof(struct channel), GFP_KERNEL); |
| 1497 | if (!chann) |
| 1498 | return -ENOMEM; |
| 1499 | |
| 1500 | chann->conn = conn; |
| 1501 | INIT_LIST_HEAD(&chann->chann_list); |
| 1502 | list_add(&chann->chann_list, &sess->ksmbd_chann_list); |
| 1503 | } |
| 1504 | } |
| 1505 | |
| 1506 | if (conn->ops->generate_signingkey) { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1507 | rc = conn->ops->generate_signingkey(sess, conn); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1508 | if (rc) { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1509 | ksmbd_debug(SMB, "SMB3 signing key generation failed\n"); |
Namjae Jeon | 58090b1 | 2021-07-16 14:52:09 +0900 | [diff] [blame] | 1510 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1511 | } |
| 1512 | } |
| 1513 | |
Namjae Jeon | 51a1387 | 2021-09-29 13:09:24 +0900 | [diff] [blame] | 1514 | if (!ksmbd_conn_lookup_dialect(conn)) { |
| 1515 | pr_err("fail to verify the dialect\n"); |
| 1516 | return -ENOENT; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1517 | } |
| 1518 | return 0; |
| 1519 | } |
| 1520 | |
| 1521 | #ifdef CONFIG_SMB_SERVER_KERBEROS5 |
| 1522 | static int krb5_authenticate(struct ksmbd_work *work) |
| 1523 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 1524 | struct smb2_sess_setup_req *req = smb2_get_msg(work->request_buf); |
| 1525 | struct smb2_sess_setup_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1526 | struct ksmbd_conn *conn = work->conn; |
| 1527 | struct ksmbd_session *sess = work->sess; |
| 1528 | char *in_blob, *out_blob; |
| 1529 | struct channel *chann = NULL; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1530 | u64 prev_sess_id; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1531 | int in_len, out_len; |
| 1532 | int retval; |
| 1533 | |
| 1534 | in_blob = (char *)&req->hdr.ProtocolId + |
| 1535 | le16_to_cpu(req->SecurityBufferOffset); |
| 1536 | in_len = le16_to_cpu(req->SecurityBufferLength); |
| 1537 | out_blob = (char *)&rsp->hdr.ProtocolId + |
| 1538 | le16_to_cpu(rsp->SecurityBufferOffset); |
| 1539 | out_len = work->response_sz - |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 1540 | (le16_to_cpu(rsp->SecurityBufferOffset) + 4); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1541 | |
| 1542 | /* Check previous session */ |
| 1543 | prev_sess_id = le64_to_cpu(req->PreviousSessionId); |
| 1544 | if (prev_sess_id && prev_sess_id != sess->id) |
| 1545 | destroy_previous_session(sess->user, prev_sess_id); |
| 1546 | |
| 1547 | if (sess->state == SMB2_SESSION_VALID) |
| 1548 | ksmbd_free_user(sess->user); |
| 1549 | |
| 1550 | retval = ksmbd_krb5_authenticate(sess, in_blob, in_len, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1551 | out_blob, &out_len); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1552 | if (retval) { |
| 1553 | ksmbd_debug(SMB, "krb5 authentication failed\n"); |
Namjae Jeon | 58090b1 | 2021-07-16 14:52:09 +0900 | [diff] [blame] | 1554 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1555 | } |
| 1556 | rsp->SecurityBufferLength = cpu_to_le16(out_len); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 1557 | inc_rfc1001_len(work->response_buf, out_len - 1); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1558 | |
| 1559 | if ((conn->sign || server_conf.enforced_signing) || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1560 | (req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1561 | sess->sign = true; |
| 1562 | |
| 1563 | if ((conn->vals->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION) && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1564 | conn->ops->generate_encryptionkey) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1565 | retval = conn->ops->generate_encryptionkey(sess); |
| 1566 | if (retval) { |
| 1567 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1568 | "SMB3 encryption key generation failed\n"); |
Namjae Jeon | 58090b1 | 2021-07-16 14:52:09 +0900 | [diff] [blame] | 1569 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1570 | } |
| 1571 | sess->enc = true; |
| 1572 | rsp->SessionFlags = SMB2_SESSION_FLAG_ENCRYPT_DATA_LE; |
| 1573 | sess->sign = false; |
| 1574 | } |
| 1575 | |
| 1576 | if (conn->dialect >= SMB30_PROT_ID) { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1577 | chann = lookup_chann_list(sess, conn); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1578 | if (!chann) { |
| 1579 | chann = kmalloc(sizeof(struct channel), GFP_KERNEL); |
| 1580 | if (!chann) |
| 1581 | return -ENOMEM; |
| 1582 | |
| 1583 | chann->conn = conn; |
| 1584 | INIT_LIST_HEAD(&chann->chann_list); |
| 1585 | list_add(&chann->chann_list, &sess->ksmbd_chann_list); |
| 1586 | } |
| 1587 | } |
| 1588 | |
| 1589 | if (conn->ops->generate_signingkey) { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1590 | retval = conn->ops->generate_signingkey(sess, conn); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1591 | if (retval) { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1592 | ksmbd_debug(SMB, "SMB3 signing key generation failed\n"); |
Namjae Jeon | 58090b1 | 2021-07-16 14:52:09 +0900 | [diff] [blame] | 1593 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1594 | } |
| 1595 | } |
| 1596 | |
Namjae Jeon | 51a1387 | 2021-09-29 13:09:24 +0900 | [diff] [blame] | 1597 | if (!ksmbd_conn_lookup_dialect(conn)) { |
| 1598 | pr_err("fail to verify the dialect\n"); |
| 1599 | return -ENOENT; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1600 | } |
| 1601 | return 0; |
| 1602 | } |
| 1603 | #else |
| 1604 | static int krb5_authenticate(struct ksmbd_work *work) |
| 1605 | { |
| 1606 | return -EOPNOTSUPP; |
| 1607 | } |
| 1608 | #endif |
| 1609 | |
| 1610 | int smb2_sess_setup(struct ksmbd_work *work) |
| 1611 | { |
| 1612 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 1613 | struct smb2_sess_setup_req *req = smb2_get_msg(work->request_buf); |
| 1614 | struct smb2_sess_setup_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1615 | struct ksmbd_session *sess; |
| 1616 | struct negotiate_message *negblob; |
Marios Makassikis | 0d994cd | 2021-10-19 17:39:38 +0200 | [diff] [blame] | 1617 | unsigned int negblob_len, negblob_off; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1618 | int rc = 0; |
| 1619 | |
| 1620 | ksmbd_debug(SMB, "Received request for session setup\n"); |
| 1621 | |
| 1622 | rsp->StructureSize = cpu_to_le16(9); |
| 1623 | rsp->SessionFlags = 0; |
| 1624 | rsp->SecurityBufferOffset = cpu_to_le16(72); |
| 1625 | rsp->SecurityBufferLength = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 1626 | inc_rfc1001_len(work->response_buf, 9); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1627 | |
| 1628 | if (!req->hdr.SessionId) { |
| 1629 | sess = ksmbd_smb2_session_create(); |
| 1630 | if (!sess) { |
| 1631 | rc = -ENOMEM; |
| 1632 | goto out_err; |
| 1633 | } |
| 1634 | rsp->hdr.SessionId = cpu_to_le64(sess->id); |
| 1635 | ksmbd_session_register(conn, sess); |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1636 | } else if (conn->dialect >= SMB30_PROT_ID && |
| 1637 | (server_conf.flags & KSMBD_GLOBAL_FLAG_SMB3_MULTICHANNEL) && |
| 1638 | req->Flags & SMB2_SESSION_REQ_FLAG_BINDING) { |
| 1639 | u64 sess_id = le64_to_cpu(req->hdr.SessionId); |
| 1640 | |
| 1641 | sess = ksmbd_session_lookup_slowpath(sess_id); |
| 1642 | if (!sess) { |
| 1643 | rc = -ENOENT; |
| 1644 | goto out_err; |
| 1645 | } |
| 1646 | |
| 1647 | if (conn->dialect != sess->conn->dialect) { |
| 1648 | rc = -EINVAL; |
| 1649 | goto out_err; |
| 1650 | } |
| 1651 | |
| 1652 | if (!(req->hdr.Flags & SMB2_FLAGS_SIGNED)) { |
| 1653 | rc = -EINVAL; |
| 1654 | goto out_err; |
| 1655 | } |
| 1656 | |
| 1657 | if (strncmp(conn->ClientGUID, sess->conn->ClientGUID, |
| 1658 | SMB2_CLIENT_GUID_SIZE)) { |
| 1659 | rc = -ENOENT; |
| 1660 | goto out_err; |
| 1661 | } |
| 1662 | |
| 1663 | if (sess->state == SMB2_SESSION_IN_PROGRESS) { |
| 1664 | rc = -EACCES; |
| 1665 | goto out_err; |
| 1666 | } |
| 1667 | |
| 1668 | if (sess->state == SMB2_SESSION_EXPIRED) { |
| 1669 | rc = -EFAULT; |
| 1670 | goto out_err; |
| 1671 | } |
| 1672 | |
| 1673 | if (ksmbd_session_lookup(conn, sess_id)) { |
| 1674 | rc = -EACCES; |
| 1675 | goto out_err; |
| 1676 | } |
| 1677 | |
| 1678 | conn->binding = true; |
| 1679 | } else if ((conn->dialect < SMB30_PROT_ID || |
| 1680 | server_conf.flags & KSMBD_GLOBAL_FLAG_SMB3_MULTICHANNEL) && |
| 1681 | (req->Flags & SMB2_SESSION_REQ_FLAG_BINDING)) { |
Colin Ian King | 4951a84 | 2021-07-06 13:05:01 +0100 | [diff] [blame] | 1682 | sess = NULL; |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1683 | rc = -EACCES; |
| 1684 | goto out_err; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1685 | } else { |
| 1686 | sess = ksmbd_session_lookup(conn, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1687 | le64_to_cpu(req->hdr.SessionId)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1688 | if (!sess) { |
| 1689 | rc = -ENOENT; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1690 | goto out_err; |
| 1691 | } |
| 1692 | } |
| 1693 | work->sess = sess; |
| 1694 | |
| 1695 | if (sess->state == SMB2_SESSION_EXPIRED) |
| 1696 | sess->state = SMB2_SESSION_IN_PROGRESS; |
| 1697 | |
Marios Makassikis | 0d994cd | 2021-10-19 17:39:38 +0200 | [diff] [blame] | 1698 | negblob_off = le16_to_cpu(req->SecurityBufferOffset); |
| 1699 | negblob_len = le16_to_cpu(req->SecurityBufferLength); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 1700 | if (negblob_off < offsetof(struct smb2_sess_setup_req, Buffer) || |
Marios Makassikis | 0d994cd | 2021-10-19 17:39:38 +0200 | [diff] [blame] | 1701 | negblob_len < offsetof(struct negotiate_message, NegotiateFlags)) |
| 1702 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1703 | |
Marios Makassikis | 0d994cd | 2021-10-19 17:39:38 +0200 | [diff] [blame] | 1704 | negblob = (struct negotiate_message *)((char *)&req->hdr.ProtocolId + |
| 1705 | negblob_off); |
| 1706 | |
| 1707 | if (decode_negotiation_token(conn, negblob, negblob_len) == 0) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1708 | if (conn->mechToken) |
| 1709 | negblob = (struct negotiate_message *)conn->mechToken; |
| 1710 | } |
| 1711 | |
| 1712 | if (server_conf.auth_mechs & conn->auth_mechs) { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1713 | rc = generate_preauth_hash(work); |
| 1714 | if (rc) |
| 1715 | goto out_err; |
| 1716 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1717 | if (conn->preferred_auth_mech & |
| 1718 | (KSMBD_AUTH_KRB5 | KSMBD_AUTH_MSKRB5)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1719 | rc = krb5_authenticate(work); |
| 1720 | if (rc) { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1721 | rc = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1722 | goto out_err; |
| 1723 | } |
| 1724 | |
| 1725 | ksmbd_conn_set_good(work); |
| 1726 | sess->state = SMB2_SESSION_VALID; |
Muhammad Usama Anjum | 822bc8e | 2021-04-02 09:25:35 +0900 | [diff] [blame] | 1727 | kfree(sess->Preauth_HashValue); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1728 | sess->Preauth_HashValue = NULL; |
| 1729 | } else if (conn->preferred_auth_mech == KSMBD_AUTH_NTLMSSP) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1730 | if (negblob->MessageType == NtLmNegotiate) { |
Marios Makassikis | 0d994cd | 2021-10-19 17:39:38 +0200 | [diff] [blame] | 1731 | rc = ntlm_negotiate(work, negblob, negblob_len); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1732 | if (rc) |
| 1733 | goto out_err; |
| 1734 | rsp->hdr.Status = |
| 1735 | STATUS_MORE_PROCESSING_REQUIRED; |
| 1736 | /* |
| 1737 | * Note: here total size -1 is done as an |
| 1738 | * adjustment for 0 size blob |
| 1739 | */ |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 1740 | inc_rfc1001_len(work->response_buf, |
| 1741 | le16_to_cpu(rsp->SecurityBufferLength) - 1); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1742 | |
| 1743 | } else if (negblob->MessageType == NtLmAuthenticate) { |
| 1744 | rc = ntlm_authenticate(work); |
| 1745 | if (rc) |
| 1746 | goto out_err; |
| 1747 | |
| 1748 | ksmbd_conn_set_good(work); |
| 1749 | sess->state = SMB2_SESSION_VALID; |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1750 | if (conn->binding) { |
| 1751 | struct preauth_session *preauth_sess; |
| 1752 | |
| 1753 | preauth_sess = |
| 1754 | ksmbd_preauth_session_lookup(conn, sess->id); |
| 1755 | if (preauth_sess) { |
| 1756 | list_del(&preauth_sess->preauth_entry); |
| 1757 | kfree(preauth_sess); |
| 1758 | } |
| 1759 | } |
Muhammad Usama Anjum | 822bc8e | 2021-04-02 09:25:35 +0900 | [diff] [blame] | 1760 | kfree(sess->Preauth_HashValue); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1761 | sess->Preauth_HashValue = NULL; |
| 1762 | } |
| 1763 | } else { |
| 1764 | /* TODO: need one more negotiation */ |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 1765 | pr_err("Not support the preferred authentication\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1766 | rc = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1767 | } |
| 1768 | } else { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 1769 | pr_err("Not support authentication\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1770 | rc = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1771 | } |
| 1772 | |
| 1773 | out_err: |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1774 | if (rc == -EINVAL) |
| 1775 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 1776 | else if (rc == -ENOENT) |
| 1777 | rsp->hdr.Status = STATUS_USER_SESSION_DELETED; |
| 1778 | else if (rc == -EACCES) |
| 1779 | rsp->hdr.Status = STATUS_REQUEST_NOT_ACCEPTED; |
| 1780 | else if (rc == -EFAULT) |
| 1781 | rsp->hdr.Status = STATUS_NETWORK_SESSION_EXPIRED; |
Namjae Jeon | 58090b1 | 2021-07-16 14:52:09 +0900 | [diff] [blame] | 1782 | else if (rc == -ENOMEM) |
| 1783 | rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES; |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 1784 | else if (rc) |
| 1785 | rsp->hdr.Status = STATUS_LOGON_FAILURE; |
| 1786 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1787 | if (conn->use_spnego && conn->mechToken) { |
| 1788 | kfree(conn->mechToken); |
| 1789 | conn->mechToken = NULL; |
| 1790 | } |
| 1791 | |
Namjae Jeon | 621be84 | 2021-10-13 17:28:31 +0900 | [diff] [blame] | 1792 | if (rc < 0) { |
| 1793 | /* |
| 1794 | * SecurityBufferOffset should be set to zero |
| 1795 | * in session setup error response. |
| 1796 | */ |
| 1797 | rsp->SecurityBufferOffset = 0; |
| 1798 | |
| 1799 | if (sess) { |
| 1800 | bool try_delay = false; |
| 1801 | |
| 1802 | /* |
| 1803 | * To avoid dictionary attacks (repeated session setups rapidly sent) to |
| 1804 | * connect to server, ksmbd make a delay of a 5 seconds on session setup |
| 1805 | * failure to make it harder to send enough random connection requests |
| 1806 | * to break into a server. |
| 1807 | */ |
| 1808 | if (sess->user && sess->user->flags & KSMBD_USER_FLAG_DELAY_SESSION) |
| 1809 | try_delay = true; |
| 1810 | |
| 1811 | ksmbd_session_destroy(sess); |
| 1812 | work->sess = NULL; |
| 1813 | if (try_delay) |
| 1814 | ssleep(5); |
| 1815 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1816 | } |
| 1817 | |
| 1818 | return rc; |
| 1819 | } |
| 1820 | |
| 1821 | /** |
| 1822 | * smb2_tree_connect() - handler for smb2 tree connect command |
| 1823 | * @work: smb work containing smb request buffer |
| 1824 | * |
| 1825 | * Return: 0 on success, otherwise error |
| 1826 | */ |
| 1827 | int smb2_tree_connect(struct ksmbd_work *work) |
| 1828 | { |
| 1829 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 1830 | struct smb2_tree_connect_req *req = smb2_get_msg(work->request_buf); |
| 1831 | struct smb2_tree_connect_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1832 | struct ksmbd_session *sess = work->sess; |
| 1833 | char *treename = NULL, *name = NULL; |
| 1834 | struct ksmbd_tree_conn_status status; |
| 1835 | struct ksmbd_share_config *share; |
| 1836 | int rc = -EINVAL; |
| 1837 | |
| 1838 | treename = smb_strndup_from_utf16(req->Buffer, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1839 | le16_to_cpu(req->PathLength), true, |
| 1840 | conn->local_nls); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1841 | if (IS_ERR(treename)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 1842 | pr_err("treename is NULL\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1843 | status.ret = KSMBD_TREE_CONN_STATUS_ERROR; |
| 1844 | goto out_err1; |
| 1845 | } |
| 1846 | |
Stephen Rothwell | 36ba386 | 2021-03-17 17:01:15 +0900 | [diff] [blame] | 1847 | name = ksmbd_extract_sharename(treename); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1848 | if (IS_ERR(name)) { |
| 1849 | status.ret = KSMBD_TREE_CONN_STATUS_ERROR; |
| 1850 | goto out_err1; |
| 1851 | } |
| 1852 | |
| 1853 | ksmbd_debug(SMB, "tree connect request for tree %s treename %s\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1854 | name, treename); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1855 | |
| 1856 | status = ksmbd_tree_conn_connect(sess, name); |
| 1857 | if (status.ret == KSMBD_TREE_CONN_STATUS_OK) |
| 1858 | rsp->hdr.Id.SyncId.TreeId = cpu_to_le32(status.tree_conn->id); |
| 1859 | else |
| 1860 | goto out_err1; |
| 1861 | |
| 1862 | share = status.tree_conn->share_conf; |
| 1863 | if (test_share_config_flag(share, KSMBD_SHARE_FLAG_PIPE)) { |
| 1864 | ksmbd_debug(SMB, "IPC share path request\n"); |
| 1865 | rsp->ShareType = SMB2_SHARE_TYPE_PIPE; |
| 1866 | rsp->MaximalAccess = FILE_READ_DATA_LE | FILE_READ_EA_LE | |
| 1867 | FILE_EXECUTE_LE | FILE_READ_ATTRIBUTES_LE | |
| 1868 | FILE_DELETE_LE | FILE_READ_CONTROL_LE | |
| 1869 | FILE_WRITE_DAC_LE | FILE_WRITE_OWNER_LE | |
| 1870 | FILE_SYNCHRONIZE_LE; |
| 1871 | } else { |
| 1872 | rsp->ShareType = SMB2_SHARE_TYPE_DISK; |
| 1873 | rsp->MaximalAccess = FILE_READ_DATA_LE | FILE_READ_EA_LE | |
| 1874 | FILE_EXECUTE_LE | FILE_READ_ATTRIBUTES_LE; |
| 1875 | if (test_tree_conn_flag(status.tree_conn, |
| 1876 | KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
| 1877 | rsp->MaximalAccess |= FILE_WRITE_DATA_LE | |
| 1878 | FILE_APPEND_DATA_LE | FILE_WRITE_EA_LE | |
Wan Jiabing | 3aefd54 | 2021-06-07 12:54:32 +0800 | [diff] [blame] | 1879 | FILE_DELETE_LE | FILE_WRITE_ATTRIBUTES_LE | |
| 1880 | FILE_DELETE_CHILD_LE | FILE_READ_CONTROL_LE | |
| 1881 | FILE_WRITE_DAC_LE | FILE_WRITE_OWNER_LE | |
| 1882 | FILE_SYNCHRONIZE_LE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1883 | } |
| 1884 | } |
| 1885 | |
| 1886 | status.tree_conn->maximal_access = le32_to_cpu(rsp->MaximalAccess); |
| 1887 | if (conn->posix_ext_supported) |
| 1888 | status.tree_conn->posix_extensions = true; |
| 1889 | |
| 1890 | out_err1: |
| 1891 | rsp->StructureSize = cpu_to_le16(16); |
| 1892 | rsp->Capabilities = 0; |
| 1893 | rsp->Reserved = 0; |
| 1894 | /* default manual caching */ |
| 1895 | rsp->ShareFlags = SMB2_SHAREFLAG_MANUAL_CACHING; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 1896 | inc_rfc1001_len(work->response_buf, 16); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1897 | |
| 1898 | if (!IS_ERR(treename)) |
| 1899 | kfree(treename); |
| 1900 | if (!IS_ERR(name)) |
| 1901 | kfree(name); |
| 1902 | |
| 1903 | switch (status.ret) { |
| 1904 | case KSMBD_TREE_CONN_STATUS_OK: |
| 1905 | rsp->hdr.Status = STATUS_SUCCESS; |
| 1906 | rc = 0; |
| 1907 | break; |
| 1908 | case KSMBD_TREE_CONN_STATUS_NO_SHARE: |
| 1909 | rsp->hdr.Status = STATUS_BAD_NETWORK_PATH; |
| 1910 | break; |
| 1911 | case -ENOMEM: |
| 1912 | case KSMBD_TREE_CONN_STATUS_NOMEM: |
| 1913 | rsp->hdr.Status = STATUS_NO_MEMORY; |
| 1914 | break; |
| 1915 | case KSMBD_TREE_CONN_STATUS_ERROR: |
| 1916 | case KSMBD_TREE_CONN_STATUS_TOO_MANY_CONNS: |
| 1917 | case KSMBD_TREE_CONN_STATUS_TOO_MANY_SESSIONS: |
| 1918 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 1919 | break; |
| 1920 | case -EINVAL: |
| 1921 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 1922 | break; |
| 1923 | default: |
| 1924 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 1925 | } |
| 1926 | |
| 1927 | return rc; |
| 1928 | } |
| 1929 | |
| 1930 | /** |
| 1931 | * smb2_create_open_flags() - convert smb open flags to unix open flags |
| 1932 | * @file_present: is file already present |
| 1933 | * @access: file access flags |
| 1934 | * @disposition: file disposition flags |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 1935 | * @may_flags: set with MAY_ flags |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1936 | * |
| 1937 | * Return: file open flags |
| 1938 | */ |
| 1939 | static int smb2_create_open_flags(bool file_present, __le32 access, |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 1940 | __le32 disposition, |
| 1941 | int *may_flags) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1942 | { |
| 1943 | int oflags = O_NONBLOCK | O_LARGEFILE; |
| 1944 | |
| 1945 | if (access & FILE_READ_DESIRED_ACCESS_LE && |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 1946 | access & FILE_WRITE_DESIRE_ACCESS_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1947 | oflags |= O_RDWR; |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 1948 | *may_flags = MAY_OPEN | MAY_READ | MAY_WRITE; |
| 1949 | } else if (access & FILE_WRITE_DESIRE_ACCESS_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1950 | oflags |= O_WRONLY; |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 1951 | *may_flags = MAY_OPEN | MAY_WRITE; |
| 1952 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1953 | oflags |= O_RDONLY; |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 1954 | *may_flags = MAY_OPEN | MAY_READ; |
| 1955 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1956 | |
| 1957 | if (access == FILE_READ_ATTRIBUTES_LE) |
| 1958 | oflags |= O_PATH; |
| 1959 | |
| 1960 | if (file_present) { |
| 1961 | switch (disposition & FILE_CREATE_MASK_LE) { |
| 1962 | case FILE_OPEN_LE: |
| 1963 | case FILE_CREATE_LE: |
| 1964 | break; |
| 1965 | case FILE_SUPERSEDE_LE: |
| 1966 | case FILE_OVERWRITE_LE: |
| 1967 | case FILE_OVERWRITE_IF_LE: |
| 1968 | oflags |= O_TRUNC; |
| 1969 | break; |
| 1970 | default: |
| 1971 | break; |
| 1972 | } |
| 1973 | } else { |
| 1974 | switch (disposition & FILE_CREATE_MASK_LE) { |
| 1975 | case FILE_SUPERSEDE_LE: |
| 1976 | case FILE_CREATE_LE: |
| 1977 | case FILE_OPEN_IF_LE: |
| 1978 | case FILE_OVERWRITE_IF_LE: |
| 1979 | oflags |= O_CREAT; |
| 1980 | break; |
| 1981 | case FILE_OPEN_LE: |
| 1982 | case FILE_OVERWRITE_LE: |
| 1983 | oflags &= ~O_CREAT; |
| 1984 | break; |
| 1985 | default: |
| 1986 | break; |
| 1987 | } |
| 1988 | } |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 1989 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1990 | return oflags; |
| 1991 | } |
| 1992 | |
| 1993 | /** |
| 1994 | * smb2_tree_disconnect() - handler for smb tree connect request |
| 1995 | * @work: smb work containing request buffer |
| 1996 | * |
| 1997 | * Return: 0 |
| 1998 | */ |
| 1999 | int smb2_tree_disconnect(struct ksmbd_work *work) |
| 2000 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 2001 | struct smb2_tree_disconnect_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2002 | struct ksmbd_session *sess = work->sess; |
| 2003 | struct ksmbd_tree_connect *tcon = work->tcon; |
| 2004 | |
| 2005 | rsp->StructureSize = cpu_to_le16(4); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 2006 | inc_rfc1001_len(work->response_buf, 4); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2007 | |
| 2008 | ksmbd_debug(SMB, "request\n"); |
| 2009 | |
| 2010 | if (!tcon) { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 2011 | struct smb2_tree_disconnect_req *req = |
| 2012 | smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2013 | |
| 2014 | ksmbd_debug(SMB, "Invalid tid %d\n", req->hdr.Id.SyncId.TreeId); |
| 2015 | rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED; |
| 2016 | smb2_set_err_rsp(work); |
| 2017 | return 0; |
| 2018 | } |
| 2019 | |
| 2020 | ksmbd_close_tree_conn_fds(work); |
| 2021 | ksmbd_tree_conn_disconnect(sess, tcon); |
| 2022 | return 0; |
| 2023 | } |
| 2024 | |
| 2025 | /** |
| 2026 | * smb2_session_logoff() - handler for session log off request |
| 2027 | * @work: smb work containing request buffer |
| 2028 | * |
| 2029 | * Return: 0 |
| 2030 | */ |
| 2031 | int smb2_session_logoff(struct ksmbd_work *work) |
| 2032 | { |
| 2033 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 2034 | struct smb2_logoff_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2035 | struct ksmbd_session *sess = work->sess; |
| 2036 | |
| 2037 | rsp->StructureSize = cpu_to_le16(4); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 2038 | inc_rfc1001_len(work->response_buf, 4); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2039 | |
| 2040 | ksmbd_debug(SMB, "request\n"); |
| 2041 | |
| 2042 | /* Got a valid session, set connection state */ |
| 2043 | WARN_ON(sess->conn != conn); |
| 2044 | |
| 2045 | /* setting CifsExiting here may race with start_tcp_sess */ |
| 2046 | ksmbd_conn_set_need_reconnect(work); |
| 2047 | ksmbd_close_session_fds(work); |
| 2048 | ksmbd_conn_wait_idle(conn); |
| 2049 | |
| 2050 | if (ksmbd_tree_conn_session_logoff(sess)) { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 2051 | struct smb2_logoff_req *req = smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2052 | |
| 2053 | ksmbd_debug(SMB, "Invalid tid %d\n", req->hdr.Id.SyncId.TreeId); |
| 2054 | rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED; |
| 2055 | smb2_set_err_rsp(work); |
| 2056 | return 0; |
| 2057 | } |
| 2058 | |
| 2059 | ksmbd_destroy_file_table(&sess->file_table); |
| 2060 | sess->state = SMB2_SESSION_EXPIRED; |
| 2061 | |
| 2062 | ksmbd_free_user(sess->user); |
| 2063 | sess->user = NULL; |
| 2064 | |
| 2065 | /* let start_tcp_sess free connection info now */ |
| 2066 | ksmbd_conn_set_need_negotiate(work); |
| 2067 | return 0; |
| 2068 | } |
| 2069 | |
| 2070 | /** |
| 2071 | * create_smb2_pipe() - create IPC pipe |
| 2072 | * @work: smb work containing request buffer |
| 2073 | * |
| 2074 | * Return: 0 on success, otherwise error |
| 2075 | */ |
| 2076 | static noinline int create_smb2_pipe(struct ksmbd_work *work) |
| 2077 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 2078 | struct smb2_create_rsp *rsp = smb2_get_msg(work->response_buf); |
| 2079 | struct smb2_create_req *req = smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2080 | int id; |
| 2081 | int err; |
| 2082 | char *name; |
| 2083 | |
| 2084 | name = smb_strndup_from_utf16(req->Buffer, le16_to_cpu(req->NameLength), |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2085 | 1, work->conn->local_nls); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2086 | if (IS_ERR(name)) { |
| 2087 | rsp->hdr.Status = STATUS_NO_MEMORY; |
| 2088 | err = PTR_ERR(name); |
| 2089 | goto out; |
| 2090 | } |
| 2091 | |
| 2092 | id = ksmbd_session_rpc_open(work->sess, name); |
Marios Makassikis | 79caa96 | 2021-05-06 11:38:35 +0900 | [diff] [blame] | 2093 | if (id < 0) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2094 | pr_err("Unable to open RPC pipe: %d\n", id); |
Marios Makassikis | 79caa96 | 2021-05-06 11:38:35 +0900 | [diff] [blame] | 2095 | err = id; |
| 2096 | goto out; |
| 2097 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2098 | |
Marios Makassikis | 79caa96 | 2021-05-06 11:38:35 +0900 | [diff] [blame] | 2099 | rsp->hdr.Status = STATUS_SUCCESS; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2100 | rsp->StructureSize = cpu_to_le16(89); |
| 2101 | rsp->OplockLevel = SMB2_OPLOCK_LEVEL_NONE; |
| 2102 | rsp->Reserved = 0; |
| 2103 | rsp->CreateAction = cpu_to_le32(FILE_OPENED); |
| 2104 | |
| 2105 | rsp->CreationTime = cpu_to_le64(0); |
| 2106 | rsp->LastAccessTime = cpu_to_le64(0); |
| 2107 | rsp->ChangeTime = cpu_to_le64(0); |
| 2108 | rsp->AllocationSize = cpu_to_le64(0); |
| 2109 | rsp->EndofFile = cpu_to_le64(0); |
| 2110 | rsp->FileAttributes = ATTR_NORMAL_LE; |
| 2111 | rsp->Reserved2 = 0; |
| 2112 | rsp->VolatileFileId = cpu_to_le64(id); |
| 2113 | rsp->PersistentFileId = 0; |
| 2114 | rsp->CreateContextsOffset = 0; |
| 2115 | rsp->CreateContextsLength = 0; |
| 2116 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 2117 | inc_rfc1001_len(work->response_buf, 88); /* StructureSize - 1*/ |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2118 | kfree(name); |
| 2119 | return 0; |
| 2120 | |
| 2121 | out: |
Marios Makassikis | 79caa96 | 2021-05-06 11:38:35 +0900 | [diff] [blame] | 2122 | switch (err) { |
| 2123 | case -EINVAL: |
| 2124 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 2125 | break; |
| 2126 | case -ENOSPC: |
| 2127 | case -ENOMEM: |
| 2128 | rsp->hdr.Status = STATUS_NO_MEMORY; |
| 2129 | break; |
| 2130 | } |
| 2131 | |
| 2132 | if (!IS_ERR(name)) |
| 2133 | kfree(name); |
| 2134 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2135 | smb2_set_err_rsp(work); |
| 2136 | return err; |
| 2137 | } |
| 2138 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2139 | /** |
| 2140 | * smb2_set_ea() - handler for setting extended attributes using set |
| 2141 | * info command |
| 2142 | * @eabuf: set info command buffer |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 2143 | * @buf_len: set info command buffer length |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2144 | * @path: dentry path for get ea |
| 2145 | * |
| 2146 | * Return: 0 on success, otherwise error |
| 2147 | */ |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 2148 | static int smb2_set_ea(struct smb2_ea_info *eabuf, unsigned int buf_len, |
| 2149 | struct path *path) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2150 | { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2151 | struct user_namespace *user_ns = mnt_user_ns(path->mnt); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2152 | char *attr_name = NULL, *value; |
| 2153 | int rc = 0; |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 2154 | unsigned int next = 0; |
| 2155 | |
| 2156 | if (buf_len < sizeof(struct smb2_ea_info) + eabuf->EaNameLength + |
| 2157 | le16_to_cpu(eabuf->EaValueLength)) |
| 2158 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2159 | |
| 2160 | attr_name = kmalloc(XATTR_NAME_MAX + 1, GFP_KERNEL); |
| 2161 | if (!attr_name) |
| 2162 | return -ENOMEM; |
| 2163 | |
| 2164 | do { |
| 2165 | if (!eabuf->EaNameLength) |
| 2166 | goto next; |
| 2167 | |
| 2168 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2169 | "name : <%s>, name_len : %u, value_len : %u, next : %u\n", |
| 2170 | eabuf->name, eabuf->EaNameLength, |
| 2171 | le16_to_cpu(eabuf->EaValueLength), |
| 2172 | le32_to_cpu(eabuf->NextEntryOffset)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2173 | |
| 2174 | if (eabuf->EaNameLength > |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2175 | (XATTR_NAME_MAX - XATTR_USER_PREFIX_LEN)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2176 | rc = -EINVAL; |
| 2177 | break; |
| 2178 | } |
| 2179 | |
| 2180 | memcpy(attr_name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN); |
| 2181 | memcpy(&attr_name[XATTR_USER_PREFIX_LEN], eabuf->name, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2182 | eabuf->EaNameLength); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2183 | attr_name[XATTR_USER_PREFIX_LEN + eabuf->EaNameLength] = '\0'; |
| 2184 | value = (char *)&eabuf->name + eabuf->EaNameLength + 1; |
| 2185 | |
| 2186 | if (!eabuf->EaValueLength) { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2187 | rc = ksmbd_vfs_casexattr_len(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2188 | path->dentry, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2189 | attr_name, |
| 2190 | XATTR_USER_PREFIX_LEN + |
| 2191 | eabuf->EaNameLength); |
| 2192 | |
| 2193 | /* delete the EA only when it exits */ |
| 2194 | if (rc > 0) { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2195 | rc = ksmbd_vfs_remove_xattr(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2196 | path->dentry, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2197 | attr_name); |
| 2198 | |
| 2199 | if (rc < 0) { |
| 2200 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2201 | "remove xattr failed(%d)\n", |
| 2202 | rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2203 | break; |
| 2204 | } |
| 2205 | } |
| 2206 | |
| 2207 | /* if the EA doesn't exist, just do nothing. */ |
| 2208 | rc = 0; |
| 2209 | } else { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2210 | rc = ksmbd_vfs_setxattr(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2211 | path->dentry, attr_name, value, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2212 | le16_to_cpu(eabuf->EaValueLength), 0); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2213 | if (rc < 0) { |
| 2214 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2215 | "ksmbd_vfs_setxattr is failed(%d)\n", |
| 2216 | rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2217 | break; |
| 2218 | } |
| 2219 | } |
| 2220 | |
| 2221 | next: |
| 2222 | next = le32_to_cpu(eabuf->NextEntryOffset); |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 2223 | if (next == 0 || buf_len < next) |
| 2224 | break; |
| 2225 | buf_len -= next; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2226 | eabuf = (struct smb2_ea_info *)((char *)eabuf + next); |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 2227 | if (next < (u32)eabuf->EaNameLength + le16_to_cpu(eabuf->EaValueLength)) |
| 2228 | break; |
| 2229 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2230 | } while (next != 0); |
| 2231 | |
| 2232 | kfree(attr_name); |
| 2233 | return rc; |
| 2234 | } |
| 2235 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2236 | static noinline int smb2_set_stream_name_xattr(struct path *path, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2237 | struct ksmbd_file *fp, |
| 2238 | char *stream_name, int s_type) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2239 | { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2240 | struct user_namespace *user_ns = mnt_user_ns(path->mnt); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2241 | size_t xattr_stream_size; |
| 2242 | char *xattr_stream_name; |
| 2243 | int rc; |
| 2244 | |
| 2245 | rc = ksmbd_vfs_xattr_stream_name(stream_name, |
| 2246 | &xattr_stream_name, |
| 2247 | &xattr_stream_size, |
| 2248 | s_type); |
| 2249 | if (rc) |
| 2250 | return rc; |
| 2251 | |
| 2252 | fp->stream.name = xattr_stream_name; |
| 2253 | fp->stream.size = xattr_stream_size; |
| 2254 | |
| 2255 | /* Check if there is stream prefix in xattr space */ |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2256 | rc = ksmbd_vfs_casexattr_len(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2257 | path->dentry, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2258 | xattr_stream_name, |
| 2259 | xattr_stream_size); |
| 2260 | if (rc >= 0) |
| 2261 | return 0; |
| 2262 | |
| 2263 | if (fp->cdoption == FILE_OPEN_LE) { |
| 2264 | ksmbd_debug(SMB, "XATTR stream name lookup failed: %d\n", rc); |
| 2265 | return -EBADF; |
| 2266 | } |
| 2267 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2268 | rc = ksmbd_vfs_setxattr(user_ns, path->dentry, |
| 2269 | xattr_stream_name, NULL, 0, 0); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2270 | if (rc < 0) |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2271 | pr_err("Failed to store XATTR stream name :%d\n", rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2272 | return 0; |
| 2273 | } |
| 2274 | |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 2275 | static int smb2_remove_smb_xattrs(struct path *path) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2276 | { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2277 | struct user_namespace *user_ns = mnt_user_ns(path->mnt); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2278 | char *name, *xattr_list = NULL; |
| 2279 | ssize_t xattr_list_len; |
| 2280 | int err = 0; |
| 2281 | |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 2282 | xattr_list_len = ksmbd_vfs_listxattr(path->dentry, &xattr_list); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2283 | if (xattr_list_len < 0) { |
| 2284 | goto out; |
| 2285 | } else if (!xattr_list_len) { |
| 2286 | ksmbd_debug(SMB, "empty xattr in the file\n"); |
| 2287 | goto out; |
| 2288 | } |
| 2289 | |
| 2290 | for (name = xattr_list; name - xattr_list < xattr_list_len; |
| 2291 | name += strlen(name) + 1) { |
| 2292 | ksmbd_debug(SMB, "%s, len %zd\n", name, strlen(name)); |
| 2293 | |
| 2294 | if (strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2295 | strncmp(&name[XATTR_USER_PREFIX_LEN], DOS_ATTRIBUTE_PREFIX, |
| 2296 | DOS_ATTRIBUTE_PREFIX_LEN) && |
| 2297 | strncmp(&name[XATTR_USER_PREFIX_LEN], STREAM_PREFIX, STREAM_PREFIX_LEN)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2298 | continue; |
| 2299 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2300 | err = ksmbd_vfs_remove_xattr(user_ns, path->dentry, name); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2301 | if (err) |
| 2302 | ksmbd_debug(SMB, "remove xattr failed : %s\n", name); |
| 2303 | } |
| 2304 | out: |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 2305 | kvfree(xattr_list); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2306 | return err; |
| 2307 | } |
| 2308 | |
| 2309 | static int smb2_create_truncate(struct path *path) |
| 2310 | { |
| 2311 | int rc = vfs_truncate(path, 0); |
| 2312 | |
| 2313 | if (rc) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2314 | pr_err("vfs_truncate failed, rc %d\n", rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2315 | return rc; |
| 2316 | } |
| 2317 | |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 2318 | rc = smb2_remove_smb_xattrs(path); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2319 | if (rc == -EOPNOTSUPP) |
| 2320 | rc = 0; |
| 2321 | if (rc) |
| 2322 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2323 | "ksmbd_truncate_stream_name_xattr failed, rc %d\n", |
| 2324 | rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2325 | return rc; |
| 2326 | } |
| 2327 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2328 | 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] | 2329 | struct ksmbd_file *fp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2330 | { |
| 2331 | struct xattr_dos_attrib da = {0}; |
| 2332 | int rc; |
| 2333 | |
| 2334 | if (!test_share_config_flag(tcon->share_conf, |
| 2335 | KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) |
| 2336 | return; |
| 2337 | |
| 2338 | da.version = 4; |
| 2339 | da.attr = le32_to_cpu(fp->f_ci->m_fattr); |
| 2340 | da.itime = da.create_time = fp->create_time; |
| 2341 | da.flags = XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME | |
| 2342 | XATTR_DOSINFO_ITIME; |
| 2343 | |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2344 | rc = ksmbd_vfs_set_dos_attrib_xattr(mnt_user_ns(path->mnt), |
| 2345 | path->dentry, &da); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2346 | if (rc) |
| 2347 | ksmbd_debug(SMB, "failed to store file attribute into xattr\n"); |
| 2348 | } |
| 2349 | |
| 2350 | static void smb2_update_xattrs(struct ksmbd_tree_connect *tcon, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2351 | struct path *path, struct ksmbd_file *fp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2352 | { |
| 2353 | struct xattr_dos_attrib da; |
| 2354 | int rc; |
| 2355 | |
| 2356 | fp->f_ci->m_fattr &= ~(ATTR_HIDDEN_LE | ATTR_SYSTEM_LE); |
| 2357 | |
| 2358 | /* get FileAttributes from XATTR_NAME_DOS_ATTRIBUTE */ |
| 2359 | if (!test_share_config_flag(tcon->share_conf, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2360 | KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2361 | return; |
| 2362 | |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2363 | rc = ksmbd_vfs_get_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 > 0) { |
| 2366 | fp->f_ci->m_fattr = cpu_to_le32(da.attr); |
| 2367 | fp->create_time = da.create_time; |
| 2368 | fp->itime = da.itime; |
| 2369 | } |
| 2370 | } |
| 2371 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2372 | 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] | 2373 | int open_flags, umode_t posix_mode, bool is_dir) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2374 | { |
| 2375 | struct ksmbd_tree_connect *tcon = work->tcon; |
| 2376 | struct ksmbd_share_config *share = tcon->share_conf; |
| 2377 | umode_t mode; |
| 2378 | int rc; |
| 2379 | |
| 2380 | if (!(open_flags & O_CREAT)) |
| 2381 | return -EBADF; |
| 2382 | |
| 2383 | ksmbd_debug(SMB, "file does not exist, so creating\n"); |
| 2384 | if (is_dir == true) { |
| 2385 | ksmbd_debug(SMB, "creating directory\n"); |
| 2386 | |
| 2387 | mode = share_config_directory_mode(share, posix_mode); |
| 2388 | rc = ksmbd_vfs_mkdir(work, name, mode); |
| 2389 | if (rc) |
| 2390 | return rc; |
| 2391 | } else { |
| 2392 | ksmbd_debug(SMB, "creating regular file\n"); |
| 2393 | |
| 2394 | mode = share_config_create_mode(share, posix_mode); |
| 2395 | rc = ksmbd_vfs_create(work, name, mode); |
| 2396 | if (rc) |
| 2397 | return rc; |
| 2398 | } |
| 2399 | |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 2400 | rc = ksmbd_vfs_kern_path(work, name, 0, path, 0); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2401 | if (rc) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2402 | pr_err("cannot get linux path (%s), err = %d\n", |
| 2403 | name, rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2404 | return rc; |
| 2405 | } |
| 2406 | return 0; |
| 2407 | } |
| 2408 | |
| 2409 | static int smb2_create_sd_buffer(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2410 | struct smb2_create_req *req, |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 2411 | struct path *path) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2412 | { |
| 2413 | struct create_context *context; |
Hyunchul Lee | 21dd1fd | 2021-07-09 17:06:34 +0900 | [diff] [blame] | 2414 | struct create_sd_buf_req *sd_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2415 | |
| 2416 | if (!req->CreateContextsOffset) |
Hyunchul Lee | 21dd1fd | 2021-07-09 17:06:34 +0900 | [diff] [blame] | 2417 | return -ENOENT; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2418 | |
| 2419 | /* Parse SD BUFFER create contexts */ |
| 2420 | context = smb2_find_context_vals(req, SMB2_CREATE_SD_BUFFER); |
Hyunchul Lee | 21dd1fd | 2021-07-09 17:06:34 +0900 | [diff] [blame] | 2421 | if (!context) |
| 2422 | return -ENOENT; |
| 2423 | else if (IS_ERR(context)) |
| 2424 | return PTR_ERR(context); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2425 | |
Hyunchul Lee | 21dd1fd | 2021-07-09 17:06:34 +0900 | [diff] [blame] | 2426 | ksmbd_debug(SMB, |
| 2427 | "Set ACLs using SMB2_CREATE_SD_BUFFER context\n"); |
| 2428 | sd_buf = (struct create_sd_buf_req *)context; |
Hyunchul Lee | 8f77150 | 2021-09-24 22:22:22 +0900 | [diff] [blame] | 2429 | if (le16_to_cpu(context->DataOffset) + |
| 2430 | le32_to_cpu(context->DataLength) < |
| 2431 | sizeof(struct create_sd_buf_req)) |
| 2432 | return -EINVAL; |
Hyunchul Lee | 21dd1fd | 2021-07-09 17:06:34 +0900 | [diff] [blame] | 2433 | return set_info_sec(work->conn, work->tcon, path, &sd_buf->ntsd, |
| 2434 | le32_to_cpu(sd_buf->ccontext.DataLength), true); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2435 | } |
| 2436 | |
Christian Brauner | 43205ca | 2021-08-23 17:13:50 +0200 | [diff] [blame] | 2437 | static void ksmbd_acls_fattr(struct smb_fattr *fattr, |
| 2438 | struct user_namespace *mnt_userns, |
| 2439 | struct inode *inode) |
Namjae Jeon | 3d47e54 | 2021-04-20 14:25:35 +0900 | [diff] [blame] | 2440 | { |
Christian Brauner | 43205ca | 2021-08-23 17:13:50 +0200 | [diff] [blame] | 2441 | fattr->cf_uid = i_uid_into_mnt(mnt_userns, inode); |
| 2442 | fattr->cf_gid = i_gid_into_mnt(mnt_userns, inode); |
Namjae Jeon | 3d47e54 | 2021-04-20 14:25:35 +0900 | [diff] [blame] | 2443 | fattr->cf_mode = inode->i_mode; |
Namjae Jeon | 777cad1 | 2021-08-13 08:15:33 +0900 | [diff] [blame] | 2444 | fattr->cf_acls = NULL; |
Namjae Jeon | 3d47e54 | 2021-04-20 14:25:35 +0900 | [diff] [blame] | 2445 | fattr->cf_dacls = NULL; |
| 2446 | |
Namjae Jeon | 777cad1 | 2021-08-13 08:15:33 +0900 | [diff] [blame] | 2447 | if (IS_ENABLED(CONFIG_FS_POSIX_ACL)) { |
| 2448 | fattr->cf_acls = get_acl(inode, ACL_TYPE_ACCESS); |
| 2449 | if (S_ISDIR(inode->i_mode)) |
| 2450 | fattr->cf_dacls = get_acl(inode, ACL_TYPE_DEFAULT); |
| 2451 | } |
Namjae Jeon | 3d47e54 | 2021-04-20 14:25:35 +0900 | [diff] [blame] | 2452 | } |
| 2453 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2454 | /** |
| 2455 | * smb2_open() - handler for smb file open request |
| 2456 | * @work: smb work containing request buffer |
| 2457 | * |
| 2458 | * Return: 0 on success, otherwise error |
| 2459 | */ |
| 2460 | int smb2_open(struct ksmbd_work *work) |
| 2461 | { |
| 2462 | struct ksmbd_conn *conn = work->conn; |
| 2463 | struct ksmbd_session *sess = work->sess; |
| 2464 | struct ksmbd_tree_connect *tcon = work->tcon; |
| 2465 | struct smb2_create_req *req; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 2466 | struct smb2_create_rsp *rsp; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2467 | struct path path; |
| 2468 | struct ksmbd_share_config *share = tcon->share_conf; |
| 2469 | struct ksmbd_file *fp = NULL; |
| 2470 | struct file *filp = NULL; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2471 | struct user_namespace *user_ns = NULL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2472 | struct kstat stat; |
| 2473 | struct create_context *context; |
| 2474 | struct lease_ctx_info *lc = NULL; |
| 2475 | struct create_ea_buf_req *ea_buf = NULL; |
| 2476 | struct oplock_info *opinfo; |
| 2477 | __le32 *next_ptr = NULL; |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 2478 | 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] | 2479 | int rc = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2480 | int contxt_cnt = 0, query_disk_id = 0; |
| 2481 | int maximal_access_ctxt = 0, posix_ctxt = 0; |
| 2482 | int s_type = 0; |
| 2483 | int next_off = 0; |
| 2484 | char *name = NULL; |
| 2485 | char *stream_name = NULL; |
| 2486 | bool file_present = false, created = false, already_permitted = false; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2487 | int share_ret, need_truncate = 0; |
| 2488 | u64 time; |
| 2489 | umode_t posix_mode = 0; |
| 2490 | __le32 daccess, maximal_access = 0; |
| 2491 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2492 | WORK_BUFFERS(work, req, rsp); |
| 2493 | |
| 2494 | if (req->hdr.NextCommand && !work->next_smb2_rcv_hdr_off && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2495 | (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2496 | ksmbd_debug(SMB, "invalid flag in chained command\n"); |
| 2497 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 2498 | smb2_set_err_rsp(work); |
| 2499 | return -EINVAL; |
| 2500 | } |
| 2501 | |
| 2502 | if (test_share_config_flag(share, KSMBD_SHARE_FLAG_PIPE)) { |
| 2503 | ksmbd_debug(SMB, "IPC pipe create request\n"); |
| 2504 | return create_smb2_pipe(work); |
| 2505 | } |
| 2506 | |
| 2507 | if (req->NameLength) { |
| 2508 | if ((req->CreateOptions & FILE_DIRECTORY_FILE_LE) && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2509 | *(char *)req->Buffer == '\\') { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2510 | pr_err("not allow directory name included leading slash\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2511 | rc = -EINVAL; |
| 2512 | goto err_out1; |
| 2513 | } |
| 2514 | |
| 2515 | name = smb2_get_name(share, |
| 2516 | req->Buffer, |
| 2517 | le16_to_cpu(req->NameLength), |
| 2518 | work->conn->local_nls); |
| 2519 | if (IS_ERR(name)) { |
| 2520 | rc = PTR_ERR(name); |
| 2521 | if (rc != -ENOMEM) |
| 2522 | rc = -ENOENT; |
Dan Carpenter | 8b99f35 | 2021-08-02 08:14:03 +0900 | [diff] [blame] | 2523 | name = NULL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2524 | goto err_out1; |
| 2525 | } |
| 2526 | |
| 2527 | ksmbd_debug(SMB, "converted name = %s\n", name); |
| 2528 | if (strchr(name, ':')) { |
| 2529 | if (!test_share_config_flag(work->tcon->share_conf, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2530 | KSMBD_SHARE_FLAG_STREAMS)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2531 | rc = -EBADF; |
| 2532 | goto err_out1; |
| 2533 | } |
| 2534 | rc = parse_stream_name(name, &stream_name, &s_type); |
| 2535 | if (rc < 0) |
| 2536 | goto err_out1; |
| 2537 | } |
| 2538 | |
| 2539 | rc = ksmbd_validate_filename(name); |
| 2540 | if (rc < 0) |
| 2541 | goto err_out1; |
| 2542 | |
| 2543 | if (ksmbd_share_veto_filename(share, name)) { |
| 2544 | rc = -ENOENT; |
| 2545 | ksmbd_debug(SMB, "Reject open(), vetoed file: %s\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2546 | name); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2547 | goto err_out1; |
| 2548 | } |
| 2549 | } else { |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 2550 | name = kstrdup("", GFP_KERNEL); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2551 | if (!name) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2552 | rc = -ENOMEM; |
| 2553 | goto err_out1; |
| 2554 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2555 | } |
| 2556 | |
| 2557 | req_op_level = req->RequestedOplockLevel; |
Namjae Jeon | 73f9dad | 2021-04-16 14:12:06 +0900 | [diff] [blame] | 2558 | if (req_op_level == SMB2_OPLOCK_LEVEL_LEASE) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2559 | lc = parse_lease_state(req); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2560 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2561 | if (le32_to_cpu(req->ImpersonationLevel) > le32_to_cpu(IL_DELEGATE_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2562 | pr_err("Invalid impersonationlevel : 0x%x\n", |
| 2563 | le32_to_cpu(req->ImpersonationLevel)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2564 | rc = -EIO; |
| 2565 | rsp->hdr.Status = STATUS_BAD_IMPERSONATION_LEVEL; |
| 2566 | goto err_out1; |
| 2567 | } |
| 2568 | |
| 2569 | if (req->CreateOptions && !(req->CreateOptions & CREATE_OPTIONS_MASK)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2570 | pr_err("Invalid create options : 0x%x\n", |
| 2571 | le32_to_cpu(req->CreateOptions)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2572 | rc = -EINVAL; |
| 2573 | goto err_out1; |
| 2574 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2575 | if (req->CreateOptions & FILE_SEQUENTIAL_ONLY_LE && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2576 | req->CreateOptions & FILE_RANDOM_ACCESS_LE) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2577 | req->CreateOptions = ~(FILE_SEQUENTIAL_ONLY_LE); |
| 2578 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2579 | if (req->CreateOptions & |
| 2580 | (FILE_OPEN_BY_FILE_ID_LE | CREATE_TREE_CONNECTION | |
| 2581 | FILE_RESERVE_OPFILTER_LE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2582 | rc = -EOPNOTSUPP; |
| 2583 | goto err_out1; |
| 2584 | } |
| 2585 | |
| 2586 | if (req->CreateOptions & FILE_DIRECTORY_FILE_LE) { |
| 2587 | if (req->CreateOptions & FILE_NON_DIRECTORY_FILE_LE) { |
| 2588 | rc = -EINVAL; |
| 2589 | goto err_out1; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2590 | } else if (req->CreateOptions & FILE_NO_COMPRESSION_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2591 | req->CreateOptions = ~(FILE_NO_COMPRESSION_LE); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2592 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2593 | } |
| 2594 | } |
| 2595 | |
| 2596 | if (le32_to_cpu(req->CreateDisposition) > |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2597 | le32_to_cpu(FILE_OVERWRITE_IF_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2598 | pr_err("Invalid create disposition : 0x%x\n", |
| 2599 | le32_to_cpu(req->CreateDisposition)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2600 | rc = -EINVAL; |
| 2601 | goto err_out1; |
| 2602 | } |
| 2603 | |
| 2604 | if (!(req->DesiredAccess & DESIRED_ACCESS_MASK)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2605 | pr_err("Invalid desired access : 0x%x\n", |
| 2606 | le32_to_cpu(req->DesiredAccess)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2607 | rc = -EACCES; |
| 2608 | goto err_out1; |
| 2609 | } |
| 2610 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2611 | if (req->FileAttributes && !(req->FileAttributes & ATTR_MASK_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2612 | pr_err("Invalid file attribute : 0x%x\n", |
| 2613 | le32_to_cpu(req->FileAttributes)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2614 | rc = -EINVAL; |
| 2615 | goto err_out1; |
| 2616 | } |
| 2617 | |
| 2618 | if (req->CreateContextsOffset) { |
| 2619 | /* Parse non-durable handle create contexts */ |
| 2620 | context = smb2_find_context_vals(req, SMB2_CREATE_EA_BUFFER); |
Namjae Jeon | f19b396 | 2021-07-13 09:59:34 +0900 | [diff] [blame] | 2621 | if (IS_ERR(context)) { |
| 2622 | rc = PTR_ERR(context); |
| 2623 | goto err_out1; |
| 2624 | } else if (context) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2625 | ea_buf = (struct create_ea_buf_req *)context; |
Hyunchul Lee | 8f77150 | 2021-09-24 22:22:22 +0900 | [diff] [blame] | 2626 | if (le16_to_cpu(context->DataOffset) + |
| 2627 | le32_to_cpu(context->DataLength) < |
| 2628 | sizeof(struct create_ea_buf_req)) { |
| 2629 | rc = -EINVAL; |
| 2630 | goto err_out1; |
| 2631 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2632 | if (req->CreateOptions & FILE_NO_EA_KNOWLEDGE_LE) { |
| 2633 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 2634 | rc = -EACCES; |
| 2635 | goto err_out1; |
| 2636 | } |
| 2637 | } |
| 2638 | |
| 2639 | context = smb2_find_context_vals(req, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2640 | SMB2_CREATE_QUERY_MAXIMAL_ACCESS_REQUEST); |
Namjae Jeon | f19b396 | 2021-07-13 09:59:34 +0900 | [diff] [blame] | 2641 | if (IS_ERR(context)) { |
| 2642 | rc = PTR_ERR(context); |
| 2643 | goto err_out1; |
| 2644 | } else if (context) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2645 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2646 | "get query maximal access context\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2647 | maximal_access_ctxt = 1; |
| 2648 | } |
| 2649 | |
| 2650 | context = smb2_find_context_vals(req, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2651 | SMB2_CREATE_TIMEWARP_REQUEST); |
Namjae Jeon | f19b396 | 2021-07-13 09:59:34 +0900 | [diff] [blame] | 2652 | if (IS_ERR(context)) { |
| 2653 | rc = PTR_ERR(context); |
| 2654 | goto err_out1; |
| 2655 | } else if (context) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2656 | ksmbd_debug(SMB, "get timewarp context\n"); |
| 2657 | rc = -EBADF; |
| 2658 | goto err_out1; |
| 2659 | } |
| 2660 | |
| 2661 | if (tcon->posix_extensions) { |
| 2662 | context = smb2_find_context_vals(req, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2663 | SMB2_CREATE_TAG_POSIX); |
Namjae Jeon | f19b396 | 2021-07-13 09:59:34 +0900 | [diff] [blame] | 2664 | if (IS_ERR(context)) { |
| 2665 | rc = PTR_ERR(context); |
| 2666 | goto err_out1; |
| 2667 | } else if (context) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2668 | struct create_posix *posix = |
| 2669 | (struct create_posix *)context; |
Hyunchul Lee | 8f77150 | 2021-09-24 22:22:22 +0900 | [diff] [blame] | 2670 | if (le16_to_cpu(context->DataOffset) + |
| 2671 | le32_to_cpu(context->DataLength) < |
| 2672 | sizeof(struct create_posix)) { |
| 2673 | rc = -EINVAL; |
| 2674 | goto err_out1; |
| 2675 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2676 | ksmbd_debug(SMB, "get posix context\n"); |
| 2677 | |
| 2678 | posix_mode = le32_to_cpu(posix->Mode); |
| 2679 | posix_ctxt = 1; |
| 2680 | } |
| 2681 | } |
| 2682 | } |
| 2683 | |
| 2684 | if (ksmbd_override_fsids(work)) { |
| 2685 | rc = -ENOMEM; |
| 2686 | goto err_out1; |
| 2687 | } |
| 2688 | |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 2689 | rc = ksmbd_vfs_kern_path(work, name, LOOKUP_NO_SYMLINKS, &path, 1); |
Namjae Jeon | 4ea4779 | 2021-09-21 14:19:33 +0900 | [diff] [blame] | 2690 | if (!rc) { |
| 2691 | if (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2692 | /* |
| 2693 | * If file exists with under flags, return access |
| 2694 | * denied error. |
| 2695 | */ |
| 2696 | if (req->CreateDisposition == FILE_OVERWRITE_IF_LE || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2697 | req->CreateDisposition == FILE_OPEN_IF_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2698 | rc = -EACCES; |
| 2699 | path_put(&path); |
| 2700 | goto err_out; |
| 2701 | } |
| 2702 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2703 | if (!test_tree_conn_flag(tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2704 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2705 | "User does not have write permission\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2706 | rc = -EACCES; |
| 2707 | path_put(&path); |
| 2708 | goto err_out; |
| 2709 | } |
Namjae Jeon | 4ea4779 | 2021-09-21 14:19:33 +0900 | [diff] [blame] | 2710 | } else if (d_is_symlink(path.dentry)) { |
| 2711 | rc = -EACCES; |
| 2712 | path_put(&path); |
| 2713 | goto err_out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2714 | } |
| 2715 | } |
| 2716 | |
| 2717 | if (rc) { |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 2718 | if (rc != -ENOENT) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2719 | goto err_out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2720 | 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] | 2721 | name, rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2722 | rc = 0; |
| 2723 | } else { |
| 2724 | file_present = true; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2725 | user_ns = mnt_user_ns(path.mnt); |
| 2726 | generic_fillattr(user_ns, d_inode(path.dentry), &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2727 | } |
| 2728 | if (stream_name) { |
| 2729 | if (req->CreateOptions & FILE_DIRECTORY_FILE_LE) { |
| 2730 | if (s_type == DATA_STREAM) { |
| 2731 | rc = -EIO; |
| 2732 | rsp->hdr.Status = STATUS_NOT_A_DIRECTORY; |
| 2733 | } |
| 2734 | } else { |
| 2735 | if (S_ISDIR(stat.mode) && s_type == DATA_STREAM) { |
| 2736 | rc = -EIO; |
| 2737 | rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY; |
| 2738 | } |
| 2739 | } |
| 2740 | |
| 2741 | if (req->CreateOptions & FILE_DIRECTORY_FILE_LE && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2742 | req->FileAttributes & ATTR_NORMAL_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2743 | rsp->hdr.Status = STATUS_NOT_A_DIRECTORY; |
| 2744 | rc = -EIO; |
| 2745 | } |
| 2746 | |
| 2747 | if (rc < 0) |
| 2748 | goto err_out; |
| 2749 | } |
| 2750 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2751 | if (file_present && req->CreateOptions & FILE_NON_DIRECTORY_FILE_LE && |
| 2752 | S_ISDIR(stat.mode) && !(req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2753 | ksmbd_debug(SMB, "open() argument is a directory: %s, %x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2754 | name, req->CreateOptions); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2755 | rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY; |
| 2756 | rc = -EIO; |
| 2757 | goto err_out; |
| 2758 | } |
| 2759 | |
| 2760 | if (file_present && (req->CreateOptions & FILE_DIRECTORY_FILE_LE) && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2761 | !(req->CreateDisposition == FILE_CREATE_LE) && |
| 2762 | !S_ISDIR(stat.mode)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2763 | rsp->hdr.Status = STATUS_NOT_A_DIRECTORY; |
| 2764 | rc = -EIO; |
| 2765 | goto err_out; |
| 2766 | } |
| 2767 | |
| 2768 | if (!stream_name && file_present && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2769 | req->CreateDisposition == FILE_CREATE_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2770 | rc = -EEXIST; |
| 2771 | goto err_out; |
| 2772 | } |
| 2773 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2774 | daccess = smb_map_generic_desired_access(req->DesiredAccess); |
| 2775 | |
| 2776 | if (file_present && !(req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) { |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 2777 | rc = smb_check_perm_dacl(conn, &path, &daccess, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2778 | sess->user->uid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2779 | if (rc) |
| 2780 | goto err_out; |
| 2781 | } |
| 2782 | |
| 2783 | if (daccess & FILE_MAXIMAL_ACCESS_LE) { |
| 2784 | if (!file_present) { |
| 2785 | daccess = cpu_to_le32(GENERIC_ALL_FLAGS); |
| 2786 | } else { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2787 | rc = ksmbd_vfs_query_maximal_access(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2788 | path.dentry, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2789 | &daccess); |
| 2790 | if (rc) |
| 2791 | goto err_out; |
| 2792 | already_permitted = true; |
| 2793 | } |
| 2794 | maximal_access = daccess; |
| 2795 | } |
| 2796 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2797 | open_flags = smb2_create_open_flags(file_present, daccess, |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 2798 | req->CreateDisposition, |
| 2799 | &may_flags); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2800 | |
| 2801 | if (!test_tree_conn_flag(tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
| 2802 | if (open_flags & O_CREAT) { |
| 2803 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2804 | "User does not have write permission\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2805 | rc = -EACCES; |
| 2806 | goto err_out; |
| 2807 | } |
| 2808 | } |
| 2809 | |
| 2810 | /*create file if not present */ |
| 2811 | if (!file_present) { |
| 2812 | rc = smb2_creat(work, &path, name, open_flags, posix_mode, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2813 | req->CreateOptions & FILE_DIRECTORY_FILE_LE); |
Marios Makassikis | d337a44 | 2021-07-27 09:24:51 +0900 | [diff] [blame] | 2814 | if (rc) { |
| 2815 | if (rc == -ENOENT) { |
| 2816 | rc = -EIO; |
| 2817 | rsp->hdr.Status = STATUS_OBJECT_PATH_NOT_FOUND; |
| 2818 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2819 | goto err_out; |
Marios Makassikis | d337a44 | 2021-07-27 09:24:51 +0900 | [diff] [blame] | 2820 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2821 | |
| 2822 | created = true; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2823 | user_ns = mnt_user_ns(path.mnt); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2824 | if (ea_buf) { |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 2825 | if (le32_to_cpu(ea_buf->ccontext.DataLength) < |
| 2826 | sizeof(struct smb2_ea_info)) { |
| 2827 | rc = -EINVAL; |
| 2828 | goto err_out; |
| 2829 | } |
| 2830 | |
| 2831 | rc = smb2_set_ea(&ea_buf->ea, |
| 2832 | le32_to_cpu(ea_buf->ccontext.DataLength), |
| 2833 | &path); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2834 | if (rc == -EOPNOTSUPP) |
| 2835 | rc = 0; |
| 2836 | else if (rc) |
| 2837 | goto err_out; |
| 2838 | } |
| 2839 | } else if (!already_permitted) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2840 | /* FILE_READ_ATTRIBUTE is allowed without inode_permission, |
| 2841 | * because execute(search) permission on a parent directory, |
| 2842 | * is already granted. |
| 2843 | */ |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2844 | if (daccess & ~(FILE_READ_ATTRIBUTES_LE | FILE_READ_CONTROL_LE)) { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2845 | rc = inode_permission(user_ns, |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 2846 | d_inode(path.dentry), |
| 2847 | may_flags); |
Namjae Jeon | ff1d572 | 2021-04-13 13:18:10 +0900 | [diff] [blame] | 2848 | if (rc) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2849 | goto err_out; |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 2850 | |
| 2851 | if ((daccess & FILE_DELETE_LE) || |
| 2852 | (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2853 | rc = ksmbd_vfs_may_delete(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2854 | path.dentry); |
Hyunchul Lee | 6c5e36d | 2021-06-23 13:48:24 +0900 | [diff] [blame] | 2855 | if (rc) |
| 2856 | goto err_out; |
| 2857 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2858 | } |
| 2859 | } |
| 2860 | |
| 2861 | rc = ksmbd_query_inode_status(d_inode(path.dentry->d_parent)); |
| 2862 | if (rc == KSMBD_INODE_STATUS_PENDING_DELETE) { |
| 2863 | rc = -EBUSY; |
| 2864 | goto err_out; |
| 2865 | } |
| 2866 | |
| 2867 | rc = 0; |
| 2868 | filp = dentry_open(&path, open_flags, current_cred()); |
| 2869 | if (IS_ERR(filp)) { |
| 2870 | rc = PTR_ERR(filp); |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2871 | pr_err("dentry open for dir failed, rc %d\n", rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2872 | goto err_out; |
| 2873 | } |
| 2874 | |
| 2875 | if (file_present) { |
| 2876 | if (!(open_flags & O_TRUNC)) |
| 2877 | file_info = FILE_OPENED; |
| 2878 | else |
| 2879 | file_info = FILE_OVERWRITTEN; |
| 2880 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2881 | if ((req->CreateDisposition & FILE_CREATE_MASK_LE) == |
| 2882 | FILE_SUPERSEDE_LE) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2883 | file_info = FILE_SUPERSEDED; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2884 | } else if (open_flags & O_CREAT) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2885 | file_info = FILE_CREATED; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2886 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2887 | |
| 2888 | ksmbd_vfs_set_fadvise(filp, req->CreateOptions); |
| 2889 | |
| 2890 | /* Obtain Volatile-ID */ |
| 2891 | fp = ksmbd_open_fd(work, filp); |
| 2892 | if (IS_ERR(fp)) { |
| 2893 | fput(filp); |
| 2894 | rc = PTR_ERR(fp); |
| 2895 | fp = NULL; |
| 2896 | goto err_out; |
| 2897 | } |
| 2898 | |
| 2899 | /* Get Persistent-ID */ |
| 2900 | ksmbd_open_durable_fd(fp); |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 2901 | if (!has_file_id(fp->persistent_id)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2902 | rc = -ENOMEM; |
| 2903 | goto err_out; |
| 2904 | } |
| 2905 | |
| 2906 | fp->filename = name; |
| 2907 | fp->cdoption = req->CreateDisposition; |
| 2908 | fp->daccess = daccess; |
| 2909 | fp->saccess = req->ShareAccess; |
| 2910 | fp->coption = req->CreateOptions; |
| 2911 | |
| 2912 | /* Set default windows and posix acls if creating new file */ |
| 2913 | if (created) { |
| 2914 | int posix_acl_rc; |
Namjae Jeon | fba08fa | 2021-04-15 10:29:39 +0900 | [diff] [blame] | 2915 | struct inode *inode = d_inode(path.dentry); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2916 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2917 | posix_acl_rc = ksmbd_vfs_inherit_posix_acl(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2918 | inode, |
| 2919 | d_inode(path.dentry->d_parent)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2920 | if (posix_acl_rc) |
| 2921 | ksmbd_debug(SMB, "inherit posix acl failed : %d\n", posix_acl_rc); |
| 2922 | |
| 2923 | if (test_share_config_flag(work->tcon->share_conf, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2924 | KSMBD_SHARE_FLAG_ACL_XATTR)) { |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 2925 | rc = smb_inherit_dacl(conn, &path, sess->user->uid, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2926 | sess->user->gid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2927 | } |
| 2928 | |
| 2929 | if (rc) { |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 2930 | rc = smb2_create_sd_buffer(work, req, &path); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2931 | if (rc) { |
| 2932 | if (posix_acl_rc) |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2933 | ksmbd_vfs_set_init_posix_acl(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2934 | inode); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2935 | |
| 2936 | if (test_share_config_flag(work->tcon->share_conf, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2937 | KSMBD_SHARE_FLAG_ACL_XATTR)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2938 | struct smb_fattr fattr; |
| 2939 | struct smb_ntsd *pntsd; |
Namjae Jeon | 3d47e54 | 2021-04-20 14:25:35 +0900 | [diff] [blame] | 2940 | int pntsd_size, ace_num = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2941 | |
Christian Brauner | 43205ca | 2021-08-23 17:13:50 +0200 | [diff] [blame] | 2942 | ksmbd_acls_fattr(&fattr, user_ns, inode); |
Marios Makassikis | e6b1059 | 2021-04-15 10:24:56 +0900 | [diff] [blame] | 2943 | if (fattr.cf_acls) |
| 2944 | ace_num = fattr.cf_acls->a_count; |
Namjae Jeon | 3d47e54 | 2021-04-20 14:25:35 +0900 | [diff] [blame] | 2945 | if (fattr.cf_dacls) |
| 2946 | ace_num += fattr.cf_dacls->a_count; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2947 | |
| 2948 | pntsd = kmalloc(sizeof(struct smb_ntsd) + |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2949 | sizeof(struct smb_sid) * 3 + |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2950 | sizeof(struct smb_acl) + |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2951 | sizeof(struct smb_ace) * ace_num * 2, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2952 | GFP_KERNEL); |
| 2953 | if (!pntsd) |
| 2954 | goto err_out; |
| 2955 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2956 | rc = build_sec_desc(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2957 | pntsd, NULL, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2958 | OWNER_SECINFO | |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 2959 | GROUP_SECINFO | |
| 2960 | DACL_SECINFO, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2961 | &pntsd_size, &fattr); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2962 | posix_acl_release(fattr.cf_acls); |
| 2963 | posix_acl_release(fattr.cf_dacls); |
| 2964 | |
| 2965 | rc = ksmbd_vfs_set_sd_xattr(conn, |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 2966 | user_ns, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 2967 | path.dentry, |
| 2968 | pntsd, |
| 2969 | pntsd_size); |
Namjae Jeon | 3d47e54 | 2021-04-20 14:25:35 +0900 | [diff] [blame] | 2970 | kfree(pntsd); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2971 | if (rc) |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 2972 | pr_err("failed to store ntacl in xattr : %d\n", |
| 2973 | rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2974 | } |
| 2975 | } |
| 2976 | } |
| 2977 | rc = 0; |
| 2978 | } |
| 2979 | |
| 2980 | if (stream_name) { |
| 2981 | rc = smb2_set_stream_name_xattr(&path, |
| 2982 | fp, |
| 2983 | stream_name, |
| 2984 | s_type); |
| 2985 | if (rc) |
| 2986 | goto err_out; |
| 2987 | file_info = FILE_CREATED; |
| 2988 | } |
| 2989 | |
| 2990 | fp->attrib_only = !(req->DesiredAccess & ~(FILE_READ_ATTRIBUTES_LE | |
| 2991 | FILE_WRITE_ATTRIBUTES_LE | FILE_SYNCHRONIZE_LE)); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 2992 | if (!S_ISDIR(file_inode(filp)->i_mode) && open_flags & O_TRUNC && |
| 2993 | !fp->attrib_only && !stream_name) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 2994 | smb_break_all_oplock(work, fp); |
| 2995 | need_truncate = 1; |
| 2996 | } |
| 2997 | |
| 2998 | /* fp should be searchable through ksmbd_inode.m_fp_list |
| 2999 | * after daccess, saccess, attrib_only, and stream are |
| 3000 | * initialized. |
| 3001 | */ |
| 3002 | write_lock(&fp->f_ci->m_lock); |
| 3003 | list_add(&fp->node, &fp->f_ci->m_fp_list); |
| 3004 | write_unlock(&fp->f_ci->m_lock); |
| 3005 | |
| 3006 | rc = ksmbd_vfs_getattr(&path, &stat); |
| 3007 | if (rc) { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 3008 | generic_fillattr(user_ns, d_inode(path.dentry), &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3009 | rc = 0; |
| 3010 | } |
| 3011 | |
| 3012 | /* Check delete pending among previous fp before oplock break */ |
| 3013 | if (ksmbd_inode_pending_delete(fp)) { |
| 3014 | rc = -EBUSY; |
| 3015 | goto err_out; |
| 3016 | } |
| 3017 | |
| 3018 | share_ret = ksmbd_smb_check_shared_mode(fp->filp, fp); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3019 | if (!test_share_config_flag(work->tcon->share_conf, KSMBD_SHARE_FLAG_OPLOCKS) || |
| 3020 | (req_op_level == SMB2_OPLOCK_LEVEL_LEASE && |
| 3021 | !(conn->vals->capabilities & SMB2_GLOBAL_CAP_LEASING))) { |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 3022 | if (share_ret < 0 && !S_ISDIR(file_inode(fp->filp)->i_mode)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3023 | rc = share_ret; |
| 3024 | goto err_out; |
| 3025 | } |
| 3026 | } else { |
| 3027 | if (req_op_level == SMB2_OPLOCK_LEVEL_LEASE) { |
| 3028 | req_op_level = smb2_map_lease_to_oplock(lc->req_state); |
| 3029 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3030 | "lease req for(%s) req oplock state 0x%x, lease state 0x%x\n", |
| 3031 | name, req_op_level, lc->req_state); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3032 | rc = find_same_lease_key(sess, fp->f_ci, lc); |
| 3033 | if (rc) |
| 3034 | goto err_out; |
| 3035 | } else if (open_flags == O_RDONLY && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3036 | (req_op_level == SMB2_OPLOCK_LEVEL_BATCH || |
| 3037 | req_op_level == SMB2_OPLOCK_LEVEL_EXCLUSIVE)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3038 | req_op_level = SMB2_OPLOCK_LEVEL_II; |
| 3039 | |
| 3040 | rc = smb_grant_oplock(work, req_op_level, |
| 3041 | fp->persistent_id, fp, |
| 3042 | le32_to_cpu(req->hdr.Id.SyncId.TreeId), |
| 3043 | lc, share_ret); |
| 3044 | if (rc < 0) |
| 3045 | goto err_out; |
| 3046 | } |
| 3047 | |
| 3048 | if (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE) |
| 3049 | ksmbd_fd_set_delete_on_close(fp, file_info); |
| 3050 | |
| 3051 | if (need_truncate) { |
| 3052 | rc = smb2_create_truncate(&path); |
| 3053 | if (rc) |
| 3054 | goto err_out; |
| 3055 | } |
| 3056 | |
| 3057 | if (req->CreateContextsOffset) { |
| 3058 | struct create_alloc_size_req *az_req; |
| 3059 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3060 | az_req = (struct create_alloc_size_req *)smb2_find_context_vals(req, |
| 3061 | SMB2_CREATE_ALLOCATION_SIZE); |
Namjae Jeon | f19b396 | 2021-07-13 09:59:34 +0900 | [diff] [blame] | 3062 | if (IS_ERR(az_req)) { |
| 3063 | rc = PTR_ERR(az_req); |
| 3064 | goto err_out; |
| 3065 | } else if (az_req) { |
Hyunchul Lee | 8f77150 | 2021-09-24 22:22:22 +0900 | [diff] [blame] | 3066 | loff_t alloc_size; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3067 | int err; |
| 3068 | |
Hyunchul Lee | 8f77150 | 2021-09-24 22:22:22 +0900 | [diff] [blame] | 3069 | if (le16_to_cpu(az_req->ccontext.DataOffset) + |
| 3070 | le32_to_cpu(az_req->ccontext.DataLength) < |
| 3071 | sizeof(struct create_alloc_size_req)) { |
| 3072 | rc = -EINVAL; |
| 3073 | goto err_out; |
| 3074 | } |
| 3075 | alloc_size = le64_to_cpu(az_req->AllocationSize); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3076 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3077 | "request smb2 create allocate size : %llu\n", |
| 3078 | alloc_size); |
Namjae Jeon | e8c0619 | 2021-06-22 11:06:11 +0900 | [diff] [blame] | 3079 | smb_break_all_levII_oplock(work, fp, 1); |
| 3080 | err = vfs_fallocate(fp->filp, FALLOC_FL_KEEP_SIZE, 0, |
| 3081 | alloc_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3082 | if (err < 0) |
| 3083 | ksmbd_debug(SMB, |
Namjae Jeon | e8c0619 | 2021-06-22 11:06:11 +0900 | [diff] [blame] | 3084 | "vfs_fallocate is failed : %d\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3085 | err); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3086 | } |
| 3087 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3088 | context = smb2_find_context_vals(req, SMB2_CREATE_QUERY_ON_DISK_ID); |
Namjae Jeon | f19b396 | 2021-07-13 09:59:34 +0900 | [diff] [blame] | 3089 | if (IS_ERR(context)) { |
| 3090 | rc = PTR_ERR(context); |
| 3091 | goto err_out; |
| 3092 | } else if (context) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3093 | ksmbd_debug(SMB, "get query on disk id context\n"); |
| 3094 | query_disk_id = 1; |
| 3095 | } |
| 3096 | } |
| 3097 | |
| 3098 | if (stat.result_mask & STATX_BTIME) |
| 3099 | fp->create_time = ksmbd_UnixTimeToNT(stat.btime); |
| 3100 | else |
| 3101 | fp->create_time = ksmbd_UnixTimeToNT(stat.ctime); |
| 3102 | if (req->FileAttributes || fp->f_ci->m_fattr == 0) |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3103 | fp->f_ci->m_fattr = |
| 3104 | 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] | 3105 | |
| 3106 | if (!created) |
| 3107 | smb2_update_xattrs(tcon, &path, fp); |
| 3108 | else |
| 3109 | smb2_new_xattrs(tcon, &path, fp); |
| 3110 | |
| 3111 | memcpy(fp->client_guid, conn->ClientGUID, SMB2_CLIENT_GUID_SIZE); |
| 3112 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 3113 | generic_fillattr(user_ns, file_inode(fp->filp), |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 3114 | &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3115 | |
| 3116 | rsp->StructureSize = cpu_to_le16(89); |
| 3117 | rcu_read_lock(); |
| 3118 | opinfo = rcu_dereference(fp->f_opinfo); |
| 3119 | rsp->OplockLevel = opinfo != NULL ? opinfo->level : 0; |
| 3120 | rcu_read_unlock(); |
| 3121 | rsp->Reserved = 0; |
| 3122 | rsp->CreateAction = cpu_to_le32(file_info); |
| 3123 | rsp->CreationTime = cpu_to_le64(fp->create_time); |
| 3124 | time = ksmbd_UnixTimeToNT(stat.atime); |
| 3125 | rsp->LastAccessTime = cpu_to_le64(time); |
| 3126 | time = ksmbd_UnixTimeToNT(stat.mtime); |
| 3127 | rsp->LastWriteTime = cpu_to_le64(time); |
| 3128 | time = ksmbd_UnixTimeToNT(stat.ctime); |
| 3129 | rsp->ChangeTime = cpu_to_le64(time); |
| 3130 | rsp->AllocationSize = S_ISDIR(stat.mode) ? 0 : |
| 3131 | cpu_to_le64(stat.blocks << 9); |
| 3132 | rsp->EndofFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size); |
| 3133 | rsp->FileAttributes = fp->f_ci->m_fattr; |
| 3134 | |
| 3135 | rsp->Reserved2 = 0; |
| 3136 | |
| 3137 | rsp->PersistentFileId = cpu_to_le64(fp->persistent_id); |
| 3138 | rsp->VolatileFileId = cpu_to_le64(fp->volatile_id); |
| 3139 | |
| 3140 | rsp->CreateContextsOffset = 0; |
| 3141 | rsp->CreateContextsLength = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 3142 | inc_rfc1001_len(work->response_buf, 88); /* StructureSize - 1*/ |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3143 | |
| 3144 | /* If lease is request send lease context response */ |
| 3145 | if (opinfo && opinfo->is_lease) { |
| 3146 | struct create_context *lease_ccontext; |
| 3147 | |
| 3148 | ksmbd_debug(SMB, "lease granted on(%s) lease state 0x%x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3149 | name, opinfo->o_lease->state); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3150 | rsp->OplockLevel = SMB2_OPLOCK_LEVEL_LEASE; |
| 3151 | |
| 3152 | lease_ccontext = (struct create_context *)rsp->Buffer; |
| 3153 | contxt_cnt++; |
| 3154 | create_lease_buf(rsp->Buffer, opinfo->o_lease); |
| 3155 | le32_add_cpu(&rsp->CreateContextsLength, |
| 3156 | conn->vals->create_lease_size); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 3157 | inc_rfc1001_len(work->response_buf, |
| 3158 | conn->vals->create_lease_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3159 | next_ptr = &lease_ccontext->Next; |
| 3160 | next_off = conn->vals->create_lease_size; |
| 3161 | } |
| 3162 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3163 | if (maximal_access_ctxt) { |
| 3164 | struct create_context *mxac_ccontext; |
| 3165 | |
| 3166 | if (maximal_access == 0) |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 3167 | ksmbd_vfs_query_maximal_access(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 3168 | path.dentry, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3169 | &maximal_access); |
| 3170 | mxac_ccontext = (struct create_context *)(rsp->Buffer + |
| 3171 | le32_to_cpu(rsp->CreateContextsLength)); |
| 3172 | contxt_cnt++; |
| 3173 | create_mxac_rsp_buf(rsp->Buffer + |
| 3174 | le32_to_cpu(rsp->CreateContextsLength), |
| 3175 | le32_to_cpu(maximal_access)); |
| 3176 | le32_add_cpu(&rsp->CreateContextsLength, |
| 3177 | conn->vals->create_mxac_size); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 3178 | inc_rfc1001_len(work->response_buf, |
| 3179 | conn->vals->create_mxac_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3180 | if (next_ptr) |
| 3181 | *next_ptr = cpu_to_le32(next_off); |
| 3182 | next_ptr = &mxac_ccontext->Next; |
| 3183 | next_off = conn->vals->create_mxac_size; |
| 3184 | } |
| 3185 | |
| 3186 | if (query_disk_id) { |
| 3187 | struct create_context *disk_id_ccontext; |
| 3188 | |
| 3189 | disk_id_ccontext = (struct create_context *)(rsp->Buffer + |
| 3190 | le32_to_cpu(rsp->CreateContextsLength)); |
| 3191 | contxt_cnt++; |
| 3192 | create_disk_id_rsp_buf(rsp->Buffer + |
| 3193 | le32_to_cpu(rsp->CreateContextsLength), |
| 3194 | stat.ino, tcon->id); |
| 3195 | le32_add_cpu(&rsp->CreateContextsLength, |
| 3196 | conn->vals->create_disk_id_size); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 3197 | inc_rfc1001_len(work->response_buf, |
| 3198 | conn->vals->create_disk_id_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3199 | if (next_ptr) |
| 3200 | *next_ptr = cpu_to_le32(next_off); |
| 3201 | next_ptr = &disk_id_ccontext->Next; |
| 3202 | next_off = conn->vals->create_disk_id_size; |
| 3203 | } |
| 3204 | |
| 3205 | if (posix_ctxt) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3206 | contxt_cnt++; |
| 3207 | create_posix_rsp_buf(rsp->Buffer + |
| 3208 | le32_to_cpu(rsp->CreateContextsLength), |
| 3209 | fp); |
| 3210 | le32_add_cpu(&rsp->CreateContextsLength, |
| 3211 | conn->vals->create_posix_size); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 3212 | inc_rfc1001_len(work->response_buf, |
| 3213 | conn->vals->create_posix_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3214 | if (next_ptr) |
| 3215 | *next_ptr = cpu_to_le32(next_off); |
| 3216 | } |
| 3217 | |
| 3218 | if (contxt_cnt > 0) { |
| 3219 | rsp->CreateContextsOffset = |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 3220 | cpu_to_le32(offsetof(struct smb2_create_rsp, Buffer)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3221 | } |
| 3222 | |
| 3223 | err_out: |
| 3224 | if (file_present || created) |
| 3225 | path_put(&path); |
| 3226 | ksmbd_revert_fsids(work); |
| 3227 | err_out1: |
| 3228 | if (rc) { |
| 3229 | if (rc == -EINVAL) |
| 3230 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 3231 | else if (rc == -EOPNOTSUPP) |
| 3232 | rsp->hdr.Status = STATUS_NOT_SUPPORTED; |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 3233 | else if (rc == -EACCES || rc == -ESTALE || rc == -EXDEV) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3234 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 3235 | else if (rc == -ENOENT) |
| 3236 | rsp->hdr.Status = STATUS_OBJECT_NAME_INVALID; |
| 3237 | else if (rc == -EPERM) |
| 3238 | rsp->hdr.Status = STATUS_SHARING_VIOLATION; |
| 3239 | else if (rc == -EBUSY) |
| 3240 | rsp->hdr.Status = STATUS_DELETE_PENDING; |
| 3241 | else if (rc == -EBADF) |
| 3242 | rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND; |
| 3243 | else if (rc == -ENOEXEC) |
| 3244 | rsp->hdr.Status = STATUS_DUPLICATE_OBJECTID; |
| 3245 | else if (rc == -ENXIO) |
| 3246 | rsp->hdr.Status = STATUS_NO_SUCH_DEVICE; |
| 3247 | else if (rc == -EEXIST) |
| 3248 | rsp->hdr.Status = STATUS_OBJECT_NAME_COLLISION; |
| 3249 | else if (rc == -EMFILE) |
| 3250 | rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES; |
| 3251 | if (!rsp->hdr.Status) |
| 3252 | rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR; |
| 3253 | |
| 3254 | if (!fp || !fp->filename) |
| 3255 | kfree(name); |
| 3256 | if (fp) |
| 3257 | ksmbd_fd_put(work, fp); |
| 3258 | smb2_set_err_rsp(work); |
| 3259 | ksmbd_debug(SMB, "Error response: %x\n", rsp->hdr.Status); |
| 3260 | } |
| 3261 | |
| 3262 | kfree(lc); |
| 3263 | |
| 3264 | return 0; |
| 3265 | } |
| 3266 | |
| 3267 | static int readdir_info_level_struct_sz(int info_level) |
| 3268 | { |
| 3269 | switch (info_level) { |
| 3270 | case FILE_FULL_DIRECTORY_INFORMATION: |
| 3271 | return sizeof(struct file_full_directory_info); |
| 3272 | case FILE_BOTH_DIRECTORY_INFORMATION: |
| 3273 | return sizeof(struct file_both_directory_info); |
| 3274 | case FILE_DIRECTORY_INFORMATION: |
| 3275 | return sizeof(struct file_directory_info); |
| 3276 | case FILE_NAMES_INFORMATION: |
| 3277 | return sizeof(struct file_names_info); |
| 3278 | case FILEID_FULL_DIRECTORY_INFORMATION: |
| 3279 | return sizeof(struct file_id_full_dir_info); |
| 3280 | case FILEID_BOTH_DIRECTORY_INFORMATION: |
| 3281 | return sizeof(struct file_id_both_directory_info); |
| 3282 | case SMB_FIND_FILE_POSIX_INFO: |
| 3283 | return sizeof(struct smb2_posix_info); |
| 3284 | default: |
| 3285 | return -EOPNOTSUPP; |
| 3286 | } |
| 3287 | } |
| 3288 | |
| 3289 | static int dentry_name(struct ksmbd_dir_info *d_info, int info_level) |
| 3290 | { |
| 3291 | switch (info_level) { |
| 3292 | case FILE_FULL_DIRECTORY_INFORMATION: |
| 3293 | { |
| 3294 | struct file_full_directory_info *ffdinfo; |
| 3295 | |
| 3296 | ffdinfo = (struct file_full_directory_info *)d_info->rptr; |
| 3297 | d_info->rptr += le32_to_cpu(ffdinfo->NextEntryOffset); |
| 3298 | d_info->name = ffdinfo->FileName; |
| 3299 | d_info->name_len = le32_to_cpu(ffdinfo->FileNameLength); |
| 3300 | return 0; |
| 3301 | } |
| 3302 | case FILE_BOTH_DIRECTORY_INFORMATION: |
| 3303 | { |
| 3304 | struct file_both_directory_info *fbdinfo; |
| 3305 | |
| 3306 | fbdinfo = (struct file_both_directory_info *)d_info->rptr; |
| 3307 | d_info->rptr += le32_to_cpu(fbdinfo->NextEntryOffset); |
| 3308 | d_info->name = fbdinfo->FileName; |
| 3309 | d_info->name_len = le32_to_cpu(fbdinfo->FileNameLength); |
| 3310 | return 0; |
| 3311 | } |
| 3312 | case FILE_DIRECTORY_INFORMATION: |
| 3313 | { |
| 3314 | struct file_directory_info *fdinfo; |
| 3315 | |
| 3316 | fdinfo = (struct file_directory_info *)d_info->rptr; |
| 3317 | d_info->rptr += le32_to_cpu(fdinfo->NextEntryOffset); |
| 3318 | d_info->name = fdinfo->FileName; |
| 3319 | d_info->name_len = le32_to_cpu(fdinfo->FileNameLength); |
| 3320 | return 0; |
| 3321 | } |
| 3322 | case FILE_NAMES_INFORMATION: |
| 3323 | { |
| 3324 | struct file_names_info *fninfo; |
| 3325 | |
| 3326 | fninfo = (struct file_names_info *)d_info->rptr; |
| 3327 | d_info->rptr += le32_to_cpu(fninfo->NextEntryOffset); |
| 3328 | d_info->name = fninfo->FileName; |
| 3329 | d_info->name_len = le32_to_cpu(fninfo->FileNameLength); |
| 3330 | return 0; |
| 3331 | } |
| 3332 | case FILEID_FULL_DIRECTORY_INFORMATION: |
| 3333 | { |
| 3334 | struct file_id_full_dir_info *dinfo; |
| 3335 | |
| 3336 | dinfo = (struct file_id_full_dir_info *)d_info->rptr; |
| 3337 | d_info->rptr += le32_to_cpu(dinfo->NextEntryOffset); |
| 3338 | d_info->name = dinfo->FileName; |
| 3339 | d_info->name_len = le32_to_cpu(dinfo->FileNameLength); |
| 3340 | return 0; |
| 3341 | } |
| 3342 | case FILEID_BOTH_DIRECTORY_INFORMATION: |
| 3343 | { |
| 3344 | struct file_id_both_directory_info *fibdinfo; |
| 3345 | |
| 3346 | fibdinfo = (struct file_id_both_directory_info *)d_info->rptr; |
| 3347 | d_info->rptr += le32_to_cpu(fibdinfo->NextEntryOffset); |
| 3348 | d_info->name = fibdinfo->FileName; |
| 3349 | d_info->name_len = le32_to_cpu(fibdinfo->FileNameLength); |
| 3350 | return 0; |
| 3351 | } |
| 3352 | case SMB_FIND_FILE_POSIX_INFO: |
| 3353 | { |
| 3354 | struct smb2_posix_info *posix_info; |
| 3355 | |
| 3356 | posix_info = (struct smb2_posix_info *)d_info->rptr; |
| 3357 | d_info->rptr += le32_to_cpu(posix_info->NextEntryOffset); |
| 3358 | d_info->name = posix_info->name; |
| 3359 | d_info->name_len = le32_to_cpu(posix_info->name_len); |
| 3360 | return 0; |
| 3361 | } |
| 3362 | default: |
| 3363 | return -EINVAL; |
| 3364 | } |
| 3365 | } |
| 3366 | |
| 3367 | /** |
| 3368 | * smb2_populate_readdir_entry() - encode directory entry in smb2 response |
| 3369 | * buffer |
| 3370 | * @conn: connection instance |
| 3371 | * @info_level: smb information level |
| 3372 | * @d_info: structure included variables for query dir |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 3373 | * @user_ns: user namespace |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3374 | * @ksmbd_kstat: ksmbd wrapper of dirent stat information |
| 3375 | * |
| 3376 | * if directory has many entries, find first can't read it fully. |
| 3377 | * find next might be called multiple times to read remaining dir entries |
| 3378 | * |
| 3379 | * Return: 0 on success, otherwise error |
| 3380 | */ |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3381 | 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] | 3382 | struct ksmbd_dir_info *d_info, |
| 3383 | struct ksmbd_kstat *ksmbd_kstat) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3384 | { |
| 3385 | int next_entry_offset = 0; |
| 3386 | char *conv_name; |
| 3387 | int conv_len; |
| 3388 | void *kstat; |
Namjae Jeon | dac0ec6 | 2021-07-07 14:57:24 +0900 | [diff] [blame] | 3389 | int struct_sz, rc = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3390 | |
| 3391 | conv_name = ksmbd_convert_dir_info_name(d_info, |
| 3392 | conn->local_nls, |
| 3393 | &conv_len); |
| 3394 | if (!conv_name) |
| 3395 | return -ENOMEM; |
| 3396 | |
| 3397 | /* Somehow the name has only terminating NULL bytes */ |
| 3398 | if (conv_len < 0) { |
Namjae Jeon | dac0ec6 | 2021-07-07 14:57:24 +0900 | [diff] [blame] | 3399 | rc = -EINVAL; |
| 3400 | goto free_conv_name; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3401 | } |
| 3402 | |
| 3403 | struct_sz = readdir_info_level_struct_sz(info_level); |
| 3404 | next_entry_offset = ALIGN(struct_sz - 1 + conv_len, |
| 3405 | KSMBD_DIR_INFO_ALIGNMENT); |
| 3406 | |
| 3407 | if (next_entry_offset > d_info->out_buf_len) { |
| 3408 | d_info->out_buf_len = 0; |
Namjae Jeon | dac0ec6 | 2021-07-07 14:57:24 +0900 | [diff] [blame] | 3409 | rc = -ENOSPC; |
| 3410 | goto free_conv_name; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3411 | } |
| 3412 | |
| 3413 | kstat = d_info->wptr; |
| 3414 | if (info_level != FILE_NAMES_INFORMATION) |
| 3415 | kstat = ksmbd_vfs_init_kstat(&d_info->wptr, ksmbd_kstat); |
| 3416 | |
| 3417 | switch (info_level) { |
| 3418 | case FILE_FULL_DIRECTORY_INFORMATION: |
| 3419 | { |
| 3420 | struct file_full_directory_info *ffdinfo; |
| 3421 | |
| 3422 | ffdinfo = (struct file_full_directory_info *)kstat; |
| 3423 | ffdinfo->FileNameLength = cpu_to_le32(conv_len); |
| 3424 | ffdinfo->EaSize = |
| 3425 | smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode); |
| 3426 | if (ffdinfo->EaSize) |
| 3427 | ffdinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE; |
| 3428 | if (d_info->hide_dot_file && d_info->name[0] == '.') |
| 3429 | ffdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE; |
| 3430 | memcpy(ffdinfo->FileName, conv_name, conv_len); |
| 3431 | ffdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3432 | break; |
| 3433 | } |
| 3434 | case FILE_BOTH_DIRECTORY_INFORMATION: |
| 3435 | { |
| 3436 | struct file_both_directory_info *fbdinfo; |
| 3437 | |
| 3438 | fbdinfo = (struct file_both_directory_info *)kstat; |
| 3439 | fbdinfo->FileNameLength = cpu_to_le32(conv_len); |
| 3440 | fbdinfo->EaSize = |
| 3441 | smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode); |
| 3442 | if (fbdinfo->EaSize) |
| 3443 | fbdinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE; |
| 3444 | fbdinfo->ShortNameLength = 0; |
| 3445 | fbdinfo->Reserved = 0; |
| 3446 | if (d_info->hide_dot_file && d_info->name[0] == '.') |
| 3447 | fbdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE; |
| 3448 | memcpy(fbdinfo->FileName, conv_name, conv_len); |
| 3449 | fbdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3450 | break; |
| 3451 | } |
| 3452 | case FILE_DIRECTORY_INFORMATION: |
| 3453 | { |
| 3454 | struct file_directory_info *fdinfo; |
| 3455 | |
| 3456 | fdinfo = (struct file_directory_info *)kstat; |
| 3457 | fdinfo->FileNameLength = cpu_to_le32(conv_len); |
| 3458 | if (d_info->hide_dot_file && d_info->name[0] == '.') |
| 3459 | fdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE; |
| 3460 | memcpy(fdinfo->FileName, conv_name, conv_len); |
| 3461 | fdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3462 | break; |
| 3463 | } |
| 3464 | case FILE_NAMES_INFORMATION: |
| 3465 | { |
| 3466 | struct file_names_info *fninfo; |
| 3467 | |
| 3468 | fninfo = (struct file_names_info *)kstat; |
| 3469 | fninfo->FileNameLength = cpu_to_le32(conv_len); |
| 3470 | memcpy(fninfo->FileName, conv_name, conv_len); |
| 3471 | fninfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3472 | break; |
| 3473 | } |
| 3474 | case FILEID_FULL_DIRECTORY_INFORMATION: |
| 3475 | { |
| 3476 | struct file_id_full_dir_info *dinfo; |
| 3477 | |
| 3478 | dinfo = (struct file_id_full_dir_info *)kstat; |
| 3479 | dinfo->FileNameLength = cpu_to_le32(conv_len); |
| 3480 | dinfo->EaSize = |
| 3481 | smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode); |
| 3482 | if (dinfo->EaSize) |
| 3483 | dinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE; |
| 3484 | dinfo->Reserved = 0; |
| 3485 | dinfo->UniqueId = cpu_to_le64(ksmbd_kstat->kstat->ino); |
| 3486 | if (d_info->hide_dot_file && d_info->name[0] == '.') |
| 3487 | dinfo->ExtFileAttributes |= ATTR_HIDDEN_LE; |
| 3488 | memcpy(dinfo->FileName, conv_name, conv_len); |
| 3489 | dinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3490 | break; |
| 3491 | } |
| 3492 | case FILEID_BOTH_DIRECTORY_INFORMATION: |
| 3493 | { |
| 3494 | struct file_id_both_directory_info *fibdinfo; |
| 3495 | |
| 3496 | fibdinfo = (struct file_id_both_directory_info *)kstat; |
| 3497 | fibdinfo->FileNameLength = cpu_to_le32(conv_len); |
| 3498 | fibdinfo->EaSize = |
| 3499 | smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode); |
| 3500 | if (fibdinfo->EaSize) |
| 3501 | fibdinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE; |
| 3502 | fibdinfo->UniqueId = cpu_to_le64(ksmbd_kstat->kstat->ino); |
| 3503 | fibdinfo->ShortNameLength = 0; |
| 3504 | fibdinfo->Reserved = 0; |
| 3505 | fibdinfo->Reserved2 = cpu_to_le16(0); |
| 3506 | if (d_info->hide_dot_file && d_info->name[0] == '.') |
| 3507 | fibdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE; |
| 3508 | memcpy(fibdinfo->FileName, conv_name, conv_len); |
| 3509 | fibdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3510 | break; |
| 3511 | } |
| 3512 | case SMB_FIND_FILE_POSIX_INFO: |
| 3513 | { |
| 3514 | struct smb2_posix_info *posix_info; |
| 3515 | u64 time; |
| 3516 | |
| 3517 | posix_info = (struct smb2_posix_info *)kstat; |
| 3518 | posix_info->Ignored = 0; |
| 3519 | posix_info->CreationTime = cpu_to_le64(ksmbd_kstat->create_time); |
| 3520 | time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->ctime); |
| 3521 | posix_info->ChangeTime = cpu_to_le64(time); |
| 3522 | time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->atime); |
| 3523 | posix_info->LastAccessTime = cpu_to_le64(time); |
| 3524 | time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->mtime); |
| 3525 | posix_info->LastWriteTime = cpu_to_le64(time); |
| 3526 | posix_info->EndOfFile = cpu_to_le64(ksmbd_kstat->kstat->size); |
| 3527 | posix_info->AllocationSize = cpu_to_le64(ksmbd_kstat->kstat->blocks << 9); |
| 3528 | posix_info->DeviceId = cpu_to_le32(ksmbd_kstat->kstat->rdev); |
| 3529 | posix_info->HardLinks = cpu_to_le32(ksmbd_kstat->kstat->nlink); |
| 3530 | posix_info->Mode = cpu_to_le32(ksmbd_kstat->kstat->mode); |
| 3531 | posix_info->Inode = cpu_to_le64(ksmbd_kstat->kstat->ino); |
| 3532 | posix_info->DosAttributes = |
| 3533 | S_ISDIR(ksmbd_kstat->kstat->mode) ? ATTR_DIRECTORY_LE : ATTR_ARCHIVE_LE; |
| 3534 | if (d_info->hide_dot_file && d_info->name[0] == '.') |
| 3535 | posix_info->DosAttributes |= ATTR_HIDDEN_LE; |
Christian Brauner | 475d6f9 | 2021-08-23 17:13:48 +0200 | [diff] [blame] | 3536 | 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] | 3537 | SIDNFS_USER, (struct smb_sid *)&posix_info->SidBuffer[0]); |
Christian Brauner | 475d6f9 | 2021-08-23 17:13:48 +0200 | [diff] [blame] | 3538 | 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] | 3539 | SIDNFS_GROUP, (struct smb_sid *)&posix_info->SidBuffer[20]); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3540 | memcpy(posix_info->name, conv_name, conv_len); |
| 3541 | posix_info->name_len = cpu_to_le32(conv_len); |
| 3542 | posix_info->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3543 | break; |
| 3544 | } |
| 3545 | |
| 3546 | } /* switch (info_level) */ |
| 3547 | |
| 3548 | d_info->last_entry_offset = d_info->data_count; |
| 3549 | d_info->data_count += next_entry_offset; |
Marios Makassikis | e7735c8 | 2021-05-06 11:40:02 +0900 | [diff] [blame] | 3550 | d_info->out_buf_len -= next_entry_offset; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3551 | d_info->wptr += next_entry_offset; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3552 | |
| 3553 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3554 | "info_level : %d, buf_len :%d, next_offset : %d, data_count : %d\n", |
| 3555 | info_level, d_info->out_buf_len, |
| 3556 | next_entry_offset, d_info->data_count); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3557 | |
Namjae Jeon | dac0ec6 | 2021-07-07 14:57:24 +0900 | [diff] [blame] | 3558 | free_conv_name: |
| 3559 | kfree(conv_name); |
| 3560 | return rc; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3561 | } |
| 3562 | |
| 3563 | struct smb2_query_dir_private { |
| 3564 | struct ksmbd_work *work; |
| 3565 | char *search_pattern; |
| 3566 | struct ksmbd_file *dir_fp; |
| 3567 | |
| 3568 | struct ksmbd_dir_info *d_info; |
| 3569 | int info_level; |
| 3570 | }; |
| 3571 | |
| 3572 | static void lock_dir(struct ksmbd_file *dir_fp) |
| 3573 | { |
| 3574 | struct dentry *dir = dir_fp->filp->f_path.dentry; |
| 3575 | |
| 3576 | inode_lock_nested(d_inode(dir), I_MUTEX_PARENT); |
| 3577 | } |
| 3578 | |
| 3579 | static void unlock_dir(struct ksmbd_file *dir_fp) |
| 3580 | { |
| 3581 | struct dentry *dir = dir_fp->filp->f_path.dentry; |
| 3582 | |
| 3583 | inode_unlock(d_inode(dir)); |
| 3584 | } |
| 3585 | |
| 3586 | static int process_query_dir_entries(struct smb2_query_dir_private *priv) |
| 3587 | { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 3588 | 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] | 3589 | struct kstat kstat; |
| 3590 | struct ksmbd_kstat ksmbd_kstat; |
| 3591 | int rc; |
| 3592 | int i; |
| 3593 | |
| 3594 | for (i = 0; i < priv->d_info->num_entry; i++) { |
| 3595 | struct dentry *dent; |
| 3596 | |
| 3597 | if (dentry_name(priv->d_info, priv->info_level)) |
| 3598 | return -EINVAL; |
| 3599 | |
| 3600 | lock_dir(priv->dir_fp); |
Christian Brauner | da1e7ad | 2021-08-23 17:13:47 +0200 | [diff] [blame] | 3601 | dent = lookup_one(user_ns, priv->d_info->name, |
| 3602 | priv->dir_fp->filp->f_path.dentry, |
| 3603 | priv->d_info->name_len); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3604 | unlock_dir(priv->dir_fp); |
| 3605 | |
| 3606 | if (IS_ERR(dent)) { |
| 3607 | ksmbd_debug(SMB, "Cannot lookup `%s' [%ld]\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3608 | priv->d_info->name, |
| 3609 | PTR_ERR(dent)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3610 | continue; |
| 3611 | } |
| 3612 | if (unlikely(d_is_negative(dent))) { |
| 3613 | dput(dent); |
| 3614 | ksmbd_debug(SMB, "Negative dentry `%s'\n", |
| 3615 | priv->d_info->name); |
| 3616 | continue; |
| 3617 | } |
| 3618 | |
| 3619 | ksmbd_kstat.kstat = &kstat; |
| 3620 | if (priv->info_level != FILE_NAMES_INFORMATION) |
| 3621 | ksmbd_vfs_fill_dentry_attrs(priv->work, |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 3622 | user_ns, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3623 | dent, |
| 3624 | &ksmbd_kstat); |
| 3625 | |
| 3626 | rc = smb2_populate_readdir_entry(priv->work->conn, |
| 3627 | priv->info_level, |
| 3628 | priv->d_info, |
| 3629 | &ksmbd_kstat); |
| 3630 | dput(dent); |
| 3631 | if (rc) |
| 3632 | return rc; |
| 3633 | } |
| 3634 | return 0; |
| 3635 | } |
| 3636 | |
| 3637 | static int reserve_populate_dentry(struct ksmbd_dir_info *d_info, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3638 | int info_level) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3639 | { |
| 3640 | int struct_sz; |
| 3641 | int conv_len; |
| 3642 | int next_entry_offset; |
| 3643 | |
| 3644 | struct_sz = readdir_info_level_struct_sz(info_level); |
| 3645 | if (struct_sz == -EOPNOTSUPP) |
| 3646 | return -EOPNOTSUPP; |
| 3647 | |
| 3648 | conv_len = (d_info->name_len + 1) * 2; |
| 3649 | next_entry_offset = ALIGN(struct_sz - 1 + conv_len, |
| 3650 | KSMBD_DIR_INFO_ALIGNMENT); |
| 3651 | |
| 3652 | if (next_entry_offset > d_info->out_buf_len) { |
| 3653 | d_info->out_buf_len = 0; |
| 3654 | return -ENOSPC; |
| 3655 | } |
| 3656 | |
| 3657 | switch (info_level) { |
| 3658 | case FILE_FULL_DIRECTORY_INFORMATION: |
| 3659 | { |
| 3660 | struct file_full_directory_info *ffdinfo; |
| 3661 | |
| 3662 | ffdinfo = (struct file_full_directory_info *)d_info->wptr; |
| 3663 | memcpy(ffdinfo->FileName, d_info->name, d_info->name_len); |
| 3664 | ffdinfo->FileName[d_info->name_len] = 0x00; |
| 3665 | ffdinfo->FileNameLength = cpu_to_le32(d_info->name_len); |
| 3666 | ffdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3667 | break; |
| 3668 | } |
| 3669 | case FILE_BOTH_DIRECTORY_INFORMATION: |
| 3670 | { |
| 3671 | struct file_both_directory_info *fbdinfo; |
| 3672 | |
| 3673 | fbdinfo = (struct file_both_directory_info *)d_info->wptr; |
| 3674 | memcpy(fbdinfo->FileName, d_info->name, d_info->name_len); |
| 3675 | fbdinfo->FileName[d_info->name_len] = 0x00; |
| 3676 | fbdinfo->FileNameLength = cpu_to_le32(d_info->name_len); |
| 3677 | fbdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3678 | break; |
| 3679 | } |
| 3680 | case FILE_DIRECTORY_INFORMATION: |
| 3681 | { |
| 3682 | struct file_directory_info *fdinfo; |
| 3683 | |
| 3684 | fdinfo = (struct file_directory_info *)d_info->wptr; |
| 3685 | memcpy(fdinfo->FileName, d_info->name, d_info->name_len); |
| 3686 | fdinfo->FileName[d_info->name_len] = 0x00; |
| 3687 | fdinfo->FileNameLength = cpu_to_le32(d_info->name_len); |
| 3688 | fdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3689 | break; |
| 3690 | } |
| 3691 | case FILE_NAMES_INFORMATION: |
| 3692 | { |
| 3693 | struct file_names_info *fninfo; |
| 3694 | |
| 3695 | fninfo = (struct file_names_info *)d_info->wptr; |
| 3696 | memcpy(fninfo->FileName, d_info->name, d_info->name_len); |
| 3697 | fninfo->FileName[d_info->name_len] = 0x00; |
| 3698 | fninfo->FileNameLength = cpu_to_le32(d_info->name_len); |
| 3699 | fninfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3700 | break; |
| 3701 | } |
| 3702 | case FILEID_FULL_DIRECTORY_INFORMATION: |
| 3703 | { |
| 3704 | struct file_id_full_dir_info *dinfo; |
| 3705 | |
| 3706 | dinfo = (struct file_id_full_dir_info *)d_info->wptr; |
| 3707 | memcpy(dinfo->FileName, d_info->name, d_info->name_len); |
| 3708 | dinfo->FileName[d_info->name_len] = 0x00; |
| 3709 | dinfo->FileNameLength = cpu_to_le32(d_info->name_len); |
| 3710 | dinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3711 | break; |
| 3712 | } |
| 3713 | case FILEID_BOTH_DIRECTORY_INFORMATION: |
| 3714 | { |
| 3715 | struct file_id_both_directory_info *fibdinfo; |
| 3716 | |
| 3717 | fibdinfo = (struct file_id_both_directory_info *)d_info->wptr; |
| 3718 | memcpy(fibdinfo->FileName, d_info->name, d_info->name_len); |
| 3719 | fibdinfo->FileName[d_info->name_len] = 0x00; |
| 3720 | fibdinfo->FileNameLength = cpu_to_le32(d_info->name_len); |
| 3721 | fibdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); |
| 3722 | break; |
| 3723 | } |
| 3724 | case SMB_FIND_FILE_POSIX_INFO: |
| 3725 | { |
| 3726 | struct smb2_posix_info *posix_info; |
| 3727 | |
| 3728 | posix_info = (struct smb2_posix_info *)d_info->wptr; |
| 3729 | memcpy(posix_info->name, d_info->name, d_info->name_len); |
| 3730 | posix_info->name[d_info->name_len] = 0x00; |
| 3731 | posix_info->name_len = cpu_to_le32(d_info->name_len); |
| 3732 | posix_info->NextEntryOffset = |
| 3733 | cpu_to_le32(next_entry_offset); |
| 3734 | break; |
| 3735 | } |
| 3736 | } /* switch (info_level) */ |
| 3737 | |
| 3738 | d_info->num_entry++; |
| 3739 | d_info->out_buf_len -= next_entry_offset; |
| 3740 | d_info->wptr += next_entry_offset; |
| 3741 | return 0; |
| 3742 | } |
| 3743 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3744 | 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] | 3745 | loff_t offset, u64 ino, unsigned int d_type) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3746 | { |
| 3747 | struct ksmbd_readdir_data *buf; |
| 3748 | struct smb2_query_dir_private *priv; |
| 3749 | struct ksmbd_dir_info *d_info; |
| 3750 | int rc; |
| 3751 | |
| 3752 | buf = container_of(ctx, struct ksmbd_readdir_data, ctx); |
| 3753 | priv = buf->private; |
| 3754 | d_info = priv->d_info; |
| 3755 | |
| 3756 | /* dot and dotdot entries are already reserved */ |
| 3757 | if (!strcmp(".", name) || !strcmp("..", name)) |
| 3758 | return 0; |
| 3759 | if (ksmbd_share_veto_filename(priv->work->tcon->share_conf, name)) |
| 3760 | return 0; |
Namjae Jeon | b24c933 | 2021-03-21 17:32:19 +0900 | [diff] [blame] | 3761 | if (!match_pattern(name, namlen, priv->search_pattern)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3762 | return 0; |
| 3763 | |
| 3764 | d_info->name = name; |
| 3765 | d_info->name_len = namlen; |
| 3766 | rc = reserve_populate_dentry(d_info, priv->info_level); |
| 3767 | if (rc) |
| 3768 | return rc; |
| 3769 | if (d_info->flags & SMB2_RETURN_SINGLE_ENTRY) { |
| 3770 | d_info->out_buf_len = 0; |
| 3771 | return 0; |
| 3772 | } |
| 3773 | return 0; |
| 3774 | } |
| 3775 | |
| 3776 | static void restart_ctx(struct dir_context *ctx) |
| 3777 | { |
| 3778 | ctx->pos = 0; |
| 3779 | } |
| 3780 | |
| 3781 | static int verify_info_level(int info_level) |
| 3782 | { |
| 3783 | switch (info_level) { |
| 3784 | case FILE_FULL_DIRECTORY_INFORMATION: |
| 3785 | case FILE_BOTH_DIRECTORY_INFORMATION: |
| 3786 | case FILE_DIRECTORY_INFORMATION: |
| 3787 | case FILE_NAMES_INFORMATION: |
| 3788 | case FILEID_FULL_DIRECTORY_INFORMATION: |
| 3789 | case FILEID_BOTH_DIRECTORY_INFORMATION: |
| 3790 | case SMB_FIND_FILE_POSIX_INFO: |
| 3791 | break; |
| 3792 | default: |
| 3793 | return -EOPNOTSUPP; |
| 3794 | } |
| 3795 | |
| 3796 | return 0; |
| 3797 | } |
| 3798 | |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 3799 | static int smb2_calc_max_out_buf_len(struct ksmbd_work *work, |
| 3800 | unsigned short hdr2_len, |
| 3801 | unsigned int out_buf_len) |
| 3802 | { |
| 3803 | int free_len; |
| 3804 | |
| 3805 | if (out_buf_len > work->conn->vals->max_trans_size) |
| 3806 | return -EINVAL; |
| 3807 | |
| 3808 | free_len = (int)(work->response_sz - |
| 3809 | (get_rfc1002_len(work->response_buf) + 4)) - |
| 3810 | hdr2_len; |
| 3811 | if (free_len < 0) |
| 3812 | return -EINVAL; |
| 3813 | |
| 3814 | return min_t(int, out_buf_len, free_len); |
| 3815 | } |
| 3816 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3817 | int smb2_query_dir(struct ksmbd_work *work) |
| 3818 | { |
| 3819 | struct ksmbd_conn *conn = work->conn; |
| 3820 | struct smb2_query_directory_req *req; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 3821 | struct smb2_query_directory_rsp *rsp; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3822 | struct ksmbd_share_config *share = work->tcon->share_conf; |
| 3823 | struct ksmbd_file *dir_fp = NULL; |
| 3824 | struct ksmbd_dir_info d_info; |
| 3825 | int rc = 0; |
| 3826 | char *srch_ptr = NULL; |
| 3827 | unsigned char srch_flag; |
| 3828 | int buffer_sz; |
| 3829 | struct smb2_query_dir_private query_dir_private = {NULL, }; |
| 3830 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3831 | WORK_BUFFERS(work, req, rsp); |
| 3832 | |
| 3833 | if (ksmbd_override_fsids(work)) { |
| 3834 | rsp->hdr.Status = STATUS_NO_MEMORY; |
| 3835 | smb2_set_err_rsp(work); |
| 3836 | return -ENOMEM; |
| 3837 | } |
| 3838 | |
| 3839 | rc = verify_info_level(req->FileInformationClass); |
| 3840 | if (rc) { |
| 3841 | rc = -EFAULT; |
| 3842 | goto err_out2; |
| 3843 | } |
| 3844 | |
| 3845 | dir_fp = ksmbd_lookup_fd_slow(work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3846 | le64_to_cpu(req->VolatileFileId), |
| 3847 | le64_to_cpu(req->PersistentFileId)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3848 | if (!dir_fp) { |
| 3849 | rc = -EBADF; |
| 3850 | goto err_out2; |
| 3851 | } |
| 3852 | |
| 3853 | if (!(dir_fp->daccess & FILE_LIST_DIRECTORY_LE) || |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 3854 | inode_permission(file_mnt_user_ns(dir_fp->filp), |
| 3855 | file_inode(dir_fp->filp), |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3856 | MAY_READ | MAY_EXEC)) { |
Namjae Jeon | 493fa2f | 2021-06-29 09:22:16 +0900 | [diff] [blame] | 3857 | pr_err("no right to enumerate directory (%pd)\n", |
| 3858 | dir_fp->filp->f_path.dentry); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3859 | rc = -EACCES; |
| 3860 | goto err_out2; |
| 3861 | } |
| 3862 | |
| 3863 | if (!S_ISDIR(file_inode(dir_fp->filp)->i_mode)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 3864 | pr_err("can't do query dir for a file\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3865 | rc = -EINVAL; |
| 3866 | goto err_out2; |
| 3867 | } |
| 3868 | |
| 3869 | srch_flag = req->Flags; |
| 3870 | srch_ptr = smb_strndup_from_utf16(req->Buffer, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3871 | le16_to_cpu(req->FileNameLength), 1, |
| 3872 | conn->local_nls); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3873 | if (IS_ERR(srch_ptr)) { |
| 3874 | ksmbd_debug(SMB, "Search Pattern not found\n"); |
| 3875 | rc = -EINVAL; |
| 3876 | goto err_out2; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3877 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3878 | ksmbd_debug(SMB, "Search pattern is %s\n", srch_ptr); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3879 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3880 | |
| 3881 | ksmbd_debug(SMB, "Directory name is %s\n", dir_fp->filename); |
| 3882 | |
| 3883 | if (srch_flag & SMB2_REOPEN || srch_flag & SMB2_RESTART_SCANS) { |
| 3884 | ksmbd_debug(SMB, "Restart directory scan\n"); |
| 3885 | generic_file_llseek(dir_fp->filp, 0, SEEK_SET); |
| 3886 | restart_ctx(&dir_fp->readdir_data.ctx); |
| 3887 | } |
| 3888 | |
| 3889 | memset(&d_info, 0, sizeof(struct ksmbd_dir_info)); |
| 3890 | d_info.wptr = (char *)rsp->Buffer; |
| 3891 | d_info.rptr = (char *)rsp->Buffer; |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 3892 | d_info.out_buf_len = |
| 3893 | smb2_calc_max_out_buf_len(work, 8, |
| 3894 | le32_to_cpu(req->OutputBufferLength)); |
| 3895 | if (d_info.out_buf_len < 0) { |
| 3896 | rc = -EINVAL; |
| 3897 | goto err_out; |
| 3898 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3899 | d_info.flags = srch_flag; |
| 3900 | |
| 3901 | /* |
| 3902 | * reserve dot and dotdot entries in head of buffer |
| 3903 | * in first response |
| 3904 | */ |
| 3905 | rc = ksmbd_populate_dot_dotdot_entries(work, req->FileInformationClass, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 3906 | dir_fp, &d_info, srch_ptr, |
| 3907 | smb2_populate_readdir_entry); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3908 | if (rc == -ENOSPC) |
| 3909 | rc = 0; |
| 3910 | else if (rc) |
| 3911 | goto err_out; |
| 3912 | |
| 3913 | if (test_share_config_flag(share, KSMBD_SHARE_FLAG_HIDE_DOT_FILES)) |
| 3914 | d_info.hide_dot_file = true; |
| 3915 | |
| 3916 | buffer_sz = d_info.out_buf_len; |
| 3917 | d_info.rptr = d_info.wptr; |
| 3918 | query_dir_private.work = work; |
| 3919 | query_dir_private.search_pattern = srch_ptr; |
| 3920 | query_dir_private.dir_fp = dir_fp; |
| 3921 | query_dir_private.d_info = &d_info; |
| 3922 | query_dir_private.info_level = req->FileInformationClass; |
| 3923 | dir_fp->readdir_data.private = &query_dir_private; |
| 3924 | set_ctx_actor(&dir_fp->readdir_data.ctx, __query_dir); |
| 3925 | |
Namjae Jeon | e8c0619 | 2021-06-22 11:06:11 +0900 | [diff] [blame] | 3926 | rc = iterate_dir(dir_fp->filp, &dir_fp->readdir_data.ctx); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3927 | if (rc == 0) |
| 3928 | restart_ctx(&dir_fp->readdir_data.ctx); |
| 3929 | if (rc == -ENOSPC) |
| 3930 | rc = 0; |
| 3931 | if (rc) |
| 3932 | goto err_out; |
| 3933 | |
| 3934 | d_info.wptr = d_info.rptr; |
| 3935 | d_info.out_buf_len = buffer_sz; |
| 3936 | rc = process_query_dir_entries(&query_dir_private); |
| 3937 | if (rc) |
| 3938 | goto err_out; |
| 3939 | |
| 3940 | if (!d_info.data_count && d_info.out_buf_len >= 0) { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3941 | if (srch_flag & SMB2_RETURN_SINGLE_ENTRY && !is_asterisk(srch_ptr)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3942 | rsp->hdr.Status = STATUS_NO_SUCH_FILE; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 3943 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3944 | dir_fp->dot_dotdot[0] = dir_fp->dot_dotdot[1] = 0; |
| 3945 | rsp->hdr.Status = STATUS_NO_MORE_FILES; |
| 3946 | } |
| 3947 | rsp->StructureSize = cpu_to_le16(9); |
| 3948 | rsp->OutputBufferOffset = cpu_to_le16(0); |
| 3949 | rsp->OutputBufferLength = cpu_to_le32(0); |
| 3950 | rsp->Buffer[0] = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 3951 | inc_rfc1001_len(work->response_buf, 9); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3952 | } else { |
| 3953 | ((struct file_directory_info *) |
| 3954 | ((char *)rsp->Buffer + d_info.last_entry_offset)) |
| 3955 | ->NextEntryOffset = 0; |
| 3956 | |
| 3957 | rsp->StructureSize = cpu_to_le16(9); |
| 3958 | rsp->OutputBufferOffset = cpu_to_le16(72); |
| 3959 | rsp->OutputBufferLength = cpu_to_le32(d_info.data_count); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 3960 | inc_rfc1001_len(work->response_buf, 8 + d_info.data_count); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3961 | } |
| 3962 | |
| 3963 | kfree(srch_ptr); |
| 3964 | ksmbd_fd_put(work, dir_fp); |
| 3965 | ksmbd_revert_fsids(work); |
| 3966 | return 0; |
| 3967 | |
| 3968 | err_out: |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 3969 | pr_err("error while processing smb2 query dir rc = %d\n", rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 3970 | kfree(srch_ptr); |
| 3971 | |
| 3972 | err_out2: |
| 3973 | if (rc == -EINVAL) |
| 3974 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 3975 | else if (rc == -EACCES) |
| 3976 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 3977 | else if (rc == -ENOENT) |
| 3978 | rsp->hdr.Status = STATUS_NO_SUCH_FILE; |
| 3979 | else if (rc == -EBADF) |
| 3980 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 3981 | else if (rc == -ENOMEM) |
| 3982 | rsp->hdr.Status = STATUS_NO_MEMORY; |
| 3983 | else if (rc == -EFAULT) |
| 3984 | rsp->hdr.Status = STATUS_INVALID_INFO_CLASS; |
| 3985 | if (!rsp->hdr.Status) |
| 3986 | rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR; |
| 3987 | |
| 3988 | smb2_set_err_rsp(work); |
| 3989 | ksmbd_fd_put(work, dir_fp); |
| 3990 | ksmbd_revert_fsids(work); |
| 3991 | return 0; |
| 3992 | } |
| 3993 | |
| 3994 | /** |
| 3995 | * buffer_check_err() - helper function to check buffer errors |
| 3996 | * @reqOutputBufferLength: max buffer length expected in command response |
| 3997 | * @rsp: query info response buffer contains output buffer length |
| 3998 | * @infoclass_size: query info class response buffer size |
| 3999 | * |
| 4000 | * Return: 0 on success, otherwise error |
| 4001 | */ |
| 4002 | static int buffer_check_err(int reqOutputBufferLength, |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 4003 | struct smb2_query_info_rsp *rsp, |
| 4004 | void *rsp_org, int infoclass_size) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4005 | { |
| 4006 | if (reqOutputBufferLength < le32_to_cpu(rsp->OutputBufferLength)) { |
| 4007 | if (reqOutputBufferLength < infoclass_size) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4008 | pr_err("Invalid Buffer Size Requested\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4009 | rsp->hdr.Status = STATUS_INFO_LENGTH_MISMATCH; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 4010 | *(__be32 *)rsp_org = cpu_to_be32(sizeof(struct smb2_hdr)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4011 | return -EINVAL; |
| 4012 | } |
| 4013 | |
| 4014 | ksmbd_debug(SMB, "Buffer Overflow\n"); |
| 4015 | rsp->hdr.Status = STATUS_BUFFER_OVERFLOW; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 4016 | *(__be32 *)rsp_org = cpu_to_be32(sizeof(struct smb2_hdr) + |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4017 | reqOutputBufferLength); |
| 4018 | rsp->OutputBufferLength = cpu_to_le32(reqOutputBufferLength); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4019 | } |
| 4020 | return 0; |
| 4021 | } |
| 4022 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 4023 | static void get_standard_info_pipe(struct smb2_query_info_rsp *rsp, |
| 4024 | void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4025 | { |
| 4026 | struct smb2_file_standard_info *sinfo; |
| 4027 | |
| 4028 | sinfo = (struct smb2_file_standard_info *)rsp->Buffer; |
| 4029 | |
| 4030 | sinfo->AllocationSize = cpu_to_le64(4096); |
| 4031 | sinfo->EndOfFile = cpu_to_le64(0); |
| 4032 | sinfo->NumberOfLinks = cpu_to_le32(1); |
| 4033 | sinfo->DeletePending = 1; |
| 4034 | sinfo->Directory = 0; |
| 4035 | rsp->OutputBufferLength = |
| 4036 | cpu_to_le32(sizeof(struct smb2_file_standard_info)); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 4037 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_standard_info)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4038 | } |
| 4039 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 4040 | static void get_internal_info_pipe(struct smb2_query_info_rsp *rsp, u64 num, |
| 4041 | void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4042 | { |
| 4043 | struct smb2_file_internal_info *file_info; |
| 4044 | |
| 4045 | file_info = (struct smb2_file_internal_info *)rsp->Buffer; |
| 4046 | |
| 4047 | /* any unique number */ |
| 4048 | file_info->IndexNumber = cpu_to_le64(num | (1ULL << 63)); |
| 4049 | rsp->OutputBufferLength = |
| 4050 | cpu_to_le32(sizeof(struct smb2_file_internal_info)); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 4051 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_internal_info)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4052 | } |
| 4053 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4054 | static int smb2_get_info_file_pipe(struct ksmbd_session *sess, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4055 | struct smb2_query_info_req *req, |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 4056 | struct smb2_query_info_rsp *rsp, |
| 4057 | void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4058 | { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4059 | u64 id; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4060 | int rc; |
| 4061 | |
| 4062 | /* |
| 4063 | * Windows can sometime send query file info request on |
| 4064 | * pipe without opening it, checking error condition here |
| 4065 | */ |
| 4066 | id = le64_to_cpu(req->VolatileFileId); |
| 4067 | if (!ksmbd_session_rpc_method(sess, id)) |
| 4068 | return -ENOENT; |
| 4069 | |
| 4070 | ksmbd_debug(SMB, "FileInfoClass %u, FileId 0x%llx\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4071 | req->FileInfoClass, le64_to_cpu(req->VolatileFileId)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4072 | |
| 4073 | switch (req->FileInfoClass) { |
| 4074 | case FILE_STANDARD_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 4075 | get_standard_info_pipe(rsp, rsp_org); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4076 | rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength), |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 4077 | rsp, rsp_org, |
| 4078 | FILE_STANDARD_INFORMATION_SIZE); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4079 | break; |
| 4080 | case FILE_INTERNAL_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 4081 | get_internal_info_pipe(rsp, id, rsp_org); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4082 | rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength), |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 4083 | rsp, rsp_org, |
| 4084 | FILE_INTERNAL_INFORMATION_SIZE); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4085 | break; |
| 4086 | default: |
| 4087 | ksmbd_debug(SMB, "smb2_info_file_pipe for %u not supported\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4088 | req->FileInfoClass); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4089 | rc = -EOPNOTSUPP; |
| 4090 | } |
| 4091 | return rc; |
| 4092 | } |
| 4093 | |
| 4094 | /** |
| 4095 | * smb2_get_ea() - handler for smb2 get extended attribute command |
| 4096 | * @work: smb work containing query info command buffer |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 4097 | * @fp: ksmbd_file pointer |
| 4098 | * @req: get extended attribute request |
| 4099 | * @rsp: response buffer pointer |
| 4100 | * @rsp_org: base response buffer pointer in case of chained response |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4101 | * |
| 4102 | * Return: 0 on success, otherwise error |
| 4103 | */ |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4104 | 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] | 4105 | struct smb2_query_info_req *req, |
| 4106 | struct smb2_query_info_rsp *rsp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4107 | { |
| 4108 | struct smb2_ea_info *eainfo, *prev_eainfo; |
| 4109 | char *name, *ptr, *xattr_list = NULL, *buf; |
| 4110 | int rc, name_len, value_len, xattr_list_len, idx; |
| 4111 | ssize_t buf_free_len, alignment_bytes, next_offset, rsp_data_cnt = 0; |
| 4112 | struct smb2_ea_info_req *ea_req = NULL; |
| 4113 | struct path *path; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 4114 | struct user_namespace *user_ns = file_mnt_user_ns(fp->filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4115 | |
| 4116 | if (!(fp->daccess & FILE_READ_EA_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4117 | pr_err("Not permitted to read ext attr : 0x%x\n", |
| 4118 | fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4119 | return -EACCES; |
| 4120 | } |
| 4121 | |
| 4122 | path = &fp->filp->f_path; |
| 4123 | /* single EA entry is requested with given user.* name */ |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4124 | if (req->InputBufferLength) { |
Namjae Jeon | 6d56262 | 2021-09-18 18:45:12 +0900 | [diff] [blame] | 4125 | if (le32_to_cpu(req->InputBufferLength) < |
| 4126 | sizeof(struct smb2_ea_info_req)) |
| 4127 | return -EINVAL; |
| 4128 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4129 | ea_req = (struct smb2_ea_info_req *)req->Buffer; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4130 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4131 | /* need to send all EAs, if no specific EA is requested*/ |
| 4132 | if (le32_to_cpu(req->Flags) & SL_RETURN_SINGLE_ENTRY) |
| 4133 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4134 | "All EAs are requested but need to send single EA entry in rsp flags 0x%x\n", |
| 4135 | le32_to_cpu(req->Flags)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4136 | } |
| 4137 | |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 4138 | buf_free_len = |
| 4139 | smb2_calc_max_out_buf_len(work, 8, |
| 4140 | le32_to_cpu(req->OutputBufferLength)); |
| 4141 | if (buf_free_len < 0) |
| 4142 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4143 | |
| 4144 | rc = ksmbd_vfs_listxattr(path->dentry, &xattr_list); |
| 4145 | if (rc < 0) { |
| 4146 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 4147 | goto out; |
| 4148 | } else if (!rc) { /* there is no EA in the file */ |
| 4149 | ksmbd_debug(SMB, "no ea data in the file\n"); |
| 4150 | goto done; |
| 4151 | } |
| 4152 | xattr_list_len = rc; |
| 4153 | |
| 4154 | ptr = (char *)rsp->Buffer; |
| 4155 | eainfo = (struct smb2_ea_info *)ptr; |
| 4156 | prev_eainfo = eainfo; |
| 4157 | idx = 0; |
| 4158 | |
| 4159 | while (idx < xattr_list_len) { |
| 4160 | name = xattr_list + idx; |
| 4161 | name_len = strlen(name); |
| 4162 | |
| 4163 | ksmbd_debug(SMB, "%s, len %d\n", name, name_len); |
| 4164 | idx += name_len + 1; |
| 4165 | |
| 4166 | /* |
| 4167 | * CIFS does not support EA other than user.* namespace, |
| 4168 | * still keep the framework generic, to list other attrs |
| 4169 | * in future. |
| 4170 | */ |
| 4171 | if (strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) |
| 4172 | continue; |
| 4173 | |
| 4174 | if (!strncmp(&name[XATTR_USER_PREFIX_LEN], STREAM_PREFIX, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4175 | STREAM_PREFIX_LEN)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4176 | continue; |
| 4177 | |
| 4178 | if (req->InputBufferLength && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4179 | strncmp(&name[XATTR_USER_PREFIX_LEN], ea_req->name, |
| 4180 | ea_req->EaNameLength)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4181 | continue; |
| 4182 | |
| 4183 | if (!strncmp(&name[XATTR_USER_PREFIX_LEN], |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4184 | DOS_ATTRIBUTE_PREFIX, DOS_ATTRIBUTE_PREFIX_LEN)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4185 | continue; |
| 4186 | |
| 4187 | if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) |
| 4188 | name_len -= XATTR_USER_PREFIX_LEN; |
| 4189 | |
| 4190 | ptr = (char *)(&eainfo->name + name_len + 1); |
| 4191 | buf_free_len -= (offsetof(struct smb2_ea_info, name) + |
| 4192 | name_len + 1); |
| 4193 | /* bailout if xattr can't fit in buf_free_len */ |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 4194 | value_len = ksmbd_vfs_getxattr(user_ns, path->dentry, |
| 4195 | name, &buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4196 | if (value_len <= 0) { |
| 4197 | rc = -ENOENT; |
| 4198 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 4199 | goto out; |
| 4200 | } |
| 4201 | |
| 4202 | buf_free_len -= value_len; |
| 4203 | if (buf_free_len < 0) { |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 4204 | kfree(buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4205 | break; |
| 4206 | } |
| 4207 | |
| 4208 | memcpy(ptr, buf, value_len); |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 4209 | kfree(buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4210 | |
| 4211 | ptr += value_len; |
| 4212 | eainfo->Flags = 0; |
| 4213 | eainfo->EaNameLength = name_len; |
| 4214 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4215 | if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4216 | memcpy(eainfo->name, &name[XATTR_USER_PREFIX_LEN], |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4217 | name_len); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4218 | else |
| 4219 | memcpy(eainfo->name, name, name_len); |
| 4220 | |
| 4221 | eainfo->name[name_len] = '\0'; |
| 4222 | eainfo->EaValueLength = cpu_to_le16(value_len); |
| 4223 | next_offset = offsetof(struct smb2_ea_info, name) + |
| 4224 | name_len + 1 + value_len; |
| 4225 | |
| 4226 | /* align next xattr entry at 4 byte bundary */ |
| 4227 | alignment_bytes = ((next_offset + 3) & ~3) - next_offset; |
| 4228 | if (alignment_bytes) { |
| 4229 | memset(ptr, '\0', alignment_bytes); |
| 4230 | ptr += alignment_bytes; |
| 4231 | next_offset += alignment_bytes; |
| 4232 | buf_free_len -= alignment_bytes; |
| 4233 | } |
| 4234 | eainfo->NextEntryOffset = cpu_to_le32(next_offset); |
| 4235 | prev_eainfo = eainfo; |
| 4236 | eainfo = (struct smb2_ea_info *)ptr; |
| 4237 | rsp_data_cnt += next_offset; |
| 4238 | |
| 4239 | if (req->InputBufferLength) { |
| 4240 | ksmbd_debug(SMB, "single entry requested\n"); |
| 4241 | break; |
| 4242 | } |
| 4243 | } |
| 4244 | |
| 4245 | /* no more ea entries */ |
| 4246 | prev_eainfo->NextEntryOffset = 0; |
| 4247 | done: |
| 4248 | rc = 0; |
| 4249 | if (rsp_data_cnt == 0) |
| 4250 | rsp->hdr.Status = STATUS_NO_EAS_ON_FILE; |
| 4251 | rsp->OutputBufferLength = cpu_to_le32(rsp_data_cnt); |
| 4252 | inc_rfc1001_len(rsp_org, rsp_data_cnt); |
| 4253 | out: |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 4254 | kvfree(xattr_list); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4255 | return rc; |
| 4256 | } |
| 4257 | |
| 4258 | static void get_file_access_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4259 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4260 | { |
| 4261 | struct smb2_file_access_info *file_info; |
| 4262 | |
| 4263 | file_info = (struct smb2_file_access_info *)rsp->Buffer; |
| 4264 | file_info->AccessFlags = fp->daccess; |
| 4265 | rsp->OutputBufferLength = |
| 4266 | cpu_to_le32(sizeof(struct smb2_file_access_info)); |
| 4267 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_access_info)); |
| 4268 | } |
| 4269 | |
| 4270 | static int get_file_basic_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4271 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4272 | { |
Namjae Jeon | 88d3005 | 2021-09-29 15:37:18 +0900 | [diff] [blame] | 4273 | struct smb2_file_basic_info *basic_info; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4274 | struct kstat stat; |
| 4275 | u64 time; |
| 4276 | |
| 4277 | if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4278 | pr_err("no right to read the attributes : 0x%x\n", |
| 4279 | fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4280 | return -EACCES; |
| 4281 | } |
| 4282 | |
Namjae Jeon | 88d3005 | 2021-09-29 15:37:18 +0900 | [diff] [blame] | 4283 | basic_info = (struct smb2_file_basic_info *)rsp->Buffer; |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 4284 | generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp), |
| 4285 | &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4286 | basic_info->CreationTime = cpu_to_le64(fp->create_time); |
| 4287 | time = ksmbd_UnixTimeToNT(stat.atime); |
| 4288 | basic_info->LastAccessTime = cpu_to_le64(time); |
| 4289 | time = ksmbd_UnixTimeToNT(stat.mtime); |
| 4290 | basic_info->LastWriteTime = cpu_to_le64(time); |
| 4291 | time = ksmbd_UnixTimeToNT(stat.ctime); |
| 4292 | basic_info->ChangeTime = cpu_to_le64(time); |
| 4293 | basic_info->Attributes = fp->f_ci->m_fattr; |
| 4294 | basic_info->Pad1 = 0; |
| 4295 | rsp->OutputBufferLength = |
Namjae Jeon | 88d3005 | 2021-09-29 15:37:18 +0900 | [diff] [blame] | 4296 | cpu_to_le32(sizeof(struct smb2_file_basic_info)); |
| 4297 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_basic_info)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4298 | return 0; |
| 4299 | } |
| 4300 | |
| 4301 | static unsigned long long get_allocation_size(struct inode *inode, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4302 | struct kstat *stat) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4303 | { |
| 4304 | unsigned long long alloc_size = 0; |
| 4305 | |
| 4306 | if (!S_ISDIR(stat->mode)) { |
| 4307 | if ((inode->i_blocks << 9) <= stat->size) |
| 4308 | alloc_size = stat->size; |
| 4309 | else |
| 4310 | alloc_size = inode->i_blocks << 9; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4311 | } |
| 4312 | |
| 4313 | return alloc_size; |
| 4314 | } |
| 4315 | |
| 4316 | static void get_file_standard_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4317 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4318 | { |
| 4319 | struct smb2_file_standard_info *sinfo; |
| 4320 | unsigned int delete_pending; |
| 4321 | struct inode *inode; |
| 4322 | struct kstat stat; |
| 4323 | |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 4324 | inode = file_inode(fp->filp); |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 4325 | generic_fillattr(file_mnt_user_ns(fp->filp), inode, &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4326 | |
| 4327 | sinfo = (struct smb2_file_standard_info *)rsp->Buffer; |
| 4328 | delete_pending = ksmbd_inode_pending_delete(fp); |
| 4329 | |
| 4330 | sinfo->AllocationSize = cpu_to_le64(get_allocation_size(inode, &stat)); |
| 4331 | sinfo->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size); |
| 4332 | sinfo->NumberOfLinks = cpu_to_le32(get_nlink(&stat) - delete_pending); |
| 4333 | sinfo->DeletePending = delete_pending; |
| 4334 | sinfo->Directory = S_ISDIR(stat.mode) ? 1 : 0; |
| 4335 | rsp->OutputBufferLength = |
| 4336 | cpu_to_le32(sizeof(struct smb2_file_standard_info)); |
| 4337 | inc_rfc1001_len(rsp_org, |
| 4338 | sizeof(struct smb2_file_standard_info)); |
| 4339 | } |
| 4340 | |
| 4341 | static void get_file_alignment_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4342 | void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4343 | { |
| 4344 | struct smb2_file_alignment_info *file_info; |
| 4345 | |
| 4346 | file_info = (struct smb2_file_alignment_info *)rsp->Buffer; |
| 4347 | file_info->AlignmentRequirement = 0; |
| 4348 | rsp->OutputBufferLength = |
| 4349 | cpu_to_le32(sizeof(struct smb2_file_alignment_info)); |
| 4350 | inc_rfc1001_len(rsp_org, |
| 4351 | sizeof(struct smb2_file_alignment_info)); |
| 4352 | } |
| 4353 | |
| 4354 | static int get_file_all_info(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4355 | struct smb2_query_info_rsp *rsp, |
| 4356 | struct ksmbd_file *fp, |
| 4357 | void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4358 | { |
| 4359 | struct ksmbd_conn *conn = work->conn; |
| 4360 | struct smb2_file_all_info *file_info; |
| 4361 | unsigned int delete_pending; |
| 4362 | struct inode *inode; |
| 4363 | struct kstat stat; |
| 4364 | int conv_len; |
| 4365 | char *filename; |
| 4366 | u64 time; |
| 4367 | |
| 4368 | if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) { |
| 4369 | ksmbd_debug(SMB, "no right to read the attributes : 0x%x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4370 | fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4371 | return -EACCES; |
| 4372 | } |
| 4373 | |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 4374 | filename = convert_to_nt_pathname(fp->filename); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4375 | if (!filename) |
| 4376 | return -ENOMEM; |
| 4377 | |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 4378 | inode = file_inode(fp->filp); |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 4379 | generic_fillattr(file_mnt_user_ns(fp->filp), inode, &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4380 | |
| 4381 | ksmbd_debug(SMB, "filename = %s\n", filename); |
| 4382 | delete_pending = ksmbd_inode_pending_delete(fp); |
| 4383 | file_info = (struct smb2_file_all_info *)rsp->Buffer; |
| 4384 | |
| 4385 | file_info->CreationTime = cpu_to_le64(fp->create_time); |
| 4386 | time = ksmbd_UnixTimeToNT(stat.atime); |
| 4387 | file_info->LastAccessTime = cpu_to_le64(time); |
| 4388 | time = ksmbd_UnixTimeToNT(stat.mtime); |
| 4389 | file_info->LastWriteTime = cpu_to_le64(time); |
| 4390 | time = ksmbd_UnixTimeToNT(stat.ctime); |
| 4391 | file_info->ChangeTime = cpu_to_le64(time); |
| 4392 | file_info->Attributes = fp->f_ci->m_fattr; |
| 4393 | file_info->Pad1 = 0; |
| 4394 | file_info->AllocationSize = |
| 4395 | cpu_to_le64(get_allocation_size(inode, &stat)); |
| 4396 | file_info->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size); |
| 4397 | file_info->NumberOfLinks = |
| 4398 | cpu_to_le32(get_nlink(&stat) - delete_pending); |
| 4399 | file_info->DeletePending = delete_pending; |
| 4400 | file_info->Directory = S_ISDIR(stat.mode) ? 1 : 0; |
| 4401 | file_info->Pad2 = 0; |
| 4402 | file_info->IndexNumber = cpu_to_le64(stat.ino); |
| 4403 | file_info->EASize = 0; |
| 4404 | file_info->AccessFlags = fp->daccess; |
| 4405 | file_info->CurrentByteOffset = cpu_to_le64(fp->filp->f_pos); |
| 4406 | file_info->Mode = fp->coption; |
| 4407 | file_info->AlignmentRequirement = 0; |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4408 | conv_len = smbConvertToUTF16((__le16 *)file_info->FileName, filename, |
| 4409 | PATH_MAX, conn->local_nls, 0); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4410 | conv_len *= 2; |
| 4411 | file_info->FileNameLength = cpu_to_le32(conv_len); |
| 4412 | rsp->OutputBufferLength = |
| 4413 | cpu_to_le32(sizeof(struct smb2_file_all_info) + conv_len - 1); |
| 4414 | kfree(filename); |
| 4415 | inc_rfc1001_len(rsp_org, le32_to_cpu(rsp->OutputBufferLength)); |
| 4416 | return 0; |
| 4417 | } |
| 4418 | |
| 4419 | static void get_file_alternate_info(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4420 | struct smb2_query_info_rsp *rsp, |
| 4421 | struct ksmbd_file *fp, |
| 4422 | void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4423 | { |
| 4424 | struct ksmbd_conn *conn = work->conn; |
| 4425 | struct smb2_file_alt_name_info *file_info; |
Namjae Jeon | 493fa2f | 2021-06-29 09:22:16 +0900 | [diff] [blame] | 4426 | struct dentry *dentry = fp->filp->f_path.dentry; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4427 | int conv_len; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4428 | |
Namjae Jeon | 493fa2f | 2021-06-29 09:22:16 +0900 | [diff] [blame] | 4429 | spin_lock(&dentry->d_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4430 | file_info = (struct smb2_file_alt_name_info *)rsp->Buffer; |
| 4431 | conv_len = ksmbd_extract_shortname(conn, |
Namjae Jeon | 493fa2f | 2021-06-29 09:22:16 +0900 | [diff] [blame] | 4432 | dentry->d_name.name, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4433 | file_info->FileName); |
Namjae Jeon | 493fa2f | 2021-06-29 09:22:16 +0900 | [diff] [blame] | 4434 | spin_unlock(&dentry->d_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4435 | file_info->FileNameLength = cpu_to_le32(conv_len); |
| 4436 | rsp->OutputBufferLength = |
| 4437 | cpu_to_le32(sizeof(struct smb2_file_alt_name_info) + conv_len); |
| 4438 | inc_rfc1001_len(rsp_org, le32_to_cpu(rsp->OutputBufferLength)); |
| 4439 | } |
| 4440 | |
| 4441 | static void get_file_stream_info(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4442 | struct smb2_query_info_rsp *rsp, |
| 4443 | struct ksmbd_file *fp, |
| 4444 | void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4445 | { |
| 4446 | struct ksmbd_conn *conn = work->conn; |
| 4447 | struct smb2_file_stream_info *file_info; |
| 4448 | char *stream_name, *xattr_list = NULL, *stream_buf; |
| 4449 | struct kstat stat; |
| 4450 | struct path *path = &fp->filp->f_path; |
| 4451 | ssize_t xattr_list_len; |
| 4452 | int nbytes = 0, streamlen, stream_name_len, next, idx = 0; |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 4453 | int buf_free_len; |
| 4454 | struct smb2_query_info_req *req = ksmbd_req_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4455 | |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 4456 | generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp), |
| 4457 | &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4458 | file_info = (struct smb2_file_stream_info *)rsp->Buffer; |
| 4459 | |
| 4460 | xattr_list_len = ksmbd_vfs_listxattr(path->dentry, &xattr_list); |
| 4461 | if (xattr_list_len < 0) { |
| 4462 | goto out; |
| 4463 | } else if (!xattr_list_len) { |
| 4464 | ksmbd_debug(SMB, "empty xattr in the file\n"); |
| 4465 | goto out; |
| 4466 | } |
| 4467 | |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 4468 | buf_free_len = |
| 4469 | smb2_calc_max_out_buf_len(work, 8, |
| 4470 | le32_to_cpu(req->OutputBufferLength)); |
| 4471 | if (buf_free_len < 0) |
| 4472 | goto out; |
| 4473 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4474 | while (idx < xattr_list_len) { |
| 4475 | stream_name = xattr_list + idx; |
| 4476 | streamlen = strlen(stream_name); |
| 4477 | idx += streamlen + 1; |
| 4478 | |
| 4479 | ksmbd_debug(SMB, "%s, len %d\n", stream_name, streamlen); |
| 4480 | |
| 4481 | if (strncmp(&stream_name[XATTR_USER_PREFIX_LEN], |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4482 | STREAM_PREFIX, STREAM_PREFIX_LEN)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4483 | continue; |
| 4484 | |
| 4485 | stream_name_len = streamlen - (XATTR_USER_PREFIX_LEN + |
| 4486 | STREAM_PREFIX_LEN); |
| 4487 | streamlen = stream_name_len; |
| 4488 | |
| 4489 | /* plus : size */ |
| 4490 | streamlen += 1; |
| 4491 | stream_buf = kmalloc(streamlen + 1, GFP_KERNEL); |
| 4492 | if (!stream_buf) |
| 4493 | break; |
| 4494 | |
| 4495 | streamlen = snprintf(stream_buf, streamlen + 1, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4496 | ":%s", &stream_name[XATTR_NAME_STREAM_LEN]); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4497 | |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 4498 | next = sizeof(struct smb2_file_stream_info) + streamlen * 2; |
| 4499 | if (next > buf_free_len) |
| 4500 | break; |
| 4501 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4502 | file_info = (struct smb2_file_stream_info *)&rsp->Buffer[nbytes]; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4503 | streamlen = smbConvertToUTF16((__le16 *)file_info->StreamName, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4504 | stream_buf, streamlen, |
| 4505 | conn->local_nls, 0); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4506 | streamlen *= 2; |
| 4507 | kfree(stream_buf); |
| 4508 | file_info->StreamNameLength = cpu_to_le32(streamlen); |
| 4509 | file_info->StreamSize = cpu_to_le64(stream_name_len); |
| 4510 | file_info->StreamAllocationSize = cpu_to_le64(stream_name_len); |
| 4511 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4512 | nbytes += next; |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 4513 | buf_free_len -= next; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4514 | file_info->NextEntryOffset = cpu_to_le32(next); |
| 4515 | } |
| 4516 | |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 4517 | if (!S_ISDIR(stat.mode) && |
| 4518 | buf_free_len >= sizeof(struct smb2_file_stream_info) + 7 * 2) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4519 | file_info = (struct smb2_file_stream_info *) |
| 4520 | &rsp->Buffer[nbytes]; |
| 4521 | streamlen = smbConvertToUTF16((__le16 *)file_info->StreamName, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4522 | "::$DATA", 7, conn->local_nls, 0); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4523 | streamlen *= 2; |
| 4524 | file_info->StreamNameLength = cpu_to_le32(streamlen); |
Namjae Jeon | 9f632331 | 2021-09-18 21:02:39 +0900 | [diff] [blame] | 4525 | file_info->StreamSize = 0; |
| 4526 | file_info->StreamAllocationSize = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4527 | nbytes += sizeof(struct smb2_file_stream_info) + streamlen; |
| 4528 | } |
| 4529 | |
| 4530 | /* last entry offset should be 0 */ |
| 4531 | file_info->NextEntryOffset = 0; |
| 4532 | out: |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 4533 | kvfree(xattr_list); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4534 | |
| 4535 | rsp->OutputBufferLength = cpu_to_le32(nbytes); |
| 4536 | inc_rfc1001_len(rsp_org, nbytes); |
| 4537 | } |
| 4538 | |
| 4539 | static void get_file_internal_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4540 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4541 | { |
| 4542 | struct smb2_file_internal_info *file_info; |
| 4543 | struct kstat stat; |
| 4544 | |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 4545 | generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp), |
| 4546 | &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4547 | file_info = (struct smb2_file_internal_info *)rsp->Buffer; |
| 4548 | file_info->IndexNumber = cpu_to_le64(stat.ino); |
| 4549 | rsp->OutputBufferLength = |
| 4550 | cpu_to_le32(sizeof(struct smb2_file_internal_info)); |
| 4551 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_internal_info)); |
| 4552 | } |
| 4553 | |
| 4554 | 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] | 4555 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4556 | { |
| 4557 | struct smb2_file_ntwrk_info *file_info; |
| 4558 | struct inode *inode; |
| 4559 | struct kstat stat; |
| 4560 | u64 time; |
| 4561 | |
| 4562 | if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4563 | pr_err("no right to read the attributes : 0x%x\n", |
| 4564 | fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4565 | return -EACCES; |
| 4566 | } |
| 4567 | |
| 4568 | file_info = (struct smb2_file_ntwrk_info *)rsp->Buffer; |
| 4569 | |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 4570 | inode = file_inode(fp->filp); |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 4571 | generic_fillattr(file_mnt_user_ns(fp->filp), inode, &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4572 | |
| 4573 | file_info->CreationTime = cpu_to_le64(fp->create_time); |
| 4574 | time = ksmbd_UnixTimeToNT(stat.atime); |
| 4575 | file_info->LastAccessTime = cpu_to_le64(time); |
| 4576 | time = ksmbd_UnixTimeToNT(stat.mtime); |
| 4577 | file_info->LastWriteTime = cpu_to_le64(time); |
| 4578 | time = ksmbd_UnixTimeToNT(stat.ctime); |
| 4579 | file_info->ChangeTime = cpu_to_le64(time); |
| 4580 | file_info->Attributes = fp->f_ci->m_fattr; |
| 4581 | file_info->AllocationSize = |
| 4582 | cpu_to_le64(get_allocation_size(inode, &stat)); |
| 4583 | file_info->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size); |
| 4584 | file_info->Reserved = cpu_to_le32(0); |
| 4585 | rsp->OutputBufferLength = |
| 4586 | cpu_to_le32(sizeof(struct smb2_file_ntwrk_info)); |
| 4587 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_ntwrk_info)); |
| 4588 | return 0; |
| 4589 | } |
| 4590 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4591 | 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] | 4592 | { |
| 4593 | struct smb2_file_ea_info *file_info; |
| 4594 | |
| 4595 | file_info = (struct smb2_file_ea_info *)rsp->Buffer; |
| 4596 | file_info->EASize = 0; |
| 4597 | rsp->OutputBufferLength = |
| 4598 | cpu_to_le32(sizeof(struct smb2_file_ea_info)); |
| 4599 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_ea_info)); |
| 4600 | } |
| 4601 | |
| 4602 | static void get_file_position_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4603 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4604 | { |
| 4605 | struct smb2_file_pos_info *file_info; |
| 4606 | |
| 4607 | file_info = (struct smb2_file_pos_info *)rsp->Buffer; |
| 4608 | file_info->CurrentByteOffset = cpu_to_le64(fp->filp->f_pos); |
| 4609 | rsp->OutputBufferLength = |
| 4610 | cpu_to_le32(sizeof(struct smb2_file_pos_info)); |
| 4611 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_pos_info)); |
| 4612 | } |
| 4613 | |
| 4614 | static void get_file_mode_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4615 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4616 | { |
| 4617 | struct smb2_file_mode_info *file_info; |
| 4618 | |
| 4619 | file_info = (struct smb2_file_mode_info *)rsp->Buffer; |
| 4620 | file_info->Mode = fp->coption & FILE_MODE_INFO_MASK; |
| 4621 | rsp->OutputBufferLength = |
| 4622 | cpu_to_le32(sizeof(struct smb2_file_mode_info)); |
| 4623 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_mode_info)); |
| 4624 | } |
| 4625 | |
| 4626 | static void get_file_compression_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4627 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4628 | { |
| 4629 | struct smb2_file_comp_info *file_info; |
| 4630 | struct kstat stat; |
| 4631 | |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 4632 | generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp), |
| 4633 | &stat); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4634 | |
| 4635 | file_info = (struct smb2_file_comp_info *)rsp->Buffer; |
| 4636 | file_info->CompressedFileSize = cpu_to_le64(stat.blocks << 9); |
| 4637 | file_info->CompressionFormat = COMPRESSION_FORMAT_NONE; |
| 4638 | file_info->CompressionUnitShift = 0; |
| 4639 | file_info->ChunkShift = 0; |
| 4640 | file_info->ClusterShift = 0; |
| 4641 | memset(&file_info->Reserved[0], 0, 3); |
| 4642 | |
| 4643 | rsp->OutputBufferLength = |
| 4644 | cpu_to_le32(sizeof(struct smb2_file_comp_info)); |
| 4645 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_comp_info)); |
| 4646 | } |
| 4647 | |
| 4648 | 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] | 4649 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4650 | { |
| 4651 | struct smb2_file_attr_tag_info *file_info; |
| 4652 | |
| 4653 | if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4654 | pr_err("no right to read the attributes : 0x%x\n", |
| 4655 | fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4656 | return -EACCES; |
| 4657 | } |
| 4658 | |
| 4659 | file_info = (struct smb2_file_attr_tag_info *)rsp->Buffer; |
| 4660 | file_info->FileAttributes = fp->f_ci->m_fattr; |
| 4661 | file_info->ReparseTag = 0; |
| 4662 | rsp->OutputBufferLength = |
| 4663 | cpu_to_le32(sizeof(struct smb2_file_attr_tag_info)); |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4664 | inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_attr_tag_info)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4665 | return 0; |
| 4666 | } |
| 4667 | |
| 4668 | static int find_file_posix_info(struct smb2_query_info_rsp *rsp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4669 | struct ksmbd_file *fp, void *rsp_org) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4670 | { |
| 4671 | struct smb311_posix_qinfo *file_info; |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 4672 | struct inode *inode = file_inode(fp->filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4673 | u64 time; |
| 4674 | |
| 4675 | file_info = (struct smb311_posix_qinfo *)rsp->Buffer; |
| 4676 | file_info->CreationTime = cpu_to_le64(fp->create_time); |
| 4677 | time = ksmbd_UnixTimeToNT(inode->i_atime); |
| 4678 | file_info->LastAccessTime = cpu_to_le64(time); |
| 4679 | time = ksmbd_UnixTimeToNT(inode->i_mtime); |
| 4680 | file_info->LastWriteTime = cpu_to_le64(time); |
| 4681 | time = ksmbd_UnixTimeToNT(inode->i_ctime); |
| 4682 | file_info->ChangeTime = cpu_to_le64(time); |
| 4683 | file_info->DosAttributes = fp->f_ci->m_fattr; |
| 4684 | file_info->Inode = cpu_to_le64(inode->i_ino); |
| 4685 | file_info->EndOfFile = cpu_to_le64(inode->i_size); |
| 4686 | file_info->AllocationSize = cpu_to_le64(inode->i_blocks << 9); |
| 4687 | file_info->HardLinks = cpu_to_le32(inode->i_nlink); |
| 4688 | file_info->Mode = cpu_to_le32(inode->i_mode); |
| 4689 | file_info->DeviceId = cpu_to_le32(inode->i_rdev); |
| 4690 | rsp->OutputBufferLength = |
| 4691 | cpu_to_le32(sizeof(struct smb311_posix_qinfo)); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4692 | inc_rfc1001_len(rsp_org, sizeof(struct smb311_posix_qinfo)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4693 | return 0; |
| 4694 | } |
| 4695 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4696 | static int smb2_get_info_file(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4697 | struct smb2_query_info_req *req, |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 4698 | struct smb2_query_info_rsp *rsp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4699 | { |
| 4700 | struct ksmbd_file *fp; |
| 4701 | int fileinfoclass = 0; |
| 4702 | int rc = 0; |
| 4703 | int file_infoclass_size; |
| 4704 | unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID; |
| 4705 | |
| 4706 | if (test_share_config_flag(work->tcon->share_conf, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4707 | KSMBD_SHARE_FLAG_PIPE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4708 | /* smb2 info file called for pipe */ |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 4709 | return smb2_get_info_file_pipe(work->sess, req, rsp, |
| 4710 | work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4711 | } |
| 4712 | |
| 4713 | if (work->next_smb2_rcv_hdr_off) { |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 4714 | if (!has_file_id(le64_to_cpu(req->VolatileFileId))) { |
| 4715 | ksmbd_debug(SMB, "Compound request set FID = %llu\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4716 | work->compound_fid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4717 | id = work->compound_fid; |
| 4718 | pid = work->compound_pfid; |
| 4719 | } |
| 4720 | } |
| 4721 | |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 4722 | if (!has_file_id(id)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4723 | id = le64_to_cpu(req->VolatileFileId); |
| 4724 | pid = le64_to_cpu(req->PersistentFileId); |
| 4725 | } |
| 4726 | |
| 4727 | fp = ksmbd_lookup_fd_slow(work, id, pid); |
| 4728 | if (!fp) |
| 4729 | return -ENOENT; |
| 4730 | |
| 4731 | fileinfoclass = req->FileInfoClass; |
| 4732 | |
| 4733 | switch (fileinfoclass) { |
| 4734 | case FILE_ACCESS_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 4735 | get_file_access_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4736 | file_infoclass_size = FILE_ACCESS_INFORMATION_SIZE; |
| 4737 | break; |
| 4738 | |
| 4739 | case FILE_BASIC_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 4740 | rc = get_file_basic_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4741 | file_infoclass_size = FILE_BASIC_INFORMATION_SIZE; |
| 4742 | break; |
| 4743 | |
| 4744 | case FILE_STANDARD_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 4745 | get_file_standard_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4746 | file_infoclass_size = FILE_STANDARD_INFORMATION_SIZE; |
| 4747 | break; |
| 4748 | |
| 4749 | case FILE_ALIGNMENT_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 4750 | get_file_alignment_info(rsp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4751 | file_infoclass_size = FILE_ALIGNMENT_INFORMATION_SIZE; |
| 4752 | break; |
| 4753 | |
| 4754 | case FILE_ALL_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 4755 | rc = get_file_all_info(work, rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4756 | file_infoclass_size = FILE_ALL_INFORMATION_SIZE; |
| 4757 | break; |
| 4758 | |
| 4759 | case FILE_ALTERNATE_NAME_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 4760 | get_file_alternate_info(work, rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4761 | file_infoclass_size = FILE_ALTERNATE_NAME_INFORMATION_SIZE; |
| 4762 | break; |
| 4763 | |
| 4764 | case FILE_STREAM_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 4765 | get_file_stream_info(work, rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4766 | file_infoclass_size = FILE_STREAM_INFORMATION_SIZE; |
| 4767 | break; |
| 4768 | |
| 4769 | case FILE_INTERNAL_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 4770 | get_file_internal_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4771 | file_infoclass_size = FILE_INTERNAL_INFORMATION_SIZE; |
| 4772 | break; |
| 4773 | |
| 4774 | case FILE_NETWORK_OPEN_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 4775 | rc = get_file_network_open_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4776 | file_infoclass_size = FILE_NETWORK_OPEN_INFORMATION_SIZE; |
| 4777 | break; |
| 4778 | |
| 4779 | case FILE_EA_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 4780 | get_file_ea_info(rsp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4781 | file_infoclass_size = FILE_EA_INFORMATION_SIZE; |
| 4782 | break; |
| 4783 | |
| 4784 | case FILE_FULL_EA_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 4785 | rc = smb2_get_ea(work, fp, req, rsp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4786 | file_infoclass_size = FILE_FULL_EA_INFORMATION_SIZE; |
| 4787 | break; |
| 4788 | |
| 4789 | case FILE_POSITION_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 4790 | get_file_position_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4791 | file_infoclass_size = FILE_POSITION_INFORMATION_SIZE; |
| 4792 | break; |
| 4793 | |
| 4794 | case FILE_MODE_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 4795 | get_file_mode_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4796 | file_infoclass_size = FILE_MODE_INFORMATION_SIZE; |
| 4797 | break; |
| 4798 | |
| 4799 | case FILE_COMPRESSION_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 4800 | get_file_compression_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4801 | file_infoclass_size = FILE_COMPRESSION_INFORMATION_SIZE; |
| 4802 | break; |
| 4803 | |
| 4804 | case FILE_ATTRIBUTE_TAG_INFORMATION: |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 4805 | rc = get_file_attribute_tag_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4806 | file_infoclass_size = FILE_ATTRIBUTE_TAG_INFORMATION_SIZE; |
| 4807 | break; |
| 4808 | case SMB_FIND_FILE_POSIX_INFO: |
| 4809 | if (!work->tcon->posix_extensions) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4810 | 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] | 4811 | rc = -EOPNOTSUPP; |
| 4812 | } else { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 4813 | rc = find_file_posix_info(rsp, fp, work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4814 | file_infoclass_size = sizeof(struct smb311_posix_qinfo); |
| 4815 | } |
| 4816 | break; |
| 4817 | default: |
| 4818 | ksmbd_debug(SMB, "fileinfoclass %d not supported yet\n", |
| 4819 | fileinfoclass); |
| 4820 | rc = -EOPNOTSUPP; |
| 4821 | } |
| 4822 | if (!rc) |
| 4823 | rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength), |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 4824 | rsp, work->response_buf, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4825 | file_infoclass_size); |
| 4826 | ksmbd_fd_put(work, fp); |
| 4827 | return rc; |
| 4828 | } |
| 4829 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4830 | static int smb2_get_info_filesystem(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 4831 | struct smb2_query_info_req *req, |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 4832 | struct smb2_query_info_rsp *rsp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4833 | { |
| 4834 | struct ksmbd_session *sess = work->sess; |
| 4835 | struct ksmbd_conn *conn = sess->conn; |
| 4836 | struct ksmbd_share_config *share = work->tcon->share_conf; |
| 4837 | int fsinfoclass = 0; |
| 4838 | struct kstatfs stfs; |
| 4839 | struct path path; |
| 4840 | int rc = 0, len; |
| 4841 | int fs_infoclass_size = 0; |
| 4842 | |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 4843 | rc = kern_path(share->path, LOOKUP_NO_SYMLINKS, &path); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4844 | if (rc) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4845 | pr_err("cannot create vfs path\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4846 | return -EIO; |
| 4847 | } |
| 4848 | |
| 4849 | rc = vfs_statfs(&path, &stfs); |
| 4850 | if (rc) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 4851 | pr_err("cannot do stat of path %s\n", share->path); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4852 | path_put(&path); |
| 4853 | return -EIO; |
| 4854 | } |
| 4855 | |
| 4856 | fsinfoclass = req->FileInfoClass; |
| 4857 | |
| 4858 | switch (fsinfoclass) { |
| 4859 | case FS_DEVICE_INFORMATION: |
| 4860 | { |
| 4861 | struct filesystem_device_info *info; |
| 4862 | |
| 4863 | info = (struct filesystem_device_info *)rsp->Buffer; |
| 4864 | |
| 4865 | info->DeviceType = cpu_to_le32(stfs.f_type); |
| 4866 | info->DeviceCharacteristics = cpu_to_le32(0x00000020); |
| 4867 | rsp->OutputBufferLength = cpu_to_le32(8); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 4868 | inc_rfc1001_len(work->response_buf, 8); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4869 | fs_infoclass_size = FS_DEVICE_INFORMATION_SIZE; |
| 4870 | break; |
| 4871 | } |
| 4872 | case FS_ATTRIBUTE_INFORMATION: |
| 4873 | { |
| 4874 | struct filesystem_attribute_info *info; |
| 4875 | size_t sz; |
| 4876 | |
| 4877 | info = (struct filesystem_attribute_info *)rsp->Buffer; |
| 4878 | info->Attributes = cpu_to_le32(FILE_SUPPORTS_OBJECT_IDS | |
| 4879 | FILE_PERSISTENT_ACLS | |
| 4880 | FILE_UNICODE_ON_DISK | |
| 4881 | FILE_CASE_PRESERVED_NAMES | |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 4882 | FILE_CASE_SENSITIVE_SEARCH | |
| 4883 | FILE_SUPPORTS_BLOCK_REFCOUNTING); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4884 | |
| 4885 | info->Attributes |= cpu_to_le32(server_conf.share_fake_fscaps); |
| 4886 | |
| 4887 | info->MaxPathNameComponentLength = cpu_to_le32(stfs.f_namelen); |
| 4888 | len = smbConvertToUTF16((__le16 *)info->FileSystemName, |
| 4889 | "NTFS", PATH_MAX, conn->local_nls, 0); |
| 4890 | len = len * 2; |
| 4891 | info->FileSystemNameLen = cpu_to_le32(len); |
| 4892 | sz = sizeof(struct filesystem_attribute_info) - 2 + len; |
| 4893 | rsp->OutputBufferLength = cpu_to_le32(sz); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 4894 | inc_rfc1001_len(work->response_buf, sz); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4895 | fs_infoclass_size = FS_ATTRIBUTE_INFORMATION_SIZE; |
| 4896 | break; |
| 4897 | } |
| 4898 | case FS_VOLUME_INFORMATION: |
| 4899 | { |
| 4900 | struct filesystem_vol_info *info; |
| 4901 | size_t sz; |
Namjae Jeon | 5d2f0b1 | 2021-10-31 09:53:50 +0900 | [diff] [blame] | 4902 | unsigned int serial_crc = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4903 | |
| 4904 | info = (struct filesystem_vol_info *)(rsp->Buffer); |
| 4905 | info->VolumeCreationTime = 0; |
Namjae Jeon | 5d2f0b1 | 2021-10-31 09:53:50 +0900 | [diff] [blame] | 4906 | serial_crc = crc32_le(serial_crc, share->name, |
| 4907 | strlen(share->name)); |
| 4908 | serial_crc = crc32_le(serial_crc, share->path, |
| 4909 | strlen(share->path)); |
| 4910 | serial_crc = crc32_le(serial_crc, ksmbd_netbios_name(), |
| 4911 | strlen(ksmbd_netbios_name())); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4912 | /* Taking dummy value of serial number*/ |
Namjae Jeon | 5d2f0b1 | 2021-10-31 09:53:50 +0900 | [diff] [blame] | 4913 | info->SerialNumber = cpu_to_le32(serial_crc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4914 | len = smbConvertToUTF16((__le16 *)info->VolumeLabel, |
| 4915 | share->name, PATH_MAX, |
| 4916 | conn->local_nls, 0); |
| 4917 | len = len * 2; |
| 4918 | info->VolumeLabelSize = cpu_to_le32(len); |
| 4919 | info->Reserved = 0; |
| 4920 | sz = sizeof(struct filesystem_vol_info) - 2 + len; |
| 4921 | rsp->OutputBufferLength = cpu_to_le32(sz); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 4922 | inc_rfc1001_len(work->response_buf, sz); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4923 | fs_infoclass_size = FS_VOLUME_INFORMATION_SIZE; |
| 4924 | break; |
| 4925 | } |
| 4926 | case FS_SIZE_INFORMATION: |
| 4927 | { |
| 4928 | struct filesystem_info *info; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4929 | |
| 4930 | info = (struct filesystem_info *)(rsp->Buffer); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4931 | info->TotalAllocationUnits = cpu_to_le64(stfs.f_blocks); |
| 4932 | info->FreeAllocationUnits = cpu_to_le64(stfs.f_bfree); |
Namjae Jeon | ee81cae | 2021-06-26 22:32:34 +0900 | [diff] [blame] | 4933 | info->SectorsPerAllocationUnit = cpu_to_le32(1); |
| 4934 | info->BytesPerSector = cpu_to_le32(stfs.f_bsize); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4935 | rsp->OutputBufferLength = cpu_to_le32(24); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 4936 | inc_rfc1001_len(work->response_buf, 24); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4937 | fs_infoclass_size = FS_SIZE_INFORMATION_SIZE; |
| 4938 | break; |
| 4939 | } |
| 4940 | case FS_FULL_SIZE_INFORMATION: |
| 4941 | { |
| 4942 | struct smb2_fs_full_size_info *info; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4943 | |
| 4944 | info = (struct smb2_fs_full_size_info *)(rsp->Buffer); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4945 | info->TotalAllocationUnits = cpu_to_le64(stfs.f_blocks); |
| 4946 | info->CallerAvailableAllocationUnits = |
| 4947 | cpu_to_le64(stfs.f_bavail); |
| 4948 | info->ActualAvailableAllocationUnits = |
| 4949 | cpu_to_le64(stfs.f_bfree); |
Namjae Jeon | ee81cae | 2021-06-26 22:32:34 +0900 | [diff] [blame] | 4950 | info->SectorsPerAllocationUnit = cpu_to_le32(1); |
| 4951 | info->BytesPerSector = cpu_to_le32(stfs.f_bsize); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4952 | rsp->OutputBufferLength = cpu_to_le32(32); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 4953 | inc_rfc1001_len(work->response_buf, 32); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4954 | fs_infoclass_size = FS_FULL_SIZE_INFORMATION_SIZE; |
| 4955 | break; |
| 4956 | } |
| 4957 | case FS_OBJECT_ID_INFORMATION: |
| 4958 | { |
| 4959 | struct object_id_info *info; |
| 4960 | |
| 4961 | info = (struct object_id_info *)(rsp->Buffer); |
| 4962 | |
| 4963 | if (!user_guest(sess->user)) |
| 4964 | memcpy(info->objid, user_passkey(sess->user), 16); |
| 4965 | else |
| 4966 | memset(info->objid, 0, 16); |
| 4967 | |
| 4968 | info->extended_info.magic = cpu_to_le32(EXTENDED_INFO_MAGIC); |
| 4969 | info->extended_info.version = cpu_to_le32(1); |
| 4970 | info->extended_info.release = cpu_to_le32(1); |
| 4971 | info->extended_info.rel_date = 0; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 4972 | 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] | 4973 | rsp->OutputBufferLength = cpu_to_le32(64); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 4974 | inc_rfc1001_len(work->response_buf, 64); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4975 | fs_infoclass_size = FS_OBJECT_ID_INFORMATION_SIZE; |
| 4976 | break; |
| 4977 | } |
| 4978 | case FS_SECTOR_SIZE_INFORMATION: |
| 4979 | { |
| 4980 | struct smb3_fs_ss_info *info; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4981 | |
| 4982 | info = (struct smb3_fs_ss_info *)(rsp->Buffer); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4983 | |
Namjae Jeon | 131bac1 | 2021-06-22 16:20:47 +0900 | [diff] [blame] | 4984 | info->LogicalBytesPerSector = cpu_to_le32(stfs.f_bsize); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4985 | info->PhysicalBytesPerSectorForAtomicity = |
Namjae Jeon | 131bac1 | 2021-06-22 16:20:47 +0900 | [diff] [blame] | 4986 | cpu_to_le32(stfs.f_bsize); |
| 4987 | info->PhysicalBytesPerSectorForPerf = cpu_to_le32(stfs.f_bsize); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4988 | info->FSEffPhysicalBytesPerSectorForAtomicity = |
Namjae Jeon | 131bac1 | 2021-06-22 16:20:47 +0900 | [diff] [blame] | 4989 | cpu_to_le32(stfs.f_bsize); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4990 | info->Flags = cpu_to_le32(SSINFO_FLAGS_ALIGNED_DEVICE | |
| 4991 | SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE); |
| 4992 | info->ByteOffsetForSectorAlignment = 0; |
| 4993 | info->ByteOffsetForPartitionAlignment = 0; |
| 4994 | rsp->OutputBufferLength = cpu_to_le32(28); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 4995 | inc_rfc1001_len(work->response_buf, 28); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 4996 | fs_infoclass_size = FS_SECTOR_SIZE_INFORMATION_SIZE; |
| 4997 | break; |
| 4998 | } |
| 4999 | case FS_CONTROL_INFORMATION: |
| 5000 | { |
| 5001 | /* |
| 5002 | * TODO : The current implementation is based on |
| 5003 | * test result with win7(NTFS) server. It's need to |
| 5004 | * modify this to get valid Quota values |
| 5005 | * from Linux kernel |
| 5006 | */ |
| 5007 | struct smb2_fs_control_info *info; |
| 5008 | |
| 5009 | info = (struct smb2_fs_control_info *)(rsp->Buffer); |
| 5010 | info->FreeSpaceStartFiltering = 0; |
| 5011 | info->FreeSpaceThreshold = 0; |
| 5012 | info->FreeSpaceStopFiltering = 0; |
| 5013 | info->DefaultQuotaThreshold = cpu_to_le64(SMB2_NO_FID); |
| 5014 | info->DefaultQuotaLimit = cpu_to_le64(SMB2_NO_FID); |
| 5015 | info->Padding = 0; |
| 5016 | rsp->OutputBufferLength = cpu_to_le32(48); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 5017 | inc_rfc1001_len(work->response_buf, 48); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5018 | fs_infoclass_size = FS_CONTROL_INFORMATION_SIZE; |
| 5019 | break; |
| 5020 | } |
| 5021 | case FS_POSIX_INFORMATION: |
| 5022 | { |
| 5023 | struct filesystem_posix_info *info; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5024 | |
| 5025 | if (!work->tcon->posix_extensions) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5026 | 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] | 5027 | rc = -EOPNOTSUPP; |
| 5028 | } else { |
| 5029 | info = (struct filesystem_posix_info *)(rsp->Buffer); |
Namjae Jeon | ee81cae | 2021-06-26 22:32:34 +0900 | [diff] [blame] | 5030 | info->OptimalTransferSize = cpu_to_le32(stfs.f_bsize); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5031 | info->BlockSize = cpu_to_le32(stfs.f_bsize); |
| 5032 | info->TotalBlocks = cpu_to_le64(stfs.f_blocks); |
| 5033 | info->BlocksAvail = cpu_to_le64(stfs.f_bfree); |
| 5034 | info->UserBlocksAvail = cpu_to_le64(stfs.f_bavail); |
| 5035 | info->TotalFileNodes = cpu_to_le64(stfs.f_files); |
| 5036 | info->FreeFileNodes = cpu_to_le64(stfs.f_ffree); |
| 5037 | rsp->OutputBufferLength = cpu_to_le32(56); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 5038 | inc_rfc1001_len(work->response_buf, 56); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5039 | fs_infoclass_size = FS_POSIX_INFORMATION_SIZE; |
| 5040 | } |
| 5041 | break; |
| 5042 | } |
| 5043 | default: |
| 5044 | path_put(&path); |
| 5045 | return -EOPNOTSUPP; |
| 5046 | } |
| 5047 | rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength), |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 5048 | rsp, work->response_buf, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5049 | fs_infoclass_size); |
| 5050 | path_put(&path); |
| 5051 | return rc; |
| 5052 | } |
| 5053 | |
| 5054 | static int smb2_get_info_sec(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5055 | struct smb2_query_info_req *req, |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 5056 | struct smb2_query_info_rsp *rsp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5057 | { |
| 5058 | struct ksmbd_file *fp; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 5059 | struct user_namespace *user_ns; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5060 | struct smb_ntsd *pntsd = (struct smb_ntsd *)rsp->Buffer, *ppntsd = NULL; |
| 5061 | struct smb_fattr fattr = {{0}}; |
| 5062 | struct inode *inode; |
| 5063 | __u32 secdesclen; |
| 5064 | unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID; |
| 5065 | int addition_info = le32_to_cpu(req->AdditionalInformation); |
| 5066 | int rc; |
| 5067 | |
Namjae Jeon | e294f78 | 2021-06-28 15:26:37 +0900 | [diff] [blame] | 5068 | if (addition_info & ~(OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO | |
| 5069 | PROTECTED_DACL_SECINFO | |
| 5070 | UNPROTECTED_DACL_SECINFO)) { |
| 5071 | pr_err("Unsupported addition info: 0x%x)\n", |
| 5072 | addition_info); |
Sebastian Gottschall | ced2b26 | 2021-04-27 15:33:54 +0900 | [diff] [blame] | 5073 | |
| 5074 | pntsd->revision = cpu_to_le16(1); |
| 5075 | pntsd->type = cpu_to_le16(SELF_RELATIVE | DACL_PROTECTED); |
| 5076 | pntsd->osidoffset = 0; |
| 5077 | pntsd->gsidoffset = 0; |
| 5078 | pntsd->sacloffset = 0; |
| 5079 | pntsd->dacloffset = 0; |
| 5080 | |
| 5081 | secdesclen = sizeof(struct smb_ntsd); |
| 5082 | rsp->OutputBufferLength = cpu_to_le32(secdesclen); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 5083 | inc_rfc1001_len(work->response_buf, secdesclen); |
Sebastian Gottschall | ced2b26 | 2021-04-27 15:33:54 +0900 | [diff] [blame] | 5084 | |
| 5085 | return 0; |
| 5086 | } |
| 5087 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5088 | if (work->next_smb2_rcv_hdr_off) { |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 5089 | if (!has_file_id(le64_to_cpu(req->VolatileFileId))) { |
| 5090 | ksmbd_debug(SMB, "Compound request set FID = %llu\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5091 | work->compound_fid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5092 | id = work->compound_fid; |
| 5093 | pid = work->compound_pfid; |
| 5094 | } |
| 5095 | } |
| 5096 | |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 5097 | if (!has_file_id(id)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5098 | id = le64_to_cpu(req->VolatileFileId); |
| 5099 | pid = le64_to_cpu(req->PersistentFileId); |
| 5100 | } |
| 5101 | |
| 5102 | fp = ksmbd_lookup_fd_slow(work, id, pid); |
| 5103 | if (!fp) |
| 5104 | return -ENOENT; |
| 5105 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 5106 | user_ns = file_mnt_user_ns(fp->filp); |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 5107 | inode = file_inode(fp->filp); |
Christian Brauner | 43205ca | 2021-08-23 17:13:50 +0200 | [diff] [blame] | 5108 | ksmbd_acls_fattr(&fattr, user_ns, inode); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5109 | |
| 5110 | if (test_share_config_flag(work->tcon->share_conf, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5111 | KSMBD_SHARE_FLAG_ACL_XATTR)) |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 5112 | ksmbd_vfs_get_sd_xattr(work->conn, user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 5113 | fp->filp->f_path.dentry, &ppntsd); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5114 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 5115 | rc = build_sec_desc(user_ns, pntsd, ppntsd, addition_info, |
| 5116 | &secdesclen, &fattr); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5117 | posix_acl_release(fattr.cf_acls); |
| 5118 | posix_acl_release(fattr.cf_dacls); |
| 5119 | kfree(ppntsd); |
| 5120 | ksmbd_fd_put(work, fp); |
| 5121 | if (rc) |
| 5122 | return rc; |
| 5123 | |
| 5124 | rsp->OutputBufferLength = cpu_to_le32(secdesclen); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 5125 | inc_rfc1001_len(work->response_buf, secdesclen); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5126 | return 0; |
| 5127 | } |
| 5128 | |
| 5129 | /** |
| 5130 | * smb2_query_info() - handler for smb2 query info command |
| 5131 | * @work: smb work containing query info request buffer |
| 5132 | * |
| 5133 | * Return: 0 on success, otherwise error |
| 5134 | */ |
| 5135 | int smb2_query_info(struct ksmbd_work *work) |
| 5136 | { |
| 5137 | struct smb2_query_info_req *req; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 5138 | struct smb2_query_info_rsp *rsp; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5139 | int rc = 0; |
| 5140 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5141 | WORK_BUFFERS(work, req, rsp); |
| 5142 | |
| 5143 | ksmbd_debug(SMB, "GOT query info request\n"); |
| 5144 | |
| 5145 | switch (req->InfoType) { |
| 5146 | case SMB2_O_INFO_FILE: |
| 5147 | ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILE\n"); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 5148 | rc = smb2_get_info_file(work, req, rsp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5149 | break; |
| 5150 | case SMB2_O_INFO_FILESYSTEM: |
| 5151 | ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILESYSTEM\n"); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 5152 | rc = smb2_get_info_filesystem(work, req, rsp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5153 | break; |
| 5154 | case SMB2_O_INFO_SECURITY: |
| 5155 | ksmbd_debug(SMB, "GOT SMB2_O_INFO_SECURITY\n"); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 5156 | rc = smb2_get_info_sec(work, req, rsp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5157 | break; |
| 5158 | default: |
| 5159 | ksmbd_debug(SMB, "InfoType %d not supported yet\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5160 | req->InfoType); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5161 | rc = -EOPNOTSUPP; |
| 5162 | } |
| 5163 | |
| 5164 | if (rc < 0) { |
| 5165 | if (rc == -EACCES) |
| 5166 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 5167 | else if (rc == -ENOENT) |
| 5168 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 5169 | else if (rc == -EIO) |
| 5170 | rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR; |
| 5171 | else if (rc == -EOPNOTSUPP || rsp->hdr.Status == 0) |
| 5172 | rsp->hdr.Status = STATUS_INVALID_INFO_CLASS; |
| 5173 | smb2_set_err_rsp(work); |
| 5174 | |
| 5175 | ksmbd_debug(SMB, "error while processing smb2 query rc = %d\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5176 | rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5177 | return rc; |
| 5178 | } |
| 5179 | rsp->StructureSize = cpu_to_le16(9); |
| 5180 | rsp->OutputBufferOffset = cpu_to_le16(72); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 5181 | inc_rfc1001_len(work->response_buf, 8); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5182 | return 0; |
| 5183 | } |
| 5184 | |
| 5185 | /** |
| 5186 | * smb2_close_pipe() - handler for closing IPC pipe |
| 5187 | * @work: smb work containing close request buffer |
| 5188 | * |
| 5189 | * Return: 0 |
| 5190 | */ |
| 5191 | static noinline int smb2_close_pipe(struct ksmbd_work *work) |
| 5192 | { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5193 | u64 id; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 5194 | struct smb2_close_req *req = smb2_get_msg(work->request_buf); |
| 5195 | struct smb2_close_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5196 | |
| 5197 | id = le64_to_cpu(req->VolatileFileId); |
| 5198 | ksmbd_session_rpc_close(work->sess, id); |
| 5199 | |
| 5200 | rsp->StructureSize = cpu_to_le16(60); |
| 5201 | rsp->Flags = 0; |
| 5202 | rsp->Reserved = 0; |
| 5203 | rsp->CreationTime = 0; |
| 5204 | rsp->LastAccessTime = 0; |
| 5205 | rsp->LastWriteTime = 0; |
| 5206 | rsp->ChangeTime = 0; |
| 5207 | rsp->AllocationSize = 0; |
| 5208 | rsp->EndOfFile = 0; |
| 5209 | rsp->Attributes = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 5210 | inc_rfc1001_len(work->response_buf, 60); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5211 | return 0; |
| 5212 | } |
| 5213 | |
| 5214 | /** |
| 5215 | * smb2_close() - handler for smb2 close file command |
| 5216 | * @work: smb work containing close request buffer |
| 5217 | * |
| 5218 | * Return: 0 |
| 5219 | */ |
| 5220 | int smb2_close(struct ksmbd_work *work) |
| 5221 | { |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 5222 | u64 volatile_id = KSMBD_NO_FID; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5223 | u64 sess_id; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5224 | struct smb2_close_req *req; |
| 5225 | struct smb2_close_rsp *rsp; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5226 | struct ksmbd_conn *conn = work->conn; |
| 5227 | struct ksmbd_file *fp; |
| 5228 | struct inode *inode; |
| 5229 | u64 time; |
| 5230 | int err = 0; |
| 5231 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5232 | WORK_BUFFERS(work, req, rsp); |
| 5233 | |
| 5234 | if (test_share_config_flag(work->tcon->share_conf, |
| 5235 | KSMBD_SHARE_FLAG_PIPE)) { |
| 5236 | ksmbd_debug(SMB, "IPC pipe close request\n"); |
| 5237 | return smb2_close_pipe(work); |
| 5238 | } |
| 5239 | |
| 5240 | sess_id = le64_to_cpu(req->hdr.SessionId); |
| 5241 | if (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS) |
| 5242 | sess_id = work->compound_sid; |
| 5243 | |
| 5244 | work->compound_sid = 0; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5245 | if (check_session_id(conn, sess_id)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5246 | work->compound_sid = sess_id; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5247 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5248 | rsp->hdr.Status = STATUS_USER_SESSION_DELETED; |
| 5249 | if (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS) |
| 5250 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 5251 | err = -EBADF; |
| 5252 | goto out; |
| 5253 | } |
| 5254 | |
| 5255 | if (work->next_smb2_rcv_hdr_off && |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 5256 | !has_file_id(le64_to_cpu(req->VolatileFileId))) { |
| 5257 | if (!has_file_id(work->compound_fid)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5258 | /* file already closed, return FILE_CLOSED */ |
| 5259 | ksmbd_debug(SMB, "file already closed\n"); |
| 5260 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 5261 | err = -EBADF; |
| 5262 | goto out; |
| 5263 | } else { |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 5264 | ksmbd_debug(SMB, |
| 5265 | "Compound request set FID = %llu:%llu\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5266 | work->compound_fid, |
| 5267 | work->compound_pfid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5268 | volatile_id = work->compound_fid; |
| 5269 | |
| 5270 | /* file closed, stored id is not valid anymore */ |
| 5271 | work->compound_fid = KSMBD_NO_FID; |
| 5272 | work->compound_pfid = KSMBD_NO_FID; |
| 5273 | } |
| 5274 | } else { |
| 5275 | volatile_id = le64_to_cpu(req->VolatileFileId); |
| 5276 | } |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 5277 | ksmbd_debug(SMB, "volatile_id = %llu\n", volatile_id); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5278 | |
| 5279 | rsp->StructureSize = cpu_to_le16(60); |
| 5280 | rsp->Reserved = 0; |
| 5281 | |
| 5282 | if (req->Flags == SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB) { |
| 5283 | fp = ksmbd_lookup_fd_fast(work, volatile_id); |
| 5284 | if (!fp) { |
| 5285 | err = -ENOENT; |
| 5286 | goto out; |
| 5287 | } |
| 5288 | |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 5289 | inode = file_inode(fp->filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5290 | rsp->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB; |
| 5291 | rsp->AllocationSize = S_ISDIR(inode->i_mode) ? 0 : |
| 5292 | cpu_to_le64(inode->i_blocks << 9); |
| 5293 | rsp->EndOfFile = cpu_to_le64(inode->i_size); |
| 5294 | rsp->Attributes = fp->f_ci->m_fattr; |
| 5295 | rsp->CreationTime = cpu_to_le64(fp->create_time); |
| 5296 | time = ksmbd_UnixTimeToNT(inode->i_atime); |
| 5297 | rsp->LastAccessTime = cpu_to_le64(time); |
| 5298 | time = ksmbd_UnixTimeToNT(inode->i_mtime); |
| 5299 | rsp->LastWriteTime = cpu_to_le64(time); |
| 5300 | time = ksmbd_UnixTimeToNT(inode->i_ctime); |
| 5301 | rsp->ChangeTime = cpu_to_le64(time); |
| 5302 | ksmbd_fd_put(work, fp); |
| 5303 | } else { |
| 5304 | rsp->Flags = 0; |
| 5305 | rsp->AllocationSize = 0; |
| 5306 | rsp->EndOfFile = 0; |
| 5307 | rsp->Attributes = 0; |
| 5308 | rsp->CreationTime = 0; |
| 5309 | rsp->LastAccessTime = 0; |
| 5310 | rsp->LastWriteTime = 0; |
| 5311 | rsp->ChangeTime = 0; |
| 5312 | } |
| 5313 | |
| 5314 | err = ksmbd_close_fd(work, volatile_id); |
| 5315 | out: |
| 5316 | if (err) { |
| 5317 | if (rsp->hdr.Status == 0) |
| 5318 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 5319 | smb2_set_err_rsp(work); |
| 5320 | } else { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 5321 | inc_rfc1001_len(work->response_buf, 60); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5322 | } |
| 5323 | |
| 5324 | return 0; |
| 5325 | } |
| 5326 | |
| 5327 | /** |
| 5328 | * smb2_echo() - handler for smb2 echo(ping) command |
| 5329 | * @work: smb work containing echo request buffer |
| 5330 | * |
| 5331 | * Return: 0 |
| 5332 | */ |
| 5333 | int smb2_echo(struct ksmbd_work *work) |
| 5334 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 5335 | struct smb2_echo_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5336 | |
| 5337 | rsp->StructureSize = cpu_to_le16(4); |
| 5338 | rsp->Reserved = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 5339 | inc_rfc1001_len(work->response_buf, 4); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5340 | return 0; |
| 5341 | } |
| 5342 | |
Christian Brauner | da1e7ad | 2021-08-23 17:13:47 +0200 | [diff] [blame] | 5343 | static int smb2_rename(struct ksmbd_work *work, |
| 5344 | struct ksmbd_file *fp, |
| 5345 | struct user_namespace *user_ns, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5346 | struct smb2_file_rename_info *file_info, |
| 5347 | struct nls_table *local_nls) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5348 | { |
| 5349 | struct ksmbd_share_config *share = fp->tcon->share_conf; |
| 5350 | char *new_name = NULL, *abs_oldname = NULL, *old_name = NULL; |
| 5351 | char *pathname = NULL; |
| 5352 | struct path path; |
| 5353 | bool file_present = true; |
| 5354 | int rc; |
| 5355 | |
| 5356 | ksmbd_debug(SMB, "setting FILE_RENAME_INFO\n"); |
| 5357 | pathname = kmalloc(PATH_MAX, GFP_KERNEL); |
| 5358 | if (!pathname) |
| 5359 | return -ENOMEM; |
| 5360 | |
| 5361 | abs_oldname = d_path(&fp->filp->f_path, pathname, PATH_MAX); |
| 5362 | if (IS_ERR(abs_oldname)) { |
| 5363 | rc = -EINVAL; |
| 5364 | goto out; |
| 5365 | } |
| 5366 | old_name = strrchr(abs_oldname, '/'); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5367 | if (old_name && old_name[1] != '\0') { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5368 | old_name++; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5369 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5370 | ksmbd_debug(SMB, "can't get last component in path %s\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5371 | abs_oldname); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5372 | rc = -ENOENT; |
| 5373 | goto out; |
| 5374 | } |
| 5375 | |
| 5376 | new_name = smb2_get_name(share, |
| 5377 | file_info->FileName, |
| 5378 | le32_to_cpu(file_info->FileNameLength), |
| 5379 | local_nls); |
| 5380 | if (IS_ERR(new_name)) { |
| 5381 | rc = PTR_ERR(new_name); |
| 5382 | goto out; |
| 5383 | } |
| 5384 | |
| 5385 | if (strchr(new_name, ':')) { |
| 5386 | int s_type; |
| 5387 | char *xattr_stream_name, *stream_name = NULL; |
| 5388 | size_t xattr_stream_size; |
| 5389 | int len; |
| 5390 | |
| 5391 | rc = parse_stream_name(new_name, &stream_name, &s_type); |
| 5392 | if (rc < 0) |
| 5393 | goto out; |
| 5394 | |
| 5395 | len = strlen(new_name); |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 5396 | if (len > 0 && new_name[len - 1] != '/') { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5397 | pr_err("not allow base filename in rename\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5398 | rc = -ESHARE; |
| 5399 | goto out; |
| 5400 | } |
| 5401 | |
| 5402 | rc = ksmbd_vfs_xattr_stream_name(stream_name, |
| 5403 | &xattr_stream_name, |
| 5404 | &xattr_stream_size, |
| 5405 | s_type); |
| 5406 | if (rc) |
| 5407 | goto out; |
| 5408 | |
Christian Brauner | da1e7ad | 2021-08-23 17:13:47 +0200 | [diff] [blame] | 5409 | rc = ksmbd_vfs_setxattr(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 5410 | fp->filp->f_path.dentry, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5411 | xattr_stream_name, |
| 5412 | NULL, 0, 0); |
| 5413 | if (rc < 0) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5414 | pr_err("failed to store stream name in xattr: %d\n", |
| 5415 | rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5416 | rc = -EINVAL; |
| 5417 | goto out; |
| 5418 | } |
| 5419 | |
| 5420 | goto out; |
| 5421 | } |
| 5422 | |
| 5423 | ksmbd_debug(SMB, "new name %s\n", new_name); |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 5424 | rc = ksmbd_vfs_kern_path(work, new_name, LOOKUP_NO_SYMLINKS, &path, 1); |
| 5425 | if (rc) { |
| 5426 | if (rc != -ENOENT) |
| 5427 | goto out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5428 | file_present = false; |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 5429 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5430 | path_put(&path); |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 5431 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5432 | |
| 5433 | if (ksmbd_share_veto_filename(share, new_name)) { |
| 5434 | rc = -ENOENT; |
| 5435 | ksmbd_debug(SMB, "Can't rename vetoed file: %s\n", new_name); |
| 5436 | goto out; |
| 5437 | } |
| 5438 | |
| 5439 | if (file_info->ReplaceIfExists) { |
| 5440 | if (file_present) { |
| 5441 | rc = ksmbd_vfs_remove_file(work, new_name); |
| 5442 | if (rc) { |
| 5443 | if (rc != -ENOTEMPTY) |
| 5444 | rc = -EINVAL; |
| 5445 | ksmbd_debug(SMB, "cannot delete %s, rc %d\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5446 | new_name, rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5447 | goto out; |
| 5448 | } |
| 5449 | } |
| 5450 | } else { |
| 5451 | if (file_present && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5452 | strncmp(old_name, path.dentry->d_name.name, strlen(old_name))) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5453 | rc = -EEXIST; |
| 5454 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5455 | "cannot rename already existing file\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5456 | goto out; |
| 5457 | } |
| 5458 | } |
| 5459 | |
| 5460 | rc = ksmbd_vfs_fp_rename(work, fp, new_name); |
| 5461 | out: |
| 5462 | kfree(pathname); |
| 5463 | if (!IS_ERR(new_name)) |
Marios Makassikis | 915f570 | 2021-04-13 13:25:57 +0900 | [diff] [blame] | 5464 | kfree(new_name); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5465 | return rc; |
| 5466 | } |
| 5467 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5468 | static int smb2_create_link(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5469 | struct ksmbd_share_config *share, |
| 5470 | struct smb2_file_link_info *file_info, |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5471 | unsigned int buf_len, struct file *filp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5472 | struct nls_table *local_nls) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5473 | { |
| 5474 | char *link_name = NULL, *target_name = NULL, *pathname = NULL; |
| 5475 | struct path path; |
| 5476 | bool file_present = true; |
| 5477 | int rc; |
| 5478 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5479 | if (buf_len < (u64)sizeof(struct smb2_file_link_info) + |
| 5480 | le32_to_cpu(file_info->FileNameLength)) |
| 5481 | return -EINVAL; |
| 5482 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5483 | ksmbd_debug(SMB, "setting FILE_LINK_INFORMATION\n"); |
| 5484 | pathname = kmalloc(PATH_MAX, GFP_KERNEL); |
| 5485 | if (!pathname) |
| 5486 | return -ENOMEM; |
| 5487 | |
| 5488 | link_name = smb2_get_name(share, |
| 5489 | file_info->FileName, |
| 5490 | le32_to_cpu(file_info->FileNameLength), |
| 5491 | local_nls); |
| 5492 | if (IS_ERR(link_name) || S_ISDIR(file_inode(filp)->i_mode)) { |
| 5493 | rc = -EINVAL; |
| 5494 | goto out; |
| 5495 | } |
| 5496 | |
| 5497 | ksmbd_debug(SMB, "link name is %s\n", link_name); |
| 5498 | target_name = d_path(&filp->f_path, pathname, PATH_MAX); |
| 5499 | if (IS_ERR(target_name)) { |
| 5500 | rc = -EINVAL; |
| 5501 | goto out; |
| 5502 | } |
| 5503 | |
| 5504 | ksmbd_debug(SMB, "target name is %s\n", target_name); |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 5505 | rc = ksmbd_vfs_kern_path(work, link_name, LOOKUP_NO_SYMLINKS, &path, 0); |
| 5506 | if (rc) { |
| 5507 | if (rc != -ENOENT) |
| 5508 | goto out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5509 | file_present = false; |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 5510 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5511 | path_put(&path); |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 5512 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5513 | |
| 5514 | if (file_info->ReplaceIfExists) { |
| 5515 | if (file_present) { |
| 5516 | rc = ksmbd_vfs_remove_file(work, link_name); |
| 5517 | if (rc) { |
| 5518 | rc = -EINVAL; |
| 5519 | ksmbd_debug(SMB, "cannot delete %s\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5520 | link_name); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5521 | goto out; |
| 5522 | } |
| 5523 | } |
| 5524 | } else { |
| 5525 | if (file_present) { |
| 5526 | rc = -EEXIST; |
| 5527 | ksmbd_debug(SMB, "link already exists\n"); |
| 5528 | goto out; |
| 5529 | } |
| 5530 | } |
| 5531 | |
| 5532 | rc = ksmbd_vfs_link(work, target_name, link_name); |
| 5533 | if (rc) |
| 5534 | rc = -EINVAL; |
| 5535 | out: |
| 5536 | if (!IS_ERR(link_name)) |
Marios Makassikis | 915f570 | 2021-04-13 13:25:57 +0900 | [diff] [blame] | 5537 | kfree(link_name); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5538 | kfree(pathname); |
| 5539 | return rc; |
| 5540 | } |
| 5541 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5542 | static int set_file_basic_info(struct ksmbd_file *fp, |
| 5543 | struct smb2_file_basic_info *file_info, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5544 | struct ksmbd_share_config *share) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5545 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5546 | struct iattr attrs; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5547 | struct file *filp; |
| 5548 | struct inode *inode; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 5549 | struct user_namespace *user_ns; |
Namjae Jeon | 4ffd526 | 2021-09-07 08:15:21 +0900 | [diff] [blame] | 5550 | int rc = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5551 | |
Marios Makassikis | 7adfd4f | 2021-04-27 15:30:22 +0900 | [diff] [blame] | 5552 | if (!(fp->daccess & FILE_WRITE_ATTRIBUTES_LE)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5553 | return -EACCES; |
| 5554 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5555 | attrs.ia_valid = 0; |
| 5556 | filp = fp->filp; |
| 5557 | inode = file_inode(filp); |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 5558 | user_ns = file_mnt_user_ns(filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5559 | |
| 5560 | if (file_info->CreationTime) |
| 5561 | fp->create_time = le64_to_cpu(file_info->CreationTime); |
| 5562 | |
| 5563 | if (file_info->LastAccessTime) { |
| 5564 | attrs.ia_atime = ksmbd_NTtimeToUnix(file_info->LastAccessTime); |
| 5565 | attrs.ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET); |
| 5566 | } |
| 5567 | |
Namjae Jeon | 64e7875 | 2021-10-03 13:19:00 +0900 | [diff] [blame] | 5568 | attrs.ia_valid |= ATTR_CTIME; |
| 5569 | if (file_info->ChangeTime) |
Christian Brauner | db7fb6f | 2021-08-26 10:07:05 +0900 | [diff] [blame] | 5570 | attrs.ia_ctime = ksmbd_NTtimeToUnix(file_info->ChangeTime); |
Namjae Jeon | 64e7875 | 2021-10-03 13:19:00 +0900 | [diff] [blame] | 5571 | else |
| 5572 | attrs.ia_ctime = inode->i_ctime; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5573 | |
| 5574 | if (file_info->LastWriteTime) { |
| 5575 | attrs.ia_mtime = ksmbd_NTtimeToUnix(file_info->LastWriteTime); |
| 5576 | attrs.ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET); |
| 5577 | } |
| 5578 | |
| 5579 | if (file_info->Attributes) { |
| 5580 | if (!S_ISDIR(inode->i_mode) && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5581 | file_info->Attributes & ATTR_DIRECTORY_LE) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5582 | pr_err("can't change a file to a directory\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5583 | return -EINVAL; |
| 5584 | } |
| 5585 | |
| 5586 | if (!(S_ISDIR(inode->i_mode) && file_info->Attributes == ATTR_NORMAL_LE)) |
| 5587 | fp->f_ci->m_fattr = file_info->Attributes | |
| 5588 | (fp->f_ci->m_fattr & ATTR_DIRECTORY_LE); |
| 5589 | } |
| 5590 | |
| 5591 | if (test_share_config_flag(share, KSMBD_SHARE_FLAG_STORE_DOS_ATTRS) && |
| 5592 | (file_info->CreationTime || file_info->Attributes)) { |
| 5593 | struct xattr_dos_attrib da = {0}; |
| 5594 | |
| 5595 | da.version = 4; |
| 5596 | da.itime = fp->itime; |
| 5597 | da.create_time = fp->create_time; |
| 5598 | da.attr = le32_to_cpu(fp->f_ci->m_fattr); |
| 5599 | da.flags = XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME | |
| 5600 | XATTR_DOSINFO_ITIME; |
| 5601 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 5602 | rc = ksmbd_vfs_set_dos_attrib_xattr(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 5603 | filp->f_path.dentry, &da); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5604 | if (rc) |
| 5605 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5606 | "failed to restore file attribute in EA\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5607 | rc = 0; |
| 5608 | } |
| 5609 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5610 | if (attrs.ia_valid) { |
| 5611 | struct dentry *dentry = filp->f_path.dentry; |
| 5612 | struct inode *inode = d_inode(dentry); |
| 5613 | |
| 5614 | if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) |
| 5615 | return -EACCES; |
| 5616 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5617 | inode_lock(inode); |
Namjae Jeon | 64e7875 | 2021-10-03 13:19:00 +0900 | [diff] [blame] | 5618 | inode->i_ctime = attrs.ia_ctime; |
| 5619 | attrs.ia_valid &= ~ATTR_CTIME; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 5620 | rc = notify_change(user_ns, dentry, &attrs, NULL); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5621 | inode_unlock(inode); |
| 5622 | } |
Christian Brauner | eb5784f | 2021-08-23 17:13:55 +0200 | [diff] [blame] | 5623 | return rc; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5624 | } |
| 5625 | |
| 5626 | static int set_file_allocation_info(struct ksmbd_work *work, |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5627 | struct ksmbd_file *fp, |
| 5628 | struct smb2_file_alloc_info *file_alloc_info) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5629 | { |
| 5630 | /* |
| 5631 | * TODO : It's working fine only when store dos attributes |
| 5632 | * is not yes. need to implement a logic which works |
| 5633 | * properly with any smb.conf option |
| 5634 | */ |
| 5635 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5636 | loff_t alloc_blks; |
| 5637 | struct inode *inode; |
| 5638 | int rc; |
| 5639 | |
Marios Makassikis | a299669 | 2021-04-27 15:29:01 +0900 | [diff] [blame] | 5640 | if (!(fp->daccess & FILE_WRITE_DATA_LE)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5641 | return -EACCES; |
| 5642 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5643 | alloc_blks = (le64_to_cpu(file_alloc_info->AllocationSize) + 511) >> 9; |
| 5644 | inode = file_inode(fp->filp); |
| 5645 | |
| 5646 | if (alloc_blks > inode->i_blocks) { |
Namjae Jeon | e8c0619 | 2021-06-22 11:06:11 +0900 | [diff] [blame] | 5647 | smb_break_all_levII_oplock(work, fp, 1); |
| 5648 | rc = vfs_fallocate(fp->filp, FALLOC_FL_KEEP_SIZE, 0, |
| 5649 | alloc_blks * 512); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5650 | if (rc && rc != -EOPNOTSUPP) { |
Namjae Jeon | e8c0619 | 2021-06-22 11:06:11 +0900 | [diff] [blame] | 5651 | pr_err("vfs_fallocate is failed : %d\n", rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5652 | return rc; |
| 5653 | } |
| 5654 | } else if (alloc_blks < inode->i_blocks) { |
| 5655 | loff_t size; |
| 5656 | |
| 5657 | /* |
| 5658 | * Allocation size could be smaller than original one |
| 5659 | * which means allocated blocks in file should be |
| 5660 | * deallocated. use truncate to cut out it, but inode |
| 5661 | * size is also updated with truncate offset. |
| 5662 | * inode size is retained by backup inode size. |
| 5663 | */ |
| 5664 | size = i_size_read(inode); |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 5665 | rc = ksmbd_vfs_truncate(work, fp, alloc_blks * 512); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5666 | if (rc) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5667 | pr_err("truncate failed! filename : %s, err %d\n", |
| 5668 | fp->filename, rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5669 | return rc; |
| 5670 | } |
| 5671 | if (size < alloc_blks * 512) |
| 5672 | i_size_write(inode, size); |
| 5673 | } |
| 5674 | return 0; |
| 5675 | } |
| 5676 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5677 | 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] | 5678 | struct smb2_file_eof_info *file_eof_info) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5679 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5680 | loff_t newsize; |
| 5681 | struct inode *inode; |
| 5682 | int rc; |
| 5683 | |
Marios Makassikis | a299669 | 2021-04-27 15:29:01 +0900 | [diff] [blame] | 5684 | if (!(fp->daccess & FILE_WRITE_DATA_LE)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5685 | return -EACCES; |
| 5686 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5687 | newsize = le64_to_cpu(file_eof_info->EndOfFile); |
| 5688 | inode = file_inode(fp->filp); |
| 5689 | |
| 5690 | /* |
| 5691 | * If FILE_END_OF_FILE_INFORMATION of set_info_file is called |
| 5692 | * on FAT32 shared device, truncate execution time is too long |
| 5693 | * and network error could cause from windows client. because |
| 5694 | * truncate of some filesystem like FAT32 fill zero data in |
| 5695 | * truncated range. |
| 5696 | */ |
| 5697 | if (inode->i_sb->s_magic != MSDOS_SUPER_MAGIC) { |
| 5698 | ksmbd_debug(SMB, "filename : %s truncated to newsize %lld\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5699 | fp->filename, newsize); |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 5700 | rc = ksmbd_vfs_truncate(work, fp, newsize); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5701 | if (rc) { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5702 | ksmbd_debug(SMB, "truncate failed! filename : %s err %d\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5703 | fp->filename, rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5704 | if (rc != -EAGAIN) |
| 5705 | rc = -EBADF; |
| 5706 | return rc; |
| 5707 | } |
| 5708 | } |
| 5709 | return 0; |
| 5710 | } |
| 5711 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5712 | 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] | 5713 | struct smb2_file_rename_info *rename_info, |
| 5714 | unsigned int buf_len) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5715 | { |
Christian Brauner | da1e7ad | 2021-08-23 17:13:47 +0200 | [diff] [blame] | 5716 | struct user_namespace *user_ns; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5717 | struct ksmbd_file *parent_fp; |
Namjae Jeon | 12202c0 | 2021-06-29 09:23:56 +0900 | [diff] [blame] | 5718 | struct dentry *parent; |
| 5719 | struct dentry *dentry = fp->filp->f_path.dentry; |
| 5720 | int ret; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5721 | |
| 5722 | if (!(fp->daccess & FILE_DELETE_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5723 | pr_err("no right to delete : 0x%x\n", fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5724 | return -EACCES; |
| 5725 | } |
| 5726 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5727 | if (buf_len < (u64)sizeof(struct smb2_file_rename_info) + |
| 5728 | le32_to_cpu(rename_info->FileNameLength)) |
| 5729 | return -EINVAL; |
| 5730 | |
Christian Brauner | da1e7ad | 2021-08-23 17:13:47 +0200 | [diff] [blame] | 5731 | user_ns = file_mnt_user_ns(fp->filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5732 | if (ksmbd_stream_fd(fp)) |
| 5733 | goto next; |
| 5734 | |
Namjae Jeon | 12202c0 | 2021-06-29 09:23:56 +0900 | [diff] [blame] | 5735 | parent = dget_parent(dentry); |
Christian Brauner | da1e7ad | 2021-08-23 17:13:47 +0200 | [diff] [blame] | 5736 | ret = ksmbd_vfs_lock_parent(user_ns, parent, dentry); |
Namjae Jeon | 12202c0 | 2021-06-29 09:23:56 +0900 | [diff] [blame] | 5737 | if (ret) { |
| 5738 | dput(parent); |
| 5739 | return ret; |
| 5740 | } |
| 5741 | |
| 5742 | parent_fp = ksmbd_lookup_fd_inode(d_inode(parent)); |
| 5743 | inode_unlock(d_inode(parent)); |
| 5744 | dput(parent); |
| 5745 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5746 | if (parent_fp) { |
| 5747 | if (parent_fp->daccess & FILE_DELETE_LE) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5748 | pr_err("parent dir is opened with delete access\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5749 | return -ESHARE; |
| 5750 | } |
| 5751 | } |
| 5752 | next: |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5753 | return smb2_rename(work, fp, user_ns, rename_info, |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5754 | work->sess->conn->local_nls); |
| 5755 | } |
| 5756 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5757 | static int set_file_disposition_info(struct ksmbd_file *fp, |
| 5758 | struct smb2_file_disposition_info *file_info) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5759 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5760 | struct inode *inode; |
| 5761 | |
| 5762 | if (!(fp->daccess & FILE_DELETE_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5763 | pr_err("no right to delete : 0x%x\n", fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5764 | return -EACCES; |
| 5765 | } |
| 5766 | |
| 5767 | inode = file_inode(fp->filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5768 | if (file_info->DeletePending) { |
| 5769 | if (S_ISDIR(inode->i_mode) && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5770 | ksmbd_vfs_empty_dir(fp) == -ENOTEMPTY) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5771 | return -EBUSY; |
| 5772 | ksmbd_set_inode_pending_delete(fp); |
| 5773 | } else { |
| 5774 | ksmbd_clear_inode_pending_delete(fp); |
| 5775 | } |
| 5776 | return 0; |
| 5777 | } |
| 5778 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5779 | static int set_file_position_info(struct ksmbd_file *fp, |
| 5780 | struct smb2_file_pos_info *file_info) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5781 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5782 | loff_t current_byte_offset; |
Namjae Jeon | ee81cae | 2021-06-26 22:32:34 +0900 | [diff] [blame] | 5783 | unsigned long sector_size; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5784 | struct inode *inode; |
| 5785 | |
| 5786 | inode = file_inode(fp->filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5787 | current_byte_offset = le64_to_cpu(file_info->CurrentByteOffset); |
Namjae Jeon | ee81cae | 2021-06-26 22:32:34 +0900 | [diff] [blame] | 5788 | sector_size = inode->i_sb->s_blocksize; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5789 | |
| 5790 | if (current_byte_offset < 0 || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5791 | (fp->coption == FILE_NO_INTERMEDIATE_BUFFERING_LE && |
| 5792 | current_byte_offset & (sector_size - 1))) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5793 | pr_err("CurrentByteOffset is not valid : %llu\n", |
| 5794 | current_byte_offset); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5795 | return -EINVAL; |
| 5796 | } |
| 5797 | |
| 5798 | fp->filp->f_pos = current_byte_offset; |
| 5799 | return 0; |
| 5800 | } |
| 5801 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5802 | static int set_file_mode_info(struct ksmbd_file *fp, |
| 5803 | struct smb2_file_mode_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 | __le32 mode; |
| 5806 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5807 | mode = file_info->Mode; |
| 5808 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5809 | if ((mode & ~FILE_MODE_INFO_MASK) || |
| 5810 | (mode & FILE_SYNCHRONOUS_IO_ALERT_LE && |
| 5811 | mode & FILE_SYNCHRONOUS_IO_NONALERT_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5812 | 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] | 5813 | return -EINVAL; |
| 5814 | } |
| 5815 | |
| 5816 | /* |
| 5817 | * TODO : need to implement consideration for |
| 5818 | * FILE_SYNCHRONOUS_IO_ALERT and FILE_SYNCHRONOUS_IO_NONALERT |
| 5819 | */ |
| 5820 | ksmbd_vfs_set_fadvise(fp->filp, mode); |
| 5821 | fp->coption = mode; |
| 5822 | return 0; |
| 5823 | } |
| 5824 | |
| 5825 | /** |
| 5826 | * smb2_set_info_file() - handler for smb2 set info command |
| 5827 | * @work: smb work containing set info command buffer |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 5828 | * @fp: ksmbd_file pointer |
| 5829 | * @info_class: smb2 set info class |
| 5830 | * @share: ksmbd_share_config pointer |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5831 | * |
| 5832 | * Return: 0 on success, otherwise error |
| 5833 | * TODO: need to implement an error handling for STATUS_INFO_LENGTH_MISMATCH |
| 5834 | */ |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5835 | 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] | 5836 | struct smb2_set_info_req *req, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5837 | struct ksmbd_share_config *share) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5838 | { |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5839 | unsigned int buf_len = le32_to_cpu(req->BufferLength); |
| 5840 | |
| 5841 | switch (req->FileInfoClass) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5842 | case FILE_BASIC_INFORMATION: |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5843 | { |
| 5844 | if (buf_len < sizeof(struct smb2_file_basic_info)) |
| 5845 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5846 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5847 | return set_file_basic_info(fp, (struct smb2_file_basic_info *)req->Buffer, share); |
| 5848 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5849 | case FILE_ALLOCATION_INFORMATION: |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5850 | { |
| 5851 | if (buf_len < sizeof(struct smb2_file_alloc_info)) |
| 5852 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5853 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5854 | return set_file_allocation_info(work, fp, |
| 5855 | (struct smb2_file_alloc_info *)req->Buffer); |
| 5856 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5857 | case FILE_END_OF_FILE_INFORMATION: |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5858 | { |
| 5859 | if (buf_len < sizeof(struct smb2_file_eof_info)) |
| 5860 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5861 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5862 | return set_end_of_file_info(work, fp, |
| 5863 | (struct smb2_file_eof_info *)req->Buffer); |
| 5864 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5865 | case FILE_RENAME_INFORMATION: |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5866 | { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5867 | if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5868 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5869 | "User does not have write permission\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5870 | return -EACCES; |
| 5871 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5872 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5873 | if (buf_len < sizeof(struct smb2_file_rename_info)) |
| 5874 | return -EINVAL; |
| 5875 | |
| 5876 | return set_rename_info(work, fp, |
| 5877 | (struct smb2_file_rename_info *)req->Buffer, |
| 5878 | buf_len); |
| 5879 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5880 | case FILE_LINK_INFORMATION: |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5881 | { |
| 5882 | if (buf_len < sizeof(struct smb2_file_link_info)) |
| 5883 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5884 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5885 | return smb2_create_link(work, work->tcon->share_conf, |
| 5886 | (struct smb2_file_link_info *)req->Buffer, |
| 5887 | buf_len, fp->filp, |
| 5888 | work->sess->conn->local_nls); |
| 5889 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5890 | case FILE_DISPOSITION_INFORMATION: |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5891 | { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5892 | if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5893 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5894 | "User does not have write permission\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5895 | return -EACCES; |
| 5896 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5897 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5898 | if (buf_len < sizeof(struct smb2_file_disposition_info)) |
| 5899 | return -EINVAL; |
| 5900 | |
| 5901 | return set_file_disposition_info(fp, |
| 5902 | (struct smb2_file_disposition_info *)req->Buffer); |
| 5903 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5904 | case FILE_FULL_EA_INFORMATION: |
| 5905 | { |
| 5906 | if (!(fp->daccess & FILE_WRITE_EA_LE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 5907 | pr_err("Not permitted to write ext attr: 0x%x\n", |
| 5908 | fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5909 | return -EACCES; |
| 5910 | } |
| 5911 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5912 | if (buf_len < sizeof(struct smb2_ea_info)) |
| 5913 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5914 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5915 | return smb2_set_ea((struct smb2_ea_info *)req->Buffer, |
| 5916 | buf_len, &fp->filp->f_path); |
| 5917 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5918 | case FILE_POSITION_INFORMATION: |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5919 | { |
| 5920 | if (buf_len < sizeof(struct smb2_file_pos_info)) |
| 5921 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5922 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5923 | return set_file_position_info(fp, (struct smb2_file_pos_info *)req->Buffer); |
| 5924 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5925 | case FILE_MODE_INFORMATION: |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5926 | { |
| 5927 | if (buf_len < sizeof(struct smb2_file_mode_info)) |
| 5928 | return -EINVAL; |
| 5929 | |
| 5930 | return set_file_mode_info(fp, (struct smb2_file_mode_info *)req->Buffer); |
| 5931 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5932 | } |
| 5933 | |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5934 | pr_err("Unimplemented Fileinfoclass :%d\n", req->FileInfoClass); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5935 | return -EOPNOTSUPP; |
| 5936 | } |
| 5937 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 5938 | 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] | 5939 | char *buffer, int buf_len) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5940 | { |
| 5941 | struct smb_ntsd *pntsd = (struct smb_ntsd *)buffer; |
| 5942 | |
| 5943 | fp->saccess |= FILE_SHARE_DELETE_LE; |
| 5944 | |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 5945 | 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] | 5946 | buf_len, false); |
| 5947 | } |
| 5948 | |
| 5949 | /** |
| 5950 | * smb2_set_info() - handler for smb2 set info command handler |
| 5951 | * @work: smb work containing set info request buffer |
| 5952 | * |
| 5953 | * Return: 0 on success, otherwise error |
| 5954 | */ |
| 5955 | int smb2_set_info(struct ksmbd_work *work) |
| 5956 | { |
| 5957 | struct smb2_set_info_req *req; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 5958 | struct smb2_set_info_rsp *rsp; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5959 | struct ksmbd_file *fp; |
| 5960 | int rc = 0; |
| 5961 | unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID; |
| 5962 | |
| 5963 | ksmbd_debug(SMB, "Received set info request\n"); |
| 5964 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5965 | if (work->next_smb2_rcv_hdr_off) { |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 5966 | req = ksmbd_req_buf_next(work); |
| 5967 | rsp = ksmbd_resp_buf_next(work); |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 5968 | if (!has_file_id(le64_to_cpu(req->VolatileFileId))) { |
| 5969 | ksmbd_debug(SMB, "Compound request set FID = %llu\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 5970 | work->compound_fid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5971 | id = work->compound_fid; |
| 5972 | pid = work->compound_pfid; |
| 5973 | } |
| 5974 | } else { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 5975 | req = smb2_get_msg(work->request_buf); |
| 5976 | rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5977 | } |
| 5978 | |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 5979 | if (!has_file_id(id)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5980 | id = le64_to_cpu(req->VolatileFileId); |
| 5981 | pid = le64_to_cpu(req->PersistentFileId); |
| 5982 | } |
| 5983 | |
| 5984 | fp = ksmbd_lookup_fd_slow(work, id, pid); |
| 5985 | if (!fp) { |
| 5986 | ksmbd_debug(SMB, "Invalid id for close: %u\n", id); |
| 5987 | rc = -ENOENT; |
| 5988 | goto err_out; |
| 5989 | } |
| 5990 | |
| 5991 | switch (req->InfoType) { |
| 5992 | case SMB2_O_INFO_FILE: |
| 5993 | ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILE\n"); |
Namjae Jeon | 9496e26 | 2021-09-29 15:41:48 +0900 | [diff] [blame] | 5994 | rc = smb2_set_info_file(work, fp, req, work->tcon->share_conf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 5995 | break; |
| 5996 | case SMB2_O_INFO_SECURITY: |
| 5997 | ksmbd_debug(SMB, "GOT SMB2_O_INFO_SECURITY\n"); |
Namjae Jeon | e70e392 | 2021-08-21 23:26:01 +0900 | [diff] [blame] | 5998 | if (ksmbd_override_fsids(work)) { |
| 5999 | rc = -ENOMEM; |
| 6000 | goto err_out; |
| 6001 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6002 | rc = smb2_set_info_sec(fp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6003 | le32_to_cpu(req->AdditionalInformation), |
| 6004 | req->Buffer, |
| 6005 | le32_to_cpu(req->BufferLength)); |
Namjae Jeon | e70e392 | 2021-08-21 23:26:01 +0900 | [diff] [blame] | 6006 | ksmbd_revert_fsids(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6007 | break; |
| 6008 | default: |
| 6009 | rc = -EOPNOTSUPP; |
| 6010 | } |
| 6011 | |
| 6012 | if (rc < 0) |
| 6013 | goto err_out; |
| 6014 | |
| 6015 | rsp->StructureSize = cpu_to_le16(2); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 6016 | inc_rfc1001_len(work->response_buf, 2); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6017 | ksmbd_fd_put(work, fp); |
| 6018 | return 0; |
| 6019 | |
| 6020 | err_out: |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 6021 | if (rc == -EACCES || rc == -EPERM || rc == -EXDEV) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6022 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 6023 | else if (rc == -EINVAL) |
| 6024 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 6025 | else if (rc == -ESHARE) |
| 6026 | rsp->hdr.Status = STATUS_SHARING_VIOLATION; |
| 6027 | else if (rc == -ENOENT) |
| 6028 | rsp->hdr.Status = STATUS_OBJECT_NAME_INVALID; |
| 6029 | else if (rc == -EBUSY || rc == -ENOTEMPTY) |
| 6030 | rsp->hdr.Status = STATUS_DIRECTORY_NOT_EMPTY; |
| 6031 | else if (rc == -EAGAIN) |
| 6032 | rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT; |
Namjae Jeon | ff1d572 | 2021-04-13 13:18:10 +0900 | [diff] [blame] | 6033 | else if (rc == -EBADF || rc == -ESTALE) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6034 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 6035 | else if (rc == -EEXIST) |
| 6036 | rsp->hdr.Status = STATUS_OBJECT_NAME_COLLISION; |
| 6037 | else if (rsp->hdr.Status == 0 || rc == -EOPNOTSUPP) |
| 6038 | rsp->hdr.Status = STATUS_INVALID_INFO_CLASS; |
| 6039 | smb2_set_err_rsp(work); |
| 6040 | ksmbd_fd_put(work, fp); |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6041 | ksmbd_debug(SMB, "error while processing smb2 query rc = %d\n", rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6042 | return rc; |
| 6043 | } |
| 6044 | |
| 6045 | /** |
| 6046 | * smb2_read_pipe() - handler for smb2 read from IPC pipe |
| 6047 | * @work: smb work containing read IPC pipe command buffer |
| 6048 | * |
| 6049 | * Return: 0 on success, otherwise error |
| 6050 | */ |
| 6051 | static noinline int smb2_read_pipe(struct ksmbd_work *work) |
| 6052 | { |
| 6053 | int nbytes = 0, err; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6054 | u64 id; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6055 | struct ksmbd_rpc_command *rpc_resp; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 6056 | struct smb2_read_req *req = smb2_get_msg(work->request_buf); |
| 6057 | struct smb2_read_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6058 | |
| 6059 | id = le64_to_cpu(req->VolatileFileId); |
| 6060 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 6061 | inc_rfc1001_len(work->response_buf, 16); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6062 | rpc_resp = ksmbd_rpc_read(work->sess, id); |
| 6063 | if (rpc_resp) { |
| 6064 | if (rpc_resp->flags != KSMBD_RPC_OK) { |
| 6065 | err = -EINVAL; |
| 6066 | goto out; |
| 6067 | } |
| 6068 | |
| 6069 | work->aux_payload_buf = |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6070 | kvmalloc(rpc_resp->payload_sz, GFP_KERNEL | __GFP_ZERO); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6071 | if (!work->aux_payload_buf) { |
| 6072 | err = -ENOMEM; |
| 6073 | goto out; |
| 6074 | } |
| 6075 | |
| 6076 | memcpy(work->aux_payload_buf, rpc_resp->payload, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6077 | rpc_resp->payload_sz); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6078 | |
| 6079 | nbytes = rpc_resp->payload_sz; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 6080 | work->resp_hdr_sz = get_rfc1002_len(work->response_buf) + 4; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6081 | work->aux_payload_sz = nbytes; |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6082 | kvfree(rpc_resp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6083 | } |
| 6084 | |
| 6085 | rsp->StructureSize = cpu_to_le16(17); |
| 6086 | rsp->DataOffset = 80; |
| 6087 | rsp->Reserved = 0; |
| 6088 | rsp->DataLength = cpu_to_le32(nbytes); |
| 6089 | rsp->DataRemaining = 0; |
| 6090 | rsp->Reserved2 = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 6091 | inc_rfc1001_len(work->response_buf, nbytes); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6092 | return 0; |
| 6093 | |
| 6094 | out: |
| 6095 | rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR; |
| 6096 | smb2_set_err_rsp(work); |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6097 | kvfree(rpc_resp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6098 | return err; |
| 6099 | } |
| 6100 | |
| 6101 | static ssize_t smb2_read_rdma_channel(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6102 | struct smb2_read_req *req, void *data_buf, |
| 6103 | size_t length) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6104 | { |
| 6105 | struct smb2_buffer_desc_v1 *desc = |
| 6106 | (struct smb2_buffer_desc_v1 *)&req->Buffer[0]; |
| 6107 | int err; |
| 6108 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6109 | if (work->conn->dialect == SMB30_PROT_ID && |
| 6110 | req->Channel != SMB2_CHANNEL_RDMA_V1) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6111 | return -EINVAL; |
| 6112 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6113 | if (req->ReadChannelInfoOffset == 0 || |
| 6114 | le16_to_cpu(req->ReadChannelInfoLength) < sizeof(*desc)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6115 | return -EINVAL; |
| 6116 | |
| 6117 | work->need_invalidate_rkey = |
| 6118 | (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE); |
| 6119 | work->remote_key = le32_to_cpu(desc->token); |
| 6120 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6121 | err = ksmbd_conn_rdma_write(work->conn, data_buf, length, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6122 | le32_to_cpu(desc->token), |
| 6123 | le64_to_cpu(desc->offset), |
| 6124 | le32_to_cpu(desc->length)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6125 | if (err) |
| 6126 | return err; |
| 6127 | |
| 6128 | return length; |
| 6129 | } |
| 6130 | |
| 6131 | /** |
| 6132 | * smb2_read() - handler for smb2 read from file |
| 6133 | * @work: smb work containing read command buffer |
| 6134 | * |
| 6135 | * Return: 0 on success, otherwise error |
| 6136 | */ |
| 6137 | int smb2_read(struct ksmbd_work *work) |
| 6138 | { |
| 6139 | struct ksmbd_conn *conn = work->conn; |
| 6140 | struct smb2_read_req *req; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 6141 | struct smb2_read_rsp *rsp; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6142 | struct ksmbd_file *fp; |
| 6143 | loff_t offset; |
| 6144 | size_t length, mincount; |
| 6145 | ssize_t nbytes = 0, remain_bytes = 0; |
| 6146 | int err = 0; |
| 6147 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6148 | WORK_BUFFERS(work, req, rsp); |
| 6149 | |
| 6150 | if (test_share_config_flag(work->tcon->share_conf, |
| 6151 | KSMBD_SHARE_FLAG_PIPE)) { |
| 6152 | ksmbd_debug(SMB, "IPC pipe read request\n"); |
| 6153 | return smb2_read_pipe(work); |
| 6154 | } |
| 6155 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6156 | fp = ksmbd_lookup_fd_slow(work, le64_to_cpu(req->VolatileFileId), |
| 6157 | le64_to_cpu(req->PersistentFileId)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6158 | if (!fp) { |
Marios Makassikis | a4382db | 2021-05-06 11:34:52 +0900 | [diff] [blame] | 6159 | err = -ENOENT; |
| 6160 | goto out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6161 | } |
| 6162 | |
| 6163 | if (!(fp->daccess & (FILE_READ_DATA_LE | FILE_READ_ATTRIBUTES_LE))) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 6164 | pr_err("Not permitted to read : 0x%x\n", fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6165 | err = -EACCES; |
| 6166 | goto out; |
| 6167 | } |
| 6168 | |
| 6169 | offset = le64_to_cpu(req->Offset); |
| 6170 | length = le32_to_cpu(req->Length); |
| 6171 | mincount = le32_to_cpu(req->MinimumCount); |
| 6172 | |
| 6173 | if (length > conn->vals->max_read_size) { |
| 6174 | ksmbd_debug(SMB, "limiting read size to max size(%u)\n", |
| 6175 | conn->vals->max_read_size); |
| 6176 | err = -EINVAL; |
| 6177 | goto out; |
| 6178 | } |
| 6179 | |
Namjae Jeon | 493fa2f | 2021-06-29 09:22:16 +0900 | [diff] [blame] | 6180 | ksmbd_debug(SMB, "filename %pd, offset %lld, len %zu\n", |
| 6181 | fp->filp->f_path.dentry, offset, length); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6182 | |
Namjae Jeon | c30f4eb | 2021-06-18 10:17:37 +0900 | [diff] [blame] | 6183 | work->aux_payload_buf = kvmalloc(length, GFP_KERNEL | __GFP_ZERO); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6184 | if (!work->aux_payload_buf) { |
Dan Carpenter | c1ea111 | 2021-03-22 17:50:11 +0300 | [diff] [blame] | 6185 | err = -ENOMEM; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6186 | goto out; |
| 6187 | } |
| 6188 | |
| 6189 | nbytes = ksmbd_vfs_read(work, fp, length, &offset); |
| 6190 | if (nbytes < 0) { |
| 6191 | err = nbytes; |
| 6192 | goto out; |
| 6193 | } |
| 6194 | |
| 6195 | if ((nbytes == 0 && length != 0) || nbytes < mincount) { |
Namjae Jeon | c30f4eb | 2021-06-18 10:17:37 +0900 | [diff] [blame] | 6196 | kvfree(work->aux_payload_buf); |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 6197 | work->aux_payload_buf = NULL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6198 | rsp->hdr.Status = STATUS_END_OF_FILE; |
| 6199 | smb2_set_err_rsp(work); |
| 6200 | ksmbd_fd_put(work, fp); |
| 6201 | return 0; |
| 6202 | } |
| 6203 | |
| 6204 | ksmbd_debug(SMB, "nbytes %zu, offset %lld mincount %zu\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6205 | nbytes, offset, mincount); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6206 | |
| 6207 | if (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6208 | req->Channel == SMB2_CHANNEL_RDMA_V1) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6209 | /* write data to the client using rdma channel */ |
| 6210 | remain_bytes = smb2_read_rdma_channel(work, req, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6211 | work->aux_payload_buf, |
| 6212 | nbytes); |
Namjae Jeon | c30f4eb | 2021-06-18 10:17:37 +0900 | [diff] [blame] | 6213 | kvfree(work->aux_payload_buf); |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 6214 | work->aux_payload_buf = NULL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6215 | |
| 6216 | nbytes = 0; |
| 6217 | if (remain_bytes < 0) { |
| 6218 | err = (int)remain_bytes; |
| 6219 | goto out; |
| 6220 | } |
| 6221 | } |
| 6222 | |
| 6223 | rsp->StructureSize = cpu_to_le16(17); |
| 6224 | rsp->DataOffset = 80; |
| 6225 | rsp->Reserved = 0; |
| 6226 | rsp->DataLength = cpu_to_le32(nbytes); |
| 6227 | rsp->DataRemaining = cpu_to_le32(remain_bytes); |
| 6228 | rsp->Reserved2 = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 6229 | inc_rfc1001_len(work->response_buf, 16); |
| 6230 | work->resp_hdr_sz = get_rfc1002_len(work->response_buf) + 4; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6231 | work->aux_payload_sz = nbytes; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 6232 | inc_rfc1001_len(work->response_buf, nbytes); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6233 | ksmbd_fd_put(work, fp); |
| 6234 | return 0; |
| 6235 | |
| 6236 | out: |
| 6237 | if (err) { |
| 6238 | if (err == -EISDIR) |
| 6239 | rsp->hdr.Status = STATUS_INVALID_DEVICE_REQUEST; |
| 6240 | else if (err == -EAGAIN) |
| 6241 | rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT; |
| 6242 | else if (err == -ENOENT) |
| 6243 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 6244 | else if (err == -EACCES) |
| 6245 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 6246 | else if (err == -ESHARE) |
| 6247 | rsp->hdr.Status = STATUS_SHARING_VIOLATION; |
| 6248 | else if (err == -EINVAL) |
| 6249 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 6250 | else |
| 6251 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 6252 | |
| 6253 | smb2_set_err_rsp(work); |
| 6254 | } |
| 6255 | ksmbd_fd_put(work, fp); |
| 6256 | return err; |
| 6257 | } |
| 6258 | |
| 6259 | /** |
| 6260 | * smb2_write_pipe() - handler for smb2 write on IPC pipe |
| 6261 | * @work: smb work containing write IPC pipe command buffer |
| 6262 | * |
| 6263 | * Return: 0 on success, otherwise error |
| 6264 | */ |
| 6265 | static noinline int smb2_write_pipe(struct ksmbd_work *work) |
| 6266 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 6267 | struct smb2_write_req *req = smb2_get_msg(work->request_buf); |
| 6268 | struct smb2_write_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6269 | struct ksmbd_rpc_command *rpc_resp; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6270 | u64 id = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6271 | int err = 0, ret = 0; |
| 6272 | char *data_buf; |
| 6273 | size_t length; |
| 6274 | |
| 6275 | length = le32_to_cpu(req->Length); |
| 6276 | id = le64_to_cpu(req->VolatileFileId); |
| 6277 | |
| 6278 | if (le16_to_cpu(req->DataOffset) == |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 6279 | offsetof(struct smb2_write_req, Buffer)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6280 | data_buf = (char *)&req->Buffer[0]; |
| 6281 | } else { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 6282 | if ((u64)le16_to_cpu(req->DataOffset) + length > |
| 6283 | get_rfc1002_len(work->request_buf)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 6284 | pr_err("invalid write data offset %u, smb_len %u\n", |
| 6285 | le16_to_cpu(req->DataOffset), |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 6286 | get_rfc1002_len(work->request_buf)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6287 | err = -EINVAL; |
| 6288 | goto out; |
| 6289 | } |
| 6290 | |
| 6291 | data_buf = (char *)(((char *)&req->hdr.ProtocolId) + |
| 6292 | le16_to_cpu(req->DataOffset)); |
| 6293 | } |
| 6294 | |
| 6295 | rpc_resp = ksmbd_rpc_write(work->sess, id, data_buf, length); |
| 6296 | if (rpc_resp) { |
| 6297 | if (rpc_resp->flags == KSMBD_RPC_ENOTIMPLEMENTED) { |
| 6298 | rsp->hdr.Status = STATUS_NOT_SUPPORTED; |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6299 | kvfree(rpc_resp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6300 | smb2_set_err_rsp(work); |
| 6301 | return -EOPNOTSUPP; |
| 6302 | } |
| 6303 | if (rpc_resp->flags != KSMBD_RPC_OK) { |
| 6304 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 6305 | smb2_set_err_rsp(work); |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6306 | kvfree(rpc_resp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6307 | return ret; |
| 6308 | } |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6309 | kvfree(rpc_resp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6310 | } |
| 6311 | |
| 6312 | rsp->StructureSize = cpu_to_le16(17); |
| 6313 | rsp->DataOffset = 0; |
| 6314 | rsp->Reserved = 0; |
| 6315 | rsp->DataLength = cpu_to_le32(length); |
| 6316 | rsp->DataRemaining = 0; |
| 6317 | rsp->Reserved2 = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 6318 | inc_rfc1001_len(work->response_buf, 16); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6319 | return 0; |
| 6320 | out: |
| 6321 | if (err) { |
| 6322 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 6323 | smb2_set_err_rsp(work); |
| 6324 | } |
| 6325 | |
| 6326 | return err; |
| 6327 | } |
| 6328 | |
| 6329 | static ssize_t smb2_write_rdma_channel(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6330 | struct smb2_write_req *req, |
| 6331 | struct ksmbd_file *fp, |
| 6332 | loff_t offset, size_t length, bool sync) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6333 | { |
| 6334 | struct smb2_buffer_desc_v1 *desc; |
| 6335 | char *data_buf; |
| 6336 | int ret; |
| 6337 | ssize_t nbytes; |
| 6338 | |
| 6339 | desc = (struct smb2_buffer_desc_v1 *)&req->Buffer[0]; |
| 6340 | |
| 6341 | if (work->conn->dialect == SMB30_PROT_ID && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6342 | req->Channel != SMB2_CHANNEL_RDMA_V1) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6343 | return -EINVAL; |
| 6344 | |
| 6345 | if (req->Length != 0 || req->DataOffset != 0) |
| 6346 | return -EINVAL; |
| 6347 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6348 | if (req->WriteChannelInfoOffset == 0 || |
| 6349 | le16_to_cpu(req->WriteChannelInfoLength) < sizeof(*desc)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6350 | return -EINVAL; |
| 6351 | |
| 6352 | work->need_invalidate_rkey = |
| 6353 | (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE); |
| 6354 | work->remote_key = le32_to_cpu(desc->token); |
| 6355 | |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6356 | data_buf = kvmalloc(length, GFP_KERNEL | __GFP_ZERO); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6357 | if (!data_buf) |
| 6358 | return -ENOMEM; |
| 6359 | |
| 6360 | ret = ksmbd_conn_rdma_read(work->conn, data_buf, length, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6361 | le32_to_cpu(desc->token), |
| 6362 | le64_to_cpu(desc->offset), |
| 6363 | le32_to_cpu(desc->length)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6364 | if (ret < 0) { |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6365 | kvfree(data_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6366 | return ret; |
| 6367 | } |
| 6368 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6369 | ret = ksmbd_vfs_write(work, fp, data_buf, length, &offset, sync, &nbytes); |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 6370 | kvfree(data_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6371 | if (ret < 0) |
| 6372 | return ret; |
| 6373 | |
| 6374 | return nbytes; |
| 6375 | } |
| 6376 | |
| 6377 | /** |
| 6378 | * smb2_write() - handler for smb2 write from file |
| 6379 | * @work: smb work containing write command buffer |
| 6380 | * |
| 6381 | * Return: 0 on success, otherwise error |
| 6382 | */ |
| 6383 | int smb2_write(struct ksmbd_work *work) |
| 6384 | { |
| 6385 | struct smb2_write_req *req; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 6386 | struct smb2_write_rsp *rsp; |
Namjae Jeon | bcd62a3 | 2021-05-10 09:08:19 +0900 | [diff] [blame] | 6387 | struct ksmbd_file *fp = NULL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6388 | loff_t offset; |
| 6389 | size_t length; |
| 6390 | ssize_t nbytes; |
| 6391 | char *data_buf; |
| 6392 | bool writethrough = false; |
| 6393 | int err = 0; |
| 6394 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6395 | WORK_BUFFERS(work, req, rsp); |
| 6396 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6397 | 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] | 6398 | ksmbd_debug(SMB, "IPC pipe write request\n"); |
| 6399 | return smb2_write_pipe(work); |
| 6400 | } |
| 6401 | |
| 6402 | if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
| 6403 | ksmbd_debug(SMB, "User does not have write permission\n"); |
| 6404 | err = -EACCES; |
| 6405 | goto out; |
| 6406 | } |
| 6407 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6408 | fp = ksmbd_lookup_fd_slow(work, le64_to_cpu(req->VolatileFileId), |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6409 | le64_to_cpu(req->PersistentFileId)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6410 | if (!fp) { |
Marios Makassikis | a4382db | 2021-05-06 11:34:52 +0900 | [diff] [blame] | 6411 | err = -ENOENT; |
| 6412 | goto out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6413 | } |
| 6414 | |
| 6415 | if (!(fp->daccess & (FILE_WRITE_DATA_LE | FILE_READ_ATTRIBUTES_LE))) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 6416 | pr_err("Not permitted to write : 0x%x\n", fp->daccess); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6417 | err = -EACCES; |
| 6418 | goto out; |
| 6419 | } |
| 6420 | |
| 6421 | offset = le64_to_cpu(req->Offset); |
| 6422 | length = le32_to_cpu(req->Length); |
| 6423 | |
| 6424 | if (length > work->conn->vals->max_write_size) { |
| 6425 | ksmbd_debug(SMB, "limiting write size to max size(%u)\n", |
| 6426 | work->conn->vals->max_write_size); |
| 6427 | err = -EINVAL; |
| 6428 | goto out; |
| 6429 | } |
| 6430 | |
| 6431 | if (le32_to_cpu(req->Flags) & SMB2_WRITEFLAG_WRITE_THROUGH) |
| 6432 | writethrough = true; |
| 6433 | |
| 6434 | if (req->Channel != SMB2_CHANNEL_RDMA_V1 && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6435 | req->Channel != SMB2_CHANNEL_RDMA_V1_INVALIDATE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6436 | if (le16_to_cpu(req->DataOffset) == |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 6437 | offsetof(struct smb2_write_req, Buffer)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6438 | data_buf = (char *)&req->Buffer[0]; |
| 6439 | } else { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 6440 | if ((u64)le16_to_cpu(req->DataOffset) + length > |
| 6441 | get_rfc1002_len(work->request_buf)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 6442 | pr_err("invalid write data offset %u, smb_len %u\n", |
| 6443 | le16_to_cpu(req->DataOffset), |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 6444 | get_rfc1002_len(work->request_buf)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6445 | err = -EINVAL; |
| 6446 | goto out; |
| 6447 | } |
| 6448 | |
| 6449 | data_buf = (char *)(((char *)&req->hdr.ProtocolId) + |
| 6450 | le16_to_cpu(req->DataOffset)); |
| 6451 | } |
| 6452 | |
| 6453 | ksmbd_debug(SMB, "flags %u\n", le32_to_cpu(req->Flags)); |
| 6454 | if (le32_to_cpu(req->Flags) & SMB2_WRITEFLAG_WRITE_THROUGH) |
| 6455 | writethrough = true; |
| 6456 | |
Namjae Jeon | 493fa2f | 2021-06-29 09:22:16 +0900 | [diff] [blame] | 6457 | ksmbd_debug(SMB, "filename %pd, offset %lld, len %zu\n", |
| 6458 | fp->filp->f_path.dentry, offset, length); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6459 | err = ksmbd_vfs_write(work, fp, data_buf, length, &offset, |
| 6460 | writethrough, &nbytes); |
| 6461 | if (err < 0) |
| 6462 | goto out; |
| 6463 | } else { |
| 6464 | /* read data from the client using rdma channel, and |
| 6465 | * write the data. |
| 6466 | */ |
| 6467 | nbytes = smb2_write_rdma_channel(work, req, fp, offset, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6468 | le32_to_cpu(req->RemainingBytes), |
| 6469 | writethrough); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6470 | if (nbytes < 0) { |
| 6471 | err = (int)nbytes; |
| 6472 | goto out; |
| 6473 | } |
| 6474 | } |
| 6475 | |
| 6476 | rsp->StructureSize = cpu_to_le16(17); |
| 6477 | rsp->DataOffset = 0; |
| 6478 | rsp->Reserved = 0; |
| 6479 | rsp->DataLength = cpu_to_le32(nbytes); |
| 6480 | rsp->DataRemaining = 0; |
| 6481 | rsp->Reserved2 = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 6482 | inc_rfc1001_len(work->response_buf, 16); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6483 | ksmbd_fd_put(work, fp); |
| 6484 | return 0; |
| 6485 | |
| 6486 | out: |
| 6487 | if (err == -EAGAIN) |
| 6488 | rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT; |
| 6489 | else if (err == -ENOSPC || err == -EFBIG) |
| 6490 | rsp->hdr.Status = STATUS_DISK_FULL; |
| 6491 | else if (err == -ENOENT) |
| 6492 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 6493 | else if (err == -EACCES) |
| 6494 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 6495 | else if (err == -ESHARE) |
| 6496 | rsp->hdr.Status = STATUS_SHARING_VIOLATION; |
| 6497 | else if (err == -EINVAL) |
| 6498 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 6499 | else |
| 6500 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 6501 | |
| 6502 | smb2_set_err_rsp(work); |
| 6503 | ksmbd_fd_put(work, fp); |
| 6504 | return err; |
| 6505 | } |
| 6506 | |
| 6507 | /** |
| 6508 | * smb2_flush() - handler for smb2 flush file - fsync |
| 6509 | * @work: smb work containing flush command buffer |
| 6510 | * |
| 6511 | * Return: 0 on success, otherwise error |
| 6512 | */ |
| 6513 | int smb2_flush(struct ksmbd_work *work) |
| 6514 | { |
| 6515 | struct smb2_flush_req *req; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 6516 | struct smb2_flush_rsp *rsp; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6517 | int err; |
| 6518 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6519 | WORK_BUFFERS(work, req, rsp); |
| 6520 | |
| 6521 | ksmbd_debug(SMB, "SMB2_FLUSH called for fid %llu\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6522 | le64_to_cpu(req->VolatileFileId)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6523 | |
| 6524 | err = ksmbd_vfs_fsync(work, |
| 6525 | le64_to_cpu(req->VolatileFileId), |
| 6526 | le64_to_cpu(req->PersistentFileId)); |
| 6527 | if (err) |
| 6528 | goto out; |
| 6529 | |
| 6530 | rsp->StructureSize = cpu_to_le16(4); |
| 6531 | rsp->Reserved = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 6532 | inc_rfc1001_len(work->response_buf, 4); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6533 | return 0; |
| 6534 | |
| 6535 | out: |
| 6536 | if (err) { |
| 6537 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 6538 | smb2_set_err_rsp(work); |
| 6539 | } |
| 6540 | |
| 6541 | return err; |
| 6542 | } |
| 6543 | |
| 6544 | /** |
| 6545 | * smb2_cancel() - handler for smb2 cancel command |
| 6546 | * @work: smb work containing cancel command buffer |
| 6547 | * |
| 6548 | * Return: 0 on success, otherwise error |
| 6549 | */ |
| 6550 | int smb2_cancel(struct ksmbd_work *work) |
| 6551 | { |
| 6552 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 6553 | struct smb2_hdr *hdr = smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6554 | struct smb2_hdr *chdr; |
| 6555 | struct ksmbd_work *cancel_work = NULL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6556 | int canceled = 0; |
| 6557 | struct list_head *command_list; |
| 6558 | |
| 6559 | 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] | 6560 | hdr->MessageId, hdr->Flags); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6561 | |
| 6562 | if (hdr->Flags & SMB2_FLAGS_ASYNC_COMMAND) { |
| 6563 | command_list = &conn->async_requests; |
| 6564 | |
| 6565 | spin_lock(&conn->request_lock); |
Namjae Jeon | 6f3d5ee | 2021-06-18 10:28:52 +0900 | [diff] [blame] | 6566 | list_for_each_entry(cancel_work, command_list, |
| 6567 | async_request_entry) { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 6568 | chdr = smb2_get_msg(cancel_work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6569 | |
| 6570 | if (cancel_work->async_id != |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6571 | le64_to_cpu(hdr->Id.AsyncId)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6572 | continue; |
| 6573 | |
| 6574 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6575 | "smb2 with AsyncId %llu cancelled command = 0x%x\n", |
| 6576 | le64_to_cpu(hdr->Id.AsyncId), |
| 6577 | le16_to_cpu(chdr->Command)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6578 | canceled = 1; |
| 6579 | break; |
| 6580 | } |
| 6581 | spin_unlock(&conn->request_lock); |
| 6582 | } else { |
| 6583 | command_list = &conn->requests; |
| 6584 | |
| 6585 | spin_lock(&conn->request_lock); |
Namjae Jeon | 6f3d5ee | 2021-06-18 10:28:52 +0900 | [diff] [blame] | 6586 | list_for_each_entry(cancel_work, command_list, request_entry) { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 6587 | chdr = smb2_get_msg(cancel_work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6588 | |
| 6589 | if (chdr->MessageId != hdr->MessageId || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6590 | cancel_work == work) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6591 | continue; |
| 6592 | |
| 6593 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6594 | "smb2 with mid %llu cancelled command = 0x%x\n", |
| 6595 | le64_to_cpu(hdr->MessageId), |
| 6596 | le16_to_cpu(chdr->Command)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6597 | canceled = 1; |
| 6598 | break; |
| 6599 | } |
| 6600 | spin_unlock(&conn->request_lock); |
| 6601 | } |
| 6602 | |
| 6603 | if (canceled) { |
| 6604 | cancel_work->state = KSMBD_WORK_CANCELLED; |
| 6605 | if (cancel_work->cancel_fn) |
| 6606 | cancel_work->cancel_fn(cancel_work->cancel_argv); |
| 6607 | } |
| 6608 | |
| 6609 | /* For SMB2_CANCEL command itself send no response*/ |
| 6610 | work->send_no_response = 1; |
| 6611 | return 0; |
| 6612 | } |
| 6613 | |
| 6614 | struct file_lock *smb_flock_init(struct file *f) |
| 6615 | { |
| 6616 | struct file_lock *fl; |
| 6617 | |
| 6618 | fl = locks_alloc_lock(); |
| 6619 | if (!fl) |
| 6620 | goto out; |
| 6621 | |
| 6622 | locks_init_lock(fl); |
| 6623 | |
| 6624 | fl->fl_owner = f; |
| 6625 | fl->fl_pid = current->tgid; |
| 6626 | fl->fl_file = f; |
| 6627 | fl->fl_flags = FL_POSIX; |
| 6628 | fl->fl_ops = NULL; |
| 6629 | fl->fl_lmops = NULL; |
| 6630 | |
| 6631 | out: |
| 6632 | return fl; |
| 6633 | } |
| 6634 | |
| 6635 | static int smb2_set_flock_flags(struct file_lock *flock, int flags) |
| 6636 | { |
| 6637 | int cmd = -EINVAL; |
| 6638 | |
| 6639 | /* Checking for wrong flag combination during lock request*/ |
| 6640 | switch (flags) { |
| 6641 | case SMB2_LOCKFLAG_SHARED: |
| 6642 | ksmbd_debug(SMB, "received shared request\n"); |
| 6643 | cmd = F_SETLKW; |
| 6644 | flock->fl_type = F_RDLCK; |
| 6645 | flock->fl_flags |= FL_SLEEP; |
| 6646 | break; |
| 6647 | case SMB2_LOCKFLAG_EXCLUSIVE: |
| 6648 | ksmbd_debug(SMB, "received exclusive request\n"); |
| 6649 | cmd = F_SETLKW; |
| 6650 | flock->fl_type = F_WRLCK; |
| 6651 | flock->fl_flags |= FL_SLEEP; |
| 6652 | break; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6653 | case SMB2_LOCKFLAG_SHARED | SMB2_LOCKFLAG_FAIL_IMMEDIATELY: |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6654 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6655 | "received shared & fail immediately request\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6656 | cmd = F_SETLK; |
| 6657 | flock->fl_type = F_RDLCK; |
| 6658 | break; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6659 | case SMB2_LOCKFLAG_EXCLUSIVE | SMB2_LOCKFLAG_FAIL_IMMEDIATELY: |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6660 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6661 | "received exclusive & fail immediately request\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6662 | cmd = F_SETLK; |
| 6663 | flock->fl_type = F_WRLCK; |
| 6664 | break; |
| 6665 | case SMB2_LOCKFLAG_UNLOCK: |
| 6666 | ksmbd_debug(SMB, "received unlock request\n"); |
| 6667 | flock->fl_type = F_UNLCK; |
| 6668 | cmd = 0; |
| 6669 | break; |
| 6670 | } |
| 6671 | |
| 6672 | return cmd; |
| 6673 | } |
| 6674 | |
| 6675 | static struct ksmbd_lock *smb2_lock_init(struct file_lock *flock, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6676 | unsigned int cmd, int flags, |
| 6677 | struct list_head *lock_list) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6678 | { |
| 6679 | struct ksmbd_lock *lock; |
| 6680 | |
| 6681 | lock = kzalloc(sizeof(struct ksmbd_lock), GFP_KERNEL); |
| 6682 | if (!lock) |
| 6683 | return NULL; |
| 6684 | |
| 6685 | lock->cmd = cmd; |
| 6686 | lock->fl = flock; |
| 6687 | lock->start = flock->fl_start; |
| 6688 | lock->end = flock->fl_end; |
| 6689 | lock->flags = flags; |
| 6690 | if (lock->start == lock->end) |
| 6691 | lock->zero_len = 1; |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6692 | INIT_LIST_HEAD(&lock->clist); |
| 6693 | INIT_LIST_HEAD(&lock->flist); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6694 | INIT_LIST_HEAD(&lock->llist); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6695 | list_add_tail(&lock->llist, lock_list); |
| 6696 | |
| 6697 | return lock; |
| 6698 | } |
| 6699 | |
| 6700 | static void smb2_remove_blocked_lock(void **argv) |
| 6701 | { |
| 6702 | struct file_lock *flock = (struct file_lock *)argv[0]; |
| 6703 | |
| 6704 | ksmbd_vfs_posix_lock_unblock(flock); |
| 6705 | wake_up(&flock->fl_wait); |
| 6706 | } |
| 6707 | |
| 6708 | static inline bool lock_defer_pending(struct file_lock *fl) |
| 6709 | { |
| 6710 | /* check pending lock waiters */ |
| 6711 | return waitqueue_active(&fl->fl_wait); |
| 6712 | } |
| 6713 | |
| 6714 | /** |
| 6715 | * smb2_lock() - handler for smb2 file lock command |
| 6716 | * @work: smb work containing lock command buffer |
| 6717 | * |
| 6718 | * Return: 0 on success, otherwise error |
| 6719 | */ |
| 6720 | int smb2_lock(struct ksmbd_work *work) |
| 6721 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 6722 | struct smb2_lock_req *req = smb2_get_msg(work->request_buf); |
| 6723 | struct smb2_lock_rsp *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6724 | struct smb2_lock_element *lock_ele; |
| 6725 | struct ksmbd_file *fp = NULL; |
| 6726 | struct file_lock *flock = NULL; |
| 6727 | struct file *filp = NULL; |
| 6728 | int lock_count; |
| 6729 | int flags = 0; |
| 6730 | int cmd = 0; |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6731 | int err = -EIO, i, rc = 0; |
Namjae Jeon | 50bf80a | 2021-05-14 12:20:07 +0900 | [diff] [blame] | 6732 | u64 lock_start, lock_length; |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6733 | struct ksmbd_lock *smb_lock = NULL, *cmp_lock, *tmp, *tmp2; |
| 6734 | struct ksmbd_conn *conn; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6735 | int nolock = 0; |
| 6736 | LIST_HEAD(lock_list); |
| 6737 | LIST_HEAD(rollback_list); |
| 6738 | int prior_lock = 0; |
| 6739 | |
| 6740 | ksmbd_debug(SMB, "Received lock request\n"); |
| 6741 | fp = ksmbd_lookup_fd_slow(work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6742 | le64_to_cpu(req->VolatileFileId), |
| 6743 | le64_to_cpu(req->PersistentFileId)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6744 | if (!fp) { |
| 6745 | ksmbd_debug(SMB, "Invalid file id for lock : %llu\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6746 | le64_to_cpu(req->VolatileFileId)); |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6747 | err = -ENOENT; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6748 | goto out2; |
| 6749 | } |
| 6750 | |
| 6751 | filp = fp->filp; |
| 6752 | lock_count = le16_to_cpu(req->LockCount); |
| 6753 | lock_ele = req->locks; |
| 6754 | |
| 6755 | ksmbd_debug(SMB, "lock count is %d\n", lock_count); |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6756 | if (!lock_count) { |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6757 | err = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6758 | goto out2; |
| 6759 | } |
| 6760 | |
| 6761 | for (i = 0; i < lock_count; i++) { |
| 6762 | flags = le32_to_cpu(lock_ele[i].Flags); |
| 6763 | |
| 6764 | flock = smb_flock_init(filp); |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6765 | if (!flock) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6766 | goto out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6767 | |
| 6768 | cmd = smb2_set_flock_flags(flock, flags); |
| 6769 | |
Namjae Jeon | 50bf80a | 2021-05-14 12:20:07 +0900 | [diff] [blame] | 6770 | lock_start = le64_to_cpu(lock_ele[i].Offset); |
| 6771 | lock_length = le64_to_cpu(lock_ele[i].Length); |
| 6772 | if (lock_start > U64_MAX - lock_length) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 6773 | pr_err("Invalid lock range requested\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6774 | rsp->hdr.Status = STATUS_INVALID_LOCK_RANGE; |
| 6775 | goto out; |
| 6776 | } |
| 6777 | |
Namjae Jeon | 50bf80a | 2021-05-14 12:20:07 +0900 | [diff] [blame] | 6778 | if (lock_start > OFFSET_MAX) |
| 6779 | flock->fl_start = OFFSET_MAX; |
| 6780 | else |
| 6781 | flock->fl_start = lock_start; |
| 6782 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6783 | lock_length = le64_to_cpu(lock_ele[i].Length); |
Namjae Jeon | 50bf80a | 2021-05-14 12:20:07 +0900 | [diff] [blame] | 6784 | if (lock_length > OFFSET_MAX - flock->fl_start) |
| 6785 | lock_length = OFFSET_MAX - flock->fl_start; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6786 | |
| 6787 | flock->fl_end = flock->fl_start + lock_length; |
| 6788 | |
| 6789 | if (flock->fl_end < flock->fl_start) { |
| 6790 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6791 | "the end offset(%llx) is smaller than the start offset(%llx)\n", |
| 6792 | flock->fl_end, flock->fl_start); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6793 | rsp->hdr.Status = STATUS_INVALID_LOCK_RANGE; |
| 6794 | goto out; |
| 6795 | } |
| 6796 | |
| 6797 | /* Check conflict locks in one request */ |
| 6798 | list_for_each_entry(cmp_lock, &lock_list, llist) { |
| 6799 | if (cmp_lock->fl->fl_start <= flock->fl_start && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6800 | cmp_lock->fl->fl_end >= flock->fl_end) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6801 | if (cmp_lock->fl->fl_type != F_UNLCK && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6802 | flock->fl_type != F_UNLCK) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 6803 | pr_err("conflict two locks in one request\n"); |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6804 | err = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6805 | goto out; |
| 6806 | } |
| 6807 | } |
| 6808 | } |
| 6809 | |
| 6810 | smb_lock = smb2_lock_init(flock, cmd, flags, &lock_list); |
| 6811 | if (!smb_lock) { |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6812 | err = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6813 | goto out; |
| 6814 | } |
| 6815 | } |
| 6816 | |
| 6817 | list_for_each_entry_safe(smb_lock, tmp, &lock_list, llist) { |
| 6818 | if (smb_lock->cmd < 0) { |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6819 | err = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6820 | goto out; |
| 6821 | } |
| 6822 | |
| 6823 | if (!(smb_lock->flags & SMB2_LOCKFLAG_MASK)) { |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6824 | err = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6825 | goto out; |
| 6826 | } |
| 6827 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6828 | if ((prior_lock & (SMB2_LOCKFLAG_EXCLUSIVE | SMB2_LOCKFLAG_SHARED) && |
| 6829 | smb_lock->flags & SMB2_LOCKFLAG_UNLOCK) || |
| 6830 | (prior_lock == SMB2_LOCKFLAG_UNLOCK && |
| 6831 | !(smb_lock->flags & SMB2_LOCKFLAG_UNLOCK))) { |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6832 | err = -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6833 | goto out; |
| 6834 | } |
| 6835 | |
| 6836 | prior_lock = smb_lock->flags; |
| 6837 | |
| 6838 | if (!(smb_lock->flags & SMB2_LOCKFLAG_UNLOCK) && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 6839 | !(smb_lock->flags & SMB2_LOCKFLAG_FAIL_IMMEDIATELY)) |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6840 | goto no_check_cl; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6841 | |
| 6842 | nolock = 1; |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6843 | /* check locks in connection list */ |
| 6844 | read_lock(&conn_list_lock); |
| 6845 | list_for_each_entry(conn, &conn_list, conns_list) { |
| 6846 | spin_lock(&conn->llist_lock); |
| 6847 | list_for_each_entry_safe(cmp_lock, tmp2, &conn->lock_list, clist) { |
| 6848 | if (file_inode(cmp_lock->fl->fl_file) != |
| 6849 | file_inode(smb_lock->fl->fl_file)) |
| 6850 | continue; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6851 | |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6852 | if (smb_lock->fl->fl_type == F_UNLCK) { |
| 6853 | if (cmp_lock->fl->fl_file == smb_lock->fl->fl_file && |
| 6854 | cmp_lock->start == smb_lock->start && |
| 6855 | cmp_lock->end == smb_lock->end && |
| 6856 | !lock_defer_pending(cmp_lock->fl)) { |
| 6857 | nolock = 0; |
| 6858 | list_del(&cmp_lock->flist); |
| 6859 | list_del(&cmp_lock->clist); |
| 6860 | spin_unlock(&conn->llist_lock); |
| 6861 | read_unlock(&conn_list_lock); |
| 6862 | |
| 6863 | locks_free_lock(cmp_lock->fl); |
| 6864 | kfree(cmp_lock); |
| 6865 | goto out_check_cl; |
| 6866 | } |
| 6867 | continue; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6868 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6869 | |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6870 | if (cmp_lock->fl->fl_file == smb_lock->fl->fl_file) { |
| 6871 | if (smb_lock->flags & SMB2_LOCKFLAG_SHARED) |
| 6872 | continue; |
| 6873 | } else { |
| 6874 | if (cmp_lock->flags & SMB2_LOCKFLAG_SHARED) |
| 6875 | continue; |
| 6876 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6877 | |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6878 | /* check zero byte lock range */ |
| 6879 | if (cmp_lock->zero_len && !smb_lock->zero_len && |
| 6880 | cmp_lock->start > smb_lock->start && |
| 6881 | cmp_lock->start < smb_lock->end) { |
| 6882 | spin_unlock(&conn->llist_lock); |
| 6883 | read_unlock(&conn_list_lock); |
| 6884 | pr_err("previous lock conflict with zero byte lock range\n"); |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6885 | goto out; |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6886 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6887 | |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6888 | if (smb_lock->zero_len && !cmp_lock->zero_len && |
| 6889 | smb_lock->start > cmp_lock->start && |
| 6890 | smb_lock->start < cmp_lock->end) { |
| 6891 | spin_unlock(&conn->llist_lock); |
| 6892 | read_unlock(&conn_list_lock); |
| 6893 | pr_err("current lock conflict with zero byte lock range\n"); |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6894 | goto out; |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6895 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6896 | |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6897 | if (((cmp_lock->start <= smb_lock->start && |
| 6898 | cmp_lock->end > smb_lock->start) || |
| 6899 | (cmp_lock->start < smb_lock->end && |
| 6900 | cmp_lock->end >= smb_lock->end)) && |
| 6901 | !cmp_lock->zero_len && !smb_lock->zero_len) { |
| 6902 | spin_unlock(&conn->llist_lock); |
| 6903 | read_unlock(&conn_list_lock); |
| 6904 | pr_err("Not allow lock operation on exclusive lock range\n"); |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6905 | goto out; |
| 6906 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6907 | } |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6908 | spin_unlock(&conn->llist_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6909 | } |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6910 | read_unlock(&conn_list_lock); |
| 6911 | out_check_cl: |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6912 | if (smb_lock->fl->fl_type == F_UNLCK && nolock) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 6913 | pr_err("Try to unlock nolocked range\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6914 | rsp->hdr.Status = STATUS_RANGE_NOT_LOCKED; |
| 6915 | goto out; |
| 6916 | } |
| 6917 | |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6918 | no_check_cl: |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6919 | if (smb_lock->zero_len) { |
| 6920 | err = 0; |
| 6921 | goto skip; |
| 6922 | } |
| 6923 | |
| 6924 | flock = smb_lock->fl; |
| 6925 | list_del(&smb_lock->llist); |
| 6926 | retry: |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6927 | rc = vfs_lock_file(filp, smb_lock->cmd, flock, NULL); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6928 | skip: |
| 6929 | if (flags & SMB2_LOCKFLAG_UNLOCK) { |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6930 | if (!rc) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6931 | ksmbd_debug(SMB, "File unlocked\n"); |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6932 | } else if (rc == -ENOENT) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6933 | rsp->hdr.Status = STATUS_NOT_LOCKED; |
| 6934 | goto out; |
| 6935 | } |
| 6936 | locks_free_lock(flock); |
| 6937 | kfree(smb_lock); |
| 6938 | } else { |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6939 | if (rc == FILE_LOCK_DEFERRED) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6940 | void **argv; |
| 6941 | |
| 6942 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6943 | "would have to wait for getting lock\n"); |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6944 | spin_lock(&work->conn->llist_lock); |
| 6945 | list_add_tail(&smb_lock->clist, |
| 6946 | &work->conn->lock_list); |
| 6947 | spin_unlock(&work->conn->llist_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6948 | list_add(&smb_lock->llist, &rollback_list); |
| 6949 | |
| 6950 | argv = kmalloc(sizeof(void *), GFP_KERNEL); |
| 6951 | if (!argv) { |
| 6952 | err = -ENOMEM; |
| 6953 | goto out; |
| 6954 | } |
| 6955 | argv[0] = flock; |
| 6956 | |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 6957 | rc = setup_async_work(work, |
| 6958 | smb2_remove_blocked_lock, |
| 6959 | argv); |
| 6960 | if (rc) { |
| 6961 | err = -ENOMEM; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6962 | goto out; |
| 6963 | } |
| 6964 | spin_lock(&fp->f_lock); |
| 6965 | list_add(&work->fp_entry, &fp->blocked_works); |
| 6966 | spin_unlock(&fp->f_lock); |
| 6967 | |
| 6968 | smb2_send_interim_resp(work, STATUS_PENDING); |
| 6969 | |
Hyunchul Lee | 45a64e8 | 2021-07-10 09:34:20 +0900 | [diff] [blame] | 6970 | ksmbd_vfs_posix_lock_wait(flock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6971 | |
Hyunchul Lee | d4075ab | 2021-06-25 07:02:10 +0900 | [diff] [blame] | 6972 | if (work->state != KSMBD_WORK_ACTIVE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6973 | list_del(&smb_lock->llist); |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 6974 | spin_lock(&work->conn->llist_lock); |
| 6975 | list_del(&smb_lock->clist); |
| 6976 | spin_unlock(&work->conn->llist_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6977 | locks_free_lock(flock); |
| 6978 | |
Hyunchul Lee | d4075ab | 2021-06-25 07:02:10 +0900 | [diff] [blame] | 6979 | if (work->state == KSMBD_WORK_CANCELLED) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6980 | spin_lock(&fp->f_lock); |
| 6981 | list_del(&work->fp_entry); |
| 6982 | spin_unlock(&fp->f_lock); |
| 6983 | rsp->hdr.Status = |
| 6984 | STATUS_CANCELLED; |
| 6985 | kfree(smb_lock); |
| 6986 | smb2_send_interim_resp(work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 6987 | STATUS_CANCELLED); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 6988 | work->send_no_response = 1; |
| 6989 | goto out; |
| 6990 | } |
| 6991 | init_smb2_rsp_hdr(work); |
| 6992 | smb2_set_err_rsp(work); |
| 6993 | rsp->hdr.Status = |
| 6994 | STATUS_RANGE_NOT_LOCKED; |
| 6995 | kfree(smb_lock); |
| 6996 | goto out2; |
| 6997 | } |
| 6998 | |
| 6999 | list_del(&smb_lock->llist); |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 7000 | spin_lock(&work->conn->llist_lock); |
| 7001 | list_del(&smb_lock->clist); |
| 7002 | spin_unlock(&work->conn->llist_lock); |
| 7003 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7004 | spin_lock(&fp->f_lock); |
| 7005 | list_del(&work->fp_entry); |
| 7006 | spin_unlock(&fp->f_lock); |
| 7007 | goto retry; |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 7008 | } else if (!rc) { |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 7009 | spin_lock(&work->conn->llist_lock); |
| 7010 | list_add_tail(&smb_lock->clist, |
| 7011 | &work->conn->lock_list); |
| 7012 | list_add_tail(&smb_lock->flist, |
| 7013 | &fp->lock_list); |
| 7014 | spin_unlock(&work->conn->llist_lock); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7015 | list_add(&smb_lock->llist, &rollback_list); |
| 7016 | ksmbd_debug(SMB, "successful in taking lock\n"); |
| 7017 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7018 | goto out; |
| 7019 | } |
| 7020 | } |
| 7021 | } |
| 7022 | |
| 7023 | if (atomic_read(&fp->f_ci->op_count) > 1) |
| 7024 | smb_break_all_oplock(work, fp); |
| 7025 | |
| 7026 | rsp->StructureSize = cpu_to_le16(4); |
| 7027 | ksmbd_debug(SMB, "successful in taking lock\n"); |
| 7028 | rsp->hdr.Status = STATUS_SUCCESS; |
| 7029 | rsp->Reserved = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 7030 | inc_rfc1001_len(work->response_buf, 4); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7031 | ksmbd_fd_put(work, fp); |
Namjae Jeon | 96ad4ec | 2021-07-13 17:17:28 +0900 | [diff] [blame] | 7032 | return 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7033 | |
| 7034 | out: |
| 7035 | list_for_each_entry_safe(smb_lock, tmp, &lock_list, llist) { |
| 7036 | locks_free_lock(smb_lock->fl); |
| 7037 | list_del(&smb_lock->llist); |
| 7038 | kfree(smb_lock); |
| 7039 | } |
| 7040 | |
| 7041 | list_for_each_entry_safe(smb_lock, tmp, &rollback_list, llist) { |
| 7042 | struct file_lock *rlock = NULL; |
| 7043 | |
| 7044 | rlock = smb_flock_init(filp); |
| 7045 | rlock->fl_type = F_UNLCK; |
| 7046 | rlock->fl_start = smb_lock->start; |
| 7047 | rlock->fl_end = smb_lock->end; |
| 7048 | |
Namjae Jeon | 96ad4ec | 2021-07-13 17:17:28 +0900 | [diff] [blame] | 7049 | rc = vfs_lock_file(filp, 0, rlock, NULL); |
| 7050 | if (rc) |
| 7051 | pr_err("rollback unlock fail : %d\n", rc); |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 7052 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7053 | list_del(&smb_lock->llist); |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 7054 | spin_lock(&work->conn->llist_lock); |
| 7055 | if (!list_empty(&smb_lock->flist)) |
| 7056 | list_del(&smb_lock->flist); |
| 7057 | list_del(&smb_lock->clist); |
| 7058 | spin_unlock(&work->conn->llist_lock); |
| 7059 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7060 | locks_free_lock(smb_lock->fl); |
| 7061 | locks_free_lock(rlock); |
| 7062 | kfree(smb_lock); |
| 7063 | } |
| 7064 | out2: |
Namjae Jeon | 6c99dfc | 2021-07-27 09:40:05 +0900 | [diff] [blame] | 7065 | ksmbd_debug(SMB, "failed in taking lock(flags : %x), err : %d\n", flags, err); |
| 7066 | |
| 7067 | if (!rsp->hdr.Status) { |
| 7068 | if (err == -EINVAL) |
| 7069 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 7070 | else if (err == -ENOMEM) |
| 7071 | rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES; |
| 7072 | else if (err == -ENOENT) |
| 7073 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 7074 | else |
| 7075 | rsp->hdr.Status = STATUS_LOCK_NOT_GRANTED; |
| 7076 | } |
| 7077 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7078 | smb2_set_err_rsp(work); |
| 7079 | ksmbd_fd_put(work, fp); |
Namjae Jeon | 96ad4ec | 2021-07-13 17:17:28 +0900 | [diff] [blame] | 7080 | return err; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7081 | } |
| 7082 | |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7083 | static int fsctl_copychunk(struct ksmbd_work *work, |
| 7084 | struct copychunk_ioctl_req *ci_req, |
| 7085 | unsigned int cnt_code, |
| 7086 | unsigned int input_count, |
| 7087 | unsigned long long volatile_id, |
| 7088 | unsigned long long persistent_id, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7089 | struct smb2_ioctl_rsp *rsp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7090 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7091 | struct copychunk_ioctl_rsp *ci_rsp; |
| 7092 | struct ksmbd_file *src_fp = NULL, *dst_fp = NULL; |
| 7093 | struct srv_copychunk *chunks; |
| 7094 | unsigned int i, chunk_count, chunk_count_written = 0; |
| 7095 | unsigned int chunk_size_written = 0; |
| 7096 | loff_t total_size_written = 0; |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7097 | int ret = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7098 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7099 | ci_rsp = (struct copychunk_ioctl_rsp *)&rsp->Buffer[0]; |
| 7100 | |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7101 | rsp->VolatileFileId = cpu_to_le64(volatile_id); |
| 7102 | rsp->PersistentFileId = cpu_to_le64(persistent_id); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7103 | ci_rsp->ChunksWritten = |
| 7104 | cpu_to_le32(ksmbd_server_side_copy_max_chunk_count()); |
| 7105 | ci_rsp->ChunkBytesWritten = |
| 7106 | cpu_to_le32(ksmbd_server_side_copy_max_chunk_size()); |
| 7107 | ci_rsp->TotalBytesWritten = |
| 7108 | cpu_to_le32(ksmbd_server_side_copy_max_total_size()); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7109 | |
| 7110 | chunks = (struct srv_copychunk *)&ci_req->Chunks[0]; |
| 7111 | chunk_count = le32_to_cpu(ci_req->ChunkCount); |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7112 | if (chunk_count == 0) |
| 7113 | goto out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7114 | total_size_written = 0; |
| 7115 | |
| 7116 | /* verify the SRV_COPYCHUNK_COPY packet */ |
| 7117 | if (chunk_count > ksmbd_server_side_copy_max_chunk_count() || |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7118 | input_count < offsetof(struct copychunk_ioctl_req, Chunks) + |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7119 | chunk_count * sizeof(struct srv_copychunk)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7120 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 7121 | return -EINVAL; |
| 7122 | } |
| 7123 | |
| 7124 | for (i = 0; i < chunk_count; i++) { |
| 7125 | if (le32_to_cpu(chunks[i].Length) == 0 || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7126 | 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] | 7127 | break; |
| 7128 | total_size_written += le32_to_cpu(chunks[i].Length); |
| 7129 | } |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7130 | |
| 7131 | if (i < chunk_count || |
| 7132 | total_size_written > ksmbd_server_side_copy_max_total_size()) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7133 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 7134 | return -EINVAL; |
| 7135 | } |
| 7136 | |
| 7137 | src_fp = ksmbd_lookup_foreign_fd(work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7138 | le64_to_cpu(ci_req->ResumeKey[0])); |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7139 | dst_fp = ksmbd_lookup_fd_slow(work, volatile_id, persistent_id); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7140 | ret = -EINVAL; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7141 | if (!src_fp || |
| 7142 | src_fp->persistent_id != le64_to_cpu(ci_req->ResumeKey[1])) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7143 | rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND; |
| 7144 | goto out; |
| 7145 | } |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7146 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7147 | if (!dst_fp) { |
| 7148 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 7149 | goto out; |
| 7150 | } |
| 7151 | |
| 7152 | /* |
| 7153 | * FILE_READ_DATA should only be included in |
| 7154 | * the FSCTL_COPYCHUNK case |
| 7155 | */ |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7156 | if (cnt_code == FSCTL_COPYCHUNK && |
| 7157 | !(dst_fp->daccess & (FILE_READ_DATA_LE | FILE_GENERIC_READ_LE))) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7158 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 7159 | goto out; |
| 7160 | } |
| 7161 | |
| 7162 | ret = ksmbd_vfs_copy_file_ranges(work, src_fp, dst_fp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7163 | chunks, chunk_count, |
| 7164 | &chunk_count_written, |
| 7165 | &chunk_size_written, |
| 7166 | &total_size_written); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7167 | if (ret < 0) { |
| 7168 | if (ret == -EACCES) |
| 7169 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 7170 | if (ret == -EAGAIN) |
| 7171 | rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT; |
| 7172 | else if (ret == -EBADF) |
| 7173 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
| 7174 | else if (ret == -EFBIG || ret == -ENOSPC) |
| 7175 | rsp->hdr.Status = STATUS_DISK_FULL; |
| 7176 | else if (ret == -EINVAL) |
| 7177 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 7178 | else if (ret == -EISDIR) |
| 7179 | rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY; |
| 7180 | else if (ret == -E2BIG) |
| 7181 | rsp->hdr.Status = STATUS_INVALID_VIEW_SIZE; |
| 7182 | else |
| 7183 | rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR; |
| 7184 | } |
| 7185 | |
| 7186 | ci_rsp->ChunksWritten = cpu_to_le32(chunk_count_written); |
| 7187 | ci_rsp->ChunkBytesWritten = cpu_to_le32(chunk_size_written); |
| 7188 | ci_rsp->TotalBytesWritten = cpu_to_le32(total_size_written); |
| 7189 | out: |
| 7190 | ksmbd_fd_put(work, src_fp); |
| 7191 | ksmbd_fd_put(work, dst_fp); |
| 7192 | return ret; |
| 7193 | } |
| 7194 | |
| 7195 | static __be32 idev_ipv4_address(struct in_device *idev) |
| 7196 | { |
| 7197 | __be32 addr = 0; |
| 7198 | |
| 7199 | struct in_ifaddr *ifa; |
| 7200 | |
| 7201 | rcu_read_lock(); |
| 7202 | in_dev_for_each_ifa_rcu(ifa, idev) { |
| 7203 | if (ifa->ifa_flags & IFA_F_SECONDARY) |
| 7204 | continue; |
| 7205 | |
| 7206 | addr = ifa->ifa_address; |
| 7207 | break; |
| 7208 | } |
| 7209 | rcu_read_unlock(); |
| 7210 | return addr; |
| 7211 | } |
| 7212 | |
| 7213 | static int fsctl_query_iface_info_ioctl(struct ksmbd_conn *conn, |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7214 | struct smb2_ioctl_rsp *rsp, |
| 7215 | unsigned int out_buf_len) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7216 | { |
| 7217 | struct network_interface_info_ioctl_rsp *nii_rsp = NULL; |
| 7218 | int nbytes = 0; |
| 7219 | struct net_device *netdev; |
| 7220 | struct sockaddr_storage_rsp *sockaddr_storage; |
| 7221 | unsigned int flags; |
| 7222 | unsigned long long speed; |
Namjae Jeon | f1abdb7 | 2021-07-27 13:25:13 +0900 | [diff] [blame] | 7223 | struct sockaddr_in6 *csin6 = (struct sockaddr_in6 *)&conn->peer_addr; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7224 | |
| 7225 | rtnl_lock(); |
| 7226 | for_each_netdev(&init_net, netdev) { |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7227 | if (out_buf_len < |
| 7228 | nbytes + sizeof(struct network_interface_info_ioctl_rsp)) { |
| 7229 | rtnl_unlock(); |
| 7230 | return -ENOSPC; |
| 7231 | } |
| 7232 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7233 | if (netdev->type == ARPHRD_LOOPBACK) |
| 7234 | continue; |
| 7235 | |
| 7236 | flags = dev_get_flags(netdev); |
| 7237 | if (!(flags & IFF_RUNNING)) |
| 7238 | continue; |
| 7239 | |
| 7240 | nii_rsp = (struct network_interface_info_ioctl_rsp *) |
| 7241 | &rsp->Buffer[nbytes]; |
| 7242 | nii_rsp->IfIndex = cpu_to_le32(netdev->ifindex); |
| 7243 | |
Hyunchul Lee | 03d8d4f | 2021-07-13 16:09:34 +0900 | [diff] [blame] | 7244 | nii_rsp->Capability = 0; |
Hyunchul Lee | 03d8d4f | 2021-07-13 16:09:34 +0900 | [diff] [blame] | 7245 | if (ksmbd_rdma_capable_netdev(netdev)) |
| 7246 | nii_rsp->Capability |= cpu_to_le32(RDMA_CAPABLE); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7247 | |
| 7248 | nii_rsp->Next = cpu_to_le32(152); |
| 7249 | nii_rsp->Reserved = 0; |
| 7250 | |
| 7251 | if (netdev->ethtool_ops->get_link_ksettings) { |
| 7252 | struct ethtool_link_ksettings cmd; |
| 7253 | |
| 7254 | netdev->ethtool_ops->get_link_ksettings(netdev, &cmd); |
| 7255 | speed = cmd.base.speed; |
| 7256 | } else { |
Per Forlin | d475866 | 2021-08-30 13:23:04 +0900 | [diff] [blame] | 7257 | ksmbd_debug(SMB, "%s %s\n", netdev->name, |
| 7258 | "speed is unknown, defaulting to 1Gb/sec"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7259 | speed = SPEED_1000; |
| 7260 | } |
| 7261 | |
| 7262 | speed *= 1000000; |
| 7263 | nii_rsp->LinkSpeed = cpu_to_le64(speed); |
| 7264 | |
| 7265 | sockaddr_storage = (struct sockaddr_storage_rsp *) |
| 7266 | nii_rsp->SockAddr_Storage; |
| 7267 | memset(sockaddr_storage, 0, 128); |
| 7268 | |
Namjae Jeon | f1abdb7 | 2021-07-27 13:25:13 +0900 | [diff] [blame] | 7269 | if (conn->peer_addr.ss_family == PF_INET || |
| 7270 | ipv6_addr_v4mapped(&csin6->sin6_addr)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7271 | struct in_device *idev; |
| 7272 | |
| 7273 | sockaddr_storage->Family = cpu_to_le16(INTERNETWORK); |
| 7274 | sockaddr_storage->addr4.Port = 0; |
| 7275 | |
| 7276 | idev = __in_dev_get_rtnl(netdev); |
| 7277 | if (!idev) |
| 7278 | continue; |
| 7279 | sockaddr_storage->addr4.IPv4address = |
| 7280 | idev_ipv4_address(idev); |
| 7281 | } else { |
| 7282 | struct inet6_dev *idev6; |
| 7283 | struct inet6_ifaddr *ifa; |
| 7284 | __u8 *ipv6_addr = sockaddr_storage->addr6.IPv6address; |
| 7285 | |
| 7286 | sockaddr_storage->Family = cpu_to_le16(INTERNETWORKV6); |
| 7287 | sockaddr_storage->addr6.Port = 0; |
| 7288 | sockaddr_storage->addr6.FlowInfo = 0; |
| 7289 | |
| 7290 | idev6 = __in6_dev_get(netdev); |
| 7291 | if (!idev6) |
| 7292 | continue; |
| 7293 | |
| 7294 | list_for_each_entry(ifa, &idev6->addr_list, if_list) { |
| 7295 | if (ifa->flags & (IFA_F_TENTATIVE | |
| 7296 | IFA_F_DEPRECATED)) |
| 7297 | continue; |
| 7298 | memcpy(ipv6_addr, ifa->addr.s6_addr, 16); |
| 7299 | break; |
| 7300 | } |
| 7301 | sockaddr_storage->addr6.ScopeId = 0; |
| 7302 | } |
| 7303 | |
| 7304 | nbytes += sizeof(struct network_interface_info_ioctl_rsp); |
| 7305 | } |
| 7306 | rtnl_unlock(); |
| 7307 | |
| 7308 | /* zero if this is last one */ |
| 7309 | if (nii_rsp) |
| 7310 | nii_rsp->Next = 0; |
| 7311 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7312 | rsp->PersistentFileId = cpu_to_le64(SMB2_NO_FID); |
| 7313 | rsp->VolatileFileId = cpu_to_le64(SMB2_NO_FID); |
| 7314 | return nbytes; |
| 7315 | } |
| 7316 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7317 | static int fsctl_validate_negotiate_info(struct ksmbd_conn *conn, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7318 | struct validate_negotiate_info_req *neg_req, |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7319 | struct validate_negotiate_info_rsp *neg_rsp, |
| 7320 | unsigned int in_buf_len) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7321 | { |
| 7322 | int ret = 0; |
| 7323 | int dialect; |
| 7324 | |
Marios Makassikis | 78f1688 | 2021-10-28 21:01:27 +0200 | [diff] [blame] | 7325 | if (in_buf_len < offsetof(struct validate_negotiate_info_req, Dialects) + |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7326 | le16_to_cpu(neg_req->DialectCount) * sizeof(__le16)) |
| 7327 | return -EINVAL; |
| 7328 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7329 | dialect = ksmbd_lookup_dialect_by_id(neg_req->Dialects, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7330 | neg_req->DialectCount); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7331 | if (dialect == BAD_PROT_ID || dialect != conn->dialect) { |
| 7332 | ret = -EINVAL; |
| 7333 | goto err_out; |
| 7334 | } |
| 7335 | |
| 7336 | if (strncmp(neg_req->Guid, conn->ClientGUID, SMB2_CLIENT_GUID_SIZE)) { |
| 7337 | ret = -EINVAL; |
| 7338 | goto err_out; |
| 7339 | } |
| 7340 | |
| 7341 | if (le16_to_cpu(neg_req->SecurityMode) != conn->cli_sec_mode) { |
| 7342 | ret = -EINVAL; |
| 7343 | goto err_out; |
| 7344 | } |
| 7345 | |
| 7346 | if (le32_to_cpu(neg_req->Capabilities) != conn->cli_cap) { |
| 7347 | ret = -EINVAL; |
| 7348 | goto err_out; |
| 7349 | } |
| 7350 | |
| 7351 | neg_rsp->Capabilities = cpu_to_le32(conn->vals->capabilities); |
| 7352 | memset(neg_rsp->Guid, 0, SMB2_CLIENT_GUID_SIZE); |
| 7353 | neg_rsp->SecurityMode = cpu_to_le16(conn->srv_sec_mode); |
| 7354 | neg_rsp->Dialect = cpu_to_le16(conn->dialect); |
| 7355 | err_out: |
| 7356 | return ret; |
| 7357 | } |
| 7358 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7359 | static int fsctl_query_allocated_ranges(struct ksmbd_work *work, u64 id, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7360 | struct file_allocated_range_buffer *qar_req, |
| 7361 | struct file_allocated_range_buffer *qar_rsp, |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7362 | unsigned int in_count, unsigned int *out_count) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7363 | { |
| 7364 | struct ksmbd_file *fp; |
| 7365 | loff_t start, length; |
| 7366 | int ret = 0; |
| 7367 | |
| 7368 | *out_count = 0; |
| 7369 | if (in_count == 0) |
| 7370 | return -EINVAL; |
| 7371 | |
| 7372 | fp = ksmbd_lookup_fd_fast(work, id); |
| 7373 | if (!fp) |
| 7374 | return -ENOENT; |
| 7375 | |
| 7376 | start = le64_to_cpu(qar_req->file_offset); |
| 7377 | length = le64_to_cpu(qar_req->length); |
| 7378 | |
| 7379 | ret = ksmbd_vfs_fqar_lseek(fp, start, length, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7380 | qar_rsp, in_count, out_count); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7381 | if (ret && ret != -E2BIG) |
| 7382 | *out_count = 0; |
| 7383 | |
| 7384 | ksmbd_fd_put(work, fp); |
| 7385 | return ret; |
| 7386 | } |
| 7387 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7388 | static int fsctl_pipe_transceive(struct ksmbd_work *work, u64 id, |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7389 | unsigned int out_buf_len, |
| 7390 | struct smb2_ioctl_req *req, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7391 | struct smb2_ioctl_rsp *rsp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7392 | { |
| 7393 | struct ksmbd_rpc_command *rpc_resp; |
| 7394 | char *data_buf = (char *)&req->Buffer[0]; |
| 7395 | int nbytes = 0; |
| 7396 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7397 | rpc_resp = ksmbd_rpc_ioctl(work->sess, id, data_buf, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7398 | le32_to_cpu(req->InputCount)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7399 | if (rpc_resp) { |
| 7400 | if (rpc_resp->flags == KSMBD_RPC_SOME_NOT_MAPPED) { |
| 7401 | /* |
| 7402 | * set STATUS_SOME_NOT_MAPPED response |
| 7403 | * for unknown domain sid. |
| 7404 | */ |
| 7405 | rsp->hdr.Status = STATUS_SOME_NOT_MAPPED; |
| 7406 | } else if (rpc_resp->flags == KSMBD_RPC_ENOTIMPLEMENTED) { |
| 7407 | rsp->hdr.Status = STATUS_NOT_SUPPORTED; |
| 7408 | goto out; |
| 7409 | } else if (rpc_resp->flags != KSMBD_RPC_OK) { |
| 7410 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 7411 | goto out; |
| 7412 | } |
| 7413 | |
| 7414 | nbytes = rpc_resp->payload_sz; |
| 7415 | if (rpc_resp->payload_sz > out_buf_len) { |
| 7416 | rsp->hdr.Status = STATUS_BUFFER_OVERFLOW; |
| 7417 | nbytes = out_buf_len; |
| 7418 | } |
| 7419 | |
| 7420 | if (!rpc_resp->payload_sz) { |
| 7421 | rsp->hdr.Status = |
| 7422 | STATUS_UNEXPECTED_IO_ERROR; |
| 7423 | goto out; |
| 7424 | } |
| 7425 | |
| 7426 | memcpy((char *)rsp->Buffer, rpc_resp->payload, nbytes); |
| 7427 | } |
| 7428 | out: |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 7429 | kvfree(rpc_resp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7430 | return nbytes; |
| 7431 | } |
| 7432 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7433 | static inline int fsctl_set_sparse(struct ksmbd_work *work, u64 id, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7434 | struct file_sparse *sparse) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7435 | { |
| 7436 | struct ksmbd_file *fp; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 7437 | struct user_namespace *user_ns; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7438 | int ret = 0; |
| 7439 | __le32 old_fattr; |
| 7440 | |
| 7441 | fp = ksmbd_lookup_fd_fast(work, id); |
| 7442 | if (!fp) |
| 7443 | return -ENOENT; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 7444 | user_ns = file_mnt_user_ns(fp->filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7445 | |
| 7446 | old_fattr = fp->f_ci->m_fattr; |
| 7447 | if (sparse->SetSparse) |
| 7448 | fp->f_ci->m_fattr |= ATTR_SPARSE_FILE_LE; |
| 7449 | else |
| 7450 | fp->f_ci->m_fattr &= ~ATTR_SPARSE_FILE_LE; |
| 7451 | |
| 7452 | if (fp->f_ci->m_fattr != old_fattr && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7453 | test_share_config_flag(work->tcon->share_conf, |
| 7454 | KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7455 | struct xattr_dos_attrib da; |
| 7456 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 7457 | ret = ksmbd_vfs_get_dos_attrib_xattr(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 7458 | fp->filp->f_path.dentry, &da); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7459 | if (ret <= 0) |
| 7460 | goto out; |
| 7461 | |
| 7462 | da.attr = le32_to_cpu(fp->f_ci->m_fattr); |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 7463 | ret = ksmbd_vfs_set_dos_attrib_xattr(user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 7464 | fp->filp->f_path.dentry, &da); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7465 | if (ret) |
| 7466 | fp->f_ci->m_fattr = old_fattr; |
| 7467 | } |
| 7468 | |
| 7469 | out: |
| 7470 | ksmbd_fd_put(work, fp); |
| 7471 | return ret; |
| 7472 | } |
| 7473 | |
| 7474 | static int fsctl_request_resume_key(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7475 | struct smb2_ioctl_req *req, |
| 7476 | struct resume_key_ioctl_rsp *key_rsp) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7477 | { |
| 7478 | struct ksmbd_file *fp; |
| 7479 | |
| 7480 | fp = ksmbd_lookup_fd_slow(work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7481 | le64_to_cpu(req->VolatileFileId), |
| 7482 | le64_to_cpu(req->PersistentFileId)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7483 | if (!fp) |
| 7484 | return -ENOENT; |
| 7485 | |
| 7486 | memset(key_rsp, 0, sizeof(*key_rsp)); |
| 7487 | key_rsp->ResumeKey[0] = req->VolatileFileId; |
| 7488 | key_rsp->ResumeKey[1] = req->PersistentFileId; |
| 7489 | ksmbd_fd_put(work, fp); |
| 7490 | |
| 7491 | return 0; |
| 7492 | } |
| 7493 | |
| 7494 | /** |
| 7495 | * smb2_ioctl() - handler for smb2 ioctl command |
| 7496 | * @work: smb work containing ioctl command buffer |
| 7497 | * |
| 7498 | * Return: 0 on success, otherwise error |
| 7499 | */ |
| 7500 | int smb2_ioctl(struct ksmbd_work *work) |
| 7501 | { |
| 7502 | struct smb2_ioctl_req *req; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 7503 | struct smb2_ioctl_rsp *rsp; |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7504 | unsigned int cnt_code, nbytes = 0, out_buf_len, in_buf_len; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7505 | u64 id = KSMBD_NO_FID; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7506 | struct ksmbd_conn *conn = work->conn; |
| 7507 | int ret = 0; |
| 7508 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7509 | if (work->next_smb2_rcv_hdr_off) { |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 7510 | req = ksmbd_req_buf_next(work); |
| 7511 | rsp = ksmbd_resp_buf_next(work); |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 7512 | if (!has_file_id(le64_to_cpu(req->VolatileFileId))) { |
| 7513 | ksmbd_debug(SMB, "Compound request set FID = %llu\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7514 | work->compound_fid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7515 | id = work->compound_fid; |
| 7516 | } |
| 7517 | } else { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 7518 | req = smb2_get_msg(work->request_buf); |
| 7519 | rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7520 | } |
| 7521 | |
Namjae Jeon | 3867369 | 2021-07-08 12:32:27 +0900 | [diff] [blame] | 7522 | if (!has_file_id(id)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7523 | id = le64_to_cpu(req->VolatileFileId); |
| 7524 | |
| 7525 | if (req->Flags != cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL)) { |
| 7526 | rsp->hdr.Status = STATUS_NOT_SUPPORTED; |
| 7527 | goto out; |
| 7528 | } |
| 7529 | |
| 7530 | cnt_code = le32_to_cpu(req->CntCode); |
Hyunchul Lee | 34061d6 | 2021-10-16 08:39:54 +0900 | [diff] [blame] | 7531 | ret = smb2_calc_max_out_buf_len(work, 48, |
| 7532 | le32_to_cpu(req->MaxOutputResponse)); |
| 7533 | if (ret < 0) { |
| 7534 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 7535 | goto out; |
| 7536 | } |
| 7537 | out_buf_len = (unsigned int)ret; |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7538 | in_buf_len = le32_to_cpu(req->InputCount); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7539 | |
| 7540 | switch (cnt_code) { |
| 7541 | case FSCTL_DFS_GET_REFERRALS: |
| 7542 | case FSCTL_DFS_GET_REFERRALS_EX: |
| 7543 | /* Not support DFS yet */ |
| 7544 | rsp->hdr.Status = STATUS_FS_DRIVER_REQUIRED; |
| 7545 | goto out; |
| 7546 | case FSCTL_CREATE_OR_GET_OBJECT_ID: |
| 7547 | { |
| 7548 | struct file_object_buf_type1_ioctl_rsp *obj_buf; |
| 7549 | |
| 7550 | nbytes = sizeof(struct file_object_buf_type1_ioctl_rsp); |
| 7551 | obj_buf = (struct file_object_buf_type1_ioctl_rsp *) |
| 7552 | &rsp->Buffer[0]; |
| 7553 | |
| 7554 | /* |
| 7555 | * TODO: This is dummy implementation to pass smbtorture |
| 7556 | * Need to check correct response later |
| 7557 | */ |
| 7558 | memset(obj_buf->ObjectId, 0x0, 16); |
| 7559 | memset(obj_buf->BirthVolumeId, 0x0, 16); |
| 7560 | memset(obj_buf->BirthObjectId, 0x0, 16); |
| 7561 | memset(obj_buf->DomainId, 0x0, 16); |
| 7562 | |
| 7563 | break; |
| 7564 | } |
| 7565 | case FSCTL_PIPE_TRANSCEIVE: |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7566 | 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] | 7567 | nbytes = fsctl_pipe_transceive(work, id, out_buf_len, req, rsp); |
| 7568 | break; |
| 7569 | case FSCTL_VALIDATE_NEGOTIATE_INFO: |
| 7570 | if (conn->dialect < SMB30_PROT_ID) { |
| 7571 | ret = -EOPNOTSUPP; |
| 7572 | goto out; |
| 7573 | } |
| 7574 | |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7575 | if (in_buf_len < sizeof(struct validate_negotiate_info_req)) |
| 7576 | return -EINVAL; |
| 7577 | |
| 7578 | if (out_buf_len < sizeof(struct validate_negotiate_info_rsp)) |
| 7579 | return -EINVAL; |
| 7580 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7581 | ret = fsctl_validate_negotiate_info(conn, |
| 7582 | (struct validate_negotiate_info_req *)&req->Buffer[0], |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7583 | (struct validate_negotiate_info_rsp *)&rsp->Buffer[0], |
| 7584 | in_buf_len); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7585 | if (ret < 0) |
| 7586 | goto out; |
| 7587 | |
| 7588 | nbytes = sizeof(struct validate_negotiate_info_rsp); |
| 7589 | rsp->PersistentFileId = cpu_to_le64(SMB2_NO_FID); |
| 7590 | rsp->VolatileFileId = cpu_to_le64(SMB2_NO_FID); |
| 7591 | break; |
| 7592 | case FSCTL_QUERY_NETWORK_INTERFACE_INFO: |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7593 | ret = fsctl_query_iface_info_ioctl(conn, rsp, out_buf_len); |
| 7594 | if (ret < 0) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7595 | goto out; |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7596 | nbytes = ret; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7597 | break; |
| 7598 | case FSCTL_REQUEST_RESUME_KEY: |
| 7599 | if (out_buf_len < sizeof(struct resume_key_ioctl_rsp)) { |
| 7600 | ret = -EINVAL; |
| 7601 | goto out; |
| 7602 | } |
| 7603 | |
| 7604 | ret = fsctl_request_resume_key(work, req, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7605 | (struct resume_key_ioctl_rsp *)&rsp->Buffer[0]); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7606 | if (ret < 0) |
| 7607 | goto out; |
| 7608 | rsp->PersistentFileId = req->PersistentFileId; |
| 7609 | rsp->VolatileFileId = req->VolatileFileId; |
| 7610 | nbytes = sizeof(struct resume_key_ioctl_rsp); |
| 7611 | break; |
| 7612 | case FSCTL_COPYCHUNK: |
| 7613 | case FSCTL_COPYCHUNK_WRITE: |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7614 | if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7615 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7616 | "User does not have write permission\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7617 | ret = -EACCES; |
| 7618 | goto out; |
| 7619 | } |
| 7620 | |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7621 | if (in_buf_len < sizeof(struct copychunk_ioctl_req)) { |
| 7622 | ret = -EINVAL; |
| 7623 | goto out; |
| 7624 | } |
| 7625 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7626 | if (out_buf_len < sizeof(struct copychunk_ioctl_rsp)) { |
| 7627 | ret = -EINVAL; |
| 7628 | goto out; |
| 7629 | } |
| 7630 | |
| 7631 | nbytes = sizeof(struct copychunk_ioctl_rsp); |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7632 | rsp->VolatileFileId = req->VolatileFileId; |
| 7633 | rsp->PersistentFileId = req->PersistentFileId; |
| 7634 | fsctl_copychunk(work, |
| 7635 | (struct copychunk_ioctl_req *)&req->Buffer[0], |
| 7636 | le32_to_cpu(req->CntCode), |
| 7637 | le32_to_cpu(req->InputCount), |
| 7638 | le64_to_cpu(req->VolatileFileId), |
| 7639 | le64_to_cpu(req->PersistentFileId), |
| 7640 | rsp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7641 | break; |
| 7642 | case FSCTL_SET_SPARSE: |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7643 | if (in_buf_len < sizeof(struct file_sparse)) { |
| 7644 | ret = -EINVAL; |
| 7645 | goto out; |
| 7646 | } |
| 7647 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7648 | ret = fsctl_set_sparse(work, id, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7649 | (struct file_sparse *)&req->Buffer[0]); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7650 | if (ret < 0) |
| 7651 | goto out; |
| 7652 | break; |
| 7653 | case FSCTL_SET_ZERO_DATA: |
| 7654 | { |
| 7655 | struct file_zero_data_information *zero_data; |
| 7656 | struct ksmbd_file *fp; |
| 7657 | loff_t off, len; |
| 7658 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7659 | if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7660 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7661 | "User does not have write permission\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7662 | ret = -EACCES; |
| 7663 | goto out; |
| 7664 | } |
| 7665 | |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7666 | if (in_buf_len < sizeof(struct file_zero_data_information)) { |
| 7667 | ret = -EINVAL; |
| 7668 | goto out; |
| 7669 | } |
| 7670 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7671 | zero_data = |
| 7672 | (struct file_zero_data_information *)&req->Buffer[0]; |
| 7673 | |
| 7674 | fp = ksmbd_lookup_fd_fast(work, id); |
| 7675 | if (!fp) { |
| 7676 | ret = -ENOENT; |
| 7677 | goto out; |
| 7678 | } |
| 7679 | |
| 7680 | off = le64_to_cpu(zero_data->FileOffset); |
| 7681 | len = le64_to_cpu(zero_data->BeyondFinalZero) - off; |
| 7682 | |
| 7683 | ret = ksmbd_vfs_zero_data(work, fp, off, len); |
| 7684 | ksmbd_fd_put(work, fp); |
| 7685 | if (ret < 0) |
| 7686 | goto out; |
| 7687 | break; |
| 7688 | } |
| 7689 | case FSCTL_QUERY_ALLOCATED_RANGES: |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7690 | if (in_buf_len < sizeof(struct file_allocated_range_buffer)) { |
| 7691 | ret = -EINVAL; |
| 7692 | goto out; |
| 7693 | } |
| 7694 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7695 | ret = fsctl_query_allocated_ranges(work, id, |
| 7696 | (struct file_allocated_range_buffer *)&req->Buffer[0], |
| 7697 | (struct file_allocated_range_buffer *)&rsp->Buffer[0], |
| 7698 | out_buf_len / |
| 7699 | sizeof(struct file_allocated_range_buffer), &nbytes); |
| 7700 | if (ret == -E2BIG) { |
| 7701 | rsp->hdr.Status = STATUS_BUFFER_OVERFLOW; |
| 7702 | } else if (ret < 0) { |
| 7703 | nbytes = 0; |
| 7704 | goto out; |
| 7705 | } |
| 7706 | |
| 7707 | nbytes *= sizeof(struct file_allocated_range_buffer); |
| 7708 | break; |
| 7709 | case FSCTL_GET_REPARSE_POINT: |
| 7710 | { |
| 7711 | struct reparse_data_buffer *reparse_ptr; |
| 7712 | struct ksmbd_file *fp; |
| 7713 | |
| 7714 | reparse_ptr = (struct reparse_data_buffer *)&rsp->Buffer[0]; |
| 7715 | fp = ksmbd_lookup_fd_fast(work, id); |
| 7716 | if (!fp) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 7717 | pr_err("not found fp!!\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7718 | ret = -ENOENT; |
| 7719 | goto out; |
| 7720 | } |
| 7721 | |
| 7722 | reparse_ptr->ReparseTag = |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 7723 | smb2_get_reparse_tag_special_file(file_inode(fp->filp)->i_mode); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7724 | reparse_ptr->ReparseDataLength = 0; |
| 7725 | ksmbd_fd_put(work, fp); |
| 7726 | nbytes = sizeof(struct reparse_data_buffer); |
| 7727 | break; |
| 7728 | } |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 7729 | case FSCTL_DUPLICATE_EXTENTS_TO_FILE: |
| 7730 | { |
| 7731 | struct ksmbd_file *fp_in, *fp_out = NULL; |
| 7732 | struct duplicate_extents_to_file *dup_ext; |
| 7733 | loff_t src_off, dst_off, length, cloned; |
| 7734 | |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7735 | if (in_buf_len < sizeof(struct duplicate_extents_to_file)) { |
| 7736 | ret = -EINVAL; |
| 7737 | goto out; |
| 7738 | } |
| 7739 | |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 7740 | dup_ext = (struct duplicate_extents_to_file *)&req->Buffer[0]; |
| 7741 | |
| 7742 | fp_in = ksmbd_lookup_fd_slow(work, dup_ext->VolatileFileHandle, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7743 | dup_ext->PersistentFileHandle); |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 7744 | if (!fp_in) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 7745 | pr_err("not found file handle in duplicate extent to file\n"); |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 7746 | ret = -ENOENT; |
| 7747 | goto out; |
| 7748 | } |
| 7749 | |
| 7750 | fp_out = ksmbd_lookup_fd_fast(work, id); |
| 7751 | if (!fp_out) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 7752 | pr_err("not found fp\n"); |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 7753 | ret = -ENOENT; |
| 7754 | goto dup_ext_out; |
| 7755 | } |
| 7756 | |
| 7757 | src_off = le64_to_cpu(dup_ext->SourceFileOffset); |
| 7758 | dst_off = le64_to_cpu(dup_ext->TargetFileOffset); |
| 7759 | length = le64_to_cpu(dup_ext->ByteCount); |
| 7760 | 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] | 7761 | dst_off, length, 0); |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 7762 | if (cloned == -EXDEV || cloned == -EOPNOTSUPP) { |
| 7763 | ret = -EOPNOTSUPP; |
| 7764 | goto dup_ext_out; |
| 7765 | } else if (cloned != length) { |
Namjae Jeon | f852477 | 2021-06-18 10:28:00 +0900 | [diff] [blame] | 7766 | cloned = vfs_copy_file_range(fp_in->filp, src_off, |
| 7767 | fp_out->filp, dst_off, length, 0); |
Namjae Jeon | eb81736 | 2021-05-18 10:37:59 +0900 | [diff] [blame] | 7768 | if (cloned != length) { |
| 7769 | if (cloned < 0) |
| 7770 | ret = cloned; |
| 7771 | else |
| 7772 | ret = -EINVAL; |
| 7773 | } |
| 7774 | } |
| 7775 | |
| 7776 | dup_ext_out: |
| 7777 | ksmbd_fd_put(work, fp_in); |
| 7778 | ksmbd_fd_put(work, fp_out); |
| 7779 | if (ret < 0) |
| 7780 | goto out; |
| 7781 | break; |
| 7782 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7783 | default: |
| 7784 | ksmbd_debug(SMB, "not implemented yet ioctl command 0x%x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7785 | cnt_code); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7786 | ret = -EOPNOTSUPP; |
| 7787 | goto out; |
| 7788 | } |
| 7789 | |
| 7790 | rsp->CntCode = cpu_to_le32(cnt_code); |
| 7791 | rsp->InputCount = cpu_to_le32(0); |
| 7792 | rsp->InputOffset = cpu_to_le32(112); |
| 7793 | rsp->OutputOffset = cpu_to_le32(112); |
| 7794 | rsp->OutputCount = cpu_to_le32(nbytes); |
| 7795 | rsp->StructureSize = cpu_to_le16(49); |
| 7796 | rsp->Reserved = cpu_to_le16(0); |
| 7797 | rsp->Flags = cpu_to_le32(0); |
| 7798 | rsp->Reserved2 = cpu_to_le32(0); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 7799 | inc_rfc1001_len(work->response_buf, 48 + nbytes); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7800 | |
| 7801 | return 0; |
| 7802 | |
| 7803 | out: |
| 7804 | if (ret == -EACCES) |
| 7805 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
| 7806 | else if (ret == -ENOENT) |
| 7807 | rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND; |
| 7808 | else if (ret == -EOPNOTSUPP) |
| 7809 | rsp->hdr.Status = STATUS_NOT_SUPPORTED; |
Namjae Jeon | f7db8fd | 2021-10-08 07:31:03 +0900 | [diff] [blame] | 7810 | else if (ret == -ENOSPC) |
| 7811 | rsp->hdr.Status = STATUS_BUFFER_TOO_SMALL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7812 | else if (ret < 0 || rsp->hdr.Status == 0) |
| 7813 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 7814 | smb2_set_err_rsp(work); |
| 7815 | return 0; |
| 7816 | } |
| 7817 | |
| 7818 | /** |
| 7819 | * smb20_oplock_break_ack() - handler for smb2.0 oplock break command |
| 7820 | * @work: smb work containing oplock break command buffer |
| 7821 | * |
| 7822 | * Return: 0 |
| 7823 | */ |
| 7824 | static void smb20_oplock_break_ack(struct ksmbd_work *work) |
| 7825 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 7826 | struct smb2_oplock_break *req = smb2_get_msg(work->request_buf); |
| 7827 | struct smb2_oplock_break *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7828 | struct ksmbd_file *fp; |
| 7829 | struct oplock_info *opinfo = NULL; |
| 7830 | __le32 err = 0; |
| 7831 | int ret = 0; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7832 | u64 volatile_id, persistent_id; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7833 | char req_oplevel = 0, rsp_oplevel = 0; |
| 7834 | unsigned int oplock_change_type; |
| 7835 | |
| 7836 | volatile_id = le64_to_cpu(req->VolatileFid); |
| 7837 | persistent_id = le64_to_cpu(req->PersistentFid); |
| 7838 | req_oplevel = req->OplockLevel; |
| 7839 | ksmbd_debug(OPLOCK, "v_id %llu, p_id %llu request oplock level %d\n", |
| 7840 | volatile_id, persistent_id, req_oplevel); |
| 7841 | |
| 7842 | fp = ksmbd_lookup_fd_slow(work, volatile_id, persistent_id); |
| 7843 | if (!fp) { |
| 7844 | rsp->hdr.Status = STATUS_FILE_CLOSED; |
| 7845 | smb2_set_err_rsp(work); |
| 7846 | return; |
| 7847 | } |
| 7848 | |
| 7849 | opinfo = opinfo_get(fp); |
| 7850 | if (!opinfo) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 7851 | pr_err("unexpected null oplock_info\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7852 | rsp->hdr.Status = STATUS_INVALID_OPLOCK_PROTOCOL; |
| 7853 | smb2_set_err_rsp(work); |
| 7854 | ksmbd_fd_put(work, fp); |
| 7855 | return; |
| 7856 | } |
| 7857 | |
| 7858 | if (opinfo->level == SMB2_OPLOCK_LEVEL_NONE) { |
| 7859 | rsp->hdr.Status = STATUS_INVALID_OPLOCK_PROTOCOL; |
| 7860 | goto err_out; |
| 7861 | } |
| 7862 | |
| 7863 | if (opinfo->op_state == OPLOCK_STATE_NONE) { |
| 7864 | ksmbd_debug(SMB, "unexpected oplock state 0x%x\n", opinfo->op_state); |
| 7865 | rsp->hdr.Status = STATUS_UNSUCCESSFUL; |
| 7866 | goto err_out; |
| 7867 | } |
| 7868 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7869 | if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE || |
| 7870 | opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) && |
| 7871 | (req_oplevel != SMB2_OPLOCK_LEVEL_II && |
| 7872 | req_oplevel != SMB2_OPLOCK_LEVEL_NONE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7873 | err = STATUS_INVALID_OPLOCK_PROTOCOL; |
| 7874 | oplock_change_type = OPLOCK_WRITE_TO_NONE; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7875 | } else if (opinfo->level == SMB2_OPLOCK_LEVEL_II && |
| 7876 | req_oplevel != SMB2_OPLOCK_LEVEL_NONE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7877 | err = STATUS_INVALID_OPLOCK_PROTOCOL; |
| 7878 | oplock_change_type = OPLOCK_READ_TO_NONE; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7879 | } else if (req_oplevel == SMB2_OPLOCK_LEVEL_II || |
| 7880 | req_oplevel == SMB2_OPLOCK_LEVEL_NONE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7881 | err = STATUS_INVALID_DEVICE_STATE; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7882 | if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE || |
| 7883 | opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) && |
| 7884 | req_oplevel == SMB2_OPLOCK_LEVEL_II) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7885 | oplock_change_type = OPLOCK_WRITE_TO_READ; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7886 | } else if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE || |
| 7887 | opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) && |
| 7888 | req_oplevel == SMB2_OPLOCK_LEVEL_NONE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7889 | oplock_change_type = OPLOCK_WRITE_TO_NONE; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7890 | } else if (opinfo->level == SMB2_OPLOCK_LEVEL_II && |
| 7891 | req_oplevel == SMB2_OPLOCK_LEVEL_NONE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7892 | oplock_change_type = OPLOCK_READ_TO_NONE; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7893 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7894 | oplock_change_type = 0; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7895 | } |
| 7896 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7897 | oplock_change_type = 0; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7898 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7899 | |
| 7900 | switch (oplock_change_type) { |
| 7901 | case OPLOCK_WRITE_TO_READ: |
| 7902 | ret = opinfo_write_to_read(opinfo); |
| 7903 | rsp_oplevel = SMB2_OPLOCK_LEVEL_II; |
| 7904 | break; |
| 7905 | case OPLOCK_WRITE_TO_NONE: |
| 7906 | ret = opinfo_write_to_none(opinfo); |
| 7907 | rsp_oplevel = SMB2_OPLOCK_LEVEL_NONE; |
| 7908 | break; |
| 7909 | case OPLOCK_READ_TO_NONE: |
| 7910 | ret = opinfo_read_to_none(opinfo); |
| 7911 | rsp_oplevel = SMB2_OPLOCK_LEVEL_NONE; |
| 7912 | break; |
| 7913 | default: |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 7914 | pr_err("unknown oplock change 0x%x -> 0x%x\n", |
| 7915 | opinfo->level, rsp_oplevel); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7916 | } |
| 7917 | |
| 7918 | if (ret < 0) { |
| 7919 | rsp->hdr.Status = err; |
| 7920 | goto err_out; |
| 7921 | } |
| 7922 | |
| 7923 | opinfo_put(opinfo); |
| 7924 | ksmbd_fd_put(work, fp); |
| 7925 | opinfo->op_state = OPLOCK_STATE_NONE; |
| 7926 | wake_up_interruptible_all(&opinfo->oplock_q); |
| 7927 | |
| 7928 | rsp->StructureSize = cpu_to_le16(24); |
| 7929 | rsp->OplockLevel = rsp_oplevel; |
| 7930 | rsp->Reserved = 0; |
| 7931 | rsp->Reserved2 = 0; |
| 7932 | rsp->VolatileFid = cpu_to_le64(volatile_id); |
| 7933 | rsp->PersistentFid = cpu_to_le64(persistent_id); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 7934 | inc_rfc1001_len(work->response_buf, 24); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7935 | return; |
| 7936 | |
| 7937 | err_out: |
| 7938 | opinfo->op_state = OPLOCK_STATE_NONE; |
| 7939 | wake_up_interruptible_all(&opinfo->oplock_q); |
| 7940 | |
| 7941 | opinfo_put(opinfo); |
| 7942 | ksmbd_fd_put(work, fp); |
| 7943 | smb2_set_err_rsp(work); |
| 7944 | } |
| 7945 | |
| 7946 | static int check_lease_state(struct lease *lease, __le32 req_state) |
| 7947 | { |
| 7948 | if ((lease->new_state == |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 7949 | (SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_HANDLE_CACHING_LE)) && |
| 7950 | !(req_state & SMB2_LEASE_WRITE_CACHING_LE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7951 | lease->new_state = req_state; |
| 7952 | return 0; |
| 7953 | } |
| 7954 | |
| 7955 | if (lease->new_state == req_state) |
| 7956 | return 0; |
| 7957 | |
| 7958 | return 1; |
| 7959 | } |
| 7960 | |
| 7961 | /** |
| 7962 | * smb21_lease_break_ack() - handler for smb2.1 lease break command |
| 7963 | * @work: smb work containing lease break command buffer |
| 7964 | * |
| 7965 | * Return: 0 |
| 7966 | */ |
| 7967 | static void smb21_lease_break_ack(struct ksmbd_work *work) |
| 7968 | { |
| 7969 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 7970 | struct smb2_lease_ack *req = smb2_get_msg(work->request_buf); |
| 7971 | struct smb2_lease_ack *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7972 | struct oplock_info *opinfo; |
| 7973 | __le32 err = 0; |
| 7974 | int ret = 0; |
| 7975 | unsigned int lease_change_type; |
| 7976 | __le32 lease_state; |
| 7977 | struct lease *lease; |
| 7978 | |
| 7979 | ksmbd_debug(OPLOCK, "smb21 lease break, lease state(0x%x)\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 7980 | le32_to_cpu(req->LeaseState)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7981 | opinfo = lookup_lease_in_table(conn, req->LeaseKey); |
| 7982 | if (!opinfo) { |
| 7983 | ksmbd_debug(OPLOCK, "file not opened\n"); |
| 7984 | smb2_set_err_rsp(work); |
| 7985 | rsp->hdr.Status = STATUS_UNSUCCESSFUL; |
| 7986 | return; |
| 7987 | } |
| 7988 | lease = opinfo->o_lease; |
| 7989 | |
| 7990 | if (opinfo->op_state == OPLOCK_STATE_NONE) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 7991 | pr_err("unexpected lease break state 0x%x\n", |
| 7992 | opinfo->op_state); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 7993 | rsp->hdr.Status = STATUS_UNSUCCESSFUL; |
| 7994 | goto err_out; |
| 7995 | } |
| 7996 | |
| 7997 | if (check_lease_state(lease, req->LeaseState)) { |
| 7998 | rsp->hdr.Status = STATUS_REQUEST_NOT_ACCEPTED; |
| 7999 | ksmbd_debug(OPLOCK, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 8000 | "req lease state: 0x%x, expected state: 0x%x\n", |
| 8001 | req->LeaseState, lease->new_state); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8002 | goto err_out; |
| 8003 | } |
| 8004 | |
| 8005 | if (!atomic_read(&opinfo->breaking_cnt)) { |
| 8006 | rsp->hdr.Status = STATUS_UNSUCCESSFUL; |
| 8007 | goto err_out; |
| 8008 | } |
| 8009 | |
| 8010 | /* check for bad lease state */ |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 8011 | if (req->LeaseState & |
| 8012 | (~(SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_HANDLE_CACHING_LE))) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8013 | err = STATUS_INVALID_OPLOCK_PROTOCOL; |
| 8014 | if (lease->state & SMB2_LEASE_WRITE_CACHING_LE) |
| 8015 | lease_change_type = OPLOCK_WRITE_TO_NONE; |
| 8016 | else |
| 8017 | lease_change_type = OPLOCK_READ_TO_NONE; |
| 8018 | ksmbd_debug(OPLOCK, "handle bad lease state 0x%x -> 0x%x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 8019 | le32_to_cpu(lease->state), |
| 8020 | le32_to_cpu(req->LeaseState)); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 8021 | } else if (lease->state == SMB2_LEASE_READ_CACHING_LE && |
| 8022 | req->LeaseState != SMB2_LEASE_NONE_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8023 | err = STATUS_INVALID_OPLOCK_PROTOCOL; |
| 8024 | lease_change_type = OPLOCK_READ_TO_NONE; |
| 8025 | ksmbd_debug(OPLOCK, "handle bad lease state 0x%x -> 0x%x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 8026 | le32_to_cpu(lease->state), |
| 8027 | le32_to_cpu(req->LeaseState)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8028 | } else { |
| 8029 | /* valid lease state changes */ |
| 8030 | err = STATUS_INVALID_DEVICE_STATE; |
| 8031 | if (req->LeaseState == SMB2_LEASE_NONE_LE) { |
| 8032 | if (lease->state & SMB2_LEASE_WRITE_CACHING_LE) |
| 8033 | lease_change_type = OPLOCK_WRITE_TO_NONE; |
| 8034 | else |
| 8035 | lease_change_type = OPLOCK_READ_TO_NONE; |
| 8036 | } else if (req->LeaseState & SMB2_LEASE_READ_CACHING_LE) { |
| 8037 | if (lease->state & SMB2_LEASE_WRITE_CACHING_LE) |
| 8038 | lease_change_type = OPLOCK_WRITE_TO_READ; |
| 8039 | else |
| 8040 | lease_change_type = OPLOCK_READ_HANDLE_TO_READ; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 8041 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8042 | lease_change_type = 0; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 8043 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8044 | } |
| 8045 | |
| 8046 | switch (lease_change_type) { |
| 8047 | case OPLOCK_WRITE_TO_READ: |
| 8048 | ret = opinfo_write_to_read(opinfo); |
| 8049 | break; |
| 8050 | case OPLOCK_READ_HANDLE_TO_READ: |
| 8051 | ret = opinfo_read_handle_to_read(opinfo); |
| 8052 | break; |
| 8053 | case OPLOCK_WRITE_TO_NONE: |
| 8054 | ret = opinfo_write_to_none(opinfo); |
| 8055 | break; |
| 8056 | case OPLOCK_READ_TO_NONE: |
| 8057 | ret = opinfo_read_to_none(opinfo); |
| 8058 | break; |
| 8059 | default: |
| 8060 | ksmbd_debug(OPLOCK, "unknown lease change 0x%x -> 0x%x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 8061 | le32_to_cpu(lease->state), |
| 8062 | le32_to_cpu(req->LeaseState)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8063 | } |
| 8064 | |
| 8065 | lease_state = lease->state; |
| 8066 | opinfo->op_state = OPLOCK_STATE_NONE; |
| 8067 | wake_up_interruptible_all(&opinfo->oplock_q); |
| 8068 | atomic_dec(&opinfo->breaking_cnt); |
| 8069 | wake_up_interruptible_all(&opinfo->oplock_brk); |
| 8070 | opinfo_put(opinfo); |
| 8071 | |
| 8072 | if (ret < 0) { |
| 8073 | rsp->hdr.Status = err; |
| 8074 | goto err_out; |
| 8075 | } |
| 8076 | |
| 8077 | rsp->StructureSize = cpu_to_le16(36); |
| 8078 | rsp->Reserved = 0; |
| 8079 | rsp->Flags = 0; |
| 8080 | memcpy(rsp->LeaseKey, req->LeaseKey, 16); |
| 8081 | rsp->LeaseState = lease_state; |
| 8082 | rsp->LeaseDuration = 0; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 8083 | inc_rfc1001_len(work->response_buf, 36); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8084 | return; |
| 8085 | |
| 8086 | err_out: |
| 8087 | opinfo->op_state = OPLOCK_STATE_NONE; |
| 8088 | wake_up_interruptible_all(&opinfo->oplock_q); |
| 8089 | atomic_dec(&opinfo->breaking_cnt); |
| 8090 | wake_up_interruptible_all(&opinfo->oplock_brk); |
| 8091 | |
| 8092 | opinfo_put(opinfo); |
| 8093 | smb2_set_err_rsp(work); |
| 8094 | } |
| 8095 | |
| 8096 | /** |
| 8097 | * smb2_oplock_break() - dispatcher for smb2.0 and 2.1 oplock/lease break |
| 8098 | * @work: smb work containing oplock/lease break command buffer |
| 8099 | * |
| 8100 | * Return: 0 |
| 8101 | */ |
| 8102 | int smb2_oplock_break(struct ksmbd_work *work) |
| 8103 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 8104 | struct smb2_oplock_break *req = smb2_get_msg(work->request_buf); |
| 8105 | struct smb2_oplock_break *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8106 | |
| 8107 | switch (le16_to_cpu(req->StructureSize)) { |
| 8108 | case OP_BREAK_STRUCT_SIZE_20: |
| 8109 | smb20_oplock_break_ack(work); |
| 8110 | break; |
| 8111 | case OP_BREAK_STRUCT_SIZE_21: |
| 8112 | smb21_lease_break_ack(work); |
| 8113 | break; |
| 8114 | default: |
| 8115 | ksmbd_debug(OPLOCK, "invalid break cmd %d\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 8116 | le16_to_cpu(req->StructureSize)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8117 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
| 8118 | smb2_set_err_rsp(work); |
| 8119 | } |
| 8120 | |
| 8121 | return 0; |
| 8122 | } |
| 8123 | |
| 8124 | /** |
| 8125 | * smb2_notify() - handler for smb2 notify request |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 8126 | * @work: smb work containing notify command buffer |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8127 | * |
| 8128 | * Return: 0 |
| 8129 | */ |
| 8130 | int smb2_notify(struct ksmbd_work *work) |
| 8131 | { |
| 8132 | struct smb2_notify_req *req; |
| 8133 | struct smb2_notify_rsp *rsp; |
| 8134 | |
| 8135 | WORK_BUFFERS(work, req, rsp); |
| 8136 | |
| 8137 | if (work->next_smb2_rcv_hdr_off && req->hdr.NextCommand) { |
| 8138 | rsp->hdr.Status = STATUS_INTERNAL_ERROR; |
| 8139 | smb2_set_err_rsp(work); |
| 8140 | return 0; |
| 8141 | } |
| 8142 | |
| 8143 | smb2_set_err_rsp(work); |
| 8144 | rsp->hdr.Status = STATUS_NOT_IMPLEMENTED; |
| 8145 | return 0; |
| 8146 | } |
| 8147 | |
| 8148 | /** |
| 8149 | * smb2_is_sign_req() - handler for checking packet signing status |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 8150 | * @work: smb work containing notify command buffer |
| 8151 | * @command: SMB2 command id |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8152 | * |
| 8153 | * Return: true if packed is signed, false otherwise |
| 8154 | */ |
| 8155 | bool smb2_is_sign_req(struct ksmbd_work *work, unsigned int command) |
| 8156 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 8157 | struct smb2_hdr *rcv_hdr2 = smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8158 | |
| 8159 | if ((rcv_hdr2->Flags & SMB2_FLAGS_SIGNED) && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 8160 | command != SMB2_NEGOTIATE_HE && |
| 8161 | command != SMB2_SESSION_SETUP_HE && |
| 8162 | command != SMB2_OPLOCK_BREAK_HE) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8163 | return true; |
| 8164 | |
kernel test robot | 5616015 | 2021-05-12 09:24:37 +0900 | [diff] [blame] | 8165 | return false; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8166 | } |
| 8167 | |
| 8168 | /** |
| 8169 | * smb2_check_sign_req() - handler for req packet sign processing |
| 8170 | * @work: smb work containing notify command buffer |
| 8171 | * |
| 8172 | * Return: 1 on success, 0 otherwise |
| 8173 | */ |
| 8174 | int smb2_check_sign_req(struct ksmbd_work *work) |
| 8175 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 8176 | struct smb2_hdr *hdr; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8177 | char signature_req[SMB2_SIGNATURE_SIZE]; |
| 8178 | char signature[SMB2_HMACSHA256_SIZE]; |
| 8179 | struct kvec iov[1]; |
| 8180 | size_t len; |
| 8181 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 8182 | hdr = smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8183 | if (work->next_smb2_rcv_hdr_off) |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 8184 | hdr = ksmbd_req_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8185 | |
| 8186 | if (!hdr->NextCommand && !work->next_smb2_rcv_hdr_off) |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 8187 | len = get_rfc1002_len(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8188 | else if (hdr->NextCommand) |
| 8189 | len = le32_to_cpu(hdr->NextCommand); |
| 8190 | else |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 8191 | len = get_rfc1002_len(work->request_buf) - |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8192 | work->next_smb2_rcv_hdr_off; |
| 8193 | |
| 8194 | memcpy(signature_req, hdr->Signature, SMB2_SIGNATURE_SIZE); |
| 8195 | memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE); |
| 8196 | |
| 8197 | iov[0].iov_base = (char *)&hdr->ProtocolId; |
| 8198 | iov[0].iov_len = len; |
| 8199 | |
| 8200 | 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] | 8201 | signature)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8202 | return 0; |
| 8203 | |
| 8204 | if (memcmp(signature, signature_req, SMB2_SIGNATURE_SIZE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 8205 | pr_err("bad smb2 signature\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8206 | return 0; |
| 8207 | } |
| 8208 | |
| 8209 | return 1; |
| 8210 | } |
| 8211 | |
| 8212 | /** |
| 8213 | * smb2_set_sign_rsp() - handler for rsp packet sign processing |
| 8214 | * @work: smb work containing notify command buffer |
| 8215 | * |
| 8216 | */ |
| 8217 | void smb2_set_sign_rsp(struct ksmbd_work *work) |
| 8218 | { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 8219 | struct smb2_hdr *hdr; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8220 | struct smb2_hdr *req_hdr; |
| 8221 | char signature[SMB2_HMACSHA256_SIZE]; |
| 8222 | struct kvec iov[2]; |
| 8223 | size_t len; |
| 8224 | int n_vec = 1; |
| 8225 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 8226 | hdr = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8227 | if (work->next_smb2_rsp_hdr_off) |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 8228 | hdr = ksmbd_resp_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8229 | |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 8230 | req_hdr = ksmbd_req_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8231 | |
| 8232 | if (!work->next_smb2_rsp_hdr_off) { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 8233 | len = get_rfc1002_len(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8234 | if (req_hdr->NextCommand) |
| 8235 | len = ALIGN(len, 8); |
| 8236 | } else { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 8237 | len = get_rfc1002_len(work->response_buf) - |
| 8238 | work->next_smb2_rsp_hdr_off; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8239 | len = ALIGN(len, 8); |
| 8240 | } |
| 8241 | |
| 8242 | if (req_hdr->NextCommand) |
| 8243 | hdr->NextCommand = cpu_to_le32(len); |
| 8244 | |
| 8245 | hdr->Flags |= SMB2_FLAGS_SIGNED; |
| 8246 | memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE); |
| 8247 | |
| 8248 | iov[0].iov_base = (char *)&hdr->ProtocolId; |
| 8249 | iov[0].iov_len = len; |
| 8250 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8251 | if (work->aux_payload_sz) { |
| 8252 | iov[0].iov_len -= work->aux_payload_sz; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8253 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8254 | iov[1].iov_base = work->aux_payload_buf; |
| 8255 | iov[1].iov_len = work->aux_payload_sz; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8256 | n_vec++; |
| 8257 | } |
| 8258 | |
| 8259 | 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] | 8260 | signature)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8261 | memcpy(hdr->Signature, signature, SMB2_SIGNATURE_SIZE); |
| 8262 | } |
| 8263 | |
| 8264 | /** |
| 8265 | * smb3_check_sign_req() - handler for req packet sign processing |
| 8266 | * @work: smb work containing notify command buffer |
| 8267 | * |
| 8268 | * Return: 1 on success, 0 otherwise |
| 8269 | */ |
| 8270 | int smb3_check_sign_req(struct ksmbd_work *work) |
| 8271 | { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 8272 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8273 | char *signing_key; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 8274 | struct smb2_hdr *hdr; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8275 | struct channel *chann; |
| 8276 | char signature_req[SMB2_SIGNATURE_SIZE]; |
| 8277 | char signature[SMB2_CMACAES_SIZE]; |
| 8278 | struct kvec iov[1]; |
| 8279 | size_t len; |
| 8280 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 8281 | hdr = smb2_get_msg(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8282 | if (work->next_smb2_rcv_hdr_off) |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 8283 | hdr = ksmbd_req_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8284 | |
| 8285 | if (!hdr->NextCommand && !work->next_smb2_rcv_hdr_off) |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 8286 | len = get_rfc1002_len(work->request_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8287 | else if (hdr->NextCommand) |
| 8288 | len = le32_to_cpu(hdr->NextCommand); |
| 8289 | else |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 8290 | len = get_rfc1002_len(work->request_buf) - |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8291 | work->next_smb2_rcv_hdr_off; |
| 8292 | |
| 8293 | if (le16_to_cpu(hdr->Command) == SMB2_SESSION_SETUP_HE) { |
| 8294 | signing_key = work->sess->smb3signingkey; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8295 | } else { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 8296 | chann = lookup_chann_list(work->sess, conn); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8297 | if (!chann) |
| 8298 | return 0; |
| 8299 | signing_key = chann->smb3signingkey; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8300 | } |
| 8301 | |
| 8302 | if (!signing_key) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 8303 | pr_err("SMB3 signing key is not generated\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8304 | return 0; |
| 8305 | } |
| 8306 | |
| 8307 | memcpy(signature_req, hdr->Signature, SMB2_SIGNATURE_SIZE); |
| 8308 | memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE); |
| 8309 | iov[0].iov_base = (char *)&hdr->ProtocolId; |
| 8310 | iov[0].iov_len = len; |
| 8311 | |
| 8312 | if (ksmbd_sign_smb3_pdu(conn, signing_key, iov, 1, signature)) |
| 8313 | return 0; |
| 8314 | |
| 8315 | if (memcmp(signature, signature_req, SMB2_SIGNATURE_SIZE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 8316 | pr_err("bad smb2 signature\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8317 | return 0; |
| 8318 | } |
| 8319 | |
| 8320 | return 1; |
| 8321 | } |
| 8322 | |
| 8323 | /** |
| 8324 | * smb3_set_sign_rsp() - handler for rsp packet sign processing |
| 8325 | * @work: smb work containing notify command buffer |
| 8326 | * |
| 8327 | */ |
| 8328 | void smb3_set_sign_rsp(struct ksmbd_work *work) |
| 8329 | { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 8330 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 8331 | struct smb2_hdr *req_hdr, *hdr; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8332 | struct channel *chann; |
| 8333 | char signature[SMB2_CMACAES_SIZE]; |
| 8334 | struct kvec iov[2]; |
| 8335 | int n_vec = 1; |
| 8336 | size_t len; |
| 8337 | char *signing_key; |
| 8338 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 8339 | hdr = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8340 | if (work->next_smb2_rsp_hdr_off) |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 8341 | hdr = ksmbd_resp_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8342 | |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 8343 | req_hdr = ksmbd_req_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8344 | |
| 8345 | if (!work->next_smb2_rsp_hdr_off) { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 8346 | len = get_rfc1002_len(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8347 | if (req_hdr->NextCommand) |
| 8348 | len = ALIGN(len, 8); |
| 8349 | } else { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 8350 | len = get_rfc1002_len(work->response_buf) - |
| 8351 | work->next_smb2_rsp_hdr_off; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8352 | len = ALIGN(len, 8); |
| 8353 | } |
| 8354 | |
Namjae Jeon | 08bdbc6 | 2021-07-27 09:30:29 +0900 | [diff] [blame] | 8355 | if (conn->binding == false && |
| 8356 | le16_to_cpu(hdr->Command) == SMB2_SESSION_SETUP_HE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8357 | signing_key = work->sess->smb3signingkey; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8358 | } else { |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 8359 | chann = lookup_chann_list(work->sess, work->conn); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8360 | if (!chann) |
| 8361 | return; |
| 8362 | signing_key = chann->smb3signingkey; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8363 | } |
| 8364 | |
| 8365 | if (!signing_key) |
| 8366 | return; |
| 8367 | |
| 8368 | if (req_hdr->NextCommand) |
| 8369 | hdr->NextCommand = cpu_to_le32(len); |
| 8370 | |
| 8371 | hdr->Flags |= SMB2_FLAGS_SIGNED; |
| 8372 | memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE); |
| 8373 | iov[0].iov_base = (char *)&hdr->ProtocolId; |
| 8374 | iov[0].iov_len = len; |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8375 | if (work->aux_payload_sz) { |
| 8376 | iov[0].iov_len -= work->aux_payload_sz; |
| 8377 | iov[1].iov_base = work->aux_payload_buf; |
| 8378 | iov[1].iov_len = work->aux_payload_sz; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8379 | n_vec++; |
| 8380 | } |
| 8381 | |
| 8382 | if (!ksmbd_sign_smb3_pdu(conn, signing_key, iov, n_vec, signature)) |
| 8383 | memcpy(hdr->Signature, signature, SMB2_SIGNATURE_SIZE); |
| 8384 | } |
| 8385 | |
| 8386 | /** |
| 8387 | * smb3_preauth_hash_rsp() - handler for computing preauth hash on response |
| 8388 | * @work: smb work containing response buffer |
| 8389 | * |
| 8390 | */ |
| 8391 | void smb3_preauth_hash_rsp(struct ksmbd_work *work) |
| 8392 | { |
| 8393 | struct ksmbd_conn *conn = work->conn; |
| 8394 | struct ksmbd_session *sess = work->sess; |
| 8395 | struct smb2_hdr *req, *rsp; |
| 8396 | |
| 8397 | if (conn->dialect != SMB311_PROT_ID) |
| 8398 | return; |
| 8399 | |
| 8400 | WORK_BUFFERS(work, req, rsp); |
| 8401 | |
Namjae Jeon | 442ff9e | 2021-09-29 15:44:32 +0900 | [diff] [blame] | 8402 | if (le16_to_cpu(req->Command) == SMB2_NEGOTIATE_HE && |
| 8403 | conn->preauth_info) |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 8404 | ksmbd_gen_preauth_integrity_hash(conn, work->response_buf, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 8405 | conn->preauth_info->Preauth_HashValue); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8406 | |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 8407 | if (le16_to_cpu(rsp->Command) == SMB2_SESSION_SETUP_HE && sess) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8408 | __u8 *hash_value; |
| 8409 | |
Namjae Jeon | f5a544e | 2021-06-18 10:04:19 +0900 | [diff] [blame] | 8410 | if (conn->binding) { |
| 8411 | struct preauth_session *preauth_sess; |
| 8412 | |
| 8413 | preauth_sess = ksmbd_preauth_session_lookup(conn, sess->id); |
| 8414 | if (!preauth_sess) |
| 8415 | return; |
| 8416 | hash_value = preauth_sess->Preauth_HashValue; |
| 8417 | } else { |
| 8418 | hash_value = sess->Preauth_HashValue; |
| 8419 | if (!hash_value) |
| 8420 | return; |
| 8421 | } |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 8422 | ksmbd_gen_preauth_integrity_hash(conn, work->response_buf, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 8423 | hash_value); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8424 | } |
| 8425 | } |
| 8426 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 8427 | static void fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, char *old_buf, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 8428 | __le16 cipher_type) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8429 | { |
| 8430 | struct smb2_hdr *hdr = (struct smb2_hdr *)old_buf; |
| 8431 | unsigned int orig_len = get_rfc1002_len(old_buf); |
| 8432 | |
| 8433 | memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr)); |
| 8434 | tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM; |
| 8435 | tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len); |
| 8436 | tr_hdr->Flags = cpu_to_le16(0x01); |
Namjae Jeon | 5a0ca77 | 2021-05-06 11:43:37 +0900 | [diff] [blame] | 8437 | if (cipher_type == SMB2_ENCRYPTION_AES128_GCM || |
| 8438 | cipher_type == SMB2_ENCRYPTION_AES256_GCM) |
| 8439 | get_random_bytes(&tr_hdr->Nonce, SMB3_AES_GCM_NONCE); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8440 | else |
Namjae Jeon | 5a0ca77 | 2021-05-06 11:43:37 +0900 | [diff] [blame] | 8441 | get_random_bytes(&tr_hdr->Nonce, SMB3_AES_CCM_NONCE); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8442 | memcpy(&tr_hdr->SessionId, &hdr->SessionId, 8); |
| 8443 | inc_rfc1001_len(tr_hdr, sizeof(struct smb2_transform_hdr) - 4); |
| 8444 | inc_rfc1001_len(tr_hdr, orig_len); |
| 8445 | } |
| 8446 | |
| 8447 | int smb3_encrypt_resp(struct ksmbd_work *work) |
| 8448 | { |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8449 | char *buf = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8450 | struct smb2_transform_hdr *tr_hdr; |
| 8451 | struct kvec iov[3]; |
| 8452 | int rc = -ENOMEM; |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8453 | 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] | 8454 | |
| 8455 | if (ARRAY_SIZE(iov) < rq_nvec) |
| 8456 | return -ENOMEM; |
| 8457 | |
Namjae Jeon | 20ea7fd | 2021-03-30 12:40:47 +0900 | [diff] [blame] | 8458 | tr_hdr = kzalloc(sizeof(struct smb2_transform_hdr), GFP_KERNEL); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8459 | if (!tr_hdr) |
| 8460 | return rc; |
| 8461 | |
| 8462 | /* fill transform header */ |
| 8463 | fill_transform_hdr(tr_hdr, buf, work->conn->cipher_type); |
| 8464 | |
| 8465 | iov[0].iov_base = tr_hdr; |
| 8466 | iov[0].iov_len = sizeof(struct smb2_transform_hdr); |
| 8467 | buf_size += iov[0].iov_len - 4; |
| 8468 | |
| 8469 | iov[1].iov_base = buf + 4; |
| 8470 | iov[1].iov_len = get_rfc1002_len(buf); |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8471 | if (work->aux_payload_sz) { |
| 8472 | iov[1].iov_len = work->resp_hdr_sz - 4; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8473 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8474 | iov[2].iov_base = work->aux_payload_buf; |
| 8475 | iov[2].iov_len = work->aux_payload_sz; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8476 | buf_size += iov[2].iov_len; |
| 8477 | } |
| 8478 | buf_size += iov[1].iov_len; |
| 8479 | work->resp_hdr_sz = iov[1].iov_len; |
| 8480 | |
| 8481 | rc = ksmbd_crypt_message(work->conn, iov, rq_nvec, 1); |
| 8482 | if (rc) |
| 8483 | return rc; |
| 8484 | |
| 8485 | memmove(buf, iov[1].iov_base, iov[1].iov_len); |
| 8486 | tr_hdr->smb2_buf_length = cpu_to_be32(buf_size); |
| 8487 | work->tr_buf = tr_hdr; |
| 8488 | |
| 8489 | return rc; |
| 8490 | } |
| 8491 | |
Namjae Jeon | f4228b6 | 2021-08-12 10:16:40 +0900 | [diff] [blame] | 8492 | bool smb3_is_transform_hdr(void *buf) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8493 | { |
| 8494 | struct smb2_transform_hdr *trhdr = buf; |
| 8495 | |
| 8496 | return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM; |
| 8497 | } |
| 8498 | |
| 8499 | int smb3_decrypt_req(struct ksmbd_work *work) |
| 8500 | { |
| 8501 | struct ksmbd_conn *conn = work->conn; |
| 8502 | struct ksmbd_session *sess; |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 8503 | char *buf = work->request_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8504 | unsigned int pdu_length = get_rfc1002_len(buf); |
| 8505 | struct kvec iov[2]; |
Namjae Jeon | c7705ee | 2021-10-04 20:44:52 +0900 | [diff] [blame] | 8506 | int buf_data_size = pdu_length + 4 - |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8507 | sizeof(struct smb2_transform_hdr); |
| 8508 | struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8509 | int rc = 0; |
| 8510 | |
Namjae Jeon | c7705ee | 2021-10-04 20:44:52 +0900 | [diff] [blame] | 8511 | if (buf_data_size < sizeof(struct smb2_hdr)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 8512 | pr_err("Transform message is too small (%u)\n", |
| 8513 | pdu_length); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8514 | return -ECONNABORTED; |
| 8515 | } |
| 8516 | |
Namjae Jeon | c7705ee | 2021-10-04 20:44:52 +0900 | [diff] [blame] | 8517 | if (buf_data_size < le32_to_cpu(tr_hdr->OriginalMessageSize)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 8518 | pr_err("Transform message is broken\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8519 | return -ECONNABORTED; |
| 8520 | } |
| 8521 | |
Namjae Jeon | 4227f81 | 2021-09-29 19:52:51 +0900 | [diff] [blame] | 8522 | sess = ksmbd_session_lookup_all(conn, le64_to_cpu(tr_hdr->SessionId)); |
| 8523 | if (!sess) { |
| 8524 | pr_err("invalid session id(%llx) in transform header\n", |
| 8525 | le64_to_cpu(tr_hdr->SessionId)); |
| 8526 | return -ECONNABORTED; |
| 8527 | } |
| 8528 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8529 | iov[0].iov_base = buf; |
| 8530 | iov[0].iov_len = sizeof(struct smb2_transform_hdr); |
| 8531 | iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr); |
| 8532 | iov[1].iov_len = buf_data_size; |
| 8533 | rc = ksmbd_crypt_message(conn, iov, 2, 0); |
| 8534 | if (rc) |
| 8535 | return rc; |
| 8536 | |
| 8537 | memmove(buf + 4, iov[1].iov_base, buf_data_size); |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 8538 | *(__be32 *)buf = cpu_to_be32(buf_data_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8539 | |
| 8540 | return rc; |
| 8541 | } |
| 8542 | |
| 8543 | bool smb3_11_final_sess_setup_resp(struct ksmbd_work *work) |
| 8544 | { |
| 8545 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame^] | 8546 | struct smb2_hdr *rsp = smb2_get_msg(work->response_buf); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8547 | |
| 8548 | if (conn->dialect < SMB30_PROT_ID) |
| 8549 | return false; |
| 8550 | |
| 8551 | if (work->next_smb2_rcv_hdr_off) |
Namjae Jeon | 8a89331 | 2021-06-25 13:43:37 +0900 | [diff] [blame] | 8552 | rsp = ksmbd_resp_buf_next(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8553 | |
| 8554 | if (le16_to_cpu(rsp->Command) == SMB2_SESSION_SETUP_HE && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 8555 | rsp->Status == STATUS_SUCCESS) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8556 | return true; |
| 8557 | return false; |
| 8558 | } |