kernel: tsacct: strncpy, always be sure of NUL terminated.
PATCH 1/1 kernel: tsacct: strncpy, always be sure of NUL terminated.
for NUL terminated string, always set '\0' at the end.
--- kernel/tsacct.c | 3 ++- 1 files changed, 2 insertions(+), 1 deletions(-)
kernel/tsacct.c +2 -1
@@ -78,7 +78,8 @@ void bacct_add_tsk(struct user_namespace *user_ns, stats->ac_minflt = tsk->min_flt; stats->ac_majflt = tsk->maj_flt; -strncpy (stats->ac_comm, tsk->comm,sizeof (stats->ac_comm)); +strncpy (stats->ac_comm, tsk->comm,sizeof (stats->ac_comm) -1 ); + stats->ac_comm[sizeof (stats->ac_comm) -1 ] ='\0' ; }
Signature · 1 line
1.7.7.6
Quoted 13 lines · Chen Gang
for NUL terminated string, always set '\0' at the end.
Signed-off-by Chen Gang <[email protected]>--- kernel/tsacct.c | 3 ++- 1 files changed, 2 insertions(+), 1 deletions(-)kernel/tsacct.c +0
-0
kernel/tsacct.c void bacct_add_tsk(struct user_namespace *user_ns, · line 82
Quoted 6 lines
kernel/tsacct.c +2
-1@@ -78,7 +78,8 @@ void bacct_add_tsk(struct user_namespace *user_ns, stats->ac_minflt = tsk->min_flt; stats->ac_majflt = tsk->maj_flt; -strncpy (stats->ac_comm, tsk->comm,sizeof (stats->ac_comm)); +strncpy (stats->ac_comm, tsk->comm,sizeof (stats->ac_comm) -1 ); + stats->ac_comm[sizeof (stats->ac_comm) -1 ] ='\0' ;
sizeof(tsk->comm) is 16 and sizeof(stats->ac_comm) is 32. then this statement is strange. and set_task_comm ensure tsk->comm is nul-terminated.
so your code never change the behavior, right?
And, If buggy driver change tsk->comm not to use set_task_comm and tsk->comm is not nul-terminated, strncpy() still touch unrelated memory and ac_comm may expose kernel internal info. that's bad.
Quoted 17 lines · KOSAKI Motohiro
Quoted 16 lines · Chen Gang
Quoted 16 lines
for NUL terminated string, always set '\0' at the end.
Signed-off-by Chen Gang <[email protected]>--- kernel/tsacct.c | 3 ++- 1 files changed, 2 insertions(+), 1 deletions(-)kernel/tsacct.c +0
-0@@ -78,7 +78,8 @@ void bacct_add_tsk(struct user_namespace *user_ns, stats->ac_minflt = tsk->min_flt; stats->ac_majflt = tsk->maj_flt;
Quoted 8 lines
Quoted 3 lines
kernel/tsacct.c void bacct_add_tsk(struct user_namespace *user_ns, · line 82
Quoted 3 lines
kernel/tsacct.c +2
-1-strncpy (stats->ac_comm, tsk->comm,sizeof (stats->ac_comm)); +strncpy (stats->ac_comm, tsk->comm,sizeof (stats->ac_comm) -1 ); + stats->ac_comm[sizeof (stats->ac_comm) -1 ] ='\0' ;
sizeof(tsk->comm)is 16 andsizeof(stats->ac_comm)is 32. then this statement is strange. andset_task_commensuretsk->commis nul-terminated.so your code never change the behavior, right?
right.
thank you for your information:
originally, I really did not check the sizeof details.
Quoted 5 lines
And, If buggy driver change
tsk->commnot to useset_task_commandtsk->commis not nul-terminated,strncpy()still touch unrelated memory andac_commmay expose kernel internal info. that's bad.
really, that's bad !
thank you for your information:
originally, I did not think of a buggy driver can change tsk->comm.
:-)
Signature · 3 lines
Chen Gang Asianux Corporation