|
33 | 33 | #include "cifsfs.h"
|
34 | 34 | #include "cifs_ioctl.h"
|
35 | 35 | #include "smb2proto.h"
|
| 36 | +#include "smb2glob.h" |
36 | 37 | #include <linux/btrfs.h>
|
37 | 38 |
|
38 | 39 | static long cifs_ioctl_query_info(unsigned int xid, struct file *filep,
|
@@ -214,48 +215,112 @@ static int cifs_shutdown(struct super_block *sb, unsigned long arg)
|
214 | 215 | return 0;
|
215 | 216 | }
|
216 | 217 |
|
217 |
| -static int cifs_dump_full_key(struct cifs_tcon *tcon, unsigned long arg) |
| 218 | +static int cifs_dump_full_key(struct cifs_tcon *tcon, struct smb3_full_key_debug_info __user *in) |
218 | 219 | {
|
219 |
| - struct smb3_full_key_debug_info pfull_key_inf; |
220 |
| - __u64 suid; |
221 |
| - struct list_head *tmp; |
| 220 | + struct smb3_full_key_debug_info out; |
222 | 221 | struct cifs_ses *ses;
|
| 222 | + int rc = 0; |
223 | 223 | bool found = false;
|
| 224 | + u8 __user *end; |
224 | 225 |
|
225 |
| - if (!smb3_encryption_required(tcon)) |
226 |
| - return -EOPNOTSUPP; |
| 226 | + if (!smb3_encryption_required(tcon)) { |
| 227 | + rc = -EOPNOTSUPP; |
| 228 | + goto out; |
| 229 | + } |
| 230 | + |
| 231 | + /* copy user input into our output buffer */ |
| 232 | + if (copy_from_user(&out, in, sizeof(out))) { |
| 233 | + rc = -EINVAL; |
| 234 | + goto out; |
| 235 | + } |
| 236 | + |
| 237 | + if (!out.session_id) { |
| 238 | + /* if ses id is 0, use current user session */ |
| 239 | + ses = tcon->ses; |
| 240 | + } else { |
| 241 | + /* otherwise if a session id is given, look for it in all our sessions */ |
| 242 | + struct cifs_ses *ses_it = NULL; |
| 243 | + struct TCP_Server_Info *server_it = NULL; |
227 | 244 |
|
228 |
| - ses = tcon->ses; /* default to user id for current user */ |
229 |
| - if (get_user(suid, (__u64 __user *)arg)) |
230 |
| - suid = 0; |
231 |
| - if (suid) { |
232 |
| - /* search to see if there is a session with a matching SMB UID */ |
233 | 245 | spin_lock(&cifs_tcp_ses_lock);
|
234 |
| - list_for_each(tmp, &tcon->ses->server->smb_ses_list) { |
235 |
| - ses = list_entry(tmp, struct cifs_ses, smb_ses_list); |
236 |
| - if (ses->Suid == suid) { |
237 |
| - found = true; |
238 |
| - break; |
| 246 | + list_for_each_entry(server_it, &cifs_tcp_ses_list, tcp_ses_list) { |
| 247 | + list_for_each_entry(ses_it, &server_it->smb_ses_list, smb_ses_list) { |
| 248 | + if (ses_it->Suid == out.session_id) { |
| 249 | + ses = ses_it; |
| 250 | + /* |
| 251 | + * since we are using the session outside the crit |
| 252 | + * section, we need to make sure it won't be released |
| 253 | + * so increment its refcount |
| 254 | + */ |
| 255 | + ses->ses_count++; |
| 256 | + found = true; |
| 257 | + goto search_end; |
| 258 | + } |
239 | 259 | }
|
240 | 260 | }
|
| 261 | +search_end: |
241 | 262 | spin_unlock(&cifs_tcp_ses_lock);
|
242 |
| - if (found == false) |
243 |
| - return -EINVAL; |
244 |
| - } /* else uses default user's SMB UID (ie current user) */ |
245 |
| - |
246 |
| - pfull_key_inf.cipher_type = le16_to_cpu(ses->server->cipher_type); |
247 |
| - pfull_key_inf.Suid = ses->Suid; |
248 |
| - memcpy(pfull_key_inf.auth_key, ses->auth_key.response, |
249 |
| - 16 /* SMB2_NTLMV2_SESSKEY_SIZE */); |
250 |
| - memcpy(pfull_key_inf.smb3decryptionkey, ses->smb3decryptionkey, |
251 |
| - 32 /* SMB3_ENC_DEC_KEY_SIZE */); |
252 |
| - memcpy(pfull_key_inf.smb3encryptionkey, |
253 |
| - ses->smb3encryptionkey, 32 /* SMB3_ENC_DEC_KEY_SIZE */); |
254 |
| - if (copy_to_user((void __user *)arg, &pfull_key_inf, |
255 |
| - sizeof(struct smb3_full_key_debug_info))) |
256 |
| - return -EFAULT; |
| 263 | + if (!found) { |
| 264 | + rc = -ENOENT; |
| 265 | + goto out; |
| 266 | + } |
| 267 | + } |
257 | 268 |
|
258 |
| - return 0; |
| 269 | + switch (ses->server->cipher_type) { |
| 270 | + case SMB2_ENCRYPTION_AES128_CCM: |
| 271 | + case SMB2_ENCRYPTION_AES128_GCM: |
| 272 | + out.session_key_length = CIFS_SESS_KEY_SIZE; |
| 273 | + out.server_in_key_length = out.server_out_key_length = SMB3_GCM128_CRYPTKEY_SIZE; |
| 274 | + break; |
| 275 | + case SMB2_ENCRYPTION_AES256_CCM: |
| 276 | + case SMB2_ENCRYPTION_AES256_GCM: |
| 277 | + out.session_key_length = CIFS_SESS_KEY_SIZE; |
| 278 | + out.server_in_key_length = out.server_out_key_length = SMB3_GCM256_CRYPTKEY_SIZE; |
| 279 | + break; |
| 280 | + default: |
| 281 | + rc = -EOPNOTSUPP; |
| 282 | + goto out; |
| 283 | + } |
| 284 | + |
| 285 | + /* check if user buffer is big enough to store all the keys */ |
| 286 | + if (out.in_size < sizeof(out) + out.session_key_length + out.server_in_key_length |
| 287 | + + out.server_out_key_length) { |
| 288 | + rc = -ENOBUFS; |
| 289 | + goto out; |
| 290 | + } |
| 291 | + |
| 292 | + out.session_id = ses->Suid; |
| 293 | + out.cipher_type = le16_to_cpu(ses->server->cipher_type); |
| 294 | + |
| 295 | + /* overwrite user input with our output */ |
| 296 | + if (copy_to_user(in, &out, sizeof(out))) { |
| 297 | + rc = -EINVAL; |
| 298 | + goto out; |
| 299 | + } |
| 300 | + |
| 301 | + /* append all the keys at the end of the user buffer */ |
| 302 | + end = in->data; |
| 303 | + if (copy_to_user(end, ses->auth_key.response, out.session_key_length)) { |
| 304 | + rc = -EINVAL; |
| 305 | + goto out; |
| 306 | + } |
| 307 | + end += out.session_key_length; |
| 308 | + |
| 309 | + if (copy_to_user(end, ses->smb3encryptionkey, out.server_in_key_length)) { |
| 310 | + rc = -EINVAL; |
| 311 | + goto out; |
| 312 | + } |
| 313 | + end += out.server_in_key_length; |
| 314 | + |
| 315 | + if (copy_to_user(end, ses->smb3decryptionkey, out.server_out_key_length)) { |
| 316 | + rc = -EINVAL; |
| 317 | + goto out; |
| 318 | + } |
| 319 | + |
| 320 | +out: |
| 321 | + if (found) |
| 322 | + cifs_put_smb_ses(ses); |
| 323 | + return rc; |
259 | 324 | }
|
260 | 325 |
|
261 | 326 | long cifs_ioctl(struct file *filep, unsigned int command, unsigned long arg)
|
@@ -371,6 +436,10 @@ long cifs_ioctl(struct file *filep, unsigned int command, unsigned long arg)
|
371 | 436 | rc = -EOPNOTSUPP;
|
372 | 437 | break;
|
373 | 438 | case CIFS_DUMP_KEY:
|
| 439 | + /* |
| 440 | + * Dump encryption keys. This is an old ioctl that only |
| 441 | + * handles AES-128-{CCM,GCM}. |
| 442 | + */ |
374 | 443 | if (pSMBFile == NULL)
|
375 | 444 | break;
|
376 | 445 | if (!capable(CAP_SYS_ADMIN)) {
|
@@ -398,20 +467,18 @@ long cifs_ioctl(struct file *filep, unsigned int command, unsigned long arg)
|
398 | 467 | else
|
399 | 468 | rc = 0;
|
400 | 469 | break;
|
401 |
| - /* |
402 |
| - * Dump full key (32 bytes instead of 16 bytes) is |
403 |
| - * needed if GCM256 (stronger encryption) negotiated |
404 |
| - */ |
405 | 470 | case CIFS_DUMP_FULL_KEY:
|
| 471 | + /* |
| 472 | + * Dump encryption keys (handles any key sizes) |
| 473 | + */ |
406 | 474 | if (pSMBFile == NULL)
|
407 | 475 | break;
|
408 | 476 | if (!capable(CAP_SYS_ADMIN)) {
|
409 | 477 | rc = -EACCES;
|
410 | 478 | break;
|
411 | 479 | }
|
412 | 480 | tcon = tlink_tcon(pSMBFile->tlink);
|
413 |
| - rc = cifs_dump_full_key(tcon, arg); |
414 |
| - |
| 481 | + rc = cifs_dump_full_key(tcon, (void __user *)arg); |
415 | 482 | break;
|
416 | 483 | case CIFS_IOC_NOTIFY:
|
417 | 484 | if (!S_ISDIR(inode->i_mode)) {
|
|
0 commit comments