Skip to content

forbid unsafe #357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[![Safety Dance](https://img.shields.io/badge/unsafe-forbidden-success.svg)](https://github.com/rust-secure-code/safety-dance/)

Rust-Lightning, not Rusty's Lightning!
=====

Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
//! instead of having a rather-separate lightning appendage to a wallet.

#![cfg_attr(not(feature = "fuzztarget"), deny(missing_docs))]
#![forbid(unsafe_code)]

extern crate bitcoin;
extern crate bitcoin_hashes;
Expand Down
3 changes: 2 additions & 1 deletion src/ln/msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,7 @@ pub trait RoutingMessageHandler : Send + Sync {
fn get_next_node_announcements(&self, starting_point: Option<&PublicKey>, batch_amount: u8) -> Vec<NodeAnnouncement>;
}

#[derive(Default, Clone)]
pub(crate) struct OnionRealm0HopData {
pub(crate) short_channel_id: u64,
pub(crate) amt_to_forward: u64,
Expand All @@ -708,12 +709,12 @@ mod fuzzy_internal_msgs {
// them from untrusted input):

use super::OnionRealm0HopData;
#[derive(Default, Clone)]
pub struct OnionHopData {
pub(crate) realm: u8,
pub(crate) data: OnionRealm0HopData,
pub(crate) hmac: [u8; 32],
}
unsafe impl ::util::internal_traits::NoDealloc for OnionHopData{}

pub struct DecodedOnionErrorPacket {
pub(crate) hmac: [u8; 32],
Expand Down
11 changes: 4 additions & 7 deletions src/ln/onion_utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ln::channelmanager::{PaymentHash, HTLCSource};
use ln::msgs;
use ln::router::{Route,RouteHop};
use util::{byte_utils, internal_traits};
use util::byte_utils;
use util::chacha20::ChaCha20;
use util::errors::{self, APIError};
use util::ser::{Readable, Writeable};
Expand All @@ -17,7 +17,6 @@ use secp256k1::Secp256k1;
use secp256k1::ecdh::SharedSecret;
use secp256k1;

use std::ptr;
use std::io::Cursor;
use std::sync::Arc;

Expand Down Expand Up @@ -113,9 +112,7 @@ pub(super) fn build_onion_payloads(route: &Route, starting_htlc_offset: u32) ->
let mut cur_value_msat = 0u64;
let mut cur_cltv = starting_htlc_offset;
let mut last_short_channel_id = 0;
let mut res: Vec<msgs::OnionHopData> = Vec::with_capacity(route.hops.len());
internal_traits::test_no_dealloc::<msgs::OnionHopData>(None);
unsafe { res.set_len(route.hops.len()); }
let mut res: Vec<msgs::OnionHopData> = vec![msgs::OnionHopData::default(); route.hops.len()];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would prefer to just insert(0, ...) at each loop iteration to make things even cleaner.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do not know how to insert if not initialized. vec capacity != len

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if vec length is not 20*65 shift will fail

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I missed this, point was we don't shift on this vec, only on a later version of it, this one is always route.hops.len() in len at the end.


for (idx, hop) in route.hops.iter().enumerate().rev() {
// First hop gets special values so that it can check, on receipt, that everything is
Expand Down Expand Up @@ -147,8 +144,8 @@ pub(super) fn build_onion_payloads(route: &Route, starting_htlc_offset: u32) ->

#[inline]
fn shift_arr_right(arr: &mut [u8; 20*65]) {
unsafe {
ptr::copy(arr[0..].as_ptr(), arr[65..].as_mut_ptr(), 19*65);
for i in (65..20*65).rev() {
arr[i] = arr[i-65];
}
for i in 0..65 {
arr[i] = 0;
Expand Down
7 changes: 0 additions & 7 deletions src/util/internal_traits.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ pub(crate) mod chacha20;
#[cfg(not(feature = "fuzztarget"))]
pub(crate) mod poly1305;
pub(crate) mod chacha20poly1305rfc;
pub(crate) mod internal_traits;
pub(crate) mod transaction_utils;

#[macro_use]
Expand Down