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

Fix double-free bug in optee-utee #127

Merged
merged 3 commits into from
May 10, 2024
Merged
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
36 changes: 36 additions & 0 deletions examples/error_handling-rs/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

TARGET ?= aarch64-unknown-linux-gnu
CROSS_COMPILE ?= aarch64-linux-gnu-

# If _HOST or _TA specific compiler/target are not specified, then use common
# compiler/target for both
CROSS_COMPILE_HOST ?= $(CROSS_COMPILE)
CROSS_COMPILE_TA ?= $(CROSS_COMPILE)
TARGET_HOST ?= $(TARGET)
TARGET_TA ?= $(TARGET)

all:
$(q)make -C host TARGET=$(TARGET_HOST) \
CROSS_COMPILE=$(CROSS_COMPILE_HOST)
$(q)make -C ta TARGET=$(TARGET_TA) \
CROSS_COMPILE=$(CROSS_COMPILE_TA)

clean:
$(q)make -C host clean
$(q)make -C ta clean
33 changes: 33 additions & 0 deletions examples/error_handling-rs/host/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

[package]
name = "error_handling-rs"
version = "0.1.0"
authors = ["Teaclave Contributors <dev@teaclave.apache.org>"]
license = "Apache-2.0"
repository = "https://github.com/apache/incubator-teaclave-trustzone-sdk.git"
description = "A test of Teaclave SDK's error handling capabilities"
edition = "2018"

[dependencies]
libc = "0.2.48"
proto = { path = "../proto" }
optee-teec = { path = "../../../optee-teec" }

[profile.release]
lto = true
37 changes: 37 additions & 0 deletions examples/error_handling-rs/host/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

NAME := error_handling-rs

TARGET ?= aarch64-unknown-linux-gnu
CROSS_COMPILE ?= aarch64-linux-gnu-
OBJCOPY := $(CROSS_COMPILE)objcopy
LINKER_CFG := target.$(TARGET).linker=\"$(CROSS_COMPILE)gcc\"

OUT_DIR := $(CURDIR)/target/$(TARGET)/release


all: host strip

host:
@cargo build --target $(TARGET) --release --config $(LINKER_CFG)

strip: host
@$(OBJCOPY) --strip-unneeded $(OUT_DIR)/$(NAME) $(OUT_DIR)/$(NAME)

clean:
@cargo clean
43 changes: 43 additions & 0 deletions examples/error_handling-rs/host/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

use optee_teec::{Context, ErrorKind, Operation, ParamType, Session, Uuid};
use optee_teec::ParamNone;
use proto::{UUID, Command};

fn main() -> optee_teec::Result<()> {
test_error_handling();
Ok(())
}

fn test_error_handling() {
let mut ctx = Context::new().unwrap();
let uuid = Uuid::parse_str(UUID).unwrap();
let mut session = ctx.open_session(uuid).unwrap();
let mut operation = Operation::new(0, ParamNone, ParamNone, ParamNone, ParamNone);

// Test successful invocation return Ok().
session.invoke_command(Command::ReturnSuccess as u32, &mut operation).expect("success");

// Test error invocation returns the requested error.
let e = session.invoke_command(Command::ReturnGenericError as u32, &mut operation).expect_err("generic error");
assert_eq!(e.kind(), ErrorKind::Generic);

// Test repeated error invocation also returns the requested error.
let e = session.invoke_command(Command::ReturnGenericError as u32, &mut operation).expect_err("generic error");
assert_eq!(e.kind(), ErrorKind::Generic);
}
30 changes: 30 additions & 0 deletions examples/error_handling-rs/proto/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

[package]
name = "proto"
version = "0.2.0"
authors = ["Teaclave Contributors <dev@teaclave.apache.org>"]
license = "Apache-2.0"
repository = "https://github.com/apache/incubator-teaclave-trustzone-sdk.git"
description = "Data structures and functions shared by host and TA."
edition = "2018"

[dependencies]

[build_dependencies]
uuid = { version = "1.6.1", default-features = false }
36 changes: 36 additions & 0 deletions examples/error_handling-rs/proto/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

use std::fs;
use std::path::PathBuf;
use std::fs::File;
use std::env;
use std::io::Write;

fn main() {
let uuid = match fs::read_to_string("../uuid.txt") {
Ok(u) => {
u.trim().to_string()
},
Err(_) => {
panic!("Cannot find uuid.txt");
}
};
let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
let mut buffer = File::create(out.join("uuid.txt")).unwrap();
write!(buffer, "{}", uuid).unwrap();
}
39 changes: 39 additions & 0 deletions examples/error_handling-rs/proto/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

#![no_std]

pub enum Command {
// Ask the TA to simply return `TEE_SUCCESS` when handling this command.
ReturnSuccess,
// Ask the TA to simply return `TEE_ERROR_GENERIC` when handling this command.
ReturnGenericError,
Unknown,
}

impl From<u32> for Command {
#[inline]
fn from(value: u32) -> Command {
match value {
0 => Command::ReturnSuccess,
1 => Command::ReturnGenericError,
_ => Command::Unknown,
}
}
}

pub const UUID: &str = &include_str!(concat!(env!("OUT_DIR"), "/uuid.txt"));
39 changes: 39 additions & 0 deletions examples/error_handling-rs/ta/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

[package]
name = "ta"
version = "0.2.0"
authors = ["Teaclave Contributors <dev@teaclave.apache.org>"]
license = "Apache-2.0"
repository = "https://github.com/apache/incubator-teaclave-trustzone-sdk.git"
description = "An example of Rust OP-TEE TrustZone SDK."
edition = "2018"

[dependencies]
proto = { path = "../proto" }
optee-utee-sys = { path = "../../../optee-utee/optee-utee-sys", default-features = false }
optee-utee = { path = "../../../optee-utee", default-features = false }

[build_dependencies]
uuid = { version = "1.6.1", default-features = false }
proto = { path = "../proto" }

[profile.release]
panic = "abort"
lto = true
opt-level = 1
42 changes: 42 additions & 0 deletions examples/error_handling-rs/ta/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

UUID ?= $(shell cat "../uuid.txt")

TARGET ?= aarch64-unknown-linux-gnu
CROSS_COMPILE ?= aarch64-linux-gnu-
OBJCOPY := $(CROSS_COMPILE)objcopy
LINKER_CFG := target.$(TARGET).linker=\"$(CROSS_COMPILE)ld.bfd\"

TA_SIGN_KEY ?= $(TA_DEV_KIT_DIR)/keys/default_ta.pem
SIGN := $(TA_DEV_KIT_DIR)/scripts/sign_encrypt.py
OUT_DIR := $(CURDIR)/target/$(TARGET)/release

all: ta strip sign

ta:
@cargo build --target $(TARGET) --release --config $(LINKER_CFG)

strip: ta
@$(OBJCOPY) --strip-unneeded $(OUT_DIR)/ta $(OUT_DIR)/stripped_ta

sign: strip
@$(SIGN) --uuid $(UUID) --key $(TA_SIGN_KEY) --in $(OUT_DIR)/stripped_ta --out $(OUT_DIR)/$(UUID).ta
@echo "SIGN => ${UUID}"

clean:
@cargo clean
Loading