Skip to content

Commit

Permalink
Use more specific errno codes for getxattr
Browse files Browse the repository at this point in the history
  • Loading branch information
0x09 committed Jan 16, 2024
1 parent db33e3f commit 46396f3
Showing 1 changed file with 49 additions and 35 deletions.
84 changes: 49 additions & 35 deletions src/hfsfuse.c
Original file line number Diff line number Diff line change
Expand Up @@ -360,10 +360,13 @@ static int hfsfuse_listxattr(const char* path, char* attr, size_t size) {

hfs_attribute_key_t* attr_keys;
uint32_t nattrs;
hfslib_find_attribute_records_for_cnid(vol,rec.file.cnid,&attr_keys,&nattrs,NULL);
if(hfslib_find_attribute_records_for_cnid(vol,rec.file.cnid,&attr_keys,&nattrs,NULL))
return -1;
for(uint32_t i = 0; i < nattrs; i++) {
char attrname[HFS_NAME_MAX+1];
ssize_t u8len = hfs_unistr_to_utf8(&attr_keys[i].name, attrname);
if(u8len <= 0)
continue;
ret += u8len + 1;
#ifndef __APPLE__
ret += 5; //user. prefix
Expand Down Expand Up @@ -442,48 +445,59 @@ static int hfsfuse_getxattr(const char* path, const char* attr, char* value, siz
hfs_unistr255_t attrname;
// xattr names have no normalization applied unlike catalog keys
if(hfs_utf8_to_unistr(attr,&attrname) <= 0)
goto end;
return -EINVAL;
hfs_attribute_key_t attrkey;
void* inlinedata;
if(hfslib_make_attribute_key(rec.file.cnid,0,attrname.length,attrname.unicode,&attrkey) &&
(!(ret = hfslib_find_attribute_record_with_key(vol,&attrkey,&attrec,(size ? &inlinedata : NULL),NULL)))) {
size_t attrsize = 0;
if(!hfslib_make_attribute_key(rec.file.cnid,0,attrname.length,attrname.unicode,&attrkey))
return -EFAULT; // cnid was 0
void* inlinedata = NULL;
if(hfslib_find_attribute_record_with_key(vol,&attrkey,&attrec,(size ? &inlinedata : NULL),NULL))
return -ENOATTR;

size_t attrsize = 0;
switch(attrec.type) {
case HFS_ATTR_INLINE_DATA:
attrsize = attrec.inline_record.length;
break;
case HFS_ATTR_FORK_DATA:
attrsize = attrec.fork_record.fork.logical_size;
break;
case HFS_ATTR_EXTENTS:
hfslib_error("unexpected extent attr found in getxattr. attr: %s path: %s\n", NULL, 0, attr, path);
return -EFAULT;
}
if(size) {
if(size < attrsize) {
ret = -ERANGE;
goto end;
}

switch(attrec.type) {
case HFS_ATTR_INLINE_DATA:
attrsize = attrec.inline_record.length;
break;
case HFS_ATTR_FORK_DATA:
attrsize = attrec.fork_record.fork.logical_size;
memcpy(value,inlinedata,attrsize);
break;
case HFS_ATTR_EXTENTS:
hfslib_error("unexpected extent attr found in getxattr. attr: %s path: %s\n", NULL, 0, attr, path);
goto end;
}
if(size) {
if(size < attrsize) return -ERANGE;
else {
switch(attrec.type) {
case HFS_ATTR_INLINE_DATA:
memcpy(value,inlinedata,attrsize);
free(inlinedata);
break;
case HFS_ATTR_FORK_DATA: {
hfs_extent_descriptor_t* extents = NULL;
uint16_t nextents;
if(hfslib_get_attribute_extents(vol,&attrkey,&attrec,&nextents,&extents,NULL))
goto end;
uint64_t bytesread;
hfslib_readd_with_extents(vol,value,&bytesread,attrsize,0,extents,nextents,NULL);
attrsize = bytesread;
}; break;
}
}
case HFS_ATTR_FORK_DATA: {
hfs_extent_descriptor_t* extents;
uint16_t nextents;
if(hfslib_get_attribute_extents(vol,&attrkey,&attrec,&nextents,&extents,NULL))
return -1;
uint64_t bytesread;
ret = hfslib_readd_with_extents(vol,value,&bytesread,attrsize,0,extents,nextents,NULL);
free(extents);
if(ret)
return ret;
attrsize = bytesread;
}; break;
}
return attrsize;
}

if(attrsize > (size_t)INT_MAX)
ret = -ERANGE;
else
ret = attrsize;

end:
return -ENOATTR;
free(inlinedata);
return ret;
}

#ifdef __APPLE__
Expand Down

0 comments on commit 46396f3

Please sign in to comment.