You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@teaclave.apache.org by "DemesneGH (via GitHub)" <gi...@apache.org> on 2025/02/27 11:33:38 UTC
[PR] Add secure_db_abstraction example (std-only) [incubator-teaclave-trustzone-sdk]
DemesneGH opened a new pull request, #171:
URL: https://github.com/apache/incubator-teaclave-trustzone-sdk/pull/171
- Add secure_db_abstraction example and test script
- Reduce the parallel building job for STD CI tests, which is the workaround for LTO error
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@teaclave.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@teaclave.apache.org
For additional commands, e-mail: dev-help@teaclave.apache.org
Re: [PR] Add secure_db_abstraction example (std-only) [incubator-teaclave-trustzone-sdk]
Posted by "ivila (via GitHub)" <gi...@apache.org>.
ivila commented on PR #171:
URL: https://github.com/apache/incubator-teaclave-trustzone-sdk/pull/171#issuecomment-2690136238
Sorry I forgot the submit the CR comments last night😂
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@teaclave.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@teaclave.apache.org
For additional commands, e-mail: dev-help@teaclave.apache.org
Re: [PR] Add secure_db_abstraction example (std-only) [incubator-teaclave-trustzone-sdk]
Posted by "ivila (via GitHub)" <gi...@apache.org>.
ivila commented on PR #171:
URL: https://github.com/apache/incubator-teaclave-trustzone-sdk/pull/171#issuecomment-2695908194
Sorry for the late reply, kind of busy in these days.
`Reviewed-by: Zehui Chen <iv...@apache.org>`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@teaclave.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@teaclave.apache.org
For additional commands, e-mail: dev-help@teaclave.apache.org
Re: [PR] Add secure_db_abstraction example (std-only) [incubator-teaclave-trustzone-sdk]
Posted by "DemesneGH (via GitHub)" <gi...@apache.org>.
DemesneGH commented on PR #171:
URL: https://github.com/apache/incubator-teaclave-trustzone-sdk/pull/171#issuecomment-2690125948
> let's start with a std-only version first.
@ivila Yep please help review this and provide any comments or the `Reviewed-By` tag. Thanks!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@teaclave.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@teaclave.apache.org
For additional commands, e-mail: dev-help@teaclave.apache.org
Re: [PR] Add secure_db_abstraction example (std-only) [incubator-teaclave-trustzone-sdk]
Posted by "ivila (via GitHub)" <gi...@apache.org>.
ivila commented on code in PR #171:
URL: https://github.com/apache/incubator-teaclave-trustzone-sdk/pull/171#discussion_r1973619278
##########
examples/secure_db_abstraction-rs/ta/src/secure_db/db.rs:
##########
@@ -0,0 +1,114 @@
+// 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 crate::secure_db::{
+ delete_from_secure_storage, load_from_secure_storage, save_in_secure_storage,
+};
+use anyhow::{bail, ensure, Result};
+use std::collections::{HashMap, HashSet};
+
+// SecureStorageDb is a key-value storage for TA to easily store and retrieve data.
+// First we store the key list in the secure storage, named as db_name.
+// Then we store the each key-value pairs in the secure storage.
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct SecureStorageDb {
+ name: String,
+ key_list: HashSet<String>,
+}
+
+impl SecureStorageDb {
+ pub fn open(name: String) -> Result<Self> {
+ match load_from_secure_storage(name.as_bytes())? {
+ Some(data) => {
+ let key_list = bincode::deserialize(&data)?;
+ Ok(Self { name, key_list })
+ }
+ None => {
+ // create new db
+ Ok(Self {
+ name,
+ key_list: HashSet::new(),
+ })
+ }
+ }
+ }
+
+ pub fn put(&mut self, key: String, value: Vec<u8>) -> Result<()> {
+ match save_in_secure_storage(key.as_bytes(), &value) {
+ Ok(_) => {
+ self.key_list.insert(key);
+ self.store_key_list()?;
+ }
+ Err(e) => {
+ bail!("[+] SecureStorage::insert(): save error: {}", e);
+ }
+ };
+ Ok(())
+ }
+
+ pub fn get(&self, key: &str) -> Result<Vec<u8>> {
+ ensure!(self.key_list.contains(key), "Key not found in key list");
+ match load_from_secure_storage(key.as_bytes()) {
+ Ok(Some(data)) => Ok(data),
+ Ok(None) => bail!("[+] SecureStorage::get(): object not found in db"),
+ Err(e) => {
+ bail!("[+] SecureStorage::get(): load error: {}", e);
+ }
+ }
+ }
+
+ pub fn delete(&mut self, key: &str) -> Result<()> {
+ // ensure key must exist
+ ensure!(self.key_list.contains(key), "Key not found in key list");
+ match delete_from_secure_storage(key.as_bytes()) {
+ Ok(_) => {
+ self.key_list.remove(key);
+ self.store_key_list()?;
+ }
+ Err(e) => {
+ bail!("[+] SecureStorage::delete(): delete error: {}", e);
+ }
+ };
+ Ok(())
+ }
+
+ pub fn clear(&mut self) -> Result<()> {
+ for key in self.key_list.clone() {
+ self.delete(&key)?;
+ }
+ self.key_list.clear();
+ Ok(())
+ }
+
+ pub fn list_entries_with_prefix(&self, prefix: &str) -> Result<HashMap<String, Vec<u8>>> {
Review Comment:
Can we return an Iterator? Just a suggestion.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@teaclave.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@teaclave.apache.org
For additional commands, e-mail: dev-help@teaclave.apache.org
Re: [PR] Add secure_db_abstraction example (std-only) [incubator-teaclave-trustzone-sdk]
Posted by "DemesneGH (via GitHub)" <gi...@apache.org>.
DemesneGH commented on code in PR #171:
URL: https://github.com/apache/incubator-teaclave-trustzone-sdk/pull/171#discussion_r1975348374
##########
examples/secure_db_abstraction-rs/ta/src/secure_db/backend.rs:
##########
@@ -0,0 +1,90 @@
+// 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 anyhow::{bail, Result};
+use optee_utee::{DataFlag, ObjectStorageConstants, PersistentObject};
+
+// Wrapper functions for OP-TEE raw API
+
+pub fn save_in_secure_storage(obj_id: &[u8], data: &[u8]) -> Result<()> {
+ let obj_data_flag = DataFlag::ACCESS_READ
+ | DataFlag::ACCESS_WRITE
+ | DataFlag::ACCESS_WRITE_META
+ | DataFlag::OVERWRITE;
+
+ match PersistentObject::create(
+ ObjectStorageConstants::Private,
+ obj_id,
+ obj_data_flag,
+ None,
+ data,
+ ) {
+ Err(e) => {
+ bail!("[-] {:?}: failed to create object: {:?}", &obj_id, e);
+ }
+ Ok(_) => {
+ return Ok(());
+ }
+ }
+}
+
+pub fn load_from_secure_storage(obj_id: &[u8]) -> Result<Option<Vec<u8>>> {
+ match PersistentObject::open(
+ ObjectStorageConstants::Private,
+ obj_id,
+ DataFlag::ACCESS_READ | DataFlag::SHARE_READ,
+ ) {
+ Err(e) => match e.kind() {
+ optee_utee::ErrorKind::ItemNotFound => {
+ return Ok(None);
+ }
+ _ => {
+ bail!("[-] {:?}: failed to open object: {:?}", &obj_id, e);
+ }
+ },
+
+ Ok(object) => {
+ let obj_info = object.info()?;
+ let mut buf = vec![0u8; obj_info.data_size() as usize];
+
+ let read_bytes = object.read(&mut buf)?;
+ if read_bytes != obj_info.data_size() as u32 {
+ bail!("[-] {:?}: failed to read data", &obj_id);
+ }
+
+ return Ok(Some(buf));
+ }
+ }
+}
+
+pub fn delete_from_secure_storage(obj_id: &[u8]) -> Result<()> {
+ match PersistentObject::open(
+ ObjectStorageConstants::Private,
+ &obj_id,
+ DataFlag::ACCESS_READ | DataFlag::ACCESS_WRITE_META,
+ ) {
+ Err(e) => {
+ bail!("[-] {:?}: failed to open object: {:?}", &obj_id, e);
+ }
+
+ Ok(mut object) => {
+ object.close_and_delete()?;
+ std::mem::forget(object);
Review Comment:
This comes from legacy example code `secure_storage-rs` and related explanation is: https://github.com/apache/incubator-teaclave-trustzone-sdk/blob/main/optee-utee/src/object.rs#L1087-L1089
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@teaclave.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@teaclave.apache.org
For additional commands, e-mail: dev-help@teaclave.apache.org
Re: [PR] Add secure_db_abstraction example (std-only) [incubator-teaclave-trustzone-sdk]
Posted by "DemesneGH (via GitHub)" <gi...@apache.org>.
DemesneGH commented on PR #171:
URL: https://github.com/apache/incubator-teaclave-trustzone-sdk/pull/171#issuecomment-2696780497
Seems one issue exists in setup scripts which leads to CI error. Merging this PR and will open a new PR for fix.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@teaclave.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@teaclave.apache.org
For additional commands, e-mail: dev-help@teaclave.apache.org
Re: [PR] Add secure_db_abstraction example (std-only) [incubator-teaclave-trustzone-sdk]
Posted by "DemesneGH (via GitHub)" <gi...@apache.org>.
DemesneGH commented on code in PR #171:
URL: https://github.com/apache/incubator-teaclave-trustzone-sdk/pull/171#discussion_r1975416145
##########
examples/secure_db_abstraction-rs/ta/src/secure_db/db.rs:
##########
@@ -0,0 +1,114 @@
+// 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 crate::secure_db::{
+ delete_from_secure_storage, load_from_secure_storage, save_in_secure_storage,
+};
+use anyhow::{bail, ensure, Result};
+use std::collections::{HashMap, HashSet};
+
+// SecureStorageDb is a key-value storage for TA to easily store and retrieve data.
+// First we store the key list in the secure storage, named as db_name.
+// Then we store the each key-value pairs in the secure storage.
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct SecureStorageDb {
+ name: String,
+ key_list: HashSet<String>,
+}
+
+impl SecureStorageDb {
+ pub fn open(name: String) -> Result<Self> {
+ match load_from_secure_storage(name.as_bytes())? {
+ Some(data) => {
+ let key_list = bincode::deserialize(&data)?;
+ Ok(Self { name, key_list })
+ }
+ None => {
+ // create new db
+ Ok(Self {
+ name,
+ key_list: HashSet::new(),
+ })
+ }
+ }
+ }
+
+ pub fn put(&mut self, key: String, value: Vec<u8>) -> Result<()> {
+ match save_in_secure_storage(key.as_bytes(), &value) {
+ Ok(_) => {
+ self.key_list.insert(key);
+ self.store_key_list()?;
+ }
+ Err(e) => {
+ bail!("[+] SecureStorage::insert(): save error: {}", e);
+ }
+ };
+ Ok(())
+ }
+
+ pub fn get(&self, key: &str) -> Result<Vec<u8>> {
+ ensure!(self.key_list.contains(key), "Key not found in key list");
+ match load_from_secure_storage(key.as_bytes()) {
+ Ok(Some(data)) => Ok(data),
+ Ok(None) => bail!("[+] SecureStorage::get(): object not found in db"),
+ Err(e) => {
+ bail!("[+] SecureStorage::get(): load error: {}", e);
+ }
+ }
+ }
+
+ pub fn delete(&mut self, key: &str) -> Result<()> {
+ // ensure key must exist
+ ensure!(self.key_list.contains(key), "Key not found in key list");
+ match delete_from_secure_storage(key.as_bytes()) {
+ Ok(_) => {
+ self.key_list.remove(key);
+ self.store_key_list()?;
+ }
+ Err(e) => {
+ bail!("[+] SecureStorage::delete(): delete error: {}", e);
+ }
+ };
+ Ok(())
+ }
+
+ pub fn clear(&mut self) -> Result<()> {
+ for key in self.key_list.clone() {
+ self.delete(&key)?;
+ }
+ self.key_list.clear();
+ Ok(())
Review Comment:
`self.key_list.clear();` is redundant because in delete() we have `self.key_list.remove(key);`. So removed the `self.key_list.clear();`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@teaclave.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@teaclave.apache.org
For additional commands, e-mail: dev-help@teaclave.apache.org
Re: [PR] Add secure_db_abstraction example (std-only) [incubator-teaclave-trustzone-sdk]
Posted by "DemesneGH (via GitHub)" <gi...@apache.org>.
DemesneGH merged PR #171:
URL: https://github.com/apache/incubator-teaclave-trustzone-sdk/pull/171
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@teaclave.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@teaclave.apache.org
For additional commands, e-mail: dev-help@teaclave.apache.org
Re: [PR] Add secure_db_abstraction example (std-only) [incubator-teaclave-trustzone-sdk]
Posted by "DemesneGH (via GitHub)" <gi...@apache.org>.
DemesneGH commented on code in PR #171:
URL: https://github.com/apache/incubator-teaclave-trustzone-sdk/pull/171#discussion_r1975397583
##########
examples/secure_db_abstraction-rs/ta/src/secure_db/backend.rs:
##########
@@ -0,0 +1,90 @@
+// 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 anyhow::{bail, Result};
+use optee_utee::{DataFlag, ObjectStorageConstants, PersistentObject};
+
+// Wrapper functions for OP-TEE raw API
+
+pub fn save_in_secure_storage(obj_id: &[u8], data: &[u8]) -> Result<()> {
+ let obj_data_flag = DataFlag::ACCESS_READ
+ | DataFlag::ACCESS_WRITE
+ | DataFlag::ACCESS_WRITE_META
+ | DataFlag::OVERWRITE;
+
+ match PersistentObject::create(
+ ObjectStorageConstants::Private,
+ obj_id,
+ obj_data_flag,
+ None,
+ data,
+ ) {
+ Err(e) => {
+ bail!("[-] {:?}: failed to create object: {:?}", &obj_id, e);
+ }
+ Ok(_) => {
+ return Ok(());
+ }
+ }
+}
Review Comment:
updated
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@teaclave.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@teaclave.apache.org
For additional commands, e-mail: dev-help@teaclave.apache.org
Re: [PR] Add secure_db_abstraction example (std-only) [incubator-teaclave-trustzone-sdk]
Posted by "ivila (via GitHub)" <gi...@apache.org>.
ivila commented on PR #171:
URL: https://github.com/apache/incubator-teaclave-trustzone-sdk/pull/171#issuecomment-2687989161
Why don't we make it `no-std`, I think it can be easily achieved by:
1. `std::collections::{HashMap, HashSet}` => `hashbrown::{HashSet, HashMap}`
2. `std::sync::RwLock` => `spin::RwLock`
3. For other imports, just import from `core` or `alloc` directly.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@teaclave.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@teaclave.apache.org
For additional commands, e-mail: dev-help@teaclave.apache.org
Re: [PR] Add secure_db_abstraction example (std-only) [incubator-teaclave-trustzone-sdk]
Posted by "ivila (via GitHub)" <gi...@apache.org>.
ivila commented on PR #171:
URL: https://github.com/apache/incubator-teaclave-trustzone-sdk/pull/171#issuecomment-2689897262
> > Why don't we make it `no-std`, I think it can be easily achieved by:
> >
> > 1. `std::collections::{HashMap, HashSet}` => `hashbrown::{HashSet, HashMap}`
> > 2. `std::sync::RwLock` => `spin::RwLock`
> > 3. For other imports, just import from `core` or `alloc` directly.
>
> I did a quick test, and the HashMap and RwLock can be replaced with hashbrown and spin. However, `bincode` and `anyhow` with no-std need some code change:
>
> * bincode with no-std has not a stable release, I've tried the https://github.com/bincode-org/bincode/releases/tag/v2.0.0-rc.3 but the API has changed a lot.
> * Handle error using the no-std anyhow differs (reference: https://docs.rs/anyhow/latest/anyhow/#no-std-support).
>
> I suggest we review and merge the std version first, and then update to the no-std version in a separate PR. This way we can highlight the differences between the two and leave a record for the std version.
I didn't look into `bincode` and `anyhow` in detail—my mistake. In that case, I agree with your opinion; let's start with a std-only version first.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@teaclave.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@teaclave.apache.org
For additional commands, e-mail: dev-help@teaclave.apache.org
Re: [PR] Add secure_db_abstraction example (std-only) [incubator-teaclave-trustzone-sdk]
Posted by "DemesneGH (via GitHub)" <gi...@apache.org>.
DemesneGH commented on PR #171:
URL: https://github.com/apache/incubator-teaclave-trustzone-sdk/pull/171#issuecomment-2687859043
@ivila Could you help to review this PR? thanks!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@teaclave.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@teaclave.apache.org
For additional commands, e-mail: dev-help@teaclave.apache.org
Re: [PR] Add secure_db_abstraction example (std-only) [incubator-teaclave-trustzone-sdk]
Posted by "DemesneGH (via GitHub)" <gi...@apache.org>.
DemesneGH commented on PR #171:
URL: https://github.com/apache/incubator-teaclave-trustzone-sdk/pull/171#issuecomment-2689669791
> Why don't we make it `no-std`, I think it can be easily achieved by:
>
> 1. `std::collections::{HashMap, HashSet}` => `hashbrown::{HashSet, HashMap}`
> 2. `std::sync::RwLock` => `spin::RwLock`
> 3. For other imports, just import from `core` or `alloc` directly.
I did a quick test, and the HashMap and RwLock can be replaced with hashbrown and spin.
However, `bincode` and `anyhow` with no-std need some code change:
- bincode with no-std has not a stable release, I've tried the https://github.com/bincode-org/bincode/releases/tag/v2.0.0-rc.3 but the API has changed a lot.
- Handle error using the no-std anyhow differs (reference: https://docs.rs/anyhow/latest/anyhow/#no-std-support).
I suggest we review and merge the std version first, and then update to the no-std version in a separate PR. This way we can highlight the differences between the two and leave a record for the std version.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@teaclave.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@teaclave.apache.org
For additional commands, e-mail: dev-help@teaclave.apache.org
Re: [PR] Add secure_db_abstraction example (std-only) [incubator-teaclave-trustzone-sdk]
Posted by "ivila (via GitHub)" <gi...@apache.org>.
ivila commented on code in PR #171:
URL: https://github.com/apache/incubator-teaclave-trustzone-sdk/pull/171#discussion_r1973523432
##########
examples/secure_db_abstraction-rs/ta/src/secure_db/backend.rs:
##########
@@ -0,0 +1,90 @@
+// 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 anyhow::{bail, Result};
+use optee_utee::{DataFlag, ObjectStorageConstants, PersistentObject};
+
+// Wrapper functions for OP-TEE raw API
+
+pub fn save_in_secure_storage(obj_id: &[u8], data: &[u8]) -> Result<()> {
+ let obj_data_flag = DataFlag::ACCESS_READ
+ | DataFlag::ACCESS_WRITE
+ | DataFlag::ACCESS_WRITE_META
+ | DataFlag::OVERWRITE;
+
+ match PersistentObject::create(
+ ObjectStorageConstants::Private,
+ obj_id,
+ obj_data_flag,
+ None,
+ data,
+ ) {
+ Err(e) => {
+ bail!("[-] {:?}: failed to create object: {:?}", &obj_id, e);
+ }
+ Ok(_) => {
+ return Ok(());
+ }
+ }
+}
+
+pub fn load_from_secure_storage(obj_id: &[u8]) -> Result<Option<Vec<u8>>> {
+ match PersistentObject::open(
+ ObjectStorageConstants::Private,
+ obj_id,
+ DataFlag::ACCESS_READ | DataFlag::SHARE_READ,
+ ) {
+ Err(e) => match e.kind() {
+ optee_utee::ErrorKind::ItemNotFound => {
+ return Ok(None);
+ }
+ _ => {
+ bail!("[-] {:?}: failed to open object: {:?}", &obj_id, e);
+ }
+ },
+
+ Ok(object) => {
+ let obj_info = object.info()?;
+ let mut buf = vec![0u8; obj_info.data_size() as usize];
+
+ let read_bytes = object.read(&mut buf)?;
+ if read_bytes != obj_info.data_size() as u32 {
+ bail!("[-] {:?}: failed to read data", &obj_id);
+ }
+
+ return Ok(Some(buf));
+ }
+ }
+}
+
+pub fn delete_from_secure_storage(obj_id: &[u8]) -> Result<()> {
+ match PersistentObject::open(
+ ObjectStorageConstants::Private,
+ &obj_id,
+ DataFlag::ACCESS_READ | DataFlag::ACCESS_WRITE_META,
+ ) {
+ Err(e) => {
+ bail!("[-] {:?}: failed to open object: {:?}", &obj_id, e);
+ }
+
+ Ok(mut object) => {
+ object.close_and_delete()?;
+ std::mem::forget(object);
Review Comment:
Why we need to call `std::mem::forget` here?
##########
examples/secure_db_abstraction-rs/ta/src/secure_db/backend.rs:
##########
@@ -0,0 +1,90 @@
+// 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 anyhow::{bail, Result};
+use optee_utee::{DataFlag, ObjectStorageConstants, PersistentObject};
+
+// Wrapper functions for OP-TEE raw API
+
+pub fn save_in_secure_storage(obj_id: &[u8], data: &[u8]) -> Result<()> {
+ let obj_data_flag = DataFlag::ACCESS_READ
+ | DataFlag::ACCESS_WRITE
+ | DataFlag::ACCESS_WRITE_META
+ | DataFlag::OVERWRITE;
+
+ match PersistentObject::create(
+ ObjectStorageConstants::Private,
+ obj_id,
+ obj_data_flag,
+ None,
+ data,
+ ) {
+ Err(e) => {
+ bail!("[-] {:?}: failed to create object: {:?}", &obj_id, e);
+ }
+ Ok(_) => {
+ return Ok(());
+ }
+ }
+}
Review Comment:
I think we should just use `map_err`
```rust
PersistentObject::create(
ObjectStorageConstants::Private,
obj_id,
obj_data_flag,
None,
data,
).map_err(|e| bail!("[-] {:?}: failed to create object: {:?}", &obj_id, e))
```
##########
examples/secure_db_abstraction-rs/ta/src/secure_db/db.rs:
##########
@@ -0,0 +1,114 @@
+// 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 crate::secure_db::{
+ delete_from_secure_storage, load_from_secure_storage, save_in_secure_storage,
+};
+use anyhow::{bail, ensure, Result};
+use std::collections::{HashMap, HashSet};
+
+// SecureStorageDb is a key-value storage for TA to easily store and retrieve data.
+// First we store the key list in the secure storage, named as db_name.
+// Then we store the each key-value pairs in the secure storage.
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct SecureStorageDb {
+ name: String,
+ key_list: HashSet<String>,
+}
+
+impl SecureStorageDb {
+ pub fn open(name: String) -> Result<Self> {
+ match load_from_secure_storage(name.as_bytes())? {
+ Some(data) => {
+ let key_list = bincode::deserialize(&data)?;
+ Ok(Self { name, key_list })
+ }
+ None => {
+ // create new db
+ Ok(Self {
+ name,
+ key_list: HashSet::new(),
+ })
+ }
+ }
+ }
+
+ pub fn put(&mut self, key: String, value: Vec<u8>) -> Result<()> {
+ match save_in_secure_storage(key.as_bytes(), &value) {
+ Ok(_) => {
+ self.key_list.insert(key);
+ self.store_key_list()?;
+ }
+ Err(e) => {
+ bail!("[+] SecureStorage::insert(): save error: {}", e);
+ }
+ };
+ Ok(())
+ }
+
+ pub fn get(&self, key: &str) -> Result<Vec<u8>> {
+ ensure!(self.key_list.contains(key), "Key not found in key list");
+ match load_from_secure_storage(key.as_bytes()) {
+ Ok(Some(data)) => Ok(data),
+ Ok(None) => bail!("[+] SecureStorage::get(): object not found in db"),
+ Err(e) => {
+ bail!("[+] SecureStorage::get(): load error: {}", e);
+ }
+ }
+ }
+
+ pub fn delete(&mut self, key: &str) -> Result<()> {
+ // ensure key must exist
+ ensure!(self.key_list.contains(key), "Key not found in key list");
+ match delete_from_secure_storage(key.as_bytes()) {
+ Ok(_) => {
+ self.key_list.remove(key);
+ self.store_key_list()?;
+ }
+ Err(e) => {
+ bail!("[+] SecureStorage::delete(): delete error: {}", e);
+ }
+ };
+ Ok(())
+ }
+
+ pub fn clear(&mut self) -> Result<()> {
+ for key in self.key_list.clone() {
+ self.delete(&key)?;
+ }
+ self.key_list.clear();
+ Ok(())
+ }
+
+ pub fn list_entries_with_prefix(&self, prefix: &str) -> Result<HashMap<String, Vec<u8>>> {
Review Comment:
Can we return an Iterator? Just a suggestion.
##########
examples/secure_db_abstraction-rs/ta/src/secure_db/db.rs:
##########
@@ -0,0 +1,114 @@
+// 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 crate::secure_db::{
+ delete_from_secure_storage, load_from_secure_storage, save_in_secure_storage,
+};
+use anyhow::{bail, ensure, Result};
+use std::collections::{HashMap, HashSet};
+
+// SecureStorageDb is a key-value storage for TA to easily store and retrieve data.
+// First we store the key list in the secure storage, named as db_name.
+// Then we store the each key-value pairs in the secure storage.
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct SecureStorageDb {
+ name: String,
+ key_list: HashSet<String>,
+}
+
+impl SecureStorageDb {
+ pub fn open(name: String) -> Result<Self> {
+ match load_from_secure_storage(name.as_bytes())? {
+ Some(data) => {
+ let key_list = bincode::deserialize(&data)?;
+ Ok(Self { name, key_list })
+ }
+ None => {
+ // create new db
+ Ok(Self {
+ name,
+ key_list: HashSet::new(),
+ })
+ }
+ }
+ }
+
+ pub fn put(&mut self, key: String, value: Vec<u8>) -> Result<()> {
+ match save_in_secure_storage(key.as_bytes(), &value) {
+ Ok(_) => {
+ self.key_list.insert(key);
+ self.store_key_list()?;
+ }
+ Err(e) => {
+ bail!("[+] SecureStorage::insert(): save error: {}", e);
+ }
+ };
+ Ok(())
+ }
+
+ pub fn get(&self, key: &str) -> Result<Vec<u8>> {
+ ensure!(self.key_list.contains(key), "Key not found in key list");
+ match load_from_secure_storage(key.as_bytes()) {
+ Ok(Some(data)) => Ok(data),
+ Ok(None) => bail!("[+] SecureStorage::get(): object not found in db"),
+ Err(e) => {
+ bail!("[+] SecureStorage::get(): load error: {}", e);
+ }
+ }
+ }
+
+ pub fn delete(&mut self, key: &str) -> Result<()> {
+ // ensure key must exist
+ ensure!(self.key_list.contains(key), "Key not found in key list");
+ match delete_from_secure_storage(key.as_bytes()) {
+ Ok(_) => {
+ self.key_list.remove(key);
+ self.store_key_list()?;
+ }
+ Err(e) => {
+ bail!("[+] SecureStorage::delete(): delete error: {}", e);
+ }
+ };
+ Ok(())
+ }
+
+ pub fn clear(&mut self) -> Result<()> {
+ for key in self.key_list.clone() {
+ self.delete(&key)?;
+ }
+ self.key_list.clear();
+ Ok(())
Review Comment:
Shouldn't the key_list delete the key in the same step as the storage? What if the deletion fails midway through processing the key_list?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@teaclave.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@teaclave.apache.org
For additional commands, e-mail: dev-help@teaclave.apache.org