#define HOST_IS_BIG_ENDIAN (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
#endif
+/*
+ * The longest file name component to allow for in the input directory tree.
+ * Ext2fs (and many others) allow up to 255 bytes. A couple of filesystems
+ * allow longer (e.g. smbfs 1024), but there isn't much use in supporting
+ * >255-byte names in the input directory tree given that such names get
+ * truncated to 255 bytes when written to cramfs.
+ */
+#define MAX_INPUT_NAMELEN 255
+
/*
* Reasonably terse representation of the inode data.
*/
static void do_directory(char *path, struct cramfs_inode *i)
{
- int pathlen = strlen(path);
+ size_t pathlen = strlen(path);
+ size_t pathbufsz = pathlen + MAX_INPUT_NAMELEN + 1;
int count = i->size;
unsigned long offset = i->offset << 2;
- char *newpath = xmalloc(pathlen + 256);
+ char *newpath = xmalloc(pathbufsz);
if (offset == 0 && count != 0)
errx(FSCK_EX_UNCORRECTED,
errx_path(_("illegal filename"), name, newlen);
if (*extract_dir != '\0' && is_dangerous_filename(name, newlen))
errx_path(_("dangerous filename"), name, newlen);
- memcpy(newpath + pathlen, name, newlen);
- newpath[pathlen + newlen] = 0;
if (newlen == 0)
errx(FSCK_EX_UNCORRECTED, _("filename length is zero"));
+ if (pathlen + newlen + 1 > pathbufsz)
+ errx(FSCK_EX_UNCORRECTED, _("filename length is too large"));
+
+ memcpy(newpath + pathlen, name, newlen);
+ newpath[pathlen + newlen] = '\0';
if ((pathlen + newlen) - strlen(newpath) > 3)
errx(FSCK_EX_UNCORRECTED, _("bad filename length"));
expand_fs(newpath, child);
return equal;
}
-/*
- * The longest file name component to allow for in the input directory tree.
- * Ext2fs (and many others) allow up to 255 bytes. A couple of filesystems
- * allow longer (e.g. smbfs 1024), but there isn't much use in supporting
- * >255-byte names in the input directory tree given that such names get
- * truncated to 255 bytes when written to cramfs.
- */
-#define MAX_INPUT_NAMELEN 255
static int find_identical_file(struct entry *orig, struct entry *new, loff_t *fslen_ub)
{