summaryrefslogtreecommitdiff
path: root/apps/open_plugin.c
diff options
context:
space:
mode:
authorWilliam Wilgus <wilgus.william@gmail.com>2021-10-18 01:23:07 -0400
committerWilliam Wilgus <me.theuser@yahoo.com>2021-10-18 23:30:27 -0400
commite1553d860dc42a819fe71913d5a68a77fbf64a6e (patch)
treec29ceec89472e2c249ae3e04ba837de573b6559d /apps/open_plugin.c
parent0f68866ae5b02e3e154f31b6da12b090380db4b0 (diff)
downloadrockbox-e1553d860dc42a819fe71913d5a68a77fbf64a6e.tar.gz
rockbox-e1553d860dc42a819fe71913d5a68a77fbf64a6e.zip
Open_Plugin add checksum on struct offsets
Adding a checksum over the struct offset will allow checking for compatibility across machines rather than using packed structs to ensure compability For any file created by the user from the device this isn't really a concern But for files between machines, across installs (sim v device), possibly even across compilers this at least will alert the user rather than returning junk data Change-Id: Id0531bbaa7013dce24dece270849f0a10ac99c20
Diffstat (limited to 'apps/open_plugin.c')
-rw-r--r--apps/open_plugin.c43
1 files changed, 32 insertions, 11 deletions
diff --git a/apps/open_plugin.c b/apps/open_plugin.c
index 32b5195334..fe18071454 100644
--- a/apps/open_plugin.c
+++ b/apps/open_plugin.c
@@ -38,6 +38,8 @@
38 38
39struct open_plugin_entry_t open_plugin_entry = {0}; 39struct open_plugin_entry_t open_plugin_entry = {0};
40 40
41static const uint32_t open_plugin_csum = OPEN_PLUGIN_CHECKSUM;
42
41static const int op_entry_sz = sizeof(struct open_plugin_entry_t); 43static const int op_entry_sz = sizeof(struct open_plugin_entry_t);
42 44
43static int open_plugin_hash_get_entry(uint32_t hash, 45static int open_plugin_hash_get_entry(uint32_t hash,
@@ -60,6 +62,13 @@ static inline void op_clear_entry(struct open_plugin_entry_t *entry)
60 entry->lang_id = -1; 62 entry->lang_id = -1;
61} 63}
62 64
65static int op_entry_checksum(struct open_plugin_entry_t *entry)
66{
67 if (!entry || entry->checksum != open_plugin_csum)
68 return 0;
69 return 1;
70}
71
63static int op_find_entry(int fd, struct open_plugin_entry_t *entry, 72static int op_find_entry(int fd, struct open_plugin_entry_t *entry,
64 uint32_t hash, int32_t lang_id) 73 uint32_t hash, int32_t lang_id)
65{ 74{
@@ -75,6 +84,13 @@ static int op_find_entry(int fd, struct open_plugin_entry_t *entry,
75 (entry->hash == hash && hash != 0) || 84 (entry->hash == hash && hash != 0) ||
76 (lang_id == OPEN_PLUGIN_LANG_IGNOREALL))/* return first entry found */ 85 (lang_id == OPEN_PLUGIN_LANG_IGNOREALL))/* return first entry found */
77 { 86 {
87 /* sanity check */
88 if (op_entry_checksum(entry) <= 0)
89 {
90 splashf(HZ * 2, "OpenPlugin Invalid entry");
91 ret = OPEN_PLUGIN_NOT_FOUND;
92 break;
93 }
78 /* NULL terminate fields NOTE -- all are actually +1 larger */ 94 /* NULL terminate fields NOTE -- all are actually +1 larger */
79 entry->name[OPEN_PLUGIN_NAMESZ] = '\0'; 95 entry->name[OPEN_PLUGIN_NAMESZ] = '\0';
80 /*entry->key[OPEN_PLUGIN_BUFSZ] = '\0';*/ 96 /*entry->key[OPEN_PLUGIN_BUFSZ] = '\0';*/
@@ -111,24 +127,27 @@ static int op_update_dat(struct open_plugin_entry_t *entry, bool clear)
111 logf("OP update hash: %x lang_id: %d", hash, lang_id); 127 logf("OP update hash: %x lang_id: %d", hash, lang_id);
112 logf("OP update name: %s clear: %d", entry->name, (int) clear); 128 logf("OP update name: %s clear: %d", entry->name, (int) clear);
113 logf("OP update %s %s %s", entry->name, entry->path, entry->param); 129 logf("OP update %s %s %s", entry->name, entry->path, entry->param);
130
114#if (CONFIG_STORAGE & STORAGE_ATA) /* Harddrive -- update existing */ 131#if (CONFIG_STORAGE & STORAGE_ATA) /* Harddrive -- update existing */
115 logf("OP update *Updating entries* %s", OPEN_PLUGIN_DAT); 132 logf("OP update *Updating entries* %s", OPEN_PLUGIN_DAT);
116 fd = open(OPEN_PLUGIN_DAT, O_RDWR | O_CREAT, 0666); 133 fd = open(OPEN_PLUGIN_DAT, O_RDWR | O_CREAT, 0666);
117 134
118 if (fd < 0) 135 if (fd < 0)
119 return OPEN_PLUGIN_NOT_FOUND; 136 return OPEN_PLUGIN_NOT_FOUND;
120 /* Only read the hash and lang id */ 137 /* Only read the hash lang id and checksum */
121 uint32_t hash_langid[2] = {0}; 138 uint32_t hash_langid_csum[3] = {0};
122 while (read(fd, &hash_langid, sizeof(hash_langid)) == sizeof(hash_langid)) 139 const off_t hlc_sz = sizeof(hash_langid_csum);
140 while (read(fd, &hash_langid_csum, hlc_sz) == hlc_sz)
123 { 141 {
124 if (hash_langid[0] == hash || (int32_t)hash_langid[1] == lang_id) 142 if ((hash_langid_csum[0] == hash || (int32_t)hash_langid_csum[1] == lang_id) &&
143 hash_langid_csum[2] == open_plugin_csum)
125 { 144 {
126 logf("OP update *Entry Exists* hash: %x langid: %d", 145 logf("OP update *Entry Exists* hash: %x langid: %d",
127 hash_langid[0], (int32_t)hash_langid[1]); 146 hash_langid_csum[0], (int32_t)hash_langid[1]);
128 lseek(fd, 0-sizeof(hash_langid), SEEK_CUR);/* back to the start of record */ 147 lseek(fd, 0-hlc_sz, SEEK_CUR);/* back to the start of record */
129 break; 148 break;
130 } 149 }
131 lseek(fd, op_entry_sz - sizeof(hash_langid), SEEK_CUR); /* finish record */ 150 lseek(fd, op_entry_sz - hlc_sz, SEEK_CUR); /* finish record */
132 } 151 }
133 write(fd, entry, op_entry_sz); 152 write(fd, entry, op_entry_sz);
134 close(fd); 153 close(fd);
@@ -146,7 +165,8 @@ static int op_update_dat(struct open_plugin_entry_t *entry, bool clear)
146 /* copy non-duplicate entries back from original */ 165 /* copy non-duplicate entries back from original */
147 while (read(fd1, entry, op_entry_sz) == op_entry_sz) 166 while (read(fd1, entry, op_entry_sz) == op_entry_sz)
148 { 167 {
149 if (entry->hash != hash && entry->lang_id != lang_id) 168 if (entry->hash != hash && entry->lang_id != lang_id &&
169 op_entry_checksum(entry) > 0)
150 { 170 {
151 write(fd, entry, op_entry_sz); 171 write(fd, entry, op_entry_sz);
152 } 172 }
@@ -202,8 +222,9 @@ uint32_t open_plugin_add_path(const char *key, const char *plugin, const char *p
202 222
203 if (plugin) 223 if (plugin)
204 { 224 {
205 open_plugin_entry.hash = hash; 225 open_plugin_entry.hash = hash;
206 open_plugin_entry.lang_id = lang_id; 226 open_plugin_entry.lang_id = lang_id;
227 open_plugin_entry.checksum = open_plugin_csum;
207 /* name */ 228 /* name */
208 if (path_basename(plugin, (const char **)&pos) == 0) 229 if (path_basename(plugin, (const char **)&pos) == 0)
209 pos = "\0"; 230 pos = "\0";
@@ -242,7 +263,7 @@ retnhash:
242 263
243void open_plugin_browse(const char *key) 264void open_plugin_browse(const char *key)
244{ 265{
245 logf("OP Browse"); 266 logf("OP browse");
246 struct browse_context browse; 267 struct browse_context browse;
247 char tmp_buf[OPEN_PLUGIN_BUFSZ+1]; 268 char tmp_buf[OPEN_PLUGIN_BUFSZ+1];
248 open_plugin_get_entry(key, &open_plugin_entry); 269 open_plugin_get_entry(key, &open_plugin_entry);