Commit 92734092 authored by Arnd Bergmann's avatar Arnd Bergmann Committed by James Morris

tomoyo: fix timestamping for y2038

Tomoyo uses an open-coded version of time_to_tm() to create a timestamp
from the current time as read by get_seconds(). This will overflow and
give wrong results on 32-bit systems in 2038.

To correct this, this changes the code to use ktime_get_real_seconds()
and the generic time64_to_tm() function that are both y2038-safe.
Using the library function avoids adding an expensive 64-bit division
in this code and can benefit from any optimizations we do in common
code.
Acked-by: default avatarTetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Signed-off-by: default avatarArnd Bergmann <arnd@arndb.de>
Signed-off-by: default avatarJames Morris <james.l.morris@oracle.com>
parent dbbbe110
...@@ -156,7 +156,7 @@ static char *tomoyo_print_header(struct tomoyo_request_info *r) ...@@ -156,7 +156,7 @@ static char *tomoyo_print_header(struct tomoyo_request_info *r)
if (!buffer) if (!buffer)
return NULL; return NULL;
tomoyo_convert_time(get_seconds(), &stamp); tomoyo_convert_time(ktime_get_real_seconds(), &stamp);
pos = snprintf(buffer, tomoyo_buffer_len - 1, pos = snprintf(buffer, tomoyo_buffer_len - 1,
"#%04u/%02u/%02u %02u:%02u:%02u# profile=%u mode=%s " "#%04u/%02u/%02u %02u:%02u:%02u# profile=%u mode=%s "
......
...@@ -2256,7 +2256,7 @@ static const char * const tomoyo_memory_headers[TOMOYO_MAX_MEMORY_STAT] = { ...@@ -2256,7 +2256,7 @@ static const char * const tomoyo_memory_headers[TOMOYO_MAX_MEMORY_STAT] = {
/* Timestamp counter for last updated. */ /* Timestamp counter for last updated. */
static unsigned int tomoyo_stat_updated[TOMOYO_MAX_POLICY_STAT]; static unsigned int tomoyo_stat_updated[TOMOYO_MAX_POLICY_STAT];
/* Counter for number of updates. */ /* Counter for number of updates. */
static unsigned int tomoyo_stat_modified[TOMOYO_MAX_POLICY_STAT]; static time64_t tomoyo_stat_modified[TOMOYO_MAX_POLICY_STAT];
/** /**
* tomoyo_update_stat - Update statistic counters. * tomoyo_update_stat - Update statistic counters.
...@@ -2271,7 +2271,7 @@ void tomoyo_update_stat(const u8 index) ...@@ -2271,7 +2271,7 @@ void tomoyo_update_stat(const u8 index)
* I don't use atomic operations because race condition is not fatal. * I don't use atomic operations because race condition is not fatal.
*/ */
tomoyo_stat_updated[index]++; tomoyo_stat_updated[index]++;
tomoyo_stat_modified[index] = get_seconds(); tomoyo_stat_modified[index] = ktime_get_real_seconds();
} }
/** /**
......
...@@ -1036,7 +1036,7 @@ void tomoyo_check_acl(struct tomoyo_request_info *r, ...@@ -1036,7 +1036,7 @@ void tomoyo_check_acl(struct tomoyo_request_info *r,
bool (*check_entry) (struct tomoyo_request_info *, bool (*check_entry) (struct tomoyo_request_info *,
const struct tomoyo_acl_info *)); const struct tomoyo_acl_info *));
void tomoyo_check_profile(void); void tomoyo_check_profile(void);
void tomoyo_convert_time(time_t time, struct tomoyo_time *stamp); void tomoyo_convert_time(time64_t time, struct tomoyo_time *stamp);
void tomoyo_del_condition(struct list_head *element); void tomoyo_del_condition(struct list_head *element);
void tomoyo_fill_path_info(struct tomoyo_path_info *ptr); void tomoyo_fill_path_info(struct tomoyo_path_info *ptr);
void tomoyo_get_attributes(struct tomoyo_obj_info *obj); void tomoyo_get_attributes(struct tomoyo_obj_info *obj);
......
...@@ -86,38 +86,17 @@ const u8 tomoyo_index2category[TOMOYO_MAX_MAC_INDEX] = { ...@@ -86,38 +86,17 @@ const u8 tomoyo_index2category[TOMOYO_MAX_MAC_INDEX] = {
* @stamp: Pointer to "struct tomoyo_time". * @stamp: Pointer to "struct tomoyo_time".
* *
* Returns nothing. * Returns nothing.
*
* This function does not handle Y2038 problem.
*/ */
void tomoyo_convert_time(time_t time, struct tomoyo_time *stamp) void tomoyo_convert_time(time64_t time64, struct tomoyo_time *stamp)
{ {
static const u16 tomoyo_eom[2][12] = { struct tm tm;
{ 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }, time64_to_tm(time64, 0, &tm);
{ 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 } stamp->sec = tm.tm_sec;
}; stamp->min = tm.tm_min;
u16 y; stamp->hour = tm.tm_hour;
u8 m; stamp->day = tm.tm_mday;
bool r; stamp->month = tm.tm_mon + 1;
stamp->sec = time % 60; stamp->year = tm.tm_year + 1900;
time /= 60;
stamp->min = time % 60;
time /= 60;
stamp->hour = time % 24;
time /= 24;
for (y = 1970; ; y++) {
const unsigned short days = (y & 3) ? 365 : 366;
if (time < days)
break;
time -= days;
}
r = (y & 3) == 0;
for (m = 0; m < 11 && time >= tomoyo_eom[r][m]; m++)
;
if (m)
time -= tomoyo_eom[r][m - 1];
stamp->year = y;
stamp->month = ++m;
stamp->day = ++time;
} }
/** /**
......
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