Skip to content

Commit 6a85b1b

Browse files
Jamie McCraecarlescufi
authored andcommitted
mgmt: mcumgr: lib: fs: Add file read/write access callback
This allows an application to inspect a mcumgr file access command and either allow it or deny it with a result code. Signed-off-by: Jamie McCrae <[email protected]>
1 parent ec2b58c commit 6a85b1b

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

subsys/mgmt/mcumgr/lib/cmd/fs_mgmt/Kconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,14 @@ config FS_MGMT_PATH_SIZE
136136
of this size gets allocated on the stack during handling of file upload
137137
and download commands.
138138

139+
config FS_MGMT_FILE_ACCESS_HOOK
140+
bool "File read/write access hook"
141+
help
142+
Allows applications to control file read and write access by
143+
registering for a callback which is then triggered whenever a file
144+
read or write attempt is made. This also, optionally, allows
145+
re-writing or changing of supplied file paths.
146+
Note that this will be called multiple times for each file read and
147+
write due to mcumgr's method of stateless operation.
148+
139149
endif

subsys/mgmt/mcumgr/lib/cmd/fs_mgmt/include/fs_mgmt/fs_mgmt.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (c) 2018-2022 mcumgr authors
3+
* Copyright (c) 2022 Laird Connectivity
34
*
45
* SPDX-License-Identifier: Apache-2.0
56
*/
@@ -18,11 +19,38 @@ extern "C" {
1819
#define FS_MGMT_ID_STAT 1
1920
#define FS_MGMT_ID_HASH_CHECKSUM 2
2021

22+
#ifdef CONFIG_FS_MGMT_FILE_ACCESS_HOOK
23+
/** @typedef fs_mgmt_on_evt_cb
24+
* @brief Function to be called on fs mgmt event.
25+
*
26+
* This callback function is used to notify the application about a pending file
27+
* read/write request and to authorise or deny it.
28+
*
29+
* @param write True if write access is requested, false for read access
30+
* @param path The path of the file to query.
31+
*
32+
* @note That the path can potentially be changed by the application code so
33+
* long as it does not exceed the maximum path string size.
34+
*
35+
* @return 0 to allow read/write, MGMT_ERR_[...] code to disallow read/write.
36+
*/
37+
typedef int (*fs_mgmt_on_evt_cb)(bool write, char *path);
38+
#endif
39+
2140
/**
2241
* @brief Registers the file system management command handler group.
2342
*/
2443
void fs_mgmt_register_group(void);
2544

45+
#ifdef CONFIG_FS_MGMT_FILE_ACCESS_HOOK
46+
/**
47+
* @brief Register file read/write access event callback function.
48+
*
49+
* @param cb Callback function or NULL to disable.
50+
*/
51+
void fs_mgmt_register_evt_cb(fs_mgmt_on_evt_cb cb);
52+
#endif
53+
2654
#ifdef __cplusplus
2755
}
2856
#endif

subsys/mgmt/mcumgr/lib/cmd/fs_mgmt/src/fs_mgmt.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ static struct {
6868

6969
static const struct mgmt_handler fs_mgmt_handlers[];
7070

71+
#ifdef CONFIG_FS_MGMT_FILE_ACCESS_HOOK
72+
static fs_mgmt_on_evt_cb fs_evt_cb;
73+
#endif
74+
7175
/**
7276
* Encodes a file upload response.
7377
*/
@@ -117,6 +121,17 @@ fs_mgmt_file_download(struct mgmt_ctxt *ctxt)
117121
memcpy(path, name.value, name.len);
118122
path[name.len] = '\0';
119123

124+
#ifdef CONFIG_FS_MGMT_FILE_ACCESS_HOOK
125+
if (fs_evt_cb != NULL) {
126+
/* Send request to application to check if access should be allowed or not */
127+
rc = fs_evt_cb(false, path);
128+
129+
if (rc != 0) {
130+
return rc;
131+
}
132+
}
133+
#endif
134+
120135
/* Only the response to the first download request contains the total file
121136
* length.
122137
*/
@@ -180,6 +195,17 @@ fs_mgmt_file_upload(struct mgmt_ctxt *ctxt)
180195
memcpy(file_name, name.value, name.len);
181196
file_name[name.len] = '\0';
182197

198+
#ifdef CONFIG_FS_MGMT_FILE_ACCESS_HOOK
199+
if (fs_evt_cb != NULL) {
200+
/* Send request to application to check if access should be allowed or not */
201+
rc = fs_evt_cb(true, file_name);
202+
203+
if (rc != 0) {
204+
return rc;
205+
}
206+
}
207+
#endif
208+
183209
if (off == 0) {
184210
/* Total file length is a required field in the first chunk request. */
185211
if (len == ULLONG_MAX) {
@@ -462,3 +488,10 @@ fs_mgmt_register_group(void)
462488
#endif
463489
#endif
464490
}
491+
492+
#ifdef CONFIG_FS_MGMT_FILE_ACCESS_HOOK
493+
void fs_mgmt_register_evt_cb(fs_mgmt_on_evt_cb cb)
494+
{
495+
fs_evt_cb = cb;
496+
}
497+
#endif

0 commit comments

Comments
 (0)