Commit c3f56102 authored by Alex Elder's avatar Alex Elder Committed by Sage Weil

libceph: validate timespec conversions

A ceph timespec contains 32-bit unsigned values for its seconds and
nanoseconds components.  For a standard timespec, both fields are
signed, and the seconds field is almost surely 64 bits.

Add some explicit casts so the fact that this conversion is taking
place is obvious.  Also trip a bug if we ever try to put out of
range (negative or too big) values into a ceph timespec.
Signed-off-by: default avatarAlex Elder <elder@inktank.com>
Reviewed-by: default avatarJosh Durgin <josh.durgin@inktank.com>
parent b587398a
...@@ -154,14 +154,19 @@ static inline char *ceph_extract_encoded_string(void **p, void *end, ...@@ -154,14 +154,19 @@ static inline char *ceph_extract_encoded_string(void **p, void *end,
static inline void ceph_decode_timespec(struct timespec *ts, static inline void ceph_decode_timespec(struct timespec *ts,
const struct ceph_timespec *tv) const struct ceph_timespec *tv)
{ {
ts->tv_sec = le32_to_cpu(tv->tv_sec); ts->tv_sec = (__kernel_time_t)le32_to_cpu(tv->tv_sec);
ts->tv_nsec = le32_to_cpu(tv->tv_nsec); ts->tv_nsec = (long)le32_to_cpu(tv->tv_nsec);
} }
static inline void ceph_encode_timespec(struct ceph_timespec *tv, static inline void ceph_encode_timespec(struct ceph_timespec *tv,
const struct timespec *ts) const struct timespec *ts)
{ {
tv->tv_sec = cpu_to_le32(ts->tv_sec); BUG_ON(ts->tv_sec < 0);
tv->tv_nsec = cpu_to_le32(ts->tv_nsec); BUG_ON(ts->tv_sec > (__kernel_time_t)U32_MAX);
BUG_ON(ts->tv_nsec < 0);
BUG_ON(ts->tv_nsec > (long)U32_MAX);
tv->tv_sec = cpu_to_le32((u32)ts->tv_sec);
tv->tv_nsec = cpu_to_le32((u32)ts->tv_nsec);
} }
/* /*
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment