Skip to content

Commit

Permalink
meson: Plumb hardening with Rust
Browse files Browse the repository at this point in the history
Signed-off-by: Daiki Ueno <[email protected]>
  • Loading branch information
ueno committed Mar 30, 2021
1 parent 09ffadb commit cc990d7
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 0 deletions.
13 changes: 13 additions & 0 deletions 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
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')

0 comments on commit cc990d7

Please sign in to comment.