All of lore.kernel.org
 help / color / mirror / Atom feed
* [bug report] ovl: make sure that real fid is 32bit aligned in memory
@ 2019-12-13 10:37 Dan Carpenter
  2019-12-13 12:22 ` Miklos Szeredi
  0 siblings, 1 reply; 6+ messages in thread
From: Dan Carpenter @ 2019-12-13 10:37 UTC (permalink / raw)
  To: amir73il; +Cc: linux-unionfs

Hello Amir Goldstein,

This is a semi-automatic email about new static checker warnings.

The patch cbe7fba8edfc: "ovl: make sure that real fid is 32bit 
aligned in memory" from Nov 15, 2019, leads to the following Smatch 
complaint:

    fs/overlayfs/copy_up.c:338 ovl_set_origin()
     warn: variable dereferenced before check 'fh' (see line 337)

fs/overlayfs/copy_up.c
   336		 */
   337		err = ovl_check_setxattr(dentry, upper, OVL_XATTR_ORIGIN, fh->buf,
                                                                          ^^^^^^^
The patch adds an unconditional dereference

   338					 fh ? fh->fb.len : 0, 0);
                                         ^^
but "fh" can be NULL.

   339		kfree(fh);
   340	

regards,
dan carpenter

^ permalink raw reply	[flat|nested] 6+ messages in thread
* [bug report] ovl: make sure that real fid is 32bit aligned in memory
@ 2020-05-05 13:50 Dan Carpenter
  2020-05-05 16:13 ` Amir Goldstein
  0 siblings, 1 reply; 6+ messages in thread
From: Dan Carpenter @ 2020-05-05 13:50 UTC (permalink / raw)
  To: amir73il; +Cc: linux-unionfs

Hello Amir Goldstein,

The patch cbe7fba8edfc: "ovl: make sure that real fid is 32bit
aligned in memory" from Nov 15, 2019, leads to the following static
checker warning:

	fs/overlayfs/export.c:791 ovl_fid_to_fh()
	warn: check that subtract can't underflow

fs/overlayfs/export.c
   775  static struct ovl_fh *ovl_fid_to_fh(struct fid *fid, int buflen, int fh_type)
   776  {
   777          struct ovl_fh *fh;
   778  
   779          /* If on-wire inner fid is aligned - nothing to do */
   780          if (fh_type == OVL_FILEID_V1)
   781                  return (struct ovl_fh *)fid;
   782  
   783          if (fh_type != OVL_FILEID_V0)
   784                  return ERR_PTR(-EINVAL);
   785  
   786          fh = kzalloc(buflen, GFP_KERNEL);
   787          if (!fh)
   788                  return ERR_PTR(-ENOMEM);
   789  
   790          /* Copy unaligned inner fh into aligned buffer */
   791          memcpy(&fh->fb, fid, buflen - OVL_FH_WIRE_OFFSET);
                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^

   792          return fh;
   793  }

Samtch thinks buflen can be "0,4-128".  OVL_FH_WIRE_OFFSET is 3. The
problem is that 0 - 3 is a negative and the memcpy() will crash.

In do_handle_to_path() we do:

	handle_dwords = handle->handle_bytes >> 2;

Handle ->handle_bytes is non-zero but when it's >> 2 then it can become
zero.  It's a round down.  In ovl_fh_to_dentry() we do:

	int len = fh_len << 2;

If we rounded down to zero then "len" is still zero.  Obviously one fix
would be to add a check in ovl_fid_to_fh().

	if (buflen < sizeof(*fh))
		return ERR_PTR(-EINVAL);

But that feels like papering over the bug.

regards,
dan carpenter

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2020-05-05 18:08 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-12-13 10:37 [bug report] ovl: make sure that real fid is 32bit aligned in memory Dan Carpenter
2019-12-13 12:22 ` Miklos Szeredi
2019-12-13 12:58   ` Dan Carpenter
2020-05-05 13:50 Dan Carpenter
2020-05-05 16:13 ` Amir Goldstein
2020-05-05 18:08   ` Dan Carpenter

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.