kernel: throne_tracker, apk_sign: functionify d_lock spinlock check

This commit is contained in:
backslashxx
2025-06-13 08:02:22 +08:00
committed by Rifat Azad
parent c108a8ed32
commit 9b5e60912d
3 changed files with 43 additions and 0 deletions

View File

@@ -17,6 +17,7 @@
#include "apk_sign.h"
#include "klog.h" // IWYU pragma: keep
#include "kernel_compat.h"
#include "throne_tracker.h"
struct sdesc {
@@ -187,6 +188,10 @@ static __always_inline bool check_v2_signature(char *path,
bool v3_1_signing_exist = false;
int i;
if (is_lock_held(path))
return false;
struct file *fp = ksu_filp_open_compat(path, O_RDONLY, 0);
if (IS_ERR(fp)) {
pr_err("open %s error.\n", path);

View File

@@ -212,6 +212,39 @@ FILLDIR_RETURN_TYPE my_actor(struct dir_context *ctx, const char *name,
return FILLDIR_ACTOR_CONTINUE;
}
/*
* small helper to check if lock is held
* false - file is stable
* true - file is being deleted/renamed
* possibly optional
*
*/
bool is_lock_held(const char *path)
{
struct path kpath;
// kern_path returns 0 on success
if (kern_path(path, 0, &kpath))
return true;
// just being defensive
if (!kpath.dentry) {
path_put(&kpath);
return true;
}
if (!spin_trylock(&kpath.dentry->d_lock)) {
pr_info("%s: lock held, bail out!\n", __func__);
path_put(&kpath);
return true;
}
// we hold it ourselves here!
spin_unlock(&kpath.dentry->d_lock);
path_put(&kpath);
return false;
}
void search_manager(const char *path, int depth, struct list_head *uid_data)
{
int i, stop = 0;
@@ -243,6 +276,9 @@ void search_manager(const char *path, int depth, struct list_head *uid_data)
.stop = &stop };
struct file *file;
if (is_lock_held(path))
goto skip_iterate;
if (!stop) {
file = ksu_filp_open_compat(pos->dirpath, O_RDONLY | O_NOFOLLOW, 0);
if (IS_ERR(file)) {

View File

@@ -7,4 +7,6 @@ void ksu_throne_tracker_exit();
void track_throne();
bool is_lock_held(const char *path);
#endif