Skip to content
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

meson: Plumb hardening with Rust #363

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
15 changes: 14 additions & 1 deletion common/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,20 @@ libp11_common_sources = [
'vsock.c'
]

libp11_common_libs = []

if get_option('rustc')
libp11_common_rust_sources = [
'path.rs'
]
libp11_common_rust = static_library('p11_common_rust',
libp11_common_rust_sources,
rust_crate_type: 'staticlib')
libp11_common_libs += libp11_common_rust
endif

libp11_common = static_library('p11-common', libp11_common_sources,
link_with: libp11_common_libs,
gnu_symbol_visibility: 'hidden',
include_directories: configinc)

Expand Down Expand Up @@ -82,7 +95,7 @@ if get_option('test')
t = executable(name, '@[email protected]'.format(name),
c_args: tests_c_args,
include_directories: configinc,
dependencies: dlopen_deps,
dependencies: dlopen_deps + thread_deps,
link_with: [libp11_test, libp11_common])
test(name, t)
endforeach
Expand Down
2 changes: 2 additions & 0 deletions common/path.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
#endif


#if !defined(ENABLE_RUSTC) || !ENABLE_RUSTC
char *
p11_path_base (const char *path)
{
Expand Down Expand Up @@ -92,6 +93,7 @@ p11_path_base (const char *path)

return strndup (beg, end - beg);
}
#endif

static inline bool
is_path_separator (char ch)
Expand Down
49 changes: 49 additions & 0 deletions common/path.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// Copyright (c) 2021 Red Hat Inc.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// * Redistributions of source code must retain the above
// copyright notice, this list of conditions and the
// following disclaimer.
// * Redistributions in binary form must reproduce the
// above copyright notice, this list of conditions and
// the following disclaimer in the documentation and/or
// other materials provided with the distribution.
// * The names of contributors to this software may not be
// used to endorse or promote products derived from this
// software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
// OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
// AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
// DAMAGE.
//

use std::ffi::{CStr, CString, OsStr};
use std::os::raw::c_char;
use std::path::PathBuf;
use std::os::unix::ffi::OsStrExt;

#[no_mangle]
pub extern fn p11_path_base(name: *const c_char) -> *mut c_char {
let slice = unsafe { CStr::from_ptr(name) };
let path = PathBuf::from(OsStr::from_bytes(slice.to_bytes()));
let bytes = path
.file_name()
.and_then(|base| Some(base.as_bytes()))
.unwrap_or("".as_bytes());
CString::new(bytes)
.and_then(|c_string| Ok(c_string.into_raw()))
.unwrap_or(std::ptr::null_mut())
}
5 changes: 5 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ if get_option('nls') and cc.has_header('libintl.h')
conf.set('ENABLE_NLS', 1)
endif

conf.set10('ENABLE_RUSTC', get_option('rustc'))
if get_option('rustc')
add_languages('rust')
endif

prefix = get_option('prefix')
datadir = get_option('datadir')
bindir = get_option('bindir')
Expand Down
4 changes: 4 additions & 0 deletions meson_options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,7 @@ option('nls', type : 'boolean',
option('test', type : 'boolean',
value : true,
description : 'Enable building test programs')

option('rustc', type : 'boolean',
value : false,
description : 'Enable hardening using Rust')
2 changes: 1 addition & 1 deletion trust/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ executable('trust',
link_with: libtrust_data,
dependencies: [asn_h_dep,
libp11_kit_dep,
libp11_tool_dep] + libffi_deps + dlopen_deps + libtasn1_deps,
libp11_tool_dep] + libffi_deps + dlopen_deps + libtasn1_deps + thread_deps,
install: true)

install_data('trust-extract-compat',
Expand Down