Skip to content

Instantly share code, notes, and snippets.

@ARISTODE
Created October 7, 2024 05:39
Show Gist options
  • Select an option

  • Save ARISTODE/2bca4028a7a92d0cd3613596b6b60b6a to your computer and use it in GitHub Desktop.

Select an option

Save ARISTODE/2bca4028a7a92d0cd3613596b6b60b6a to your computer and use it in GitHub Desktop.
corrupted buffer index mgag200
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
// Dummy structures to represent kernel structures
struct device {
// Simplified device structure
int dummy;
};
struct drm_driver {
// Simplified drm_driver structure
int dummy;
};
struct drm_device {
// Simplified drm_device structure
struct device *dev;
const struct drm_driver *driver;
};
// Error pointer macro
#define ERR_PTR(error) ((void *)(long)(error))
#define IS_ERR(ptr) ((unsigned long)(ptr) > (unsigned long)(-1000))
#define PTR_ERR(ptr) ((long)(ptr))
// Dummy functions to simulate kernel functions
void *kzalloc(size_t size, int flags) {
void *ptr = calloc(1, size);
return ptr;
}
int devm_drm_dev_init(struct device *parent, struct drm_device *drm, const struct drm_driver *driver) {
// Dummy implementation
drm->dev = parent;
drm->driver = driver;
return 0; // Assume success
}
void drmm_add_final_kfree(struct drm_device *drm, void *container) {
// Dummy implementation
// In a real implementation, this would set up the container to be freed when the drm device is destroyed
}
// Main function
void *__devm_drm_dev_alloc(struct device *parent,
const struct drm_driver *driver,
size_t size, size_t offset)
{
void *container;
struct drm_device *drm;
int ret;
container = kzalloc(size, 0); // GFP_KERNEL flag is not needed in userspace
if (!container)
return ERR_PTR(-ENOMEM);
drm = (struct drm_device *)((char *)container + offset);
ret = devm_drm_dev_init(parent, drm, driver);
if (ret) {
free(container);
return ERR_PTR(ret);
}
drmm_add_final_kfree(drm, container);
return container;
}
// Test function
int main() {
struct device parent = {0};
struct drm_driver driver = {0};
size_t size = sizeof(struct drm_device) + 64; // 64 bytes extra for example
size_t offset = 64; // Offset to drm_device
void *result = __devm_drm_dev_alloc(&parent, &driver, size, offset);
if (IS_ERR(result)) {
printf("Error allocating DRM device: %ld\n", PTR_ERR(result));
} else {
printf("DRM device allocated successfully at %p\n", result);
struct drm_device *drm = (struct drm_device *)((char *)result + offset);
printf("DRM device structure at %p\n", (void *)drm);
free(result); // In real usage, this would be handled by the DRM subsystem
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment