ltp.lists.linux.it archive mirror
 help / color / mirror / Atom feed
* [LTP] [PATCH v2] fs/doio:Use the snprintf function to prevent buffer overflow
@ 2023-04-23  2:27 Hao Zeng
  2023-04-24  9:29 ` Cyril Hrubis
  0 siblings, 1 reply; 2+ messages in thread
From: Hao Zeng @ 2023-04-23  2:27 UTC (permalink / raw)
  To: chrubis; +Cc: Hao Zeng, ltp

Use the snprintf function instead of sprintf in the write_log.c file

Signed-off-by: Hao Zeng <zenghao@kylinos.cn>
---
 testcases/kernel/fs/doio/write_log.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/testcases/kernel/fs/doio/write_log.c b/testcases/kernel/fs/doio/write_log.c
index e8ef9c7cb..44e6fd165 100644
--- a/testcases/kernel/fs/doio/write_log.c
+++ b/testcases/kernel/fs/doio/write_log.c
@@ -141,7 +141,7 @@ int wlog_open(struct wlog_file *wfile, int trunc, int mode)
 
 	oflags = O_RDWR;
 	if ((wfile->w_rfd = open(wfile->w_file, oflags)) == -1) {
-		sprintf(Wlog_Error_String,
+		snprintf(Wlog_Error_String, sizeof(Wlog_Error_String),
 			"Could not open write log - open(%s, %#o) failed:  %s\n",
 			wfile->w_file, oflags, strerror(errno));
 		close(wfile->w_afd);
@@ -218,14 +218,14 @@ int wlog_record_write(struct wlog_file *wfile, struct wlog_rec *wrec,
 		reclen += 2;
 
 		if (write(wfile->w_afd, wbuf, reclen) == -1) {
-			sprintf(Wlog_Error_String,
+			snprintf(Wlog_Error_String, sizeof(Wlog_Error_String),
 				"Could not write log - write(%s, %s, %d) failed:  %s\n",
 				wfile->w_file, wbuf, reclen, strerror(errno));
 			return -1;
 		} else {
 			offset = lseek(wfile->w_afd, 0, SEEK_CUR) - reclen;
 			if (offset == -1) {
-				sprintf(Wlog_Error_String,
+				snprintf(Wlog_Error_String, sizeof(Wlog_Error_String),
 					"Could not reposition file pointer - lseek(%s, 0, SEEK_CUR) failed:  %s\n",
 					wfile->w_file, strerror(errno));
 				return -1;
@@ -233,13 +233,13 @@ int wlog_record_write(struct wlog_file *wfile, struct wlog_rec *wrec,
 		}
 	} else {
 		if ((lseek(wfile->w_rfd, offset, SEEK_SET)) == -1) {
-			sprintf(Wlog_Error_String,
+			snprintf(Wlog_Error_String, sizeof(Wlog_Error_String),
 				"Could not reposition file pointer - lseek(%s, %ld, SEEK_SET) failed:  %s\n",
 				wfile->w_file, offset, strerror(errno));
 			return -1;
 		} else {
 			if ((write(wfile->w_rfd, wbuf, reclen)) == -1) {
-				sprintf(Wlog_Error_String,
+				snprintf(Wlog_Error_String, sizeof(Wlog_Error_String),
 					"Could not write log - write(%s, %s, %d) failed:  %s\n",
 					wfile->w_file, wbuf, reclen,
 					strerror(errno));
@@ -274,14 +274,14 @@ int wlog_scan_backward(struct wlog_file *wfile, int nrecs,
 	 */
 
 	if ((lseek(fd, 0, SEEK_END)) == -1) {
-		sprintf(Wlog_Error_String,
+		snprintf(Wlog_Error_String, sizeof(Wlog_Error_String),
 			"Could not reposition file pointer - lseek(%s, 0, SEEK_END) failed:  %s\n",
 			wfile->w_file, strerror(errno));
 		return -1;
 	}
 	offset = lseek(fd, 0, SEEK_CUR);
 	if ((offset == -1)) {
-		sprintf(Wlog_Error_String,
+		snprintf(Wlog_Error_String, sizeof(Wlog_Error_String),
 			"Could not reposition file pointer - lseek(%s, 0, SEEK_CUR) failed:  %s\n",
 			wfile->w_file, strerror(errno));
 		return -1;
@@ -309,7 +309,7 @@ int wlog_scan_backward(struct wlog_file *wfile, int nrecs,
 		 * Move to the proper file offset, and read into buf
 		 */
 		if ((lseek(fd, offset, SEEK_SET)) == -1) {
-			sprintf(Wlog_Error_String,
+			snprintf(Wlog_Error_String, sizeof(Wlog_Error_String),
 				"Could not reposition file pointer - lseek(%s, %d, SEEK_SET) failed:  %s\n",
 				wfile->w_file, offset, strerror(errno));
 			return -1;
@@ -318,7 +318,7 @@ int wlog_scan_backward(struct wlog_file *wfile, int nrecs,
 		nbytes = read(fd, bufstart, bufend - bufstart - leftover);
 
 		if (nbytes == -1) {
-			sprintf(Wlog_Error_String,
+			snprintf(Wlog_Error_String, sizeof(Wlog_Error_String),
 				"Could not read history file at offset %d - read(%d, %p, %d) failed:  %s\n",
 				offset, fd, bufstart,
 				(int)(bufend - bufstart - leftover),
-- 
2.37.2


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

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

* Re: [LTP] [PATCH v2] fs/doio:Use the snprintf function to prevent buffer overflow
  2023-04-23  2:27 [LTP] [PATCH v2] fs/doio:Use the snprintf function to prevent buffer overflow Hao Zeng
@ 2023-04-24  9:29 ` Cyril Hrubis
  0 siblings, 0 replies; 2+ messages in thread
From: Cyril Hrubis @ 2023-04-24  9:29 UTC (permalink / raw)
  To: Hao Zeng; +Cc: ltp

Hi!
> Use the snprintf function instead of sprintf in the write_log.c file
> 
> Signed-off-by: Hao Zeng <zenghao@kylinos.cn>
> ---
>  testcases/kernel/fs/doio/write_log.c | 18 +++++++++---------
>  1 file changed, 9 insertions(+), 9 deletions(-)
> 
> diff --git a/testcases/kernel/fs/doio/write_log.c b/testcases/kernel/fs/doio/write_log.c
> index e8ef9c7cb..44e6fd165 100644
> --- a/testcases/kernel/fs/doio/write_log.c
> +++ b/testcases/kernel/fs/doio/write_log.c
> @@ -141,7 +141,7 @@ int wlog_open(struct wlog_file *wfile, int trunc, int mode)
>  
>  	oflags = O_RDWR;
>  	if ((wfile->w_rfd = open(wfile->w_file, oflags)) == -1) {
> -		sprintf(Wlog_Error_String,
> +		snprintf(Wlog_Error_String, sizeof(Wlog_Error_String),
>  			"Could not open write log - open(%s, %#o) failed:  %s\n",
>  			wfile->w_file, oflags, strerror(errno));
>  		close(wfile->w_afd);

There is one more sprintf() in the wlong_open() you have missed, can you
please fix that one as well?

Also with new enough GCC I'm still getting warnings about possible
truncation, looking at the Wlog_Error_String size it would make sense to
bump the buffer size to something as 2048.

-- 
Cyril Hrubis
chrubis@suse.cz

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

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

end of thread, other threads:[~2023-04-24  9:28 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-04-23  2:27 [LTP] [PATCH v2] fs/doio:Use the snprintf function to prevent buffer overflow Hao Zeng
2023-04-24  9:29 ` Cyril Hrubis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).