Skip to content

Commit dc572ef

Browse files
refactor: enable safer unsafe clippy lints (#551)
> Checks for the result of a `&self`-taking `as_ptr` being cast to a mutable pointer. https://rust-lang.github.io/rust-clippy/stable/index.html#as_ptr_cast_mut > Checks for casts of a function pointer to any integer type. https://rust-lang.github.io/rust-clippy/stable/index.html#fn_to_numeric_cast_any > Checks for `as` casts between raw pointers that change their constness, namely `*const T` to `*mut T` and `*mut T` to `*const T`. https://rust-lang.github.io/rust-clippy/stable/index.html#ptr_cast_constness > Checks for `// SAFETY:` comments on safe code. https://rust-lang.github.io/rust-clippy/stable/index.html#unnecessary_safety_comment > Checks for the doc comments of publicly visible unsafe functions and warns if there is no `# Safety` section. https://rust-lang.github.io/rust-clippy/stable/index.html#missing_safety_doc
1 parent 156621d commit dc572ef

File tree

12 files changed

+427
-292
lines changed

12 files changed

+427
-292
lines changed

Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ redundant_lifetimes = "warn"
4646
trivial_numeric_casts = "warn"
4747
unused_qualifications = "warn"
4848

49+
[workspace.lints.clippy]
50+
51+
# == Safer unsafe == #
52+
as_ptr_cast_mut = "warn"
53+
fn_to_numeric_cast_any = "warn"
54+
ptr_cast_constness = "warn"
55+
unnecessary_safety_comment = "warn"
56+
missing_safety_doc = "warn"
57+
4958
[workspace.dependencies]
5059
uuid = { version = "1.18", default-features = false }
5160
tracing = { version = "0.1", default-features = false }

ffi/src/sspi/common.rs

Lines changed: 89 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,16 @@ pub unsafe extern "system" fn AcceptSecurityContext(
105105
None => return ErrorKind::InvalidHandle.to_u32().unwrap(),
106106
};
107107

108-
// SAFETY:
109-
// - `ph_context` is either null or convertible to a reference.
110-
// - The values behind `ph_context.dw_lower` and `ph_context.dw_upper` pointers are allocated by an SSPI function.
111-
let mut sspi_context_ptr = try_execute!(unsafe { p_ctxt_handle_to_sspi_context(
112-
&mut ph_context,
113-
Some(security_package_name),
114-
attributes,
115-
)});
108+
let mut sspi_context_ptr = try_execute!(
109+
// SAFETY:
110+
// - `ph_context` is either null or convertible to a reference.
111+
// - The values behind `ph_context.dw_lower` and `ph_context.dw_upper` pointers are allocated by an SSPI function.
112+
unsafe { p_ctxt_handle_to_sspi_context(
113+
&mut ph_context,
114+
Some(security_package_name),
115+
attributes,
116+
)}
117+
);
116118

117119
// SAFETY: `sspi_context_ptr` is a valid, local pointer to the `SspiHandle` allocated by the `p_ctx_handle_to_sspi_context`.
118120
let sspi_context = unsafe { sspi_context_ptr.as_mut() };
@@ -158,12 +160,14 @@ pub unsafe extern "system" fn AcceptSecurityContext(
158160
// - `p_output` is guaranteed to be non-null due to the prior check.
159161
// - `p_output` points to a valid `SecBufferDesc` structure.
160162
let p_buffers = unsafe { (*p_output).p_buffers };
161-
// SAFETY:
162-
// - `p_buffers` is a valid pointer to an array of security buffers.
163-
// - The memory region `p_buffers` points to is valid for writes of `from_buffers.len()` elements.
164-
// - For each element in the `p_buffers` array, the `pv_buffer` pointer points to a valid memory,
165-
// that is valid for writes of `cb_buffer` bytes.
166-
try_execute!(unsafe { copy_to_c_sec_buffer(p_buffers, &output_tokens, false) });
163+
try_execute!(
164+
// SAFETY:
165+
// - `p_buffers` is a valid pointer to an array of security buffers.
166+
// - The memory region `p_buffers` points to is valid for writes of `from_buffers.len()` elements.
167+
// - For each element in the `p_buffers` array, the `pv_buffer` pointer points to a valid memory,
168+
// that is valid for writes of `cb_buffer` bytes.
169+
unsafe { copy_to_c_sec_buffer(p_buffers, &output_tokens, false) }
170+
);
167171

168172
// SAFETY: `ph_new_context` is convertible to a reference.
169173
let ph_new_context = unsafe { ph_new_context.as_mut() }.expect("ph_new_context is non-null");
@@ -219,14 +223,16 @@ pub unsafe extern "system" fn CompleteAuthToken(
219223
let p_buffers = unsafe { (*p_token).p_buffers };
220224
check_null!(p_buffers);
221225

222-
// SAFETY:
223-
// - `ph_context` is either null or convertible to a reference.
224-
// - The values behind `ph_context.dw_lower` and `ph_context.dw_upper` pointers are allocated by an SSPI function.
225-
let mut sspi_context_ptr = try_execute!(unsafe { p_ctxt_handle_to_sspi_context(
226-
&mut ph_context,
227-
None,
228-
&CredentialsAttributes::default()
229-
)});
226+
let mut sspi_context_ptr = try_execute!(
227+
// SAFETY:
228+
// - `ph_context` is either null or convertible to a reference.
229+
// - The values behind `ph_context.dw_lower` and `ph_context.dw_upper` pointers are allocated by an SSPI function.
230+
unsafe { p_ctxt_handle_to_sspi_context(
231+
&mut ph_context,
232+
None,
233+
&CredentialsAttributes::default()
234+
)}
235+
);
230236

231237
// SAFETY: `sspi_context_ptr` is a valid, local pointer to the `SspiHandle` allocated by the `p_ctx_handle_to_sspi_context`.
232238
let sspi_context = unsafe { sspi_context_ptr.as_mut() };
@@ -273,14 +279,16 @@ pub unsafe extern "system" fn DeleteSecurityContext(mut ph_context: PCtxtHandle)
273279
catch_panic!(
274280
check_null!(ph_context);
275281

276-
// SAFETY:
277-
// - `ph_context` is convertible to a reference.
278-
// - The values behind `ph_context.dw_lower` and `ph_context.dw_upper` pointers are allocated by an SSPI function.
279-
let mut sspi_context_ptr = try_execute!(unsafe { p_ctxt_handle_to_sspi_context(
280-
&mut ph_context,
281-
None,
282-
&CredentialsAttributes::default()
283-
)});
282+
let mut sspi_context_ptr = try_execute!(
283+
// SAFETY:
284+
// - `ph_context` is convertible to a reference.
285+
// - The values behind `ph_context.dw_lower` and `ph_context.dw_upper` pointers are allocated by an SSPI function.
286+
unsafe { p_ctxt_handle_to_sspi_context(
287+
&mut ph_context,
288+
None,
289+
&CredentialsAttributes::default()
290+
)}
291+
);
284292

285293
// SAFETY: `sspi_context_ptr` is a valid, local pointer to the `SspiHandle` allocated by the `p_ctx_handle_to_sspi_context`.
286294
let _context: Box<SspiHandle> = unsafe {
@@ -436,14 +444,16 @@ pub unsafe extern "system" fn EncryptMessage(
436444
let p_buffers = unsafe { (*p_message).p_buffers };
437445
check_null!(p_buffers);
438446

439-
// SAFETY:
440-
// - `ph_context` is convertible to a reference.
441-
// - The values behind `ph_context.dw_lower` and `ph_context.dw_upper` pointers are allocated by an SSPI function.
442-
let mut sspi_context_ptr = try_execute!(unsafe { p_ctxt_handle_to_sspi_context(
443-
&mut ph_context,
444-
None,
445-
&CredentialsAttributes::default()
446-
)});
447+
let mut sspi_context_ptr = try_execute!(
448+
// SAFETY:
449+
// - `ph_context` is convertible to a reference.
450+
// - The values behind `ph_context.dw_lower` and `ph_context.dw_upper` pointers are allocated by an SSPI function.
451+
unsafe { p_ctxt_handle_to_sspi_context(
452+
&mut ph_context,
453+
None,
454+
&CredentialsAttributes::default()
455+
)}
456+
);
447457

448458
// SAFETY: `sspi_context_ptr` is a valid, local pointer to the `SspiHandle` allocated by the `p_ctx_handle_to_sspi_context`.
449459
let sspi_context = unsafe { sspi_context_ptr.as_mut() };
@@ -460,24 +470,28 @@ pub unsafe extern "system" fn EncryptMessage(
460470
from_raw_parts(p_buffers, len)
461471
};
462472

463-
// SAFETY:
464-
// - `raw_buffers` array contains valid `SecBuffer` structures.
465-
// - Each `SecBuffer` have a valid `pv_buffer` pointer that is valid for reads of `cb_buffer` bytes.
466-
let mut message = try_execute!(unsafe { p_sec_buffers_to_decrypt_buffers(raw_buffers)});
473+
let mut message = try_execute!(
474+
// SAFETY:
475+
// - `raw_buffers` array contains valid `SecBuffer` structures.
476+
// - Each `SecBuffer` have a valid `pv_buffer` pointer that is valid for reads of `cb_buffer` bytes.
477+
unsafe { p_sec_buffers_to_decrypt_buffers(raw_buffers) }
478+
);
467479

468480
let result_status = sspi_context.encrypt_message(
469481
EncryptionFlags::from_bits(f_qop.try_into().unwrap()).unwrap(),
470482
&mut message,
471483
message_seq_no.try_into().unwrap(),
472484
);
473485

474-
// SAFETY:
475-
// - `p_buffers` is a valid pointer to an array of security buffers.
476-
// - The memory region `p_buffers` points to is valid for writes of `from_buffers.len()` elements.
477-
// - For each element in the `p_buffers` array, the `pv_buffer` pointer points to a valid memory,
478-
// that is valid for writes of `cb_buffer` bytes.
479-
// - There is enough space in the buffers, since we are writing encrypted data back to the same buffers it was read.
480-
try_execute!(unsafe { copy_decrypted_buffers(p_buffers, message) });
486+
try_execute!(
487+
// SAFETY:
488+
// - `p_buffers` is a valid pointer to an array of security buffers.
489+
// - The memory region `p_buffers` points to is valid for writes of `from_buffers.len()` elements.
490+
// - For each element in the `p_buffers` array, the `pv_buffer` pointer points to a valid memory,
491+
// that is valid for writes of `cb_buffer` bytes.
492+
// - There is enough space in the buffers, since we are writing encrypted data back to the same buffers it was read.
493+
unsafe { copy_decrypted_buffers(p_buffers, message) }
494+
);
481495

482496
let result = try_execute!(result_status);
483497
result.to_u32().unwrap()
@@ -518,14 +532,16 @@ pub unsafe extern "system" fn DecryptMessage(
518532
let p_buffers = unsafe { (*p_message).p_buffers };
519533
check_null!(p_buffers);
520534

521-
// SAFETY:
522-
// - `ph_context` is convertible to a reference.
523-
// - The values behind `ph_context.dw_lower` and `ph_context.dw_upper` pointers are allocated by an SSPI function.
524-
let mut sspi_context_ptr = try_execute!(unsafe { p_ctxt_handle_to_sspi_context(
525-
&mut ph_context,
526-
None,
527-
&CredentialsAttributes::default()
528-
)});
535+
let mut sspi_context_ptr = try_execute!(
536+
// SAFETY:
537+
// - `ph_context` is convertible to a reference.
538+
// - The values behind `ph_context.dw_lower` and `ph_context.dw_upper` pointers are allocated by an SSPI function.
539+
unsafe { p_ctxt_handle_to_sspi_context(
540+
&mut ph_context,
541+
None,
542+
&CredentialsAttributes::default()
543+
)}
544+
);
529545

530546
// SAFETY: `sspi_context_ptr` is a valid, local pointer to the `SspiHandle` allocated by the `p_ctx_handle_to_sspi_context`.
531547
let sspi_context = unsafe { sspi_context_ptr.as_mut() };
@@ -540,24 +556,28 @@ pub unsafe extern "system" fn DecryptMessage(
540556
// - The memory region `p_buffers` points to is valid for reads of `len` elements.
541557
let raw_buffers = unsafe { from_raw_parts(p_buffers, len) };
542558

543-
// SAFETY:
544-
// - `raw_buffers` array contains valid `SecBuffer` structures.
545-
// - Each `SecBuffer` have a valid `pv_buffer` pointer that is valid for reads of `cb_buffer` bytes.
546-
let mut message = try_execute!(unsafe { p_sec_buffers_to_decrypt_buffers(raw_buffers) });
559+
let mut message = try_execute!(
560+
// SAFETY:
561+
// - `raw_buffers` array contains valid `SecBuffer` structures.
562+
// - Each `SecBuffer` have a valid `pv_buffer` pointer that is valid for reads of `cb_buffer` bytes.
563+
unsafe { p_sec_buffers_to_decrypt_buffers(raw_buffers) }
564+
);
547565

548566
let (decryption_flags, result_status) =
549567
match sspi_context.decrypt_message(&mut message, message_seq_no.try_into().unwrap()) {
550568
Ok(flags) => (flags, Ok(())),
551569
Err(error) => (DecryptionFlags::empty(), Err(error)),
552570
};
553571

554-
// SAFETY:
555-
// - `p_buffers` is a valid pointer to an array of security buffers.
556-
// - The memory region `p_buffers` points to is valid for writes of `from_buffers.len()` elements.
557-
// - For each element in the `p_buffers` array, the `pv_buffer` pointer points to a valid memory,
558-
// that is valid for writes of `cb_buffer` bytes.
559-
// - There is enough space in the buffers, since we are writing decrypted data back to the same buffers it was read.
560-
try_execute!(unsafe { copy_decrypted_buffers(p_buffers, message) });
572+
try_execute!(
573+
// SAFETY:
574+
// - `p_buffers` is a valid pointer to an array of security buffers.
575+
// - The memory region `p_buffers` points to is valid for writes of `from_buffers.len()` elements.
576+
// - For each element in the `p_buffers` array, the `pv_buffer` pointer points to a valid memory,
577+
// that is valid for writes of `cb_buffer` bytes.
578+
// - There is enough space in the buffers, since we are writing decrypted data back to the same buffers it was read.
579+
unsafe { copy_decrypted_buffers(p_buffers, message) }
580+
);
561581

562582
// `pf_qop` can be null if this library is used as a CredSsp security package
563583
if !pf_qop.is_null() {
@@ -612,7 +632,7 @@ unsafe fn p_sec_buffers_to_decrypt_buffers(raw_buffers: &[SecBuffer]) -> sspi::R
612632

613633
/// Copies Rust-security-buffers into C-security-buffers.
614634
///
615-
/// # Safety:
635+
/// # Safety
616636
///
617637
/// This function accepts owned [from_buffers] to avoid UB and other errors. Rust-buffers should
618638
/// not be used after the data is copied into C-buffers.

ffi/src/sspi/credentials_attributes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub struct SecPkgCredentialsKdcProxySettingsW {
6666

6767
/// Extracts [KdcProxySettings].
6868
///
69-
/// # Safety:
69+
/// # Safety
7070
///
7171
/// * The pointer value must be [SecPkgCredentialsKdcProxySettingsW].
7272
/// The fields `proxy_server_offset`, `proxy_server_length`, `client_tls_cred_offset` and `client_tls_cred_length` must contain valid values.

ffi/src/sspi/sec_buffer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub struct SecBufferDesc {
2323

2424
pub type PSecBufferDesc = *mut SecBufferDesc;
2525

26-
/// # Safety:
26+
/// # Safety
2727
///
2828
/// * The input pointer can be null.
2929
/// * If the input pointer is non-null, then it must point to the valid [SecBufferDesc] structure. Moreover,
@@ -77,7 +77,7 @@ pub(crate) unsafe fn p_sec_buffers_to_security_buffers(raw_buffers: &[SecBuffer]
7777

7878
/// Copies buffers from `from_buffers` to `to_buffers`.
7979
///
80-
/// # Safety:
80+
/// # Safety
8181
///
8282
/// The `to_buffers` must be a valid pointer to an array of security buffers. It must be valid for writes of `from_buffers.len()` elements.
8383
/// Additionally, if `allocate` is `false`, each `to_buffers[i].pv_buffer` must be a valid pointer for writes of `from_buffers[i].buffer.len()` elements.

0 commit comments

Comments
 (0)