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 607f595
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 3 deletions.
22 changes: 20 additions & 2 deletions common/meson.build
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
install_headers('pkcs11.h', 'pkcs11x.h', subdir: 'p11-kit-1/p11-kit')

libp11_common_sources = [
libp11_common_c_sources = [
'argv.c',
'attrs.c',
'array.c',
Expand All @@ -18,7 +18,25 @@ libp11_common_sources = [
'vsock.c'
]

libp11_common = static_library('p11-common', libp11_common_sources,
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_c = static_library('p11-common-c', libp11_common_c_sources,
include_directories: configinc)
libp11_common_libs += libp11_common_c

libp11_common = static_library('p11-common', [],
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 !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: 4 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
project('p11-kit', 'c',
project('p11-kit', 'c', 'rust',
version: '0.23.21',
meson_version: '>= 0.49')

Expand Down Expand Up @@ -63,6 +63,9 @@ if get_option('nls') and cc.has_header('libintl.h')
conf.set('ENABLE_NLS', 1)
endif

conf.set10('ENABLE_RUSTC', get_option('rustc'))
rustc = meson.get_compiler('rust')

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 : true,
description : 'Enable hardening using Rust')

0 comments on commit 607f595

Please sign in to comment.