Skip to contentSkip to conversation

New: vsnprintf(3) example promotes code which ignores error return code

Bug 23282 bugzilla-daemon-590EEB7GvNiWaY/[email protected] 1 participant 16 messages Nov 19, 2010 – Feb 28, 2013 1 message not in the archive Man-pages: manual pages for Linux -- Sections 2, 3, 4, 5, and 7

lore
Share & tools
lore
#0 lore raw

https://bugzilla.kernel.org/show_bug.cgi?id=23282

Summary: vsnprintf(3) example promotes code which ignores error
return code
Product: Documentation
Version: unspecified
Platform: All
OS/Version: Linux
Tree: Mainline
Status: NEW
Severity: normal
Priority: P1
Component: man-pages
AssignedTo: documentation_man-pages-ztI5WcYan/[email protected]
ReportedBy: [email protected]
Regression: No

The example given in the vsnprintf(3) man page (release 3.31) attempts to be backwards compatible with glibc < 2.0.6 by assuming that a negative return code from vsnprintf indicates truncation.

If a negative return code is indicated for other reasons, the example will loop until the process' virtual memory is exhausted.

Please see the following for an example of how a malicious user could deliberately trigger this (potentially causing a denial of service).
http://my.opera.com/taviso/blog/2007/05/28/auditing-puzzle

Signature · 7 lines
Configure bugmail: https://bugzilla.kernel.org/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are watching the assignee of the bug.
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#2 lore raw

[Bug 23282] vsnprintf(3) example promotes code which ignores error return code

In reply to a message not in the archive

https://bugzilla.kernel.org/show_bug.cgi?id=23282

--- Comment #1 from Marshel Abraham <Marshel.Abraham-Mykyq1cwIDpWk0Htik3J/[email protected]> 2013-02-08 09:28:03 --- Created an attachment (id=92661)
--> (https://bugzilla.kernel.org/attachment.cgi?id=92661)
handle the error code

The above patch tries to be completely in align with the coding style.This change in example will prevent the crashing of code.

Signature · 7 lines
Configure bugmail: https://bugzilla.kernel.org/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are watching the assignee of the bug.
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#3 lore raw

[Bug 23282] vsnprintf(3) example promotes code which ignores error return code

In reply to a message not in the archive

https://bugzilla.kernel.org/show_bug.cgi?id=23282

--- Comment #2 from Graham Gower <[email protected]> 2013-02-09 00:16:38 --- Marshel, that patch does not address the ambiguity created by backwards
compatibility with glibc 2.0.6, it merely removes the backwards compatilibility
(while retaining notes that it is compatible). You also omit the return value
in your return statement.

I'd be happy with the backwards compatibility removed (and a note to that effect), given glibc 2.0 is now 15 years old. The code could then be simplified. At the time I encountered this issue, I wrote the following code, which would just error out in the case of truncation with old glibc.
http://code.google.com/p/opkg/source/browse/trunk/libopkg/sprintf_alloc.c

Signature · 7 lines
Configure bugmail: https://bugzilla.kernel.org/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are watching the assignee of the bug.
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#4 lore raw

[Bug 23282] vsnprintf(3) example promotes code which ignores error return code

In reply to a message not in the archive

https://bugzilla.kernel.org/show_bug.cgi?id=23282

Michael Kerrisk <[email protected]> changed:

What |Removed |Added
----------------------------------------------------------------------------
CC| |[email protected]

--- Comment #3 from Michael Kerrisk <[email protected]> 2013-02-09 02:08:38 ---
(In reply to comment #2)

Quoted 3 lines

I'd be happy with the backwards compatibility removed (and a note to that effect), given glibc 2.0 is now 15 years old. The code could then be simplified.

Agreed. I'd accept a patch along those lines. Does someone want to write one?

Signature · 7 lines
Configure bugmail: https://bugzilla.kernel.org/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are watching the assignee of the bug.
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#5 lore raw

[Bug 23282] vsnprintf(3) example promotes code which ignores error return code

In reply to a message not in the archive

https://bugzilla.kernel.org/show_bug.cgi?id=23282

--- Comment #4 from Marshel Abraham <Marshel.Abraham-Mykyq1cwIDpWk0Htik3J/[email protected]> 2013-02-11 15:09:28 --- Created an attachment (id=93121)
--> (https://bugzilla.kernel.org/attachment.cgi?id=93121)
handle error code

Okay this time I have tried to address the backward compatibility issue.I have made the change in NOTES section of the Documentation.I have gone through the entire Documentation and could find only two places where it is mentioned.

Regarding the return value I couldn't figure out what to return :-) apart from NULL.

I have kept the code simple and also test it.Though the codes have been changed comments are not felt like they fit perfectly.

Signature · 7 lines
Configure bugmail: https://bugzilla.kernel.org/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are watching the assignee of the bug.
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#6 lore raw

[Bug 23282] vsnprintf(3) example promotes code which ignores error return code

In reply to a message not in the archive

https://bugzilla.kernel.org/show_bug.cgi?id=23282

--- Comment #5 from Marshel Abraham <Marshel.Abraham-Mykyq1cwIDpWk0Htik3J/[email protected]> 2013-02-11 15:11:10 --- Guys, Thanks a ton for your reply.I have added another patch. Waiting for your
response.

Signature · 7 lines
Configure bugmail: https://bugzilla.kernel.org/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are watching the assignee of the bug.
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#7 lore raw

[Bug 23282] vsnprintf(3) example promotes code which ignores error return code

In reply to a message not in the archive

https://bugzilla.kernel.org/show_bug.cgi?id=23282

--- Comment #6 from Michael Kerrisk <[email protected]> 2013-02-11 21:38:50 --- Graham,

Could you review?

Thanks,

Michael

Signature · 7 lines
Configure bugmail: https://bugzilla.kernel.org/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are watching the assignee of the bug.
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#8 lore raw

[Bug 23282] vsnprintf(3) example promotes code which ignores error return code

In reply to a message not in the archive

https://bugzilla.kernel.org/show_bug.cgi?id=23282

--- Comment #7 from Graham Gower <[email protected]> 2013-02-12 07:11:35 --- I'm not able to properly look at it this week... Due to connectivity problems
I'm stuck to typing this on my phone.

Signature · 7 lines
Configure bugmail: https://bugzilla.kernel.org/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are watching the assignee of the bug.
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#9 lore raw

[Bug 23282] vsnprintf(3) example promotes code which ignores error return code

In reply to a message not in the archive

https://bugzilla.kernel.org/show_bug.cgi?id=23282

--- Comment #8 from Marshel Abraham <Marshel.Abraham-Mykyq1cwIDpWk0Htik3J/[email protected]> 2013-02-19 07:08:07 --- Just to remind you that the patch have not been reviewed. Are you back to Good
Internet...?

Signature · 7 lines
Configure bugmail: https://bugzilla.kernel.org/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are watching the assignee of the bug.
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#10 lore raw

[Bug 23282] vsnprintf(3) example promotes code which ignores error return code

In reply to a message not in the archive

https://bugzilla.kernel.org/show_bug.cgi?id=23282

--- Comment #9 from Marshel Abraham <Marshel.Abraham-Mykyq1cwIDpWk0Htik3J/[email protected]> 2013-02-22 11:28:29 --- Can Somebody review the patch...?

Signature · 7 lines
Configure bugmail: https://bugzilla.kernel.org/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are watching the assignee of the bug.
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#11 lore raw

[Bug 23282] vsnprintf(3) example promotes code which ignores error return code

In reply to a message not in the archive

https://bugzilla.kernel.org/show_bug.cgi?id=23282

--- Comment #10 from Marshel Abraham <Marshel.Abraham-Mykyq1cwIDpWk0Htik3J/[email protected]> 2013-02-22 17:23:36 --- (In reply to comment #6)

Quoted 7 lines

Graham,

Could you review?

Thanks,

Michael

Michael,

Can I expect some response...?

Signature · 7 lines
Configure bugmail: https://bugzilla.kernel.org/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are watching the assignee of the bug.
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#12 lore raw

[Bug 23282] vsnprintf(3) example promotes code which ignores error return code

In reply to a message not in the archive

https://bugzilla.kernel.org/show_bug.cgi?id=23282

--- Comment #11 from Graham Gower <[email protected]> 2013-02-22 23:01:24 --- Marshal, I don't apreciate being constantly badgered to review your patch.
*One* email, or *one* comment here would have sufficed.

man3/printf.3

Quoted 10 lines
man3/printf.3 +0 -0
@@ -892,7 +892,9 @@ and
 conforms to the C99 standard, that is, behaves as described above,

since glibc version 2.1.
Until glibc 2.0.6 they would return \-1

man3/printf.3 +3 -1
-when the output was truncated.
+when the output was truncated.In current implementation a negative value 
+is returned if an output error is encountered(see Return Value).Hence
+these functions are not compatible with glibc 2.0.6 and before.

I'm unfamiliar with man page formatting conventions, but a cursory look at the rest of this file indicates that new sentences should be on new lines. In any event, I don't think this text adds anything useful.

man3/printf.3

Quoted 9 lines

.\" .SH HISTORY
.\" UNIX V7 defines the three routines
.\" .BR printf (),

man3/printf.3 +0 -0
@@ -1024,7 +1026,7 @@ With the value:

one might obtain "Sonntag, 3. Juli, 10:02".
.PP
To allocate a sufficiently large string and print into it

man3/printf.3 +1 -1
-(code correct for both glibc 2.0 and glibc 2.1):
+(code incompatible for glibc 2.0.6 and before):

How about
"If truncation occurs in glibc versions prior to 2.0.6, this is treated as an error instead of being handled gracefully"

man3/printf.3 make_message(const char *fmt, ...) · line 1059

Quoted 12 lines

.nf

#include <stdio.h>

man3/printf.3 +5 -0
@@ -1050,18 +1052,21 @@ make_message(const char *fmt, ...)
        n = vsnprintf(p, size, fmt, ap);
        va_end(ap);
 
+	/* Check error code */
+	
+	if (n < 0)
+	    return NULL ;
+	     

The indentation here is not consistent with the rest of the example.

Quoted 20 lines
man3/printf.3 +3 -5
        /* If that worked, return the string. */
 
-        if (n > \-1 && n < size)
+        if (n < size)
            return p;
 
        /* Else try again with more space. */
 
-        if (n > \-1)    /* glibc 2.1 */
+        else
            size = n+1; /* precisely what is needed */
-        else           /* glibc 2.0 */
-            size *= 2;  /* twice the old size */
-
+        
        if ((np = realloc (p, size)) == NULL) {
            free(p);
            return NULL;
Signature · 1 line
1.7.0.4
Signature · 6 lines
Configure bugmail: https://bugzilla.kernel.org/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are watching the assignee of the bug.--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#13 lore raw

[Bug 23282] vsnprintf(3) example promotes code which ignores error return code

In reply to a message not in the archive

https://bugzilla.kernel.org/show_bug.cgi?id=23282

--- Comment #12 from Michael Kerrisk <[email protected]> 2013-02-25 06:44:02 --- @Graham: Thanks for the review Graham.

@Marshel: Thanks for the revised patch. I appreciate that you're keen to get your patch reviewed, but everyone's on private time here, so things can be slow. As such, it's best to space your pings out a bit. I'm inclined to agree with Graham's comment that the first piece of text that you added probably isn't needed. But, otherwise, if you can fix the rest of the patch up as per Graham's comments, I'm happy to apply it.

Signature · 7 lines
Configure bugmail: https://bugzilla.kernel.org/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are watching the assignee of the bug.
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#14 lore raw

[Bug 23282] vsnprintf(3) example promotes code which ignores error return code

In reply to a message not in the archive

https://bugzilla.kernel.org/show_bug.cgi?id=23282

--- Comment #13 from Marshel Abraham <Marshel.Abraham-Mykyq1cwIDpWk0Htik3J/[email protected]> 2013-02-25 16:42:46 --- Created an attachment (id=94071)
--> (https://bugzilla.kernel.org/attachment.cgi?id=94071)
handle error code: patch no.3

*One Comment* :-)

Sorry for the trouble and Thanks for your patience. I think indentation is right this time.

Please review and let me know about further changes needed.

Signature · 7 lines
Configure bugmail: https://bugzilla.kernel.org/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are watching the assignee of the bug.
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#15 lore raw

[Bug 23282] vsnprintf(3) example promotes code which ignores error return code

In reply to a message not in the archive

https://bugzilla.kernel.org/show_bug.cgi?id=23282

--- Comment #14 from Graham Gower <[email protected]> 2013-02-25 22:04:31 ---

Signature · 7 lines
Configure bugmail: https://bugzilla.kernel.org/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are watching the assignee of the bug.
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#16 lore raw

[Bug 23282] vsnprintf(3) example promotes code which ignores error return code

In reply to a message not in the archive

https://bugzilla.kernel.org/show_bug.cgi?id=23282

Michael Kerrisk <[email protected]> changed:

What |Removed |Added
----------------------------------------------------------------------------

             Status|NEW                         |RESOLVED
         Resolution|                            |CODE_FIX

--- Comment #15 from Michael Kerrisk <[email protected]> 2013-02-28 13:20:21 --- Applied.

Marshel, thanks for the fix to Graham's bug!

Signature · 7 lines
Configure bugmail: https://bugzilla.kernel.org/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are watching the assignee of the bug.
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html