Skip to content

Instantly share code, notes, and snippets.

@kerneltoast
Created April 7, 2021 23:25
Show Gist options
  • Save kerneltoast/1d6cc330b665a18da789175cfc4af27d to your computer and use it in GitHub Desktop.
Save kerneltoast/1d6cc330b665a18da789175cfc4af27d to your computer and use it in GitHub Desktop.
diff --git a/runtime/transport/symbols.c b/runtime/transport/symbols.c
index 91102fcaf..6b4d6dfba 100644
--- a/runtime/transport/symbols.c
+++ b/runtime/transport/symbols.c
@@ -163,7 +163,8 @@ read_sect_sysfs(const char* module, const char *section)
char *pathname = __getname(); // PATH_MAX sized
int rc;
unsigned long addr = 0;
- void *buffer = NULL;
+ char data[64] = {};
+ void *buffer = data;
#ifdef STAPCONF_KERNEL_READ_FILE_FROM_PATH_OFFSET
size_t size;
#else
@@ -180,16 +181,27 @@ read_sect_sysfs(const char* module, const char *section)
&buffer, 64 /* max. bytes expected */,
&size /* file_size */,
READING_UNKNOWN);
-#else
- rc = kernel_read_file_from_path(pathname, &buffer, &size, 64 /* max. bytes expected */,
- READING_UNKNOWN);
-#endif
if (rc <= 0)
goto out1;
+#else
+ // Ignore errors because the pre-offset version of
+ // kernel_read_file_from_path() always returns a spurious error saying
+ // that we don't have enough space in our buffer. To trick it, we must
+ // set the max size to 0 (which is safe because we know the output size
+ // will only ever go up to 19 bytes on 64-bit and our buffer is 64
+ // bytes), and then pass in a pre-allocated buffer to prevent it from
+ // allocating an output buffer and then freeing it internally upon
+ // error. This allows us to inspect the buffer contents even when an
+ // error is returned.
+ kernel_read_file_from_path(pathname, &buffer, &size, 0 /* max. bytes expected */,
+ READING_FIRMWARE_PREALLOC_BUFFER);
+ // Look for the hex prefix as a poor man's error check
+ if (data[0] != '0' || data[1] != 'x' || !data[2])
+ goto out1;
+#endif
rc = kstrtoul(buffer, 0, &addr);
if (rc != 0) // parse error?
addr = 0L;
- vfree (buffer);
out1:
__putname(pathname);
out:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment